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

Function TestErrorExamples

cli/exp_errors_test.go:23–71  ·  view source on GitHub ↗

TestErrorExamples will test the help output of the coder exp example-error using golden files.

(t *testing.T)

Source from the content-addressed store, hash-verified

21// TestErrorExamples will test the help output of the
22// coder exp example-error using golden files.
23func TestErrorExamples(t *testing.T) {
24 t.Parallel()
25
26 coderRootCmd := getRoot(t)
27
28 var exampleErrorRootCmd *serpent.Command
29 coderRootCmd.Walk(func(command *serpent.Command) {
30 if command.Name() == "example-error" {
31 // cannot abort early, but list is small
32 exampleErrorRootCmd = command
33 }
34 })
35 require.NotNil(t, exampleErrorRootCmd, "example-error command not found")
36
37 var cases []commandErrorCase
38
39ExtractCommandPathsLoop:
40 for _, cp := range extractCommandPaths(nil, exampleErrorRootCmd.Children) {
41 cmd := append([]string{"exp", "example-error"}, cp...)
42 name := fmt.Sprintf("coder %s", strings.Join(cmd, " "))
43 for _, tt := range cases {
44 if tt.Name == name {
45 continue ExtractCommandPathsLoop
46 }
47 }
48 cases = append(cases, commandErrorCase{Name: name, Cmd: cmd})
49 }
50
51 for _, tt := range cases {
52 t.Run(tt.Name, func(t *testing.T) {
53 t.Parallel()
54
55 var outBuf bytes.Buffer
56
57 coderRootCmd := getRoot(t)
58
59 inv, _ := clitest.NewWithCommand(t, coderRootCmd, tt.Cmd...)
60 inv.Stderr = &outBuf
61 inv.Stdout = &outBuf
62
63 err := inv.Run()
64
65 errFormatter := cli.NewPrettyErrorFormatter(&outBuf, false)
66 errFormatter.Format(err)
67
68 clitest.TestGoldenFile(t, tt.Name, outBuf.Bytes(), nil)
69 })
70 }
71}
72
73func extractCommandPaths(cmdPath []string, cmds []*serpent.Command) [][]string {
74 var cmdPaths [][]string

Callers

nothing calls this directly

Calls 9

FormatMethod · 0.95
NewWithCommandFunction · 0.92
NewPrettyErrorFormatterFunction · 0.92
TestGoldenFileFunction · 0.92
getRootFunction · 0.85
extractCommandPathsFunction · 0.85
NameMethod · 0.65
RunMethod · 0.65
BytesMethod · 0.45

Tested by

no test coverage detected