spf13--cobra/cobra/cmd/add_test.go

70 lines
1.4 KiB
Go
Raw Normal View History

2017-05-01 20:52:58 +00:00
package cmd
2019-01-31 03:47:38 +00:00
import (
"fmt"
"os"
"testing"
)
2019-01-31 03:47:38 +00:00
func TestGoldenAddCmd(t *testing.T) {
2017-05-05 08:06:27 +00:00
2019-01-31 03:47:38 +00:00
wd, _ := os.Getwd()
command := &Command{
CmdName: "test",
CmdParent: parentName,
Project: &Project{
AbsolutePath: fmt.Sprintf("%s/testproject", wd),
Legal: getLicense(),
Copyright: copyrightLine(),
// required to init
AppName: "testproject",
PkgName: "github.com/spf13/testproject",
Viper: true,
2019-01-31 03:47:38 +00:00
},
}
2017-05-05 08:06:27 +00:00
// init project first
command.Project.Create()
defer func() {
if _, err := os.Stat(command.AbsolutePath); err == nil {
os.RemoveAll(command.AbsolutePath)
}
}()
2019-01-31 03:47:38 +00:00
if err := command.Create(); err != nil {
2017-05-05 08:06:27 +00:00
t.Fatal(err)
}
2019-01-31 03:47:38 +00:00
generatedFile := fmt.Sprintf("%s/cmd/%s.go", command.AbsolutePath, command.CmdName)
goldenFile := fmt.Sprintf("testdata/%s.go.golden", command.CmdName)
err := compareFiles(generatedFile, goldenFile)
if err != nil {
2017-05-05 08:06:27 +00:00
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"},
{"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)
}
}
}