Skip to content

Commit

Permalink
write temporary test files in temp directory (spf13#937)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rhcarvalho authored and umarcor committed May 11, 2020
1 parent 6009fe4 commit 33fadcb
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions cobra/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import (
)

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",
Expand All @@ -22,6 +21,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) {

dir, err := ioutil.TempDir("", "cobra-init")
Expand Down

0 comments on commit 33fadcb

Please sign in to comment.