2017-05-01 20:52:58 +00:00
|
|
|
package cmd
|
|
|
|
|
2017-05-05 08:06:27 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// TestGoldenAddCmd initializes the project "github.com/spf13/testproject"
|
|
|
|
// in GOPATH, adds "test" command
|
|
|
|
// and compares the content of all files in cmd directory of testproject
|
|
|
|
// with appropriate golden files.
|
|
|
|
// Use -update to update existing golden files.
|
|
|
|
func TestGoldenAddCmd(t *testing.T) {
|
|
|
|
projectName := "github.com/spf13/testproject"
|
|
|
|
project := NewProject(projectName)
|
|
|
|
|
|
|
|
// Initialize the project at first.
|
|
|
|
initializeProject(project)
|
|
|
|
defer os.RemoveAll(project.AbsPath())
|
2017-07-30 06:05:07 +00:00
|
|
|
|
2017-07-30 15:14:01 +00:00
|
|
|
viper.Set("year", 2017) // For reproducible builds.
|
|
|
|
defer viper.Set("year", nil)
|
2017-05-05 08:06:27 +00:00
|
|
|
|
|
|
|
// Then add the "test" command.
|
|
|
|
cmdName := "test"
|
|
|
|
cmdPath := filepath.Join(project.CmdPath(), cmdName+".go")
|
|
|
|
createCmdFile(project.License(), cmdPath, cmdName)
|
|
|
|
|
|
|
|
expectedFiles := []string{".", "root.go", "test.go"}
|
|
|
|
gotFiles := []string{}
|
|
|
|
|
|
|
|
// Check project file hierarchy and compare the content of every single file
|
|
|
|
// with appropriate golden file.
|
|
|
|
err := filepath.Walk(project.CmdPath(), func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make path relative to project.CmdPath().
|
|
|
|
// E.g. path = "/home/user/go/src/github.com/spf13/testproject/cmd/root.go"
|
|
|
|
// then it returns just "root.go".
|
|
|
|
relPath, err := filepath.Rel(project.CmdPath(), 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 ".":
|
|
|
|
return nil
|
|
|
|
// Known files.
|
|
|
|
case "root.go", "test.go":
|
|
|
|
if *update {
|
|
|
|
got, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ioutil.WriteFile(goldenPath, got, 0644)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 20:52:58 +00:00
|
|
|
|
|
|
|
func TestValidateCmdName(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"cmdName", "cmdName"},
|
|
|
|
{"cmd_name", "cmdName"},
|
|
|
|
{"cmd-name", "cmdName"},
|
|
|
|
{"cmd______Name", "cmdName"},
|
|
|
|
{"cmd------Name", "cmdName"},
|
|
|
|
{"cmd______name", "cmdName"},
|
|
|
|
{"cmd------name", "cmdName"},
|
2017-05-05 08:30:41 +00:00
|
|
|
{"cmdName-----", "cmdName"},
|
|
|
|
{"cmdname-", "cmdname"},
|
2017-05-01 20:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
got := validateCmdName(testCase.input)
|
|
|
|
if testCase.expected != got {
|
|
|
|
t.Errorf("Expected %q, got %q", testCase.expected, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|