spf13--cobra/cobra/cmd/add_test.go
umarcor 19cf35ea77 fix: ensure that testproject is removed even after a failure (#948)
* fix: ensure that testproject is removed even after a failure

* fix: defer licenseFile

* style: simply defer os.RemoveAll

* cobra/cmd: add getProject test func
2019-09-17 09:02:42 -06:00

52 lines
1.1 KiB
Go

package cmd
import (
"fmt"
"os"
"testing"
)
func TestGoldenAddCmd(t *testing.T) {
command := &Command{
CmdName: "test",
CmdParent: parentName,
Project: getProject(),
}
defer os.RemoveAll(command.AbsolutePath)
command.Project.Create()
if err := command.Create(); err != nil {
t.Fatal(err)
}
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 {
t.Fatal(err)
}
}
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"},
}
for _, testCase := range testCases {
got := validateCmdName(testCase.input)
if testCase.expected != got {
t.Errorf("Expected %q, got %q", testCase.expected, got)
}
}
}