func TestFrmt(t *testing.T) { app.New() // prevent app.Fatal from calling dbg.Fatal //app.Debug = testing.Verbose() // gf pipe := make(chan interface{}, 2) pipe <- zx.Dir{"upath": "foo", "path": "foo"} pipe <- []byte(testpar) close(pipe) out := make(chan interface{}) /* go func() { c := app.New() defer app.Exiting() c.Args = []string {"gf", "frmt,"} app.DupIO() app.SetIO(pipe, 1) app.Cd("/zx/sys/src/clive/app") lf.Run() }() */ // frmt go func() { c := app.New() //c.Debug = testing.Verbose() defer app.Exiting() c.Args = []string{"frmt", "-w", "50", "-r"} app.DupIO() app.SetIO(pipe, 0) app.SetIO(out, 1) app.Cd("/zx/sys/src/clive/app") Run() }() outs := "" for x := range out { switch x := x.(type) { case zx.Dir: dprintf("xgot %T %s\n", x, x["upath"]) case []byte: if len(x) > 0 { x = x[:len(x)-1] } dprintf("[%s]\n", x) outs += string(x) + "\n" case error: dprintf("xgot %T %v\n", x, x) default: dprintf("xgot %T %v\n", x, x) } } dprintf("outs = `%s`\n", outs) err := cerror(out) dprintf("got sts %v\n", err) if outs != testout { t.Fatalf("bad output") } }
func main() { defer app.Exiting() app.New() app.SetIO(app.OSIn(), 0) xp.Run() app.Exits(nil) }
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 (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 }
func TestLf(t *testing.T) { app.New() // prevent app.Fatal from calling dbg.Fatal app.Debug = testing.Verbose() for i := range tests { lt := tests[i] dprintf("run %v\n", lt.args) out := make(chan interface{}) go func() { c := app.New() c.Args = lt.args defer app.Exiting() app.DupIO() app.SetIO(out, 1) app.Cd("/zx/sys/src/clive") Run() }() outs := []string{} for x := range out { d, ok := x.(zx.Dir) if !ok { dprintf("got %T %v\n", x, x) t.Fatalf("not a dir") } dprintf("got %T %s\n", d, d["upath"]) outs = append(outs, d["upath"]) } err := cerror(out) dprintf("got sts %v\n", err) if lt.fails && err == nil { t.Fatalf("didn't fail") } if !lt.fails && err != nil { t.Fatalf("failed: %s", err) } if lt.out != nil && strings.Join(lt.out, " ") != strings.Join(outs, " ") { t.Fatalf("bad output %#v", outs) } if lt.out == nil { dprintf("out: %#v\n", outs) } } }
func TestLg(t *testing.T) { app.New() // prevent app.Fatal from calling dbg.Fatal app.Debug = testing.Verbose() for i := range gets { lt := gets[i] dprintf("run %v\n", lt.args) out := make(chan interface{}) go func() { c := app.New() defer app.Exiting() app.DupIO() app.SetIO(out, 1) app.Cd("/zx/sys/src/clive") c.Args = lt.args Run() }() outs := []string{} nbytes := 0 for x := range out { switch x := x.(type) { case zx.Dir: if nbytes > 0 { outs = append(outs, "[]") } nbytes = 0 dprintf("got %T %s\n", x, x["upath"]) outs = append(outs, x["upath"]) case []byte: nbytes += len(x) dprintf("got %T [%d]\n", x, len(x)) case error: if nbytes > 0 { outs = append(outs, "[]") } nbytes = 0 dprintf("got %T %v\n", x, x) outs = append(outs, "err") default: dprintf("got %T %v\n", x, x) t.Fatalf("unexpected type %T", x) } } if nbytes >0 { outs = append(outs, "[]") } err := cerror(out) dprintf("got sts %v\n", err) if lt.fails && err == nil { t.Fatalf("didn't fail") } if !lt.fails && err != nil { t.Fatalf("failed: %s", err) } if lt.out != nil && strings.Join(lt.out, " ") != strings.Join(outs, " ") { t.Fatalf("bad output %#v", outs) } if lt.out == nil { dprintf("out: %#v\n", outs) } } }