Skip to content

Commit

Permalink
add tests for tink-cli docs command
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague committed Sep 2, 2020
1 parent 5f136fd commit f0596c4
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cmd/tink-cli/cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
143 changes: 143 additions & 0 deletions cmd/tink-cli/cmd/docs_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f0596c4

Please sign in to comment.