diff --git a/cmd/formatter/shortcut.go b/cmd/formatter/shortcut.go index a098d3866..d6add09c2 100644 --- a/cmd/formatter/shortcut.go +++ b/cmd/formatter/shortcut.go @@ -327,7 +327,10 @@ func (lk *LogKeyboard) Run(ctx context.Context, project *types.Project, options select { case <-ctx.Done(): return - case event := <-lk.keys: + case event, ok := <-lk.keys: + if !ok { // the keyboard was closed + return + } lk.handleKeyEvent(ctx, event, project, options) } } diff --git a/cmd/formatter/shortcut_test.go b/cmd/formatter/shortcut_test.go index 0f85be074..ea76ae953 100644 --- a/cmd/formatter/shortcut_test.go +++ b/cmd/formatter/shortcut_test.go @@ -60,6 +60,25 @@ func TestRunHandlesKeysUntilContextIsDone(t *testing.T) { assert.NilError(t, lk.Close(), "Close must be idempotent") } +func TestRunReturnsWhenKeyboardIsClosed(t *testing.T) { + keys := make(chan keyboard.KeyEvent) + lk := NewKeyboardManager(false, false, make(chan os.Signal, 1)) + lk.keys = keys + + done := make(chan struct{}) + go func() { + lk.Run(t.Context(), &types.Project{}, api.UpOptions{}) + close(done) + }() + + close(keys) // what keyboard.Close does to the channel it handed out + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatal("Run must return once the keyboard channel is closed") + } +} + func TestRunWithoutOpenReturnsImmediately(t *testing.T) { lk := NewKeyboardManager(false, false, make(chan os.Signal, 1)) done := make(chan struct{})