func bakeWithLog(name, lang string, types []string, out Printfer) error { typeString := "base" if len(types) != 0 { typeString = strings.Join(types, ",") } cmd := exec.Command( path.Join(os.Getenv("BAKE"), "bin", "bake"), "-v", "-o", "Owner", "-l", lang, "-n", name, "-t", typeString, ) stdout, err := cmd.StdoutPipe() if err != nil { return fmt.Errorf("couldn't get bake stdout: %v", err) } stderr, err := cmd.StderrPipe() if err != nil { return fmt.Errorf("couldn't get bake stderr: %v", err) } err = cmd.Start() if err != nil { return fmt.Errorf("couldn't start bake: %v", err) } output, err := strio.ReadAll(stdout) if err != nil { return fmt.Errorf("error reading bake stdout: %v", err) } out.Printf("%s\n", output) if errput, err := strio.ReadAll(stderr); err != nil { return fmt.Errorf("error reading bake stderr: %v", err) } else if len(errput) != 0 { return fmt.Errorf("unexpected error output: %s", errput) } return cmd.Wait() }
func (t *testCmd_) Run(expDescr string) (*result, error) { cmdLine := t.cmd() parts := strings.Split(cmdLine, " ") cmd := exec.Command(parts[0], parts[1:]...) stdout, err := cmd.StdoutPipe() if err != nil { return nil, fmt.Errorf("couldn't get stdout for '%s': %v", cmdLine, err) } stderr, err := cmd.StderrPipe() if err != nil { return nil, fmt.Errorf("couldn't get stderr for '%s': %v", cmdLine, err) } err = cmd.Start() if err != nil { return nil, fmt.Errorf("couldn't start '%s': %v", cmdLine, err) } output, err := strio.ReadAll(stdout) if err != nil { return nil, fmt.Errorf("error reading stdout for '%s': %v", cmdLine, err) } errput, err := strio.ReadAll(stderr) if err != nil { return nil, fmt.Errorf("error reading stderr for '%s': %v", cmdLine, err) } // err is set if exit status is not 0 err = cmd.Wait() exitStatus := 0 if err != nil { if strings.HasPrefix(err.Error(), exitStatusErr) { // returning non-zero is not an error in this context, // just a possible outcome of running an executable, so // we are free to overwrite the value of err in the // following if statement stat := err.Error()[len(exitStatusErr):] if exitStatus, err = strconv.Atoi(stat); err != nil { return nil, err } } else { return nil, fmt.Errorf("error waiting for '%s': %v", cmdLine, err) } } cwd, err := os.Getwd() if err != nil { return nil, err } return &result{ success_: exitStatus == 0, descr_: cmdLine, detail_: fmt.Sprintf( "dir\t%s\nstdout\t|%s\nstderr\t|%s\nexit\t%d\nwant %s", cwd, strings.Replace(output, "\n", "\n\t|", -1), strings.Replace(errput, "\n", "\n\t|", -1), exitStatus, expDescr, ), }, nil }