mirror of
https://github.com/spf13/cobra
synced 2025-05-07 13:57:21 +00:00
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:
parent
89c7ffb512
commit
748b14cfe6
1 changed files with 10 additions and 2 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue