2017-05-05 08:06:27 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2017-07-30 06:05:07 +00:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2017-05-05 08:06:27 +00:00
|
|
|
)
|
|
|
|
|
2019-01-30 07:41:41 +00:00
|
|
|
func TestGoldenInitCmd(t *testing.T) {
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
project := &Project{
|
|
|
|
AbsolutePath: wd,
|
|
|
|
PkgName: "github.com/spf13/testproject",
|
|
|
|
Legal: getLicense(),
|
|
|
|
Viper: true,
|
|
|
|
AppName: "testproject",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := project.Create()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
|
|
|
|
//for _, f := range expectedFiles {
|
|
|
|
// // read each file and compare with corresponding golden file
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
2017-05-05 08:06:27 +00:00
|
|
|
// TestGoldenInitCmd initializes the project "github.com/spf13/testproject"
|
|
|
|
// in GOPATH and compares the content of files in initialized project with
|
|
|
|
// appropriate golden files ("testdata/*.golden").
|
|
|
|
// Use -update to update existing golden files.
|
2019-01-30 07:41:41 +00:00
|
|
|
func TTestGoldenInitCmd(t *testing.T) {
|
2017-05-05 08:06:27 +00:00
|
|
|
projectName := "github.com/spf13/testproject"
|
|
|
|
project := NewProject(projectName)
|
|
|
|
defer os.RemoveAll(project.AbsPath())
|
2017-07-30 06:05:07 +00:00
|
|
|
|
2017-07-30 15:18:17 +00:00
|
|
|
viper.Set("author", "NAME HERE <EMAIL ADDRESS>")
|
|
|
|
viper.Set("license", "apache")
|
|
|
|
viper.Set("year", 2017)
|
|
|
|
defer viper.Set("author", nil)
|
|
|
|
defer viper.Set("license", nil)
|
2017-07-30 15:14:01 +00:00
|
|
|
defer viper.Set("year", nil)
|
2017-05-05 08:06:27 +00:00
|
|
|
|
|
|
|
os.Args = []string{"cobra", "init", projectName}
|
2017-05-09 09:15:48 +00:00
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2017-05-05 08:06:27 +00:00
|
|
|
t.Fatal("Error by execution:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedFiles := []string{".", "cmd", "LICENSE", "main.go", "cmd/root.go"}
|
|
|
|
gotFiles := []string{}
|
|
|
|
|
|
|
|
// Check project file hierarchy and compare the content of every single file
|
|
|
|
// with appropriate golden file.
|
|
|
|
err := filepath.Walk(project.AbsPath(), func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make path relative to project.AbsPath().
|
|
|
|
// E.g. path = "/home/user/go/src/github.com/spf13/testproject/cmd/root.go"
|
|
|
|
// then it returns just "cmd/root.go".
|
|
|
|
relPath, err := filepath.Rel(project.AbsPath(), path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
relPath = filepath.ToSlash(relPath)
|
|
|
|
gotFiles = append(gotFiles, relPath)
|
|
|
|
goldenPath := filepath.Join("testdata", filepath.Base(path)+".golden")
|
|
|
|
|
|
|
|
switch relPath {
|
2017-07-30 04:49:33 +00:00
|
|
|
// Known directories.
|
2017-05-05 08:06:27 +00:00
|
|
|
case ".", "cmd":
|
|
|
|
return nil
|
|
|
|
// Known files.
|
|
|
|
case "LICENSE", "main.go", "cmd/root.go":
|
|
|
|
if *update {
|
|
|
|
got, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-14 10:27:15 +00:00
|
|
|
if err := ioutil.WriteFile(goldenPath, got, 0644); err != nil {
|
|
|
|
t.Fatal("Error while updating file:", err)
|
|
|
|
}
|
2017-05-05 08:06:27 +00:00
|
|
|
}
|
|
|
|
return compareFiles(path, goldenPath)
|
|
|
|
}
|
|
|
|
// Unknown file.
|
|
|
|
return errors.New("unknown file: " + path)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if some files lack.
|
|
|
|
if err := checkLackFiles(expectedFiles, gotFiles); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|