-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructuring of add-template by Minh-Quan
1. add-template updates 2. overwrite functionality 3. cacheLanguages for faster parsing, cache directory for zip file 4. tests for add-template, with overwrite option 5. test for PullTemplates function Signed-off-by: Eric Stoekl <ems5311@gmail.com> Signed-off-by: Minh-Quan TRAN <account@itscaro.me>
- Loading branch information
1 parent
a2c488e
commit cd9bb4c
Showing
17 changed files
with
483 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) Alex Ellis, Eric Stoekl 2017. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
|
||
"fmt" | ||
|
||
"os" | ||
|
||
"regexp" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Args and Flags that are to be added to commands | ||
|
||
var ( | ||
repository string | ||
overwrite bool | ||
) | ||
|
||
func init() { | ||
addTemplateCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite existing templates?") | ||
|
||
faasCmd.AddCommand(addTemplateCmd) | ||
} | ||
|
||
// addTemplateCmd allows the user to fetch a template from a repository | ||
var addTemplateCmd = &cobra.Command{ | ||
Use: "add-template <repository URL>", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("A repository URL must be specified") | ||
} else { | ||
var validURL = regexp.MustCompile(`^https?://.+`) | ||
|
||
if !validURL.MatchString(args[0]) { | ||
return errors.New("The given URL does not begin with http") | ||
} | ||
} | ||
return nil | ||
}, | ||
Short: "Downloads templates from the specified github repo", | ||
Long: `Downloads the compressed github repo specified by [URL], and extracts the 'template' | ||
directory from the root of the repo, if it exists.`, | ||
Example: "faas-cli add-template https://github.com/alexellis/faas-cli", | ||
Run: runAddTemplate, | ||
} | ||
|
||
func runAddTemplate(cmd *cobra.Command, args []string) { | ||
repository = args[0] | ||
|
||
fmt.Println("Get " + repository) | ||
|
||
if err := fetchTemplates(repository, overwrite); err != nil { | ||
fmt.Println(err) | ||
|
||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) Alex Ellis, Eric Stoekl 2017. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
package commands | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_addTemplate(t *testing.T) { | ||
defer tearDown_fetch_templates(t) | ||
|
||
ts := httpTestServer(t) | ||
defer ts.Close() | ||
|
||
repository = ts.URL + "/owner/repo" | ||
faasCmd.SetArgs([]string{"add-template", repository}) | ||
faasCmd.Execute() | ||
|
||
// Verify created directories | ||
for _, dir := range []string{".cache", "template"} { | ||
if _, err := os.Stat(dir); err != nil { | ||
t.Fatalf("The directory %s was not created", dir) | ||
} | ||
} | ||
} | ||
|
||
func Test_addTemplate_with_overwriting(t *testing.T) { | ||
defer tearDown_fetch_templates(t) | ||
|
||
ts := httpTestServer(t) | ||
defer ts.Close() | ||
|
||
repository = ts.URL + "/owner/repo" | ||
faasCmd.SetArgs([]string{"add-template", repository}) | ||
faasCmd.Execute() | ||
|
||
// reset cacheCanWriteLanguage | ||
cacheCanWriteLanguage = make(map[string]bool) | ||
|
||
var buf bytes.Buffer | ||
log.SetOutput(&buf) | ||
|
||
r := regexp.MustCompile(`(?m:overwriting is not allowed)`) | ||
|
||
faasCmd.SetArgs([]string{"add-template", repository}) | ||
faasCmd.Execute() | ||
|
||
// reset cacheCanWriteLanguage | ||
cacheCanWriteLanguage = make(map[string]bool) | ||
|
||
if !r.MatchString(buf.String()) { | ||
t.Fatal(buf.String()) | ||
} | ||
|
||
buf.Reset() | ||
|
||
faasCmd.SetArgs([]string{"add-template", repository, "--overwrite"}) | ||
faasCmd.Execute() | ||
|
||
if r.MatchString(buf.String()) { | ||
t.Fatal() | ||
} | ||
|
||
// Verify created directories | ||
for _, dir := range []string{".cache", "template"} { | ||
if _, err := os.Stat(dir); err != nil { | ||
t.Fatalf("The directory %s was not created", dir) | ||
} | ||
} | ||
} | ||
|
||
func Test_addTemplate_error_no_arg(t *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
faasCmd.SetArgs([]string{"add-template"}) | ||
faasCmd.SetOutput(&buf) | ||
faasCmd.Execute() | ||
|
||
if !strings.Contains(buf.String(), "Error: A repository URL must be specified") { | ||
t.Fatal("Output does not contain the required string") | ||
} | ||
} | ||
|
||
func Test_addTemplate_error_not_valid_url(t *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
faasCmd.SetArgs([]string{"add-template", "git@github.com:alexellis/faas-cli.git"}) | ||
faasCmd.SetOutput(&buf) | ||
faasCmd.Execute() | ||
|
||
if !strings.Contains(buf.String(), "Error: The given URL does not begin with http") { | ||
t.Fatal("Output does not contain the required string") | ||
} | ||
} | ||
|
||
// httpTestServer returns a testing http server | ||
func httpTestServer(t *testing.T) *httptest.Server { | ||
const sampleMasterZipPath string = "testdata/master_test.zip" | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
if _, err := os.Stat(sampleMasterZipPath); os.IsNotExist(err) { | ||
t.Error(err) | ||
} | ||
|
||
fileData, err := ioutil.ReadFile(sampleMasterZipPath) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
w.Write(fileData) | ||
})) | ||
|
||
return ts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.