Skip to content

Commit 5229931

Browse files
committed
chore: add addl version inference tests
1 parent b4bebd5 commit 5229931

1 file changed

Lines changed: 40 additions & 21 deletions

File tree

command_test.go

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ func TestShortVersionFlagOnlyAddedToRoot(t *testing.T) {
13411341
}
13421342
}
13431343

1344-
func TestVersionInference(t *testing.T) {
1344+
func TestDirectVersionInference(t *testing.T) {
13451345
rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}
13461346
sub1 := &Command{Use: "sub1", Version: "2.0.0", Run: emptyRun}
13471347
sub2 := &Command{Use: "sub2", Version: "1.0.1", Run: emptyRun}
@@ -1360,6 +1360,25 @@ func TestVersionInference(t *testing.T) {
13601360
}
13611361
}
13621362

1363+
func TestDistantVersionInference(t *testing.T) {
1364+
rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}
1365+
sub1 := &Command{Use: "sub1", Run: emptyRun}
1366+
sub2 := &Command{Use: "sub2", Run: emptyRun}
1367+
sub1a := &Command{Use: "sub1a", Run: emptyRun}
1368+
sub2a := &Command{Use: "sub2a", Run: emptyRun}
1369+
rootCmd.AddCommand(sub1, sub2)
1370+
sub1.AddCommand(sub1a)
1371+
sub2.AddCommand(sub2a)
1372+
testCases := []*Command{sub1a, sub2a}
1373+
for _, cmd := range testCases {
1374+
output, err := executeCommand(rootCmd, cmd.Parent().Name(), cmd.Name(), "-v")
1375+
if err != nil {
1376+
t.Errorf("Unexpected error: %v", err)
1377+
}
1378+
checkStringContains(t, output, fmt.Sprintf("%s version %s", cmd.Name(), cmd.Parent().Parent().Version))
1379+
}
1380+
}
1381+
13631382
func TestVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) {
13641383
rootCmd := &Command{Use: "root", Run: emptyRun}
13651384

@@ -1404,8 +1423,8 @@ func TestShorthandVersionFlagOnlyAddedIfVersionNotDefined(t *testing.T) {
14041423
}
14051424

14061425
func TestUsageIsNotPrintedTwice(t *testing.T) {
1407-
var cmd = &Command{Use: "root"}
1408-
var sub = &Command{Use: "sub"}
1426+
cmd := &Command{Use: "root"}
1427+
sub := &Command{Use: "sub"}
14091428
cmd.AddCommand(sub)
14101429

14111430
output, _ := executeCommand(cmd, "")
@@ -1594,7 +1613,6 @@ func TestCaseSensitivityBackwardCompatibility(t *testing.T) {
15941613
if err == nil {
15951614
t.Error("Expected error on calling a command in upper case while command names are case sensitive. Got nil.")
15961615
}
1597-
15981616
}
15991617

16001618
func TestRemoveCommand(t *testing.T) {
@@ -1931,7 +1949,7 @@ func TestCommandsAreSorted(t *testing.T) {
19311949
originalNames := []string{"middle", "zlast", "afirst"}
19321950
expectedNames := []string{"afirst", "middle", "zlast"}
19331951

1934-
var rootCmd = &Command{Use: "root"}
1952+
rootCmd := &Command{Use: "root"}
19351953

19361954
for _, name := range originalNames {
19371955
rootCmd.AddCommand(&Command{Use: name})
@@ -1952,7 +1970,7 @@ func TestEnableCommandSortingIsDisabled(t *testing.T) {
19521970

19531971
originalNames := []string{"middle", "zlast", "afirst"}
19541972

1955-
var rootCmd = &Command{Use: "root"}
1973+
rootCmd := &Command{Use: "root"}
19561974

19571975
for _, name := range originalNames {
19581976
rootCmd.AddCommand(&Command{Use: name})
@@ -1969,7 +1987,7 @@ func TestEnableCommandSortingIsDisabled(t *testing.T) {
19691987
}
19701988

19711989
func TestUsageWithGroup(t *testing.T) {
1972-
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
1990+
rootCmd := &Command{Use: "root", Short: "test", Run: emptyRun}
19731991
rootCmd.CompletionOptions.DisableDefaultCmd = true
19741992

19751993
rootCmd.AddGroup(&Group{ID: "group1", Title: "group1"})
@@ -1990,7 +2008,7 @@ func TestUsageWithGroup(t *testing.T) {
19902008
}
19912009

19922010
func TestUsageHelpGroup(t *testing.T) {
1993-
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
2011+
rootCmd := &Command{Use: "root", Short: "test", Run: emptyRun}
19942012
rootCmd.CompletionOptions.DisableDefaultCmd = true
19952013

19962014
rootCmd.AddGroup(&Group{ID: "group", Title: "group"})
@@ -2008,7 +2026,7 @@ func TestUsageHelpGroup(t *testing.T) {
20082026
}
20092027

20102028
func TestUsageCompletionGroup(t *testing.T) {
2011-
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
2029+
rootCmd := &Command{Use: "root", Short: "test", Run: emptyRun}
20122030

20132031
rootCmd.AddGroup(&Group{ID: "group", Title: "group"})
20142032
rootCmd.AddGroup(&Group{ID: "help", Title: "help"})
@@ -2028,7 +2046,7 @@ func TestUsageCompletionGroup(t *testing.T) {
20282046
}
20292047

20302048
func TestUngroupedCommand(t *testing.T) {
2031-
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
2049+
rootCmd := &Command{Use: "root", Short: "test", Run: emptyRun}
20322050

20332051
rootCmd.AddGroup(&Group{ID: "group", Title: "group"})
20342052
rootCmd.AddGroup(&Group{ID: "help", Title: "help"})
@@ -2050,7 +2068,7 @@ func TestUngroupedCommand(t *testing.T) {
20502068
}
20512069

20522070
func TestAddGroup(t *testing.T) {
2053-
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
2071+
rootCmd := &Command{Use: "root", Short: "test", Run: emptyRun}
20542072

20552073
rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
20562074
rootCmd.AddCommand(&Command{Use: "cmd", GroupID: "group", Run: emptyRun})
@@ -2064,7 +2082,7 @@ func TestAddGroup(t *testing.T) {
20642082
}
20652083

20662084
func TestWrongGroupFirstLevel(t *testing.T) {
2067-
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
2085+
rootCmd := &Command{Use: "root", Short: "test", Run: emptyRun}
20682086

20692087
rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
20702088
// Use the wrong group ID
@@ -2082,8 +2100,8 @@ func TestWrongGroupFirstLevel(t *testing.T) {
20822100
}
20832101

20842102
func TestWrongGroupNestedLevel(t *testing.T) {
2085-
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
2086-
var childCmd = &Command{Use: "child", Run: emptyRun}
2103+
rootCmd := &Command{Use: "root", Short: "test", Run: emptyRun}
2104+
childCmd := &Command{Use: "child", Run: emptyRun}
20872105
rootCmd.AddCommand(childCmd)
20882106

20892107
childCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
@@ -2102,8 +2120,8 @@ func TestWrongGroupNestedLevel(t *testing.T) {
21022120
}
21032121

21042122
func TestWrongGroupForHelp(t *testing.T) {
2105-
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
2106-
var childCmd = &Command{Use: "child", Run: emptyRun}
2123+
rootCmd := &Command{Use: "root", Short: "test", Run: emptyRun}
2124+
childCmd := &Command{Use: "child", Run: emptyRun}
21072125
rootCmd.AddCommand(childCmd)
21082126

21092127
rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
@@ -2122,8 +2140,8 @@ func TestWrongGroupForHelp(t *testing.T) {
21222140
}
21232141

21242142
func TestWrongGroupForCompletion(t *testing.T) {
2125-
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
2126-
var childCmd = &Command{Use: "child", Run: emptyRun}
2143+
rootCmd := &Command{Use: "root", Short: "test", Run: emptyRun}
2144+
childCmd := &Command{Use: "child", Run: emptyRun}
21272145
rootCmd.AddCommand(childCmd)
21282146

21292147
rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
@@ -2193,7 +2211,6 @@ func TestCommandPrintRedirection(t *testing.T) {
21932211
errBuff, outBuff := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
21942212
root := &Command{
21952213
Run: func(cmd *Command, args []string) {
2196-
21972214
cmd.PrintErr("PrintErr")
21982215
cmd.PrintErrln("PrintErr", "line")
21992216
cmd.PrintErrf("PrintEr%s", "r")
@@ -2718,8 +2735,10 @@ func TestSetContextPersistentPreRun(t *testing.T) {
27182735
}
27192736
}
27202737

2721-
const VersionFlag = "--version"
2722-
const HelpFlag = "--help"
2738+
const (
2739+
VersionFlag = "--version"
2740+
HelpFlag = "--help"
2741+
)
27232742

27242743
func TestNoRootRunCommandExecutedWithVersionSet(t *testing.T) {
27252744
rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description"}

0 commit comments

Comments
 (0)