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") } }
// runs here func runCd(x *xEnv, argv ...string) error { opts := opt.New("") app.Dprintf("cd %v\n", argv) args, err := opts.Parse(argv) if err != nil { opts.Usage() return dbg.ErrUsage } if len(args) != 0 { opts.Usage() return dbg.ErrUsage } m, ok := <-app.In() if !ok { err = cerror(app.In()) app.Warn("%s", err) return err } d, ok := m.(zx.Dir) if !ok { b, ok := m.([]byte) if !ok { app.Warn("%s", dbg.ErrNotDir) return dbg.ErrNotDir } s := string(b) d, err = nsutil.Stat(s) if err != nil { app.Warn("%s: %s", s, err) return err } } if d["type"] != "d" { app.Warn("%s: %s", d["path"], dbg.ErrNotDir) return dbg.ErrNotDir } app.Cd(d["path"]) out := app.Out() if out != nil { out <- d } 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) } } }