func TestParseStdin(t *testing.T) { r, w, err := os.Pipe() if err != nil { t.Fatalf("pipe: %s", err) } fd, err := os.Open("example") if err != nil { t.Fatalf("ex: %s", err) } defer fd.Close() go func() { io.Copy(w, fd) w.Close() }() os.Stdin = r app.New() defer app.Exiting() ql := func() { c := app.AppCtx() c.Debug = testing.Verbose() app.SetIO(app.OSIn(), 0) Run() } x := app.Go(ql, "ql", "-n") <-x.Wait if x.Sts != nil { t.Fatalf("did fail") } }
func TestCmds(t *testing.T) { os.Args[0] = "ql.test" app.Debug = testing.Verbose() && false app.Verb = testing.Verbose() && false app.New() app.AppCtx().Debug = testing.Verbose() dbg.ExitDumpsStacks = testing.Verbose() defer app.Exiting() inc := make(chan interface{}, 3) inc <- []byte("hi\n") inc <- []byte("there\n") close(inc) app.SetIO(inc, 0) ql := func() { app.AppCtx().Debug = testing.Verbose() Run() } for _, c := range cmds { args := []string{"ql", "-c", c} if testing.Verbose() { args = []string{"ql", "-X", "-c", c} } x := app.Go(ql, args...) <-x.Wait if x.Sts != nil { t.Logf("did fail with sts %v", x.Sts) } } }
func newEnv(name string) *qEnv { e := &qEnv{ name: name, vars: app.Env(), cmds: map[string]*qCmd{}, runc: make(chan func()), } app.Go(e.run, "ctx") return e }
func TestParseCmd(t *testing.T) { app.New() defer app.Exiting() ql := func() { app.AppCtx().Debug = testing.Verbose() Run() } x := app.Go(ql, "ql", "-n", "-c", "pwd") <-x.Wait if x.Sts != nil { t.Fatalf("did fail") } }
func TestParseError(t *testing.T) { app.New() defer app.Exiting() ql := func() { app.AppCtx().Debug = testing.Verbose() Run() } x := app.Go(ql, "ql", "-n", "-c", "pw{d") <-x.Wait if x.Sts == nil { t.Logf("warn: ql didn't fail") } }
func (c *qCmd) start(txt string) error { c.Lock() if c.ctx != nil { c.Unlock() return fmt.Errorf("%s: one cmd is enough", c) } c.txt = txt var in chan interface{} if c.in.Len() > 0 { c.in.c = make(chan interface{}, len(c.in.msgs)) for _, m := range c.in.msgs { c.in.c <- m } close(c.in.c) in = c.in.c } outfn := func(c *qCmd, io *qIO, outc chan interface{}) { for m := range outc { c.Lock() io.addOut(m) c.Unlock() } io.eof = true io.wakeup() } out := make(chan interface{}) go outfn(c, c.out, out) err := make(chan interface{}) go outfn(c, c.err, err) c.e.runc <- func() { c.ctx = app.Go(func() { c.Unlock() app.DupDot() app.DupEnv() app.NewIO(nil) app.SetIO(in, 0) app.SetIO(out, 1) app.SetIO(err, 2) ql.Run() }, "ql", "-c", c.txt) } return nil }