Return an error when parsing environment variables for values fails
Currently cli silently (aside from IntSlice and Int64Slice which oddly printed directly to the error stream) ignores failures that occur when parsing environment variables for their value for flags that define environment variables. Instead, we should propogate up the error to the user. This is accomplished in a backwards compatible manner by adding a new, internal, interface which defines an applyWithError function that all flags here define. In v2, when we can modify the interface, we can drop this secondary interface and modify `Apply` to return an error.
This commit is contained in:
24
app_test.go
24
app_test.go
@@ -1523,7 +1523,11 @@ func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
|
||||
func TestHandleAction_WithNonFuncAction(t *testing.T) {
|
||||
app := NewApp()
|
||||
app.Action = 42
|
||||
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
|
||||
fs, err := flagSet(app.Name, app.Flags)
|
||||
if err != nil {
|
||||
t.Errorf("error creating FlagSet: %s", err)
|
||||
}
|
||||
err = HandleAction(app.Action, NewContext(app, fs, nil))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("expected to receive error from Run, got none")
|
||||
@@ -1547,7 +1551,11 @@ func TestHandleAction_WithNonFuncAction(t *testing.T) {
|
||||
func TestHandleAction_WithInvalidFuncSignature(t *testing.T) {
|
||||
app := NewApp()
|
||||
app.Action = func() string { return "" }
|
||||
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
|
||||
fs, err := flagSet(app.Name, app.Flags)
|
||||
if err != nil {
|
||||
t.Errorf("error creating FlagSet: %s", err)
|
||||
}
|
||||
err = HandleAction(app.Action, NewContext(app, fs, nil))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("expected to receive error from Run, got none")
|
||||
@@ -1571,7 +1579,11 @@ func TestHandleAction_WithInvalidFuncSignature(t *testing.T) {
|
||||
func TestHandleAction_WithInvalidFuncReturnSignature(t *testing.T) {
|
||||
app := NewApp()
|
||||
app.Action = func(_ *Context) (int, error) { return 0, nil }
|
||||
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
|
||||
fs, err := flagSet(app.Name, app.Flags)
|
||||
if err != nil {
|
||||
t.Errorf("error creating FlagSet: %s", err)
|
||||
}
|
||||
err = HandleAction(app.Action, NewContext(app, fs, nil))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("expected to receive error from Run, got none")
|
||||
@@ -1602,5 +1614,9 @@ func TestHandleAction_WithUnknownPanic(t *testing.T) {
|
||||
fn(ctx)
|
||||
return nil
|
||||
}
|
||||
HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
|
||||
fs, err := flagSet(app.Name, app.Flags)
|
||||
if err != nil {
|
||||
t.Errorf("error creating FlagSet: %s", err)
|
||||
}
|
||||
HandleAction(app.Action, NewContext(app, fs, nil))
|
||||
}
|
||||
|
Reference in New Issue
Block a user