Skip to content

Commit

Permalink
cl/427812386 Create a cc_binary to update output files (#37638)
Browse files Browse the repository at this point in the history
  • Loading branch information
antiphoton committed Feb 11, 2022
1 parent a4ad3b2 commit 5f0a1e1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions validator/cpp/engine/BUILD
Expand Up @@ -322,6 +322,16 @@ cc_library(
],
)

cc_binary(
name = "update-tests",
srcs = ["update-tests.cc"],
copts = ["-std=c++17"],
deps = [
":testing-utils",
":validator",
],
)

cc_test(
name = "validator_test",
srcs = ["validator_test.cc"],
Expand Down
43 changes: 43 additions & 0 deletions validator/cpp/engine/update-tests.cc
@@ -0,0 +1,43 @@
// A binary which runs the validator over golden test input files and overwrites
// all output files. It is useful to update a bunch of test and then inspect
// the diff.

#include <fstream>

#include "absl/strings/str_replace.h"
#include "cpp/engine/testing-utils.h"
#include "cpp/engine/validator.h"

namespace amp::validator {
namespace {

void UpdateTests() {
for (const auto& entry : testing::TestCases()) {
const testing::TestCase& test_case = entry.second;
ValidationResult result =
Validate(test_case.input_content, test_case.html_format);
std::string output_content = testing::RenderInlineResult(
test_case.name, test_case.input_content, result);
const std::string output_filename = absl::StrReplaceAll(
test_case.output_file,
{{"external/validator/testdata/", "validator/testdata/"},
{"external/amphtml-extensions/", "extensions/"}});
if (output_content == test_case.output_content) {
std::cout << "Unchanged validation: " << output_filename << std::endl;
} else {
std::cout << "Updating validation: " << output_filename << std::endl;
std::ofstream output_stream;
output_stream.open(output_filename);
output_stream << output_content;
output_stream.close();
}
}
}

} // namespace
} // namespace amp::validator

int main(int argc, char* argv[]) {
amp::validator::UpdateTests();
return 0;
}

0 comments on commit 5f0a1e1

Please sign in to comment.