diff --git a/cmd/tink-cli/cmd/docs.go b/cmd/tink-cli/cmd/docs.go index e3a6de25a..cd5adb0b5 100644 --- a/cmd/tink-cli/cmd/docs.go +++ b/cmd/tink-cli/cmd/docs.go @@ -14,7 +14,7 @@ var ( // docsCmd returns the generate command that, when run, generates // documentation func docsCmd(name string) *cobra.Command { - return &cobra.Command{ + cmd := &cobra.Command{ Use: "docs [markdown|man]", Short: "Generate documentation", Hidden: true, @@ -37,10 +37,12 @@ func docsCmd(name string) *cobra.Command { return nil }, } + cmd.Flags().StringVarP(&docsPath, "path", "p", "", "Path where documentation will be generated") + return cmd } func init() { docsCmd := docsCmd(rootCmd.CalledAs()) - docsCmd.Flags().StringVarP(&docsPath, "path", "p", "", "Path where documentation will be generated") + rootCmd.AddCommand(docsCmd) } diff --git a/cmd/tink-cli/cmd/docs_test.go b/cmd/tink-cli/cmd/docs_test.go new file mode 100644 index 000000000..e055486dc --- /dev/null +++ b/cmd/tink-cli/cmd/docs_test.go @@ -0,0 +1,143 @@ +package cmd + +import ( + "bytes" + "io/ioutil" + "os" + "path" + "strings" + "testing" + + "github.com/spf13/cobra" +) + +const ( + testCommand = "tink-cli" +) + +func Test_docsCmd(t *testing.T) { + type args struct { + name string + } + tests := []struct { + name string + args args + want *cobra.Command + cmdFunc func(*testing.T, *cobra.Command) + }{ + { + name: "NoArgs", + args: args{name: testCommand}, + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + root := c.Root() + root.SetArgs([]string{"docs"}) + if err := root.Execute(); err == nil { + t.Error("expected an error") + } + }, + }, + { + name: "Help", + args: args{name: testCommand}, + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + root := c.Root() + out := &bytes.Buffer{} + root.SetArgs([]string{"docs", "--help"}) + root.SetOutput(out) + if err := root.Execute(); err != nil { + t.Error(err) + } + if !strings.Contains(out.String(), "markdown") { + t.Error("expected help to include markdown") + } + }, + }, + { + name: "Markdown", + args: args{name: testCommand}, + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + dir, err := ioutil.TempDir("", "tink-test-*") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + root := c.Root() + root.SetArgs([]string{"docs", "markdown", "--path", dir}) + + if err := root.Execute(); err != nil { + t.Error(err) + } + + expectFile := testCommand + ".md" + _, err = os.Stat(path.Join(dir, expectFile)) + + if os.IsNotExist(err) { + t.Errorf("expected to create %s: %s", expectFile, err) + } + + if err != nil { + t.Error(err) + } + }, + }, + { + name: "Man", + args: args{name: testCommand}, + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + dir, err := ioutil.TempDir("", "tink-test-*") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + root := c.Root() + root.SetArgs([]string{"docs", "man", "--path", dir}) + + if err := root.Execute(); err != nil { + t.Error(err) + } + + expectFile := testCommand + ".1" + _, err = os.Stat(path.Join(dir, expectFile)) + + if os.IsNotExist(err) { + t.Errorf("expected to create %s: %s", expectFile, err) + } + + if err != nil { + t.Error(err) + } + }, + }, + { + name: "BadFormat", + args: args{name: testCommand}, + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + root := c.Root() + root.SetArgs([]string{"docs", "invalid"}) + if err := root.Execute(); err == nil { + t.Error("expected error") + } + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rootCmd := &cobra.Command{ + Use: testCommand, + Run: func(_ *cobra.Command, _ []string) {}, + Version: "test", + } + cmd := docsCmd(tt.args.name) + rootCmd.AddCommand(cmd) + tt.cmdFunc(t, cmd) + }) + } +} diff --git a/go.mod b/go.mod index 90e9266b2..b468999e8 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/rollbar/rollbar-go v1.0.2 // indirect github.com/satori/go.uuid v1.2.0 github.com/sirupsen/logrus v1.4.1 + github.com/spf13/afero v1.1.2 github.com/spf13/cobra v1.0.0 github.com/spf13/viper v1.4.0 github.com/stretchr/testify v1.3.0