MCPcopy Index your code
hub / github.com/coder/coder / TestCommandHelp

Function TestCommandHelp

cli/clitest/golden.go:55–97  ·  view source on GitHub ↗

TestCommandHelp will test the help output of the given commands using golden files.

(t *testing.T, getRoot func(t *testing.T) *serpent.Command, cases []CommandHelpCase)

Source from the content-addressed store, hash-verified

53// TestCommandHelp will test the help output of the given commands
54// using golden files.
55func TestCommandHelp(t *testing.T, getRoot func(t *testing.T) *serpent.Command, cases []CommandHelpCase) {
56 t.Parallel()
57 rootClient, replacements := prepareTestData(t)
58
59 root := getRoot(t)
60
61ExtractCommandPathsLoop:
62 for _, cp := range extractVisibleCommandPaths(nil, root.Children) {
63 name := fmt.Sprintf("coder %s --help", strings.Join(cp, " "))
64 //nolint:gocritic
65 cmd := append(cp, "--help")
66 for _, tt := range cases {
67 if tt.Name == name {
68 continue ExtractCommandPathsLoop
69 }
70 }
71 cases = append(cases, CommandHelpCase{Name: name, Cmd: cmd})
72 }
73
74 for _, tt := range cases {
75 t.Run(tt.Name, func(t *testing.T) {
76 t.Parallel()
77 ctx := testutil.Context(t, testutil.WaitLong)
78
79 var outBuf bytes.Buffer
80
81 caseCmd := getRoot(t)
82
83 inv, cfg := NewWithCommand(t, caseCmd, tt.Cmd...)
84 inv.Stderr = &outBuf
85 inv.Stdout = &outBuf
86 inv.Environ.Set("CODER_URL", rootClient.URL.String())
87 inv.Environ.Set("CODER_SESSION_TOKEN", rootClient.SessionToken())
88 inv.Environ.Set("CODER_CACHE_DIRECTORY", "~/.cache")
89
90 SetupConfig(t, rootClient, cfg)
91
92 StartWithWaiter(t, inv.WithContext(ctx)).RequireSuccess()
93
94 TestGoldenFile(t, tt.Name, outBuf.Bytes(), replacements)
95 })
96 }
97}
98
99// Output captures stdout and stderr from an invocation and formats them with
100// prefixes for golden file testing, preserving their interleaved order.

Callers 2

TestCommandHelpFunction · 0.92

Calls 15

ContextFunction · 0.92
prepareTestDataFunction · 0.85
getRootFunction · 0.85
NewWithCommandFunction · 0.85
SetupConfigFunction · 0.85
StartWithWaiterFunction · 0.85
TestGoldenFileFunction · 0.85
RequireSuccessMethod · 0.80
WithContextMethod · 0.80
RunMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected