File: command_test.go

package info (click to toggle)
golang-github-onsi-ginkgo-v2 2.15.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 4,112 kB
  • sloc: javascript: 59; sh: 14; makefile: 7
file content (94 lines) | stat: -rw-r--r-- 2,718 bytes parent folder | download | duplicates (2)
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
package command_test

import (
	"strings"

	. "github.com/onsi/ginkgo/v2"
	"github.com/onsi/ginkgo/v2/formatter"
	"github.com/onsi/ginkgo/v2/ginkgo/command"
	. "github.com/onsi/ginkgo/v2/internal/test_helpers"
	"github.com/onsi/ginkgo/v2/types"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
)

var _ = Describe("Command", func() {
	var c command.Command
	var rt *RunTracker

	BeforeEach(func() {
		rt = NewRunTracker()
		fs, err := types.NewGinkgoFlagSet(
			types.GinkgoFlags{
				{Name: "contrabulaturally", KeyPath: "C", Usage: "with irridiacy"},
			},
			&(struct{ C int }{C: 17}),
			types.GinkgoFlagSections{},
		)
		Ω(err).ShouldNot(HaveOccurred())
		c = command.Command{
			Name:          "enflabulate",
			Flags:         fs,
			Usage:         "flooper enflabulate <args>",
			ShortDoc:      "Enflabulate all the mandribles",
			Documentation: "Coherent quasistatic protocols will be upended if contrabulaturally is greater than 23.",
			DocLink:       "fabulous-enflabulence",
			Command:       rt.C("enflabulate"),
		}
	})

	Describe("Run", func() {
		Context("when flags fails to parse", func() {
			It("aborts with usage", func() {
				Ω(func() {
					c.Run([]string{"-not-a-flag=oops"}, []string{"additional", "args"})
				}).Should(PanicWith(SatisfyAll(
					HaveField("ExitCode", 1),
					HaveField("Error", HaveOccurred()),
					HaveField("EmitUsage", BeTrue()),
				)))

				Ω(rt).Should(HaveTrackedNothing())
			})
		})

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

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

			})
		})
	})

	Describe("Usage", func() {
		BeforeEach(func() {
			formatter.SingletonFormatter.ColorMode = formatter.ColorModePassthrough
		})

		It("emits a nicely formatted usage", func() {
			buf := gbytes.NewBuffer()
			c.EmitUsage(buf)

			expected := strings.Join([]string{
				"{{bold}}flooper enflabulate <args>{{/}}",
				"{{gray}}--------------------------{{/}}",
				"Enflabulate all the mandribles",
				"",
				"Coherent quasistatic protocols will be upended if contrabulaturally is greater",
				"than 23.",
				"",
				"{{bold}}Learn more at:{{/}} {{cyan}}{{underline}}http://onsi.github.io/ginkgo/#fabulous-enflabulence{{/}}",
				"",
				"  --contrabulaturally{{/}} [int] {{gray}}{{/}}",
				"    {{light-gray}}with irridiacy{{/}}",
				"", "",
			}, "\n")

			Ω(string(buf.Contents())).Should(Equal(expected))
		})
	})
})