1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
package command_test
import (
"fmt"
"strings"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/formatter"
. "github.com/onsi/ginkgo/v2/internal/test_helpers"
"github.com/onsi/ginkgo/v2/types"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/ginkgo/v2/ginkgo/command"
)
var _ = Describe("Program", func() {
var program command.Program
var rt *RunTracker
var buf *gbytes.Buffer
BeforeEach(func() {
rt = NewRunTracker()
defaultCommand := command.Command{Name: "alpha", Usage: "alpha usage", ShortDoc: "such usage!", Command: rt.C("alpha")}
fs, err := types.NewGinkgoFlagSet(
types.GinkgoFlags{
{Name: "decay-rate", KeyPath: "Rate", Usage: "set the decay rate, in years"},
{DeprecatedName: "old", KeyPath: "Old"},
},
&(struct {
Rate float64
Old bool
}{Rate: 17.0}),
types.GinkgoFlagSections{},
)
Ω(err).ShouldNot(HaveOccurred())
commands := []command.Command{
{Name: "beta", Flags: fs, Usage: "beta usage", ShortDoc: "such usage!", Command: rt.C("beta")},
{Name: "gamma", Command: rt.C("gamma")},
{Name: "zeta", Command: rt.C("zeta", func() {
command.Abort(command.AbortDetails{Error: fmt.Errorf("Kaboom!"), ExitCode: 17})
})},
}
deprecatedCommands := []command.DeprecatedCommand{
{Name: "delta", Deprecation: types.Deprecation{Message: "delta is for deprecated"}},
}
formatter.SingletonFormatter.ColorMode = formatter.ColorModePassthrough
buf = gbytes.NewBuffer()
program = command.Program{
Name: "omicron",
Heading: "Omicron v2.0.0",
Commands: commands,
DefaultCommand: defaultCommand,
DeprecatedCommands: deprecatedCommands,
Exiter: func(code int) {
rt.RunWithData("exit", "Code", code)
},
OutWriter: buf,
ErrWriter: buf,
}
})
Context("when called with no subcommand", func() {
BeforeEach(func() {
program.RunAndExit([]string{"omicron"}) //os.Args always includes the name of the program as the first element
})
It("runs the default command", func() {
Ω(rt).Should(HaveTracked("alpha", "exit"))
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
Ω(buf.Contents()).Should(BeEmpty())
})
})
Context("when called with the default command's name", func() {
BeforeEach(func() {
program.RunAndExit([]string{"omicron", "alpha", "args1", "args2"})
})
It("runs the default command", func() {
Ω(rt).Should(HaveTracked("alpha", "exit"))
Ω(rt).Should(HaveRunWithData("alpha", "Args", []string{"args1", "args2"}))
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
Ω(buf.Contents()).Should(BeEmpty())
})
})
Context("when called with a subcommand", func() {
BeforeEach(func() {
program.RunAndExit([]string{"omicron", "beta"})
})
It("runs that subcommand", func() {
Ω(rt).Should(HaveTracked("beta", "exit"))
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
Ω(buf.Contents()).Should(BeEmpty())
})
})
Context("when called with an unknown subcommand", func() {
BeforeEach(func() {
program.RunAndExit([]string{"omicron", "xi"})
})
It("calls the default command with arguments", func() {
Ω(rt).Should(HaveTracked("alpha", "exit"))
Ω(rt).Should(HaveRunWithData("alpha", "Args", []string{"xi"}))
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
Ω(buf.Contents()).Should(BeEmpty())
})
})
Context("when passed arguments and additional arguments", func() {
BeforeEach(func() {
program.RunAndExit([]string{"omicron", "gamma", "arg1", "-arg2", "--", "addArg1", "addArg2"})
})
It("passes both in", func() {
Ω(rt).Should(HaveTracked("gamma", "exit"))
Ω(rt).Should(HaveRunWithData("gamma", "Args", []string{"arg1", "-arg2"}, "AdditionalArgs", []string{"addArg1", "addArg2"}))
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
Ω(buf.Contents()).Should(BeEmpty())
})
})
DescribeTable("Emitting help when asked",
func(args []string) {
program.RunAndExit(args)
Ω(rt).Should(HaveTracked("exit"))
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
//HavePrefix to avoid trailing whitespace causing failures
Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
"Omicron v2.0.0",
"{{gray}}--------------{{/}}",
"For usage information for a command, run {{bold}}omicron help COMMAND{{/}}.",
"For usage information for the default command, run {{bold}}omicron help omicron{{/}} or {{bold}}omicron help alpha{{/}}.",
"",
"The following commands are available:",
" {{bold}}omicron{{/}} or omicron {{bold}}alpha{{/}} - {{gray}}alpha usage{{/}}",
" such usage!",
" {{bold}}beta{{/}} - {{gray}}beta usage{{/}}",
" such usage!",
" {{bold}}gamma{{/}} - {{gray}}{{/}}",
" {{bold}}zeta{{/}} - {{gray}}{{/}}",
}, "\n")))
},
func(args []string) string {
return fmt.Sprintf("with %s", args[1])
},
Entry(nil, []string{"omicron", "help"}),
Entry(nil, []string{"omicron", "-help"}),
Entry(nil, []string{"omicron", "--help"}),
Entry(nil, []string{"omicron", "-h"}),
Entry(nil, []string{"omicron", "--h"}),
)
DescribeTable("Emitting help for the default command",
func(args []string) {
program.RunAndExit(args)
Ω(rt).Should(HaveTracked("exit"))
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
"{{bold}}alpha usage{{/}}",
"{{gray}}-----------{{/}}",
"such usage!",
}, "\n")))
},
func(args []string) string {
return fmt.Sprintf("with %s %s", args[1], args[2])
},
Entry(nil, []string{"omicron", "help", "omicron"}),
Entry(nil, []string{"omicron", "-help", "omicron"}),
Entry(nil, []string{"omicron", "--help", "omicron"}),
Entry(nil, []string{"omicron", "-h", "omicron"}),
Entry(nil, []string{"omicron", "--h", "omicron"}),
Entry(nil, []string{"omicron", "help", "alpha"}),
Entry(nil, []string{"omicron", "-help", "alpha"}),
Entry(nil, []string{"omicron", "--help", "alpha"}),
Entry(nil, []string{"omicron", "-h", "alpha"}),
Entry(nil, []string{"omicron", "--h", "alpha"}),
Entry(nil, []string{"omicron", "alpha", "-help"}),
Entry(nil, []string{"omicron", "alpha", "--help"}),
Entry(nil, []string{"omicron", "alpha", "-h"}),
Entry(nil, []string{"omicron", "alpha", "--h"}),
)
DescribeTable("Emitting help for a known subcommand",
func(args []string) {
program.RunAndExit(args)
Ω(rt).Should(HaveTracked("exit"))
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
"{{bold}}beta usage{{/}}",
"{{gray}}----------{{/}}",
"such usage!",
"",
" --decay-rate{{/}} [float] {{gray}}{{/}}",
" {{light-gray}}set the decay rate, in years{{/}}",
}, "\n")))
},
func(args []string) string {
return fmt.Sprintf("with %s %s", args[1], args[2])
},
Entry(nil, []string{"omicron", "help", "beta"}),
Entry(nil, []string{"omicron", "-help", "beta"}),
Entry(nil, []string{"omicron", "--help", "beta"}),
Entry(nil, []string{"omicron", "-h", "beta"}),
Entry(nil, []string{"omicron", "--h", "beta"}),
Entry(nil, []string{"omicron", "beta", "-help"}),
Entry(nil, []string{"omicron", "beta", "--help"}),
Entry(nil, []string{"omicron", "beta", "-h"}),
Entry(nil, []string{"omicron", "beta", "--h"}),
)
DescribeTable("Emitting help for an unknown subcommand",
func(args []string) {
program.RunAndExit(args)
Ω(rt).Should(HaveTracked("exit"))
Ω(rt).Should(HaveRunWithData("exit", "Code", 1))
Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
"{{red}}Unknown Command: {{bold}}xi{{/}}",
"",
"Omicron v2.0.0",
"{{gray}}--------------{{/}}",
"For usage information for a command, run {{bold}}omicron help COMMAND{{/}}.",
"For usage information for the default command, run {{bold}}omicron help omicron{{/}} or {{bold}}omicron help alpha{{/}}.",
"",
"The following commands are available:",
" {{bold}}omicron{{/}} or omicron {{bold}}alpha{{/}} - {{gray}}alpha usage{{/}}",
" such usage!",
" {{bold}}beta{{/}} - {{gray}}beta usage{{/}}",
" such usage!",
" {{bold}}gamma{{/}} - {{gray}}{{/}}",
" {{bold}}zeta{{/}} - {{gray}}{{/}}",
}, "\n")))
},
func(args []string) string {
return fmt.Sprintf("with %s %s", args[1], args[2])
},
Entry(nil, []string{"omicron", "help", "xi"}),
Entry(nil, []string{"omicron", "-help", "xi"}),
Entry(nil, []string{"omicron", "--help", "xi"}),
Entry(nil, []string{"omicron", "-h", "xi"}),
Entry(nil, []string{"omicron", "--h", "xi"}),
Entry(nil, []string{"omicron", "xi", "-help"}),
Entry(nil, []string{"omicron", "xi", "--help"}),
Entry(nil, []string{"omicron", "xi", "-h"}),
Entry(nil, []string{"omicron", "xi", "--h"}),
)
Context("when called with a deprecated command", func() {
BeforeEach(func() {
program.RunAndExit([]string{"omicron", "delta"})
})
It("lets the user know the command is deprecated", func() {
Ω(rt).Should(HaveTracked("exit"))
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
"{{light-yellow}}You're using deprecated Ginkgo functionality:{{/}}",
"{{light-yellow}}============================================={{/}}",
" {{yellow}}delta is for deprecated{{/}}",
}, "\n")))
})
})
Context("when a deprecated flag is used", func() {
BeforeEach(func() {
program.RunAndExit([]string{"omicron", "beta", "-old"})
})
It("lets the user know a deprecated flag was used", func() {
Ω(rt).Should(HaveTracked("beta", "exit"))
Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
"{{light-yellow}}You're using deprecated Ginkgo functionality:{{/}}",
"{{light-yellow}}============================================={{/}}",
" {{yellow}}--old is deprecated{{/}}",
}, "\n")))
})
})
Context("when an unknown flag is used", func() {
BeforeEach(func() {
program.RunAndExit([]string{"omicron", "beta", "-zanzibar"})
})
It("emits usage for the associated subcommand", func() {
Ω(rt).Should(HaveTracked("exit"))
Ω(rt).Should(HaveRunWithData("exit", "Code", 1))
Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
"{{red}}{{bold}}omicron beta{{/}} {{red}}failed{{/}}",
" flag provided but not defined: -zanzibar",
"",
"{{bold}}beta usage{{/}}",
"{{gray}}----------{{/}}",
"such usage!",
"",
" --decay-rate{{/}} [float] {{gray}}{{/}}",
" {{light-gray}}set the decay rate, in years{{/}}",
}, "\n")))
})
})
Context("when a subcommand aborts", func() {
BeforeEach(func() {
program.RunAndExit([]string{"omicron", "zeta"})
})
It("emits information about the error", func() {
Ω(rt).Should(HaveTracked("zeta", "exit"))
Ω(rt).Should(HaveRunWithData("exit", "Code", 17))
Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
"{{red}}{{bold}}omicron zeta{{/}} {{red}}failed{{/}}",
" Kaboom!",
}, "\n")))
})
})
})
|