func TestAll(t *testing.T) { for _, v := range Tests { cc, e := parser.Parse([]byte(v.c)) if e != nil { t.Log(e) t.Fail() } fb := gap.New([]byte(v.i)) f, _ := New(fb) e = f.Run(cc) if e != nil { t.Log(e) t.Fail() } // Check so that the output is correct bs, _ := f.f.Get(0, f.f.Length()) so := string(bs) if len(so) == len(v.o) { for i := range so { if so[i] != v.o[i] { goto Fail } } // goto Fail continue } Fail: t.Log(v.desc) t.Log("Not equal output") t.Log("Expected: `", v.o, "`") t.Log("Got: `", so, "`") t.FailNow() } }
/* Operational modes: sem path/to/script/file.sem infile... read the file, execute commands and if no error write to stdout flags: -w Write output to files -a=".mod" add the extra ending to the file */ func main() { flag.Parse() if fH { fmt.Println("usage: sem [flags] path/to/script [path ...]") flag.PrintDefaults() return } // Check so that we can get the sem file and parse it if flag.NArg() < 1 { fmt.Println("error: need path to a sem file to progress") os.Exit(1) } sf, e := ioutil.ReadFile(flag.Arg(0)) if e != nil { fmt.Println("error: could not read sem file:", e) os.Exit(1) } if bytes.HasPrefix(sf, []byte("#!")) { // Ignore the shebang line i := bytes.Index(sf, []byte("\n")) sf = sf[i:] } cmds, e := parser.Parse(sf) if e != nil { fmt.Println("error: could not parse sem file") fmt.Println(e) os.Exit(1) } if flag.NArg() == 1 { if fW || fD != "" || fA != ".mod" { fmt.Println("when using stdin only stdout is supported as output") os.Exit(1) } do(cmds, os.Stdin, os.Stdout) } // Ok, so we are working on the specified files, loop each one of them and // run them through the command for i := 1; i < flag.NArg(); i++ { dofile(cmds, flag.Arg(i)) } _ = cmds }
// Try to parse the file and output the formatted file back, // if file name is empty write to stout instead func fixfile(b []byte, f string) { cmds, e := parser.Parse(b) if e != nil { fmt.Fprintf(os.Stderr, "Error occured while parsing file '%s':\n", f) fmt.Fprintln(os.Stderr, e) fmt.Fprintln(os.Stderr, "Ignoring this file") return } // If we should output it to the file we try to do that w := os.Stdout if fW && f != "" { fi, e := os.Create(f) if e != nil { fmt.Fprintln(os.Stderr, "Could not open '%s' for writing", f) return } defer fi.Close() w = fi } // Pretty print the command to the writer for _, c := range cmds { io.WriteString(w, c.String()) } }