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.
This commit is contained in:
Rodolfo Carvalho 2019-08-23 22:55:01 +02:00
parent 89c7ffb512
commit 748b14cfe6

View file

@ -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)