Skip to content
Snippets Groups Projects
reader_test.go 1.29 KiB
Newer Older
Junegunn Choi's avatar
Junegunn Choi committed
package fzf

Junegunn Choi's avatar
Junegunn Choi committed
import (
	"testing"
Junegunn Choi's avatar
Junegunn Choi committed

	"github.com/junegunn/fzf/src/util"
)
Junegunn Choi's avatar
Junegunn Choi committed

func TestReadFromCommand(t *testing.T) {
	strs := []string{}
Junegunn Choi's avatar
Junegunn Choi committed
	eb := util.NewEventBox()
Junegunn Choi's avatar
Junegunn Choi committed
	reader := Reader{
		pusher:   func(s []byte) bool { strs = append(strs, string(s)); return true },
		eventBox: eb,
		event:    int32(EvtReady)}

	reader.startEventPoller()
Junegunn Choi's avatar
Junegunn Choi committed

	// Check EventBox
Junegunn Choi's avatar
Junegunn Choi committed
	if eb.Peek(EvtReadNew) {
Junegunn Choi's avatar
Junegunn Choi committed
		t.Error("EvtReadNew should not be set yet")
Junegunn Choi's avatar
Junegunn Choi committed
	}

	// Normal command
	reader.fin(reader.readFromCommand("sh", `echo abc && echo def`))
Junegunn Choi's avatar
Junegunn Choi committed
	if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" {
		t.Errorf("%s", strs)
	}

	// Check EventBox again
	eb.WaitFor(EvtReadFin)
Junegunn Choi's avatar
Junegunn Choi committed

	// Wait should return immediately
Junegunn Choi's avatar
Junegunn Choi committed
	eb.Wait(func(events *util.Events) {
Junegunn Choi's avatar
Junegunn Choi committed
		events.Clear()
	})

	// EventBox is cleared
Junegunn Choi's avatar
Junegunn Choi committed
	if eb.Peek(EvtReadNew) {
Junegunn Choi's avatar
Junegunn Choi committed
		t.Error("EvtReadNew should not be set yet")
Junegunn Choi's avatar
Junegunn Choi committed
	}

	// Make sure that event poller is finished
	time.Sleep(readerPollIntervalMax)

	// Restart event poller
	reader.startEventPoller()

Junegunn Choi's avatar
Junegunn Choi committed
	// Failing command
	reader.fin(reader.readFromCommand("sh", `no-such-command`))
Junegunn Choi's avatar
Junegunn Choi committed
	strs = []string{}
	if len(strs) > 0 {
		t.Errorf("%s", strs)
	}

	// Check EventBox again
Junegunn Choi's avatar
Junegunn Choi committed
	if eb.Peek(EvtReadNew) {
		t.Error("Command failed. EvtReadNew should not be set")
	}
	if !eb.Peek(EvtReadFin) {
		t.Error("EvtReadFin should be set")
Junegunn Choi's avatar
Junegunn Choi committed
	}
}