|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 |
| -//nolint:goconst |
16 | 15 | package cobra
|
17 | 16 |
|
18 | 17 | import (
|
@@ -1080,189 +1079,194 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
|
1080 | 1079 | checkStringContains(t, output, childCmd.Long)
|
1081 | 1080 | }
|
1082 | 1081 |
|
1083 |
| -func TestHelpOverrideOnRoot(t *testing.T) { |
1084 |
| - var helpCalled bool |
1085 |
| - rootCmd := &Command{Use: "root"} |
1086 |
| - rootCmd.SetHelpFunc(func(c *Command, args []string) { |
1087 |
| - helpCalled = true |
1088 |
| - if c.Name() != "root" { |
1089 |
| - t.Errorf(`Expected command name: "root", got %q`, c.Name()) |
| 1082 | +type overridingHelp struct { |
| 1083 | + expectedCmdName string |
| 1084 | + expectedArgs []string |
| 1085 | + |
| 1086 | + helpCalled bool |
| 1087 | + err error |
| 1088 | +} |
| 1089 | + |
| 1090 | +func (o *overridingHelp) helpFunc() func(c *Command, args []string) { |
| 1091 | + return func(c *Command, args []string) { |
| 1092 | + o.helpCalled = true |
| 1093 | + if c.Name() != o.expectedCmdName { |
| 1094 | + o.err = fmt.Errorf("Expected command name: %q, got %q", o.expectedCmdName, c.Name()) |
| 1095 | + return |
| 1096 | + } |
| 1097 | + |
| 1098 | + if len(args) != len(o.expectedArgs) { |
| 1099 | + o.err = fmt.Errorf("Expected args %v, got %v", o.expectedArgs, args) |
| 1100 | + return |
1090 | 1101 | }
|
1091 |
| - if len(args) != 2 || args[0] != "arg1" || args[1] != "arg2" { |
1092 |
| - t.Errorf("Expected args [args1 arg2], got %v", args) |
| 1102 | + |
| 1103 | + for i, arg := range o.expectedArgs { |
| 1104 | + if args[i] != arg { |
| 1105 | + o.err = fmt.Errorf("Expected args %v, got %v", o.expectedArgs, args) |
| 1106 | + return |
| 1107 | + } |
1093 | 1108 | }
|
1094 |
| - }) |
| 1109 | + } |
| 1110 | +} |
| 1111 | + |
| 1112 | +func (o *overridingHelp) checkError() error { |
| 1113 | + if o.err != nil { |
| 1114 | + return o.err |
| 1115 | + } |
| 1116 | + if !o.helpCalled { |
| 1117 | + return fmt.Errorf("Overridden help function not called") |
| 1118 | + } |
| 1119 | + return nil |
| 1120 | +} |
| 1121 | + |
| 1122 | +func TestHelpOverrideOnRoot(t *testing.T) { |
| 1123 | + rootCmd := &Command{Use: "root"} |
| 1124 | + |
| 1125 | + override := overridingHelp{ |
| 1126 | + expectedCmdName: rootCmd.Name(), |
| 1127 | + expectedArgs: []string{"arg1", "arg2"}, |
| 1128 | + } |
| 1129 | + rootCmd.SetHelpFunc(override.helpFunc()) |
1095 | 1130 |
|
1096 | 1131 | _, err := executeCommand(rootCmd, "arg1", "arg2", "--help")
|
1097 | 1132 | if err != nil {
|
1098 |
| - t.Errorf("Unexpected error: %v", err) |
| 1133 | + t.Errorf("Unexpected error executing command: %v", err) |
1099 | 1134 | }
|
1100 | 1135 |
|
1101 |
| - if !helpCalled { |
1102 |
| - t.Error("Overridden help function not called") |
| 1136 | + if err = override.checkError(); err != nil { |
| 1137 | + t.Errorf("Unexpected error from help function: %v", err) |
1103 | 1138 | }
|
1104 | 1139 | }
|
1105 | 1140 |
|
1106 | 1141 | func TestHelpOverrideOnChild(t *testing.T) {
|
1107 |
| - var helpCalled bool |
1108 | 1142 | rootCmd := &Command{Use: "root"}
|
1109 | 1143 | subCmd := &Command{Use: "child"}
|
1110 | 1144 | rootCmd.AddCommand(subCmd)
|
1111 | 1145 |
|
1112 |
| - subCmd.SetHelpFunc(func(c *Command, args []string) { |
1113 |
| - helpCalled = true |
1114 |
| - if c.Name() != "child" { |
1115 |
| - t.Errorf(`Expected command name: "child", got %q`, c.Name()) |
1116 |
| - } |
1117 |
| - if len(args) != 2 || args[0] != "arg1" || args[1] != "arg2" { |
1118 |
| - t.Errorf("Expected args [args1 arg2], got %v", args) |
1119 |
| - } |
1120 |
| - }) |
| 1146 | + override := overridingHelp{ |
| 1147 | + expectedCmdName: subCmd.Name(), |
| 1148 | + expectedArgs: []string{"arg1", "arg2"}, |
| 1149 | + } |
| 1150 | + subCmd.SetHelpFunc(override.helpFunc()) |
1121 | 1151 |
|
1122 | 1152 | _, err := executeCommand(rootCmd, "child", "arg1", "arg2", "--help")
|
1123 | 1153 | if err != nil {
|
1124 |
| - t.Errorf("Unexpected error: %v", err) |
| 1154 | + t.Errorf("Unexpected error executing command: %v", err) |
1125 | 1155 | }
|
1126 | 1156 |
|
1127 |
| - if !helpCalled { |
1128 |
| - t.Error("Overridden help function not called") |
| 1157 | + if err = override.checkError(); err != nil { |
| 1158 | + t.Errorf("Unexpected error from help function: %v", err) |
1129 | 1159 | }
|
1130 | 1160 | }
|
1131 | 1161 |
|
1132 | 1162 | func TestHelpOverrideOnRootWithChild(t *testing.T) {
|
1133 |
| - var helpCalled bool |
1134 | 1163 | rootCmd := &Command{Use: "root"}
|
1135 | 1164 | subCmd := &Command{Use: "child"}
|
1136 | 1165 | rootCmd.AddCommand(subCmd)
|
1137 | 1166 |
|
1138 |
| - rootCmd.SetHelpFunc(func(c *Command, args []string) { |
1139 |
| - helpCalled = true |
1140 |
| - if c.Name() != "child" { |
1141 |
| - t.Errorf(`Expected command name: "child", got %q`, c.Name()) |
1142 |
| - } |
1143 |
| - if len(args) != 2 || args[0] != "arg1" || args[1] != "arg2" { |
1144 |
| - t.Errorf("Expected args [args1 arg2], got %v", args) |
1145 |
| - } |
1146 |
| - }) |
| 1167 | + override := overridingHelp{ |
| 1168 | + expectedCmdName: subCmd.Name(), |
| 1169 | + expectedArgs: []string{"arg1", "arg2"}, |
| 1170 | + } |
| 1171 | + rootCmd.SetHelpFunc(override.helpFunc()) |
1147 | 1172 |
|
1148 | 1173 | _, err := executeCommand(rootCmd, "child", "arg1", "arg2", "--help")
|
1149 | 1174 | if err != nil {
|
1150 |
| - t.Errorf("Unexpected error: %v", err) |
| 1175 | + t.Errorf("Unexpected error executing command: %v", err) |
1151 | 1176 | }
|
1152 | 1177 |
|
1153 |
| - if !helpCalled { |
1154 |
| - t.Error("Overridden help function not called") |
| 1178 | + if err = override.checkError(); err != nil { |
| 1179 | + t.Errorf("Unexpected error from help function: %v", err) |
1155 | 1180 | }
|
1156 | 1181 | }
|
1157 | 1182 |
|
1158 | 1183 | func TestHelpOverrideOnRootWithChildAndFlags(t *testing.T) {
|
1159 |
| - var helpCalled bool |
1160 | 1184 | rootCmd := &Command{Use: "root"}
|
1161 | 1185 | subCmd := &Command{Use: "child"}
|
1162 | 1186 | rootCmd.AddCommand(subCmd)
|
1163 | 1187 |
|
1164 | 1188 | var myFlag bool
|
1165 | 1189 | subCmd.Flags().BoolVar(&myFlag, "myflag", false, "")
|
1166 | 1190 |
|
1167 |
| - rootCmd.SetHelpFunc(func(c *Command, args []string) { |
1168 |
| - helpCalled = true |
1169 |
| - if c.Name() != "child" { |
1170 |
| - t.Errorf(`Expected command name: "child", got %q`, c.Name()) |
1171 |
| - } |
1172 |
| - if len(args) != 2 || args[0] != "arg1" || args[1] != "arg2" { |
1173 |
| - t.Errorf("Expected args [args1 arg2], got %v", args) |
1174 |
| - } |
1175 |
| - }) |
| 1191 | + override := overridingHelp{ |
| 1192 | + expectedCmdName: subCmd.Name(), |
| 1193 | + expectedArgs: []string{"arg1", "arg2"}, |
| 1194 | + } |
| 1195 | + rootCmd.SetHelpFunc(override.helpFunc()) |
1176 | 1196 |
|
1177 | 1197 | _, err := executeCommand(rootCmd, "child", "arg1", "--myflag", "arg2", "--help")
|
1178 | 1198 | if err != nil {
|
1179 |
| - t.Errorf("Unexpected error: %v", err) |
| 1199 | + t.Errorf("Unexpected error executing command: %v", err) |
1180 | 1200 | }
|
1181 | 1201 |
|
1182 |
| - if !helpCalled { |
1183 |
| - t.Error("Overridden help function not called") |
| 1202 | + if err = override.checkError(); err != nil { |
| 1203 | + t.Errorf("Unexpected error from help function: %v", err) |
1184 | 1204 | }
|
1185 | 1205 | }
|
1186 | 1206 |
|
1187 | 1207 | func TestHelpOverrideOnRootWithChildAndFlagsButParsingDisabled(t *testing.T) {
|
1188 |
| - var helpCalled bool |
1189 | 1208 | rootCmd := &Command{Use: "root"}
|
1190 | 1209 | subCmd := &Command{Use: "child", DisableFlagParsing: true}
|
1191 | 1210 | rootCmd.AddCommand(subCmd)
|
1192 | 1211 |
|
1193 | 1212 | var myFlag bool
|
1194 | 1213 | subCmd.Flags().BoolVar(&myFlag, "myflag", false, "")
|
1195 | 1214 |
|
1196 |
| - rootCmd.SetHelpFunc(func(c *Command, args []string) { |
1197 |
| - helpCalled = true |
1198 |
| - if c.Name() != "child" { |
1199 |
| - t.Errorf(`Expected command name: "child", got %q`, c.Name()) |
1200 |
| - } |
1201 |
| - if len(args) != 4 || |
1202 |
| - args[0] != "arg1" || args[1] != "--myflag" || args[2] != "arg2" || args[3] != "--help" { |
1203 |
| - t.Errorf("Expected args [args1 --myflag arg2 --help], got %v", args) |
1204 |
| - } |
1205 |
| - }) |
| 1215 | + override := overridingHelp{ |
| 1216 | + expectedCmdName: subCmd.Name(), |
| 1217 | + expectedArgs: []string{"arg1", "--myflag", "arg2", "--help"}, |
| 1218 | + } |
| 1219 | + rootCmd.SetHelpFunc(override.helpFunc()) |
1206 | 1220 |
|
1207 | 1221 | _, err := executeCommand(rootCmd, "child", "arg1", "--myflag", "arg2", "--help")
|
1208 | 1222 | if err != nil {
|
1209 |
| - t.Errorf("Unexpected error: %v", err) |
| 1223 | + t.Errorf("Unexpected error executing command: %v", err) |
1210 | 1224 | }
|
1211 | 1225 |
|
1212 |
| - if !helpCalled { |
1213 |
| - t.Error("Overridden help function not called") |
| 1226 | + if err = override.checkError(); err != nil { |
| 1227 | + t.Errorf("Unexpected error from help function: %v", err) |
1214 | 1228 | }
|
1215 | 1229 | }
|
1216 | 1230 |
|
1217 | 1231 | func TestHelpCommandOverrideOnChild(t *testing.T) {
|
1218 |
| - var helpCalled bool |
1219 | 1232 | rootCmd := &Command{Use: "root"}
|
1220 | 1233 | subCmd := &Command{Use: "child"}
|
1221 | 1234 | rootCmd.AddCommand(subCmd)
|
1222 | 1235 |
|
1223 |
| - subCmd.SetHelpFunc(func(c *Command, args []string) { |
1224 |
| - helpCalled = true |
1225 |
| - if c.Name() != "child" { |
1226 |
| - t.Errorf(`Expected command name: "child", got %q`, c.Name()) |
1227 |
| - } |
1228 |
| - if len(args) != 2 || args[0] != "arg1" || args[1] != "arg2" { |
1229 |
| - t.Errorf("Expected args [args1 arg2], got %v", args) |
1230 |
| - } |
1231 |
| - }) |
| 1236 | + override := overridingHelp{ |
| 1237 | + expectedCmdName: subCmd.Name(), |
| 1238 | + expectedArgs: []string{"arg1", "arg2"}, |
| 1239 | + } |
| 1240 | + subCmd.SetHelpFunc(override.helpFunc()) |
1232 | 1241 |
|
1233 | 1242 | _, err := executeCommand(rootCmd, "help", "child", "arg1", "arg2")
|
1234 | 1243 | if err != nil {
|
1235 |
| - t.Errorf("Unexpected error: %v", err) |
| 1244 | + t.Errorf("Unexpected error executing command: %v", err) |
1236 | 1245 | }
|
1237 | 1246 |
|
1238 |
| - if !helpCalled { |
1239 |
| - t.Error("Overridden help function not called") |
| 1247 | + if err = override.checkError(); err != nil { |
| 1248 | + t.Errorf("Unexpected error from help function: %v", err) |
1240 | 1249 | }
|
1241 | 1250 | }
|
1242 | 1251 |
|
1243 | 1252 | func TestHelpCommandOverrideOnRootWithChild(t *testing.T) {
|
1244 |
| - var helpCalled bool |
1245 | 1253 | rootCmd := &Command{Use: "root"}
|
1246 | 1254 | subCmd := &Command{Use: "child"}
|
1247 | 1255 | rootCmd.AddCommand(subCmd)
|
1248 | 1256 |
|
1249 |
| - rootCmd.SetHelpFunc(func(c *Command, args []string) { |
1250 |
| - helpCalled = true |
1251 |
| - if c.Name() != "child" { |
1252 |
| - t.Errorf(`Expected command name: "child", got %q`, c.Name()) |
1253 |
| - } |
1254 |
| - if len(args) != 2 || args[0] != "arg1" || args[1] != "arg2" { |
1255 |
| - t.Errorf("Expected args [args1 arg2], got %v", args) |
1256 |
| - } |
1257 |
| - }) |
| 1257 | + override := overridingHelp{ |
| 1258 | + expectedCmdName: subCmd.Name(), |
| 1259 | + expectedArgs: []string{"arg1", "arg2"}, |
| 1260 | + } |
| 1261 | + rootCmd.SetHelpFunc(override.helpFunc()) |
1258 | 1262 |
|
1259 | 1263 | _, err := executeCommand(rootCmd, "help", "child", "arg1", "arg2")
|
1260 | 1264 | if err != nil {
|
1261 |
| - t.Errorf("Unexpected error: %v", err) |
| 1265 | + t.Errorf("Unexpected error executing command: %v", err) |
1262 | 1266 | }
|
1263 | 1267 |
|
1264 |
| - if !helpCalled { |
1265 |
| - t.Error("Overridden help function not called") |
| 1268 | + if err = override.checkError(); err != nil { |
| 1269 | + t.Errorf("Unexpected error from help function: %v", err) |
1266 | 1270 | }
|
1267 | 1271 | }
|
1268 | 1272 |
|
|
0 commit comments