(name string, t *testing.T, options compileopts.Options, cmdArgs, environmentVars []string)
| 433 | } |
| 434 | |
| 435 | func runTestWithConfig(name string, t *testing.T, options compileopts.Options, cmdArgs, environmentVars []string) { |
| 436 | t.Helper() |
| 437 | // Get the expected output for this test. |
| 438 | // Note: not using filepath.Join as it strips the path separator at the end |
| 439 | // of the path. |
| 440 | path := TESTDATA + "/" + name |
| 441 | // Get the expected output for this test. |
| 442 | expectedOutputPath := path[:len(path)-3] + ".txt" |
| 443 | pkgName := "./" + path |
| 444 | if path[len(path)-1] == '/' { |
| 445 | expectedOutputPath = path + "out.txt" |
| 446 | options.Directory = path |
| 447 | pkgName = "." |
| 448 | } |
| 449 | |
| 450 | config, err := builder.NewConfig(&options) |
| 451 | if err != nil { |
| 452 | t.Fatal(err) |
| 453 | } |
| 454 | |
| 455 | // Build the test binary. |
| 456 | stdout := &bytes.Buffer{} |
| 457 | _, err = buildAndRun(pkgName, config, stdout, cmdArgs, environmentVars, 2*time.Minute, func(cmd *exec.Cmd, result builder.BuildResult) error { |
| 458 | return cmd.Run() |
| 459 | }) |
| 460 | if err != nil { |
| 461 | w := &bytes.Buffer{} |
| 462 | diagnostics.CreateDiagnostics(err).WriteTo(w, "") |
| 463 | for _, line := range strings.Split(strings.TrimRight(w.String(), "\n"), "\n") { |
| 464 | t.Log(line) |
| 465 | } |
| 466 | if stdout.Len() != 0 { |
| 467 | t.Logf("output:\n%s", stdout.String()) |
| 468 | } |
| 469 | t.Fail() |
| 470 | return |
| 471 | } |
| 472 | |
| 473 | actual := stdout.Bytes() |
| 474 | if config.EmulatorName() == "simavr" { |
| 475 | // Strip simavr log formatting. |
| 476 | actual = bytes.Replace(actual, []byte{0x1b, '[', '3', '2', 'm'}, nil, -1) |
| 477 | actual = bytes.Replace(actual, []byte{0x1b, '[', '0', 'm'}, nil, -1) |
| 478 | actual = bytes.Replace(actual, []byte{'.', '.', '\n'}, []byte{'\n'}, -1) |
| 479 | actual = bytes.Replace(actual, []byte{'\n', '.', '\n'}, []byte{'\n', '\n'}, -1) |
| 480 | } |
| 481 | if name == "testing.go" { |
| 482 | // Strip actual time. |
| 483 | re := regexp.MustCompile(`\([0-9]\.[0-9][0-9]s\)`) |
| 484 | actual = re.ReplaceAllLiteral(actual, []byte{'(', '0', '.', '0', '0', 's', ')'}) |
| 485 | } |
| 486 | |
| 487 | // Check whether the command ran successfully. |
| 488 | if err != nil { |
| 489 | t.Error("failed to run:", err) |
| 490 | } |
| 491 | checkOutput(t, expectedOutputPath, actual) |
| 492 |
no test coverage detected