X Tutup
Skip to content

Commit 7ecb6a4

Browse files
committed
Add tests for help topics
1 parent 2144921 commit 7ecb6a4

File tree

3 files changed

+83
-22
lines changed

3 files changed

+83
-22
lines changed

pkg/cmd/root/help_topic.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package root
22

33
import (
4-
"fmt"
5-
64
"github.com/MakeNowJust/heredoc"
75
"github.com/spf13/cobra"
86
)
@@ -41,6 +39,7 @@ func NewHelpTopic(topic string) *cobra.Command {
4139
Long: topicContent[topic],
4240
Hidden: true,
4341
Args: cobra.NoArgs,
42+
Run: helpTopicHelpFunc,
4443
}
4544

4645
cmd.SetHelpFunc(helpTopicHelpFunc)
@@ -50,27 +49,10 @@ func NewHelpTopic(topic string) *cobra.Command {
5049
}
5150

5251
func helpTopicHelpFunc(command *cobra.Command, args []string) {
53-
if len(args) >= 2 && args[1] != "--help" && args[1] != "-h" {
54-
command.Printf("unknown command %q for %q\n", args[1], command.CommandPath())
55-
56-
if args[1] == "help" {
57-
command.Print("\nDid you mean this?\n")
58-
command.Printf("\t%s\n\n", "--help")
59-
} else {
60-
command.Printf("\n")
61-
}
62-
63-
helpTopicUsageFunc(command)
64-
command.Printf("\n")
65-
hasFailed = true
66-
return
67-
}
68-
69-
fmt.Fprint(command.OutOrStdout(), command.Long)
52+
command.Print(command.Long)
7053
}
7154

7255
func helpTopicUsageFunc(command *cobra.Command) error {
73-
command.Printf("Usage: gh help %s", command.Use)
74-
56+
command.Printf("Usage: gh help %s", command.Use)
7557
return nil
7658
}

pkg/cmd/root/help_topic_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package root
2+
3+
import (
4+
"testing"
5+
6+
"github.com/cli/cli/pkg/iostreams"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewHelpTopic(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
topic string
14+
args []string
15+
flags []string
16+
wantsErr bool
17+
}{
18+
{
19+
name: "valid topic",
20+
topic: "environment",
21+
args: []string{},
22+
flags: []string{},
23+
wantsErr: false,
24+
},
25+
{
26+
name: "invalid topic",
27+
topic: "invalid",
28+
args: []string{},
29+
flags: []string{},
30+
wantsErr: false,
31+
},
32+
{
33+
name: "more than zero args",
34+
topic: "environment",
35+
args: []string{"invalid"},
36+
flags: []string{},
37+
wantsErr: true,
38+
},
39+
{
40+
name: "more than zero flags",
41+
topic: "environment",
42+
args: []string{},
43+
flags: []string{"--invalid"},
44+
wantsErr: true,
45+
},
46+
{
47+
name: "help arg",
48+
topic: "environment",
49+
args: []string{"help"},
50+
flags: []string{},
51+
wantsErr: true,
52+
},
53+
{
54+
name: "help flag",
55+
topic: "environment",
56+
args: []string{},
57+
flags: []string{"--help"},
58+
wantsErr: false,
59+
},
60+
}
61+
62+
for _, tt := range tests {
63+
t.Run(tt.name, func(t *testing.T) {
64+
_, _, stdout, stderr := iostreams.Test()
65+
66+
cmd := NewHelpTopic(tt.topic)
67+
cmd.SetArgs(append(tt.args, tt.flags...))
68+
cmd.SetOut(stdout)
69+
cmd.SetErr(stderr)
70+
71+
_, err := cmd.ExecuteC()
72+
if tt.wantsErr {
73+
assert.Error(t, err)
74+
return
75+
}
76+
assert.NoError(t, err)
77+
})
78+
}
79+
}

pkg/cmd/root/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
9090
cmd.AddCommand(gistCmd.NewCmdGist(f))
9191
cmd.AddCommand(NewCmdCompletion(f.IOStreams))
9292

93-
// Help Topics
93+
// Help topics
9494
cmd.AddCommand(NewHelpTopic("environment"))
9595

9696
// the `api` command should not inherit any extra HTTP headers

0 commit comments

Comments
 (0)
X Tutup