From 748b14cfe651681ef295717d3bb24244b7e66014 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Fri, 23 Aug 2019 22:55:01 +0200 Subject: [PATCH 1/2] Write temporary test files in temp directory Instead of writing to the current working directory, pick a random temp directory to test the CLI commands, keeping the working directory free of test side effects. In some cases the system default temp dir will also have some advantages like being mounted in an in-memory tmpfs. --- cobra/cmd/init_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cobra/cmd/init_test.go b/cobra/cmd/init_test.go index 8ee391061..80cc1516a 100644 --- a/cobra/cmd/init_test.go +++ b/cobra/cmd/init_test.go @@ -2,15 +2,15 @@ package cmd import ( "fmt" + "io/ioutil" "os" "path/filepath" "testing" ) func getProject() *Project { - wd, _ := os.Getwd() return &Project{ - AbsolutePath: fmt.Sprintf("%s/testproject", wd), + AbsolutePath: fmt.Sprintf("%s/testproject", mustTempDir()), Legal: getLicense(), Copyright: copyrightLine(), AppName: "testproject", @@ -19,6 +19,14 @@ func getProject() *Project { } } +func mustTempDir() string { + dir, err := ioutil.TempDir("", "cobra_cli_test_") + if err != nil { + panic(err) + } + return dir +} + func TestGoldenInitCmd(t *testing.T) { project := getProject() defer os.RemoveAll(project.AbsolutePath) From 3bba6a2481166e5a164830eb68f8a9e62ddb2eac Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Fri, 23 Aug 2019 23:35:14 +0200 Subject: [PATCH 2/2] Omit LICENSE file when licence is set to none Creating a project without a license should not create an empty LICENSE file. --- cobra/cmd/init_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++ cobra/cmd/licenses.go | 4 +++- cobra/cmd/project.go | 3 +++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/cobra/cmd/init_test.go b/cobra/cmd/init_test.go index 80cc1516a..372dad024 100644 --- a/cobra/cmd/init_test.go +++ b/cobra/cmd/init_test.go @@ -5,6 +5,9 @@ import ( "io/ioutil" "os" "path/filepath" + "reflect" + "sort" + "strings" "testing" ) @@ -45,3 +48,46 @@ func TestGoldenInitCmd(t *testing.T) { } } } + +func TestInitNoLicense(t *testing.T) { + project := getProject() + project.Legal = noLicense + defer os.RemoveAll(project.AbsolutePath) + + err := project.Create() + if err != nil { + t.Fatal(err) + } + + root := project.AbsolutePath + + want := []string{"main.go", "cmd/root.go"} + var got []string + err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + relpath, err := filepath.Rel(root, path) + if err != nil { + return err + } + got = append(got, relpath) + return nil + }) + if err != nil { + t.Fatalf("walking path %q: %v", root, err) + } + sort.StringSlice(got).Sort() + sort.StringSlice(want).Sort() + if !reflect.DeepEqual(got, want) { + t.Fatalf( + "In %s, found:\n %s\nwant:\n %s", + root, + strings.Join(got, ", "), + strings.Join(want, ", "), + ) + } +} diff --git a/cobra/cmd/licenses.go b/cobra/cmd/licenses.go index a070134dd..5f5abc89a 100644 --- a/cobra/cmd/licenses.go +++ b/cobra/cmd/licenses.go @@ -36,9 +36,11 @@ type License struct { Header string // License header for source files } +var noLicense = License{"None", []string{"none", "false"}, "", ""} + func init() { // Allows a user to not use a license. - Licenses["none"] = License{"None", []string{"none", "false"}, "", ""} + Licenses["none"] = noLicense initApache2() initMit() diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go index ecd783d03..b0c12e726 100644 --- a/cobra/cmd/project.go +++ b/cobra/cmd/project.go @@ -64,6 +64,9 @@ func (p *Project) Create() error { } // create license + if p.Legal.Name == noLicense.Name { + return nil + } return p.createLicenseFile() }