mirror of
https://github.com/spf13/cobra
synced 2024-11-16 02:37:07 +00:00
19cf35ea77
* fix: ensure that testproject is removed even after a failure * fix: defer licenseFile * style: simply defer os.RemoveAll * cobra/cmd: add getProject test func
39 lines
847 B
Go
39 lines
847 B
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func getProject() *Project {
|
|
wd, _ := os.Getwd()
|
|
return &Project{
|
|
AbsolutePath: fmt.Sprintf("%s/testproject", wd),
|
|
Legal: getLicense(),
|
|
Copyright: copyrightLine(),
|
|
AppName: "testproject",
|
|
PkgName: "github.com/spf13/testproject",
|
|
Viper: true,
|
|
}
|
|
}
|
|
|
|
func TestGoldenInitCmd(t *testing.T) {
|
|
project := getProject()
|
|
defer os.RemoveAll(project.AbsolutePath)
|
|
|
|
if err := project.Create(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
|
|
for _, f := range expectedFiles {
|
|
generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)
|
|
goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
|
|
err := compareFiles(generatedFile, goldenFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|