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 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 main() { defer app.Exiting() app.New() app.Close(0) ql.Run() app.Exits(nil) }
func main() { defer app.Exiting() app.New() app.SetIO(app.OSIn(), 0) xp.Run() app.Exits(nil) }
func main() { os.Stdin.Close() defer app.Exiting() app.New() app.Close(0) wr.Run() app.Exits(nil) }
func TestBib2ref(t *testing.T) { c := app.New() defer app.Exiting() c.Debug = testing.Verbose() lnc := nsutil.GetLines("/zx/lib/bib/zx.bib") rc := bib2ref(lnc) for ln := range rc { app.Dprintf("%s", ln) } }
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 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 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 main() { defer app.Exiting() x := app.New() stacks := false opts.NewFlag("F", "debug fuse requests", &zxfs.Debug) opts.NewFlag("D", "debug", &x.Debug) opts.NewFlag("s", "statistics", &sflag) opts.NewFlag("x", "addr: re-export locally the ql tree to this address", &xaddr) opts.NewFlag("S", "dump stacks on unmount for debugging", &stacks) args, err := opts.Parse(x.Args) if err != nil { app.Warn("%s", err) opts.Usage() app.Exits(err) } switch len(args) { case 0: case 1: mntdir = args[1] default: app.Warn("wrong number of arguments") opts.Usage() app.Exits("usage") } dprintf("debug on\n") qfs, err := qlfs.New("qlfs") if err != nil { app.Fatal(err) } qfs.Dbg = x.Debug qfs.Flags.Add("fdebug", &zxfs.Debug) st := &zx.IOstats{} qfs.IOstats = st if xaddr != "" { rfs.Server(qfs, xaddr) } err = zxfs.MountServer(qfs, mntdir) if sflag { st.Averages() app.Warn("%s iostats:\n%s\n", qfs.Name(), st) } if stacks { app.Warn("*** PANICING ON USER REQUEST (-S) ***") panic("stack dump") } if err != nil { app.Fatal("%s", err) } app.Warn("unmounted: exiting") }
func main() { defer app.Exiting() os.Args[0] = "auth" app.New() dfltdir := auth.KeyDir() dir = dfltdir opts.NewFlag("d", "adir: clive auth dir", &dir) opts.NewFlag("f", "force write of key file when file already exists", &force) args, err := opts.Parse(os.Args) if err != nil { app.Warn("%s", err) opts.Usage() app.Exits(err) } if len(args) < 3 { opts.Usage() app.Exits("usage") } name, user, secret := args[0], args[1], args[2] groups := args[3:] file := auth.KeyFile(dir, name) fi, _ := os.Stat(file) if fi != nil && !force { app.Fatal("key file already exists") } err = auth.SaveKey(dir, name, user, secret, groups...) if err != nil { app.Fatal("%s: %s", file, err) } ks, err := auth.LoadKey(dir, name) if err != nil { app.Fatal("can't load key: %s", err) } for _, k := range ks { if k.Uid == user { app.Warn("%s", file) return } } app.Fatal("bad user") }
func TestBibLoad(t *testing.T) { BibTexOk = true c := app.New() defer app.Exiting() c.Debug = testing.Verbose() b, err := Load("/zx/lib/bib") if err != nil { t.Fatalf("load: %s", err) } if testing.Verbose() { b.WriteTo(os.Stdout) } app.Dprintf("cite plan 9 networks:\n") refs := b.Cites("VisageFS") for _, r := range refs { app.Dprintf("got:\n%s\n", strings.Join(r.Reference(), "\n")) } if len(refs) == 0 { t.Fatalf("did not find visage in bib") } }
func main() { defer app.Exiting() app.New() opts.NewFlag("p", "port: port used (8080 by default)", &port) args, err := opts.Parse(os.Args) if err != nil { app.Warn("%s", err) opts.Usage() app.Exits(err) } switch len(args) { case 0: case 1: dir = args[0] default: app.Warn("too many arguments") opts.Usage() app.Exits("usage") } err = http.ListenAndServe(":"+port, http.FileServer(http.Dir(dir))) app.Exits(err) }
func TestLoad(t *testing.T) { BibTexOk = false c := app.New() defer app.Exiting() c.Debug = testing.Verbose() b, err := Load("/zx/lib/bib") if err != nil { t.Fatalf("load: %s", err) } if testing.Verbose() { b.WriteTo(os.Stdout) } app.Dprintf("cite plan 9 networks:\n") refs := b.Cites("plan", "9", "networks") for _, r := range refs { app.Dprintf("got:\n%s\n", strings.Join(r.Reference(), "\n")) } if len(refs) != 2 { t.Fatalf("wrong count for plan 9 networks at lsub bib") } }
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) } } }
func main() { defer app.Exiting() app.New() rem.Run() app.Exits(nil) }
func main() { defer app.Exiting() os.Args[0] = "Q" c := app.New() opts.NewFlag("c", "ignored for compatibility", &dummy) opts.NewFlag("D", "debug", &c.Debug) edir := dbg.Usr opts.NewFlag("e", "env: qlfs environment name (defaults to uid)", &edir) opts.NewFlag("q", "qldir: qlfs root dir (defaults to /n/ql)", &ql) args, err := opts.Parse(os.Args) if err != nil { app.Warn("%s", err) opts.Usage() app.Exits(err) } cmd := "" if len(args) == 0 { app.Warn("no command given") opts.Usage() app.Exits(err) } cmd = strings.Join(args, " ") app.Dprintf("run %s\n", cmd) _, err = os.Stat(path.Join(ql, "Ctl")) if err != nil { app.Fatal("qlfs: %s", err) } env := path.Join(ql, edir) _, err = os.Stat(env) if err != nil { err = os.Mkdir(env, 0775) } if err != nil { app.Fatal("%s", err) } cdir := fmt.Sprintf("%s/%d", env, os.Getpid()) if err := os.Mkdir(cdir, 0755); err != nil { app.Fatal("%s", err) } var b bytes.Buffer io.Copy(&b, os.Stdin) if err := ioutil.WriteFile(path.Join(cdir, "in"), b.Bytes(), 0644); err != nil { os.RemoveAll(cdir) app.Fatal("writing in: %s", err) } if err := ioutil.WriteFile(path.Join(cdir, "cmd"), []byte(cmd), 0644); err != nil { os.RemoveAll(cdir) app.Fatal("writing cmd: %s", err) } oc := make(chan bool, 2) pfd, err := os.Open(path.Join(cdir, "pout")) if err != nil { app.Warn("out: %s", err) oc <- true } else { go func() { io.Copy(os.Stdout, pfd) pfd.Close() oc <- true }() } efd, err := os.Open(path.Join(cdir, "perr")) if err != nil { app.Warn("err: %s", err) oc <- true } else { go func() { io.Copy(os.Stderr, efd) efd.Close() oc <- true }() } <-oc <-oc sts, err := ioutil.ReadFile(path.Join(cdir, "wait")) os.RemoveAll(cdir) if err != nil { app.Exits(err) } s := string(sts) if s == "success" { s = "" } app.Exits(s) }