in internal/build/cmd/generate/commands/gentests/command.go [200:318]
func (cmd *Command) processFile(fpath string) (err error) {
if utils.IsTTY() {
fmt.Fprint(os.Stderr, "\x1b[2m")
}
fmt.Fprintln(os.Stderr, strings.Repeat("━", utils.TerminalWidth()))
fmt.Fprintf(os.Stderr, "Processing %q\n", fpath)
if utils.IsTTY() {
fmt.Fprint(os.Stderr, "\x1b[0m")
}
var out io.Reader
var payloads []TestPayload
f, err := os.Open(fpath)
if err != nil {
return err
}
defer f.Close()
patched, err := PatchTestSource(fpath, f)
if err != nil {
return fmt.Errorf("cannot patch source: %s", err)
}
dec := yaml.NewDecoder(patched)
for {
var payload interface{}
err := dec.Decode(&payload)
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("parse error: %s", err)
}
payloads = append(payloads, TestPayload{fpath, payload})
}
ts := NewTestSuite(fpath, payloads)
if cmd.DebugInfo {
if utils.IsTTY() {
fmt.Fprint(os.Stderr, "\x1b[2m")
}
fmt.Fprint(os.Stderr, ts.DebugInfo())
if utils.IsTTY() {
fmt.Fprint(os.Stderr, "\n\x1b[0m")
}
}
if ts.Skip {
if utils.IsTTY() {
fmt.Fprint(os.Stderr, "\x1b[2m")
}
fmt.Fprintf(os.Stderr, "Skipping test suite %q", ts.Name())
if utils.IsTTY() {
fmt.Fprint(os.Stderr, "\n\x1b[0m")
}
return nil
}
gen := Generator{TestSuite: ts}
if cmd.Gofmt {
out, err = gen.OutputFormatted()
} else {
out, err = gen.Output()
}
if err != nil {
if cmd.DebugSource {
utils.PrintSourceWithErr(out, err)
}
return fmt.Errorf("error generating output: %s", err)
}
if cmd.DebugSource {
var src io.Reader
var buf bytes.Buffer
tee := io.TeeReader(out, &buf)
src, err = utils.Chromatize(tee)
if err != nil {
return fmt.Errorf("error syntax highligting the output: %s", err)
}
_, err = io.Copy(os.Stderr, src)
if err != nil {
return fmt.Errorf("error copying output: %s", err)
}
out = &buf
}
if cmd.Output == "-" {
_, err = io.Copy(os.Stdout, out)
if err != nil {
return fmt.Errorf("error copying output: %s", err)
}
} else {
if err := os.MkdirAll(cmd.Output, 0775); err != nil {
return fmt.Errorf("error creating directory: %s", err)
}
fName := filepath.Join(cmd.Output, gen.TestSuite.Filename()+"_test.go")
f, err := os.OpenFile(fName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("error creating file: %s", err)
}
_, err = io.Copy(f, out)
if err != nil {
return fmt.Errorf("error copying output: %s", err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("error closing file: %s", err)
}
}
return nil
}