2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
2015-01-12 03:56:17 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
2017-08-15 18:24:23 +00:00
|
|
|
"time"
|
2015-01-12 03:56:17 +00:00
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/util"
|
|
|
|
)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
func TestReadFromCommand(t *testing.T) {
|
|
|
|
strs := []string{}
|
2015-01-12 03:56:17 +00:00
|
|
|
eb := util.NewEventBox()
|
2015-01-01 19:49:30 +00:00
|
|
|
reader := Reader{
|
2015-08-02 05:25:57 +00:00
|
|
|
pusher: func(s []byte) bool { strs = append(strs, string(s)); return true },
|
2017-08-15 18:24:23 +00:00
|
|
|
eventBox: eb,
|
|
|
|
event: int32(EvtReady)}
|
|
|
|
|
|
|
|
reader.startEventPoller()
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
// Check EventBox
|
2015-02-17 10:28:10 +00:00
|
|
|
if eb.Peek(EvtReadNew) {
|
2015-01-11 18:01:24 +00:00
|
|
|
t.Error("EvtReadNew should not be set yet")
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normal command
|
2017-09-28 14:05:02 +00:00
|
|
|
reader.fin(reader.readFromCommand("sh", `echo abc && echo def`))
|
2015-01-01 19:49:30 +00:00
|
|
|
if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" {
|
|
|
|
t.Errorf("%s", strs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check EventBox again
|
2017-08-15 18:24:23 +00:00
|
|
|
eb.WaitFor(EvtReadFin)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
// Wait should return immediately
|
2015-01-12 03:56:17 +00:00
|
|
|
eb.Wait(func(events *util.Events) {
|
2015-01-01 19:49:30 +00:00
|
|
|
events.Clear()
|
|
|
|
})
|
|
|
|
|
|
|
|
// EventBox is cleared
|
2015-02-17 10:28:10 +00:00
|
|
|
if eb.Peek(EvtReadNew) {
|
2015-01-11 18:01:24 +00:00
|
|
|
t.Error("EvtReadNew should not be set yet")
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 18:24:23 +00:00
|
|
|
// Make sure that event poller is finished
|
|
|
|
time.Sleep(readerPollIntervalMax)
|
|
|
|
|
|
|
|
// Restart event poller
|
|
|
|
reader.startEventPoller()
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
// Failing command
|
2017-09-28 14:05:02 +00:00
|
|
|
reader.fin(reader.readFromCommand("sh", `no-such-command`))
|
2015-01-01 19:49:30 +00:00
|
|
|
strs = []string{}
|
|
|
|
if len(strs) > 0 {
|
|
|
|
t.Errorf("%s", strs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check EventBox again
|
2015-02-17 10:28:10 +00:00
|
|
|
if eb.Peek(EvtReadNew) {
|
2017-08-15 18:24:23 +00:00
|
|
|
t.Error("Command failed. EvtReadNew should not be set")
|
|
|
|
}
|
|
|
|
if !eb.Peek(EvtReadFin) {
|
|
|
|
t.Error("EvtReadFin should be set")
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|