(t *testing.T, filename string, options *compileopts.Options)
| 58 | } |
| 59 | |
| 60 | func testErrorMessages(t *testing.T, filename string, options *compileopts.Options) { |
| 61 | t.Parallel() |
| 62 | |
| 63 | // Parse expected error messages. |
| 64 | expected := readErrorMessages(t, filename) |
| 65 | |
| 66 | // Try to build a binary (this should fail with an error). |
| 67 | tmpdir := t.TempDir() |
| 68 | config, err := builder.NewConfig(options) |
| 69 | if err != nil { |
| 70 | t.Fatal("expected to get a compiler error") |
| 71 | } |
| 72 | err = Build(filename, tmpdir+"/out", config) |
| 73 | if err == nil { |
| 74 | t.Fatal("expected to get a compiler error") |
| 75 | } |
| 76 | |
| 77 | // Get the full ./testdata/errors directory. |
| 78 | wd, absErr := filepath.Abs("testdata/errors") |
| 79 | if absErr != nil { |
| 80 | t.Fatal(absErr) |
| 81 | } |
| 82 | |
| 83 | // Write error message out as plain text. |
| 84 | var buf bytes.Buffer |
| 85 | diagnostics.CreateDiagnostics(err).WriteTo(&buf, wd) |
| 86 | actual := strings.TrimRight(buf.String(), "\n") |
| 87 | |
| 88 | // Check whether the error is as expected. |
| 89 | if !matchErrors(t, expected, actual) { |
| 90 | t.Errorf("expected error:\n%s\ngot:\n%s", indentText(expected, "> "), indentText(actual, "> ")) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func matchErrors(t *testing.T, pattern, actual string) bool { |
| 95 | patternLines := strings.Split(pattern, "\n") |
no test coverage detected