From 6eca60504e9c6c687dc03ffc448d75ccc2f9baac Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 8 Oct 2024 17:28:04 -0400 Subject: [PATCH] headless-browser: Only rebaseline tests that would have failed We have a bit of forgiveness around allowing tests to pass with varying trailing newlines. Only write a rebaselined test to disk if it would not have passed under those conditions. --- Userland/Utilities/headless-browser.cpp | 34 +++++++++++++++---------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/Userland/Utilities/headless-browser.cpp b/Userland/Utilities/headless-browser.cpp index a8eb5121415..609250b2aec 100644 --- a/Userland/Utilities/headless-browser.cpp +++ b/Userland/Utilities/headless-browser.cpp @@ -339,27 +339,33 @@ static void run_dump_test(HeadlessWebContentView& view, Test& test, URL::URL con return TestResult::Pass; } - auto expectation_file_or_error = Core::File::open(test.expectation_path, Application::the().rebaseline ? Core::File::OpenMode::Write : Core::File::OpenMode::Read); - if (expectation_file_or_error.is_error()) { - warnln("Failed opening '{}': {}", test.expectation_path, expectation_file_or_error.error()); - return expectation_file_or_error.release_error(); - } + auto open_expectation_file = [&](auto mode) { + auto expectation_file_or_error = Core::File::open(test.expectation_path, mode); + if (expectation_file_or_error.is_error()) + warnln("Failed opening '{}': {}", test.expectation_path, expectation_file_or_error.error()); - auto expectation_file = expectation_file_or_error.release_value(); + return expectation_file_or_error; + }; + + ByteBuffer expectation; + { + auto expectation_file = TRY(open_expectation_file(Core::File::OpenMode::Read)); + expectation = TRY(expectation_file->read_until_eof()); + + auto result_trimmed = StringView { test.text }.trim("\n"sv, TrimMode::Right); + auto expectation_trimmed = StringView { expectation }.trim("\n"sv, TrimMode::Right); + + if (result_trimmed == expectation_trimmed) + return TestResult::Pass; + } if (Application::the().rebaseline) { + auto expectation_file = TRY(open_expectation_file(Core::File::OpenMode::Write)); TRY(expectation_file->write_until_depleted(test.text)); + return TestResult::Pass; } - auto expectation = TRY(expectation_file->read_until_eof()); - - auto result_trimmed = StringView { test.text }.trim("\n"sv, TrimMode::Right); - auto expectation_trimmed = StringView { expectation }.trim("\n"sv, TrimMode::Right); - - if (result_trimmed == expectation_trimmed) - return TestResult::Pass; - auto const color_output = isatty(STDOUT_FILENO) ? Diff::ColorOutput::Yes : Diff::ColorOutput::No; if (color_output == Diff::ColorOutput::Yes)