- Notifications
You must be signed in to change notification settings - Fork929
feat(cli): add golden tests for errors (#11588)#12698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
feat(cli): add golden tests for errors (#11588)
Creates golden files from `coder/cli/errors.go`.Adds a unit test to test against golden files.Adds a make file command to regenerate golden files.Abstracts test against golden files.fix: prevent data racerefactor: make formatter publicParse error command in a less brittle way
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commitc6e6634a015cd39445359992d6cfa486507001b5
There are no files selected for viewing
2 changes: 1 addition & 1 deletionMakefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
57 changes: 31 additions & 26 deletionscli/clitest/golden.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletionscli/errors_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package cli_test | ||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/v2/cli" | ||
"github.com/coder/coder/v2/cli/clitest" | ||
"github.com/coder/serpent" | ||
) | ||
type commandErrorCase struct { | ||
Name string | ||
Cmd []string | ||
} | ||
// TestErrorExamples will test the help output of the | ||
// coder exp example-error using golden files. | ||
func TestErrorExamples(t *testing.T) { | ||
t.Parallel() | ||
coderRootCmd := getRoot(t) | ||
var exampleErrorRootCmd *serpent.Command | ||
coderRootCmd.Walk(func(command *serpent.Command) { | ||
if command.Name() == "example-error" { | ||
// cannot abort early, but list is small | ||
exampleErrorRootCmd = command | ||
} | ||
}) | ||
require.NotNil(t, exampleErrorRootCmd, "example-error command not found") | ||
var cases []commandErrorCase | ||
ExtractCommandPathsLoop: | ||
for _, cp := range extractCommandPaths(nil, exampleErrorRootCmd.Children) { | ||
cmd := append([]string{"exp", "example-error"}, cp...) | ||
name := fmt.Sprintf("coder %s", strings.Join(cmd, " ")) | ||
for _, tt := range cases { | ||
if tt.Name == name { | ||
continue ExtractCommandPathsLoop | ||
} | ||
} | ||
cases = append(cases, commandErrorCase{Name: name, Cmd: cmd}) | ||
} | ||
for _, tt := range cases { | ||
tt := tt | ||
t.Run(tt.Name, func(t *testing.T) { | ||
t.Parallel() | ||
var outBuf bytes.Buffer | ||
coderRootCmd := getRoot(t) | ||
inv, _ := clitest.NewWithCommand(t, coderRootCmd, tt.Cmd...) | ||
inv.Stderr = &outBuf | ||
inv.Stdout = &outBuf | ||
// This example expects to close stdin twice and joins | ||
// the error messages to create a multi-multi error. | ||
if tt.Name == "coder exp example-error multi-multi-error" { | ||
inv.Stdin = os.Stdin | ||
} | ||
err := inv.Run() | ||
errFormatter := cli.NewPrettyErrorFormatter(&outBuf, false) | ||
errFormatter.Format(err) | ||
clitest.TestGoldenFile(t, tt.Name, outBuf.Bytes(), nil) | ||
}) | ||
} | ||
} | ||
func extractCommandPaths(cmdPath []string, cmds []*serpent.Command) [][]string { | ||
var cmdPaths [][]string | ||
for _, c := range cmds { | ||
cmdPath := append(cmdPath, c.Name()) | ||
cmdPaths = append(cmdPaths, cmdPath) | ||
cmdPaths = append(cmdPaths, extractCommandPaths(cmdPath, c.Children)...) | ||
} | ||
return cmdPaths | ||
} | ||
// Must return a fresh instance of cmds each time. | ||
func getRoot(t *testing.T) *serpent.Command { | ||
t.Helper() | ||
var root cli.RootCmd | ||
rootCmd, err := root.Command(root.AGPL()) | ||
require.NoError(t, err) | ||
return rootCmd | ||
} |
20 changes: 14 additions & 6 deletionscli/root.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 1 addition & 15 deletionscli/server_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletionscli/testdata/coder_exp_example-error_api.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Encountered an error running "coder exp example-error api", see "coder exp example-error api --help" for more information | ||
error: Top level sdk error message. | ||
Have you tried turning it off and on again? |
2 changes: 2 additions & 0 deletionscli/testdata/coder_exp_example-error_arg-required.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Encountered an error running "coder exp example-error arg-required", see "coder exp example-error arg-required --help" for more information | ||
error: wanted 1 args but got 0 [] |
2 changes: 2 additions & 0 deletionscli/testdata/coder_exp_example-error_cmd.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Encountered an error running "coder exp example-error cmd", see "coder exp example-error cmd --help" for more information | ||
error: some error: function decided not to work, and it never will |
7 changes: 7 additions & 0 deletionscli/testdata/coder_exp_example-error_multi-error.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Encountered an error running "coder exp example-error multi-error", see "coder exp example-error multi-error --help" for more information | ||
error: 3 errors encountered: Trace=[wrapped: ]) | ||
1. first error: function decided not to work, and it never will | ||
2. second error: function decided not to work, and it never will | ||
3. Trace=[wrapped api error: ] | ||
Top level sdk error message. | ||
magic dust unavailable, please try again later |
6 changes: 6 additions & 0 deletionscli/testdata/coder_exp_example-error_multi-multi-error.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
2 errors encountered: | ||
1. Encountered an error running "coder exp example-error multi-multi-error", see "coder exp example-error multi-multi-error --help" for more information | ||
error: 2 errors encountered: | ||
1. first error: function decided not to work, and it never will | ||
2. second error: function decided not to work, and it never will | ||
2. close /dev/stdin: file already closed |
1 change: 1 addition & 0 deletionscli/testdata/coder_exp_example-error_validation.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Missing values for the required flags: magic-word |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.