mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Create a UnifiedDiffMatcher to make golden test failures easier to understand (#6897)
Right now I think everyone has the habit of doing an autoupdate then
using source control for a diff. This is offering an option of better
diff output from the test.
For example:
```
TEST: toolchain/driver/testdata/fail_flush_errors.carbon !
Ran 1 tests in 81 ms wall time, 8 ms across threads
testing/file_test/file_test_base.cpp:264: Failure
Value of: SplitOutput(test_file.actual_stderr)
Expected: matches elements with union diff
Actual: { "fail_flush_errors.carbon:22:3: error: name `undeclared1` not found [NameNotFound]", " undeclared1;", " ^~~~~~~~~~~", "", "fail_flush_errors.carbon:31:3: error: `Core.String` implicitly referenced here, but package `Core` not found [CoreNotFound]", " \"undec\\x6Cared2\";", " ^~~~~~~~~~~~~~~~", "", "fail_flush_errors.carbon:35:3: error: name `undeclared2` not found [NameNotFound]", " undeclared2;", " ^~~~~~~~~~~", "", "fail_flush_errors.carbon:43:3: error: name `undeclared3` not found [NameNotFound]", " undeclared3;", " ^~~~~~~~~~~", "", "" }, union diff (- expected, + actual):
=== diff in expected elements 0 to 2:
+ fail_flush_errors.carbon:22:3: error: name `undeclared1` not found [NameNotFound]
undeclared1;
^~~~~~~~~~~
=== diff in expected elements 4 to 9:
"undec\x6Cared2";
^~~~~~~~~~~~~~~~
+ fail_flush_errors.carbon:35:3: error: name `undeclared2` not found [NameNotFound]
undeclared2;
^~~~~~~~~~~
=== diff end
Stack trace:
0x55e476d29efd: Carbon::Testing::FileTestCase::TestBody()
0x55e476dbd1f2: testing::internal::HandleExceptionsInMethodIfSupported<>()
0x55e476dbcf57: testing::Test::Run()
0x55e476dbf0bf: testing::TestInfo::Run()
... Google Test internal frames ...
To test this file alone, run:
bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/driver/testdata/fail_flush_errors.carbon
testing/file_test/file_test_base.cpp:277: Failure
Failed
Autoupdate would make changes to the file content. Run:
bazel run //toolchain/testing:file_test -- --autoupdate --file_tests=toolchain/driver/testdata/fail_flush_errors.carbon
Stack trace:
0x55e476d2a5f0: Carbon::Testing::FileTestCase::TestBody()
0x55e476dbd1f2: testing::internal::HandleExceptionsInMethodIfSupported<>()
0x55e476dbcf57: testing::Test::Run()
0x55e476dbf0bf: testing::TestInfo::Run()
... Google Test internal frames ...
[ FAILED ] ToolchainFileTest.toolchain/driver/testdata/fail_flush_errors.carbon, where GetParam() = toolchain/driver/testdata/fail_flush_errors.carbon (93 ms)
```
Assisted-by: Google Antigravity with Gemini
This commit is contained in:
@@ -50,6 +50,7 @@
|
||||
#include "llvm/Support/PrettyStackTrace.h"
|
||||
#include "llvm/Support/Process.h"
|
||||
#include "llvm/Support/ThreadPool.h"
|
||||
#include "testing/base/unified_diff_matcher.h"
|
||||
#include "testing/file_test/autoupdate.h"
|
||||
#include "testing/file_test/run_test.h"
|
||||
#include "testing/file_test/test_file.h"
|
||||
@@ -119,7 +120,6 @@ static auto SplitOutput(llvm::StringRef output)
|
||||
llvm::StringRef(output).split(lines, "\n");
|
||||
return llvm::SmallVector<std::string_view>(lines.begin(), lines.end());
|
||||
}
|
||||
|
||||
// Verify that the success and `fail_` prefix use correspond. Separately handle
|
||||
// both cases for clearer test failures.
|
||||
static auto CompareFailPrefix(llvm::StringRef filename, bool success) -> void {
|
||||
@@ -259,13 +259,9 @@ auto FileTestCase::TestBody() -> void {
|
||||
|
||||
} else {
|
||||
EXPECT_THAT(SplitOutput(test_file.actual_stdout),
|
||||
ElementsAreArray(test_file.expected_stdout))
|
||||
<< "Actual text:\n"
|
||||
<< test_file.actual_stdout;
|
||||
ElementsAreArrayWithUnifiedDiff(test_file.expected_stdout));
|
||||
EXPECT_THAT(SplitOutput(test_file.actual_stderr),
|
||||
ElementsAreArray(test_file.expected_stderr))
|
||||
<< "Actual text:\n"
|
||||
<< test_file.actual_stderr;
|
||||
ElementsAreArrayWithUnifiedDiff(test_file.expected_stderr));
|
||||
}
|
||||
|
||||
if (HasFailure()) {
|
||||
|
||||
Reference in New Issue
Block a user