MCPcopy
hub / github.com/spf13/cobra / TestSetUsageTemplate

Function TestSetUsageTemplate

command_test.go:1129–1166  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1127}
1128
1129func TestSetUsageTemplate(t *testing.T) {
1130 rootCmd := &Command{Use: "root", Run: emptyRun}
1131 childCmd := &Command{Use: "child", Run: emptyRun}
1132 rootCmd.AddCommand(childCmd)
1133
1134 rootCmd.SetUsageTemplate("WORKS {{.UseLine}}")
1135
1136 // Trigger the usage on the root command and check the new template is used
1137 got, err := executeCommand(rootCmd, "--invalid")
1138 if err == nil {
1139 t.Errorf("Expected error but did not get one")
1140 }
1141
1142 expected := "WORKS " + rootCmd.UseLine()
1143 checkStringContains(t, got, expected)
1144
1145 // Trigger the usage on the child command and check
1146 // the new template is inherited from the parent
1147 got, err = executeCommand(rootCmd, "child", "--invalid")
1148 if err == nil {
1149 t.Errorf("Expected error but did not get one")
1150 }
1151
1152 expected = "WORKS " + childCmd.UseLine()
1153 checkStringContains(t, got, expected)
1154
1155 // Reset the root command usage template and make sure
1156 // it falls back to the default
1157 rootCmd.SetUsageTemplate("")
1158 got, err = executeCommand(rootCmd, "--invalid")
1159 if err == nil {
1160 t.Errorf("Expected error but did not get one")
1161 }
1162
1163 if !strings.Contains(got, "Usage:") {
1164 t.Errorf("Expected to contain %q, got %q", "Usage:", got)
1165 }
1166}
1167
1168func TestVersionFlagExecuted(t *testing.T) {
1169 rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}

Callers

nothing calls this directly

Calls 5

AddCommandMethod · 0.95
SetUsageTemplateMethod · 0.95
UseLineMethod · 0.95
executeCommandFunction · 0.85
checkStringContainsFunction · 0.70

Tested by

no test coverage detected