diff --git a/cmd/root_cmd_test.go b/cmd/root_cmd_test.go new file mode 100755 index 0000000..1ab6169 --- /dev/null +++ b/cmd/root_cmd_test.go @@ -0,0 +1,100 @@ +package cmd + +import ( + "bytes" + "errors" + "strings" + "testing" + + "github.com/matryer/is" + "github.com/spf13/cobra" +) + +func TestRootCmd(t *testing.T) { + is := is.New(t) + + // root := &cobra.Command{Use: "root"} + // cmd.RootCmdFlags(root) + + err := rootCmd.Execute() + + is.NoErr(err) +} + +func execute(t *testing.T, c *cobra.Command, args ...string) (string, error) { + t.Helper() + + buf := new(bytes.Buffer) + c.SetOut(buf) + c.SetErr(buf) + c.SetArgs(args) + + err := c.Execute() + return strings.TrimSpace(buf.String()), err +} + +func TestSubCmd(t *testing.T) { + is := is.New(t) + + testcases := []struct { + args []string + out_check bool + err error + out string + }{ + {[]string{}, false, nil, ""}, + {[]string{"wrong"}, true, errors.New("unknown command \"wrong\" for \"tks\""), ""}, + // {[]string{"wrong", "cmd"}, true, errors.New("unknown command \"wrong\" for \"tks\""), ""}, + + {[]string{"completion"}, false, nil, ""}, + {[]string{"completion", "bash"}, false, nil, ""}, + {[]string{"completion", "fish"}, false, nil, ""}, + {[]string{"completion", "powershell"}, false, nil, ""}, + {[]string{"completion", "zsh"}, false, nil, ""}, + + {[]string{"--config"}, true, errors.New("flag needs an argument: --config"), ""}, + {[]string{"-t"}, false, nil, ""}, + + {[]string{"cluster"}, true, nil, ""}, + {[]string{"cluster", "wrong"}, true, nil, ""}, + {[]string{"cluster", "create"}, true, errors.New("required flag(s) \"contract-id\", \"csp-id\" not set"), ""}, + {[]string{"cluster", "create", "--config"}, true, errors.New("flag needs an argument: --config"), ""}, + {[]string{"cluster", "create", "--config", "xx"}, true, errors.New("required flag(s) \"contract-id\", \"csp-id\" not set"), ""}, + {[]string{"cluster", "create", "-h"}, false, nil, "Create a TACO Cluster to AWS."}, + {[]string{"cluster", "create", "--contract-id"}, true, errors.New("flag needs an argument: --contract-id"), ""}, + {[]string{"cluster", "create", "--contract-id", "1234"}, false, nil, ""}, + {[]string{"cluster", "create", "--csp-id"}, false, errors.New("flag needs an argument: --csp-id"), ""}, + {[]string{"cluster", "create", "--contract-id", "--csp-id"}, false, nil, ""}, + {[]string{"cluster", "create", "--contract-id", "abcd-efg", "--csp-id"}, true, errors.New("flag needs an argument: --csp-id"), ""}, + {[]string{"cluster", "create", "--contract-id", "--csp-id", "2345234", "hihi"}, false, nil, "Create a TACO Cluster to AWS"}, + {[]string{"cluster", "create", "--contract-id", "--csp-id", "2345234", "hihi", "--config"}, true, errors.New("flag needs an argument: --config"), ""}, + {[]string{"cluster", "create", "--contract-id", "abcd-efg", "--csp-id", "2345234", "hihi"}, false, nil, "Create a TACO Cluster to AWS"}, + + {[]string{"cluster", "list"}, true, nil, ""}, + {[]string{"cluster", "list", "-h"}, false, nil, "A longer description that spans multiple lines and likely contains examples"}, + {[]string{"cluster", "list", "--config", "xx"}, false, nil, "A longer description that spans multiple lines and likely contains examples"}, + {[]string{"cluster", "list", "--config"}, true, errors.New("flag needs an argument: --config"), ""}, + + {[]string{"endpoint"}, true, nil, ""}, + {[]string{"endpoint", "register"}, true, nil, ""}, + {[]string{"endpoint", "wrong"}, true, nil, ""}, + {[]string{"service"}, true, nil, ""}, + + {[]string{"help"}, false, nil, ""}, + {[]string{"-t"}, false, nil, ""}, + {[]string{"-h"}, false, nil, ""}, + + {[]string{"register"}, true, errors.New("unknown command \"register\" for \"tks\""), "Run 'tks --help' for usage."}, + {[]string{"abcd"}, true, errors.New("unknown command \"abcd\" for \"tks\""), ""}, + } + + for _, tc := range testcases { + out, err := execute(t, rootCmd, tc.args...) + + is.Equal(tc.err, err) + + if tc.err == nil && tc.out_check { + is.Equal(tc.out, out) + } + } +} diff --git a/go.mod b/go.mod index 5896a5f..b8eeff3 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,12 @@ go 1.16 require ( github.com/golang/mock v1.6.0 github.com/kr/text v0.2.0 // indirect + github.com/matryer/is v1.4.0 github.com/openinfradev/tks-contract v0.1.1-0.20210928021110-fe2b666327cc github.com/openinfradev/tks-proto v0.0.6-0.20211015003551-ed8f9541f40d github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.9.0 + github.com/stretchr/testify v1.7.0 golang.org/x/net v0.0.0-20211020060615-d418f374d309 // indirect golang.org/x/sys v0.0.0-20211023085530-d6a326fbbf70 // indirect google.golang.org/genproto v0.0.0-20211021150943-2b146023228c // indirect diff --git a/go.sum b/go.sum index 4a42e84..e655226 100644 --- a/go.sum +++ b/go.sum @@ -365,6 +365,8 @@ github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0U github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= +github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= +github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=