Skip to content

Commit b799d8d

Browse files
committedMar 19, 2025
emit an error and exit if the ginkgo invocation includes flags after positional arguments
1 parent a565d1f commit b799d8d

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed
 

‎ginkgo/command/command.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ func (c Command) Run(args []string, additionalArgs []string) {
2424
if err != nil {
2525
AbortWithUsage(err.Error())
2626
}
27-
27+
for _, arg := range args {
28+
if strings.HasPrefix(arg, "-") {
29+
AbortWith("Malformed arguments - make sure all flags appear {{bold}}after{{/}} the Ginkgo subcommand and {{bold}}before{{/}} your list of packages.\n{{gray}}e.g. 'ginkgo run -p my_package' is valid `ginkgo -p run my_package` is not.{{/}}")
30+
}
31+
}
2832
c.Command(args, additionalArgs)
2933
}
3034

‎ginkgo/command/command_test.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ var _ = Describe("Command", func() {
2121
fs, err := types.NewGinkgoFlagSet(
2222
types.GinkgoFlags{
2323
{Name: "contrabulaturally", KeyPath: "C", Usage: "with irridiacy"},
24+
{Name: "fillabluster", KeyPath: "F", Usage: "with grace"},
2425
},
25-
&(struct{ C int }{C: 17}),
26+
&(struct {
27+
C int
28+
F int
29+
}{C: 17, F: 12}),
2630
types.GinkgoFlagSections{},
2731
)
2832
Ω(err).ShouldNot(HaveOccurred())
@@ -41,7 +45,7 @@ var _ = Describe("Command", func() {
4145
Context("when flags fails to parse", func() {
4246
It("aborts with usage", func() {
4347
Ω(func() {
44-
c.Run([]string{"-not-a-flag=oops"}, []string{"additional", "args"})
48+
c.Run([]string{"-not-a-flag=oops"}, []string{"additional", "-args"})
4549
}).Should(PanicWith(SatisfyAll(
4650
HaveField("ExitCode", 1),
4751
HaveField("Error", HaveOccurred()),
@@ -54,14 +58,29 @@ var _ = Describe("Command", func() {
5458

5559
Context("when flags parse", func() {
5660
It("runs the command", func() {
57-
c.Run([]string{"-contrabulaturally=16", "and-an-arg", "and-another"}, []string{"additional", "args"})
61+
c.Run([]string{"-contrabulaturally=16", "and-an-arg", "and-another"}, []string{"additional", "-args"})
5862
Ω(rt).Should(HaveRun("enflabulate"))
5963

6064
Ω(rt.DataFor("enflabulate")["Args"]).Should(Equal([]string{"and-an-arg", "and-another"}))
61-
Ω(rt.DataFor("enflabulate")["AdditionalArgs"]).Should(Equal([]string{"additional", "args"}))
65+
Ω(rt.DataFor("enflabulate")["AdditionalArgs"]).Should(Equal([]string{"additional", "-args"}))
6266

6367
})
6468
})
69+
70+
Context("when flags appear after named arguments", func() {
71+
It("fails", func() {
72+
Ω(func() {
73+
c.Run([]string{"-contrabulaturally=16", "an-arg", "another-arg", "-fillabuster=10"}, []string{"additional", "-args"})
74+
}).Should(PanicWith(SatisfyAll(
75+
HaveField("ExitCode", 1),
76+
HaveField("Error", HaveOccurred()),
77+
HaveField("EmitUsage", BeFalse()),
78+
)))
79+
80+
Ω(rt).Should(HaveTrackedNothing())
81+
})
82+
})
83+
6584
})
6685

6786
Describe("Usage", func() {
@@ -85,6 +104,8 @@ var _ = Describe("Command", func() {
85104
"",
86105
" --contrabulaturally{{/}} [int] {{gray}}{{/}}",
87106
" {{light-gray}}with irridiacy{{/}}",
107+
" --fillabluster{{/}} [int] {{gray}}{{/}}",
108+
" {{light-gray}}with grace{{/}}",
88109
"", "",
89110
}, "\n")
90111

‎ginkgo/command/program_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ var _ = Describe("Program", func() {
117117

118118
Context("when passed arguments and additional arguments", func() {
119119
BeforeEach(func() {
120-
program.RunAndExit([]string{"omicron", "gamma", "arg1", "-arg2", "--", "addArg1", "addArg2"})
120+
program.RunAndExit([]string{"omicron", "gamma", "arg1", "arg2", "--", "addArg1", "addArg2"})
121121
})
122122
It("passes both in", func() {
123123
Ω(rt).Should(HaveTracked("gamma", "exit"))
124-
Ω(rt).Should(HaveRunWithData("gamma", "Args", []string{"arg1", "-arg2"}, "AdditionalArgs", []string{"addArg1", "addArg2"}))
124+
Ω(rt).Should(HaveRunWithData("gamma", "Args", []string{"arg1", "arg2"}, "AdditionalArgs", []string{"addArg1", "addArg2"}))
125125
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
126126
Ω(buf.Contents()).Should(BeEmpty())
127127
})

0 commit comments

Comments
 (0)
Please sign in to comment.