TestErrorExamples will test the help output of the coder exp example-error using golden files.
(t *testing.T)
| 21 | // TestErrorExamples will test the help output of the |
| 22 | // coder exp example-error using golden files. |
| 23 | func 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 | |
| 39 | ExtractCommandPathsLoop: |
| 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 | |
| 73 | func extractCommandPaths(cmdPath []string, cmds []*serpent.Command) [][]string { |
| 74 | var cmdPaths [][]string |
nothing calls this directly
no test coverage detected