diff --git a/peco.go b/peco.go index 8fa57c2..3966a66 100644 --- a/peco.go +++ b/peco.go @@ -219,6 +219,10 @@ func (p *Peco) Err() error { } func (p *Peco) Exit(err error) { + if pdebug.Enabled { + g := pdebug.Marker("Peco.Exit (err = %s)", err) + defer g.End() + } p.err = err if cf := p.cancelFunc; cf != nil { cf() @@ -348,7 +352,7 @@ func (p *Peco) Run(ctx context.Context) (err error) { if b := p.CurrentLineBuffer(); b.Size() == 1 { if l, err := b.LineAt(0); err == nil { p.resultCh = make(chan line.Line) - p.Exit(nil) + p.Exit(errCollectResults{}) p.resultCh <- l close(p.resultCh) } diff --git a/peco_test.go b/peco_test.go index b6f7c75..465c0e4 100644 --- a/peco_test.go +++ b/peco_test.go @@ -264,6 +264,9 @@ func TestApplyConfig(t *testing.T) { } } +// While this issue is labeled for Issue363, it tests against 376 as well. +// The test should have caught the bug for 376, but the premise of the test +// itself was wrong func TestGHIssue363(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() @@ -273,15 +276,26 @@ func TestGHIssue363(t *testing.T) { p.Stdin = bytes.NewBufferString("foo\n") var out bytes.Buffer p.Stdout = &out - if !assert.NoError(t, p.Run(ctx), "p.Run should succeed") { - return - } + + resultCh := make(chan error) + go func() { + defer close(resultCh) + select { + case <-ctx.Done(): + return + case resultCh <- p.Run(ctx): + return + } + }() select { case <-ctx.Done(): - t.Errorf("we should get here before being canceled") + t.Errorf("timeout reached") return - default: + case err := <-resultCh: + if !assert.True(t, util.IsCollectResultsError(err), "isCollectResultsError") { + return + } } if !assert.NotEqual(t, "foo\n", out.String(), "output should match") {