Example #1
0
func compareGolden(t *testing.T, godebug, test string) {
	golden, err := ioutil.ReadFile(goldenOutput(test))
	if err != nil {
		t.Fatal(err)
	}
	golden = normalizeCRLF(golden)
	cmd := exec.Command(godebug, "output", testInput(test))
	var buf bytes.Buffer
	cmd.Stdout = &buf
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		fmt.Println(buf.String())
		t.Fatal(err)
	}
	output := stripTestPrefix(buf.Bytes())
	if !bytes.Equal(output, golden) {
		if *accept {
			if err = ioutil.WriteFile(goldenOutput(test), output, 0644); err != nil {
				t.Fatal(err)
			}
			return
		}
		t.Errorf("%s: want != got. Diff:\n%s", test, diff.Diff(string(golden), string(output)))
	}
}
Example #2
0
func checkOutput(t *testing.T, want *session, tool string, output []byte) {
	testName := filepath.Base(want.filename)
	fmt.Printf("checking %s (%s)\n", testName, tool)
	got := interleaveCommands(want.input, output)
	if bytes.Equal(got, want.fullSession) {
		return
	}

	if *acceptSession {
		if err := ioutil.WriteFile(want.filename, got, 0644); err != nil {
			t.Fatal(err)
		}
		return
	}

	t.Errorf("%s: Session did not match. Diff:\n%v", testName, diff.Diff(string(want.fullSession), string(got)))
}
func runTest(t *testing.T, godebug, filename string, tt testCase, i int, session *session) {
	var buf bytes.Buffer
	command, dir := tt.Invocations[i].Cmd, tt.Invocations[i].Dir
	cmd := exec.Command(godebug, strings.Split(command, " ")[1:]...)
	cmd.Dir = filepath.FromSlash("testdata/test-filesystem/" + dir)
	cmd.Stdout = &buf
	cmd.Stderr = &buf
	cmd.Stdin = bytes.NewReader(session.input)
	setTestGopath(t, cmd)

	// Show multiple errors if they exist and format them nicely.
	var errs []string
	defer func() {
		if errs != nil {
			t.Errorf("File: %s\nDescription: %s\nWorking dir: %s\nCommand: %s\nFailures:\n\t%v",
				filename, tt.Desc, dir, command, strings.Join(errs, "\n\t"))
		}
	}()

	cmd.Env = append(cmd.Env, logFileEnvVar+"=true")
	err := cmd.Run()
	// Because we set `logFileEnvVar` above, godebug will print the
	// files it creates to stdout. Parse those lines and then pretend
	// they were not printed.
	output := stripTestPrefix(buf.Bytes())
	createdFiles, output := recordCreatedFiles(output)

	switch err.(type) {
	case nil:
		if tt.NonzeroExit {
			errs = append(errs, "got exit code == 0, wanted a nonzero exit code.")
			return
		}
	case *exec.ExitError:
		if !tt.NonzeroExit {
			errs = append(errs, fmt.Sprintf("%q failed to run: %v\n%s", command, err, output))
			return
		}
	default:
		errs = append(errs, fmt.Sprintf("%q failed to run: %v\n%s", command, err, output))
		return
	}

	// Check that we created the files we expected and did not create
	// any files we did not expect.
	errs = append(errs, checkCreatedFiles(t, createdFiles, tt.Creates)...)

	if tt.Godebugwork {
		output, err = checkGodebugwork(t, session.fullSession, output)
		if err != nil {
			errs = append(errs, err.Error())
		}
	}

	got := interleaveCommands(session.input, output)
	if equivalent(got, session.fullSession) {
		return
	}
	errs = append(errs, fmt.Sprintf("golden transcript did not match actual transcript. Diff:\n\n%v", diff.Diff(string(session.fullSession), string(got))))
}