-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Due to the plugin architecture, the user wasn't able to interrupt a plugin command via Ctrl+C. This is annoying if the plugin execution is long. By running the plugin execution in a goroutine and by listening to the command context at the same time, we can fix that.
- Loading branch information
Showing
3 changed files
with
87 additions
and
17 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
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,57 @@ | ||
package clictx_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ignite/cli/ignite/pkg/clictx" | ||
) | ||
|
||
func TestDo(t *testing.T) { | ||
ctxCanceled, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
tests := []struct { | ||
name string | ||
ctx context.Context | ||
f func() error | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "f returns nil", | ||
ctx: context.Background(), | ||
f: func() error { return nil }, | ||
}, | ||
{ | ||
name: "f returns an error", | ||
ctx: context.Background(), | ||
f: func() error { return errors.New("oups") }, | ||
expectedErr: "oups", | ||
}, | ||
{ | ||
name: "ctx is canceled", | ||
ctx: ctxCanceled, | ||
f: func() error { | ||
time.Sleep(time.Second) | ||
return nil | ||
}, | ||
expectedErr: context.Canceled.Error(), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
err := clictx.Do(tt.ctx, tt.f) | ||
|
||
if tt.expectedErr != "" { | ||
require.EqualError(err, tt.expectedErr) | ||
return | ||
} | ||
require.NoError(err) | ||
}) | ||
} | ||
} |