-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't swallow configuration error messages (#204)
* wrap config error and catch any unhandled errors * add a test * grammar
- Loading branch information
1 parent
51e5c72
commit 811dd15
Showing
2 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,50 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func TestExitCodesWorkProperly(t *testing.T) { | ||
testcases := []struct { | ||
arguments []string | ||
expectedExitCode int | ||
expectedText string | ||
}{ | ||
{arguments: []string{}, expectedExitCode: 0, expectedText: "USAGE"}, | ||
{arguments: []string{"--config", "/tmp/doesnotexist123", "start"}, expectedExitCode: 1, expectedText: "no config files found"}, | ||
} | ||
|
||
cli.OsExiter = func(int) {} | ||
for idx, tc := range testcases { | ||
t.Run(strconv.Itoa(idx), func(t *testing.T) { | ||
tc := tc | ||
|
||
c := buildCLI() | ||
c.ExitErrHandler = func(_ *cli.Context, err error) {} | ||
|
||
var buf bytes.Buffer | ||
c.Writer = &buf | ||
c.ErrWriter = &buf | ||
|
||
args := append([]string{"./ui-server"}, tc.arguments...) | ||
err := c.Run(args) | ||
|
||
if tc.expectedExitCode != 0 { | ||
assert.Error(t, err) | ||
} | ||
|
||
if err != nil { | ||
ec := err.(cli.ExitCoder) | ||
assert.Equal(t, tc.expectedExitCode, ec.ExitCode()) | ||
assert.Contains(t, ec.Error(), tc.expectedText) | ||
} else { | ||
assert.Contains(t, buf.String(), tc.expectedText) | ||
} | ||
}) | ||
} | ||
} |