-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix/update-terraform-setup
- Loading branch information
Showing
6 changed files
with
199 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/cobra/doc" | ||
) | ||
|
||
var ( | ||
docsPath string | ||
) | ||
|
||
// docsCmd returns the generate command that, when run, generates | ||
// documentation | ||
func docsCmd(name string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "docs [markdown|man]", | ||
Short: "Generate documentation", | ||
Hidden: true, | ||
ValidArgs: []string{"markdown", "man"}, | ||
Args: cobra.ExactValidArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
format := args[0] | ||
|
||
switch format { | ||
case "markdown": | ||
return doc.GenMarkdownTree(cmd.Parent(), docsPath) | ||
case "man": | ||
header := &doc.GenManHeader{Title: name} | ||
return doc.GenManTree(cmd.Parent(), header, docsPath) | ||
} | ||
// ValidArgs make this error response dead-code | ||
return fmt.Errorf("unknown format: %q", format) | ||
}, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVarP(&docsPath, "path", "p", "", "Path where documentation will be generated") | ||
return cmd | ||
} | ||
|
||
func init() { | ||
docsCmd := docsCmd(rootCmd.CalledAs()) | ||
|
||
rootCmd.AddCommand(docsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters