blob: 38c1df76be457d951d63335e4468e1d74abf968e [file] [log] [blame]
Avi Drissmandfd880852022-09-15 20:11:091// Copyright 2013 The Chromium Authors
[email protected]7173edd02011-02-09 00:31:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gayane Petrosyan945148b2021-11-04 18:52:035#include "testing/data_driven_testing/data_driven_test.h"
[email protected]7173edd02011-02-09 00:31:576
[email protected]25a4c1c2013-06-08 04:53:367#include "base/files/file_enumerator.h"
thestig819adcc82014-09-10 22:24:538#include "base/files/file_util.h"
[email protected]4cc42fe2013-06-11 04:17:109#include "base/strings/string_util.h"
jam3f2d3932017-04-26 20:28:5110#include "base/threading/thread_restrictions.h"
[email protected]7173edd02011-02-09 00:31:5711#include "testing/gtest/include/gtest/gtest.h"
Dominic Battre50103492019-09-19 08:38:1112#include "third_party/re2/src/re2/re2.h"
[email protected]7173edd02011-02-09 00:31:5713
Gayane Petrosyan945148b2021-11-04 18:52:0314namespace testing {
[email protected]7173edd02011-02-09 00:31:5715namespace {
16
17// Reads |file| into |content|, and converts Windows line-endings to Unix ones.
18// Returns true on success.
[email protected]650b2d52013-02-10 03:41:4519bool ReadFile(const base::FilePath& file, std::string* content) {
[email protected]82f84b92013-08-30 18:23:5020 if (!base::ReadFileToString(file, content))
[email protected]7173edd02011-02-09 00:31:5721 return false;
22
brettwe6dae462015-06-24 20:54:4523 base::ReplaceSubstringsAfterOffset(content, 0, "\r\n", "\n");
[email protected]7173edd02011-02-09 00:31:5724 return true;
25}
26
27// Write |content| to |file|. Returns true on success.
[email protected]650b2d52013-02-10 03:41:4528bool WriteFile(const base::FilePath& file, const std::string& content) {
Claudio DeSouza94ad8782023-02-27 09:05:4129 return base::WriteFile(file, content);
[email protected]7173edd02011-02-09 00:31:5730}
31
Dominic Battre50103492019-09-19 08:38:1132// Removes lines starting with (optional) whitespace and a #.
33void StripComments(std::string* content) {
34 RE2::GlobalReplace(
35 content,
36 // Enable multi-line mode, ^ and $ match begin/end line in addition to
37 // begin/end text.
38 "(?m)"
39 // Search for start of lines (^), ignore spaces (\\s*), and then look for
40 // '#'.
41 "^\\s*#"
42 // Consume all characters (.*) until end of line ($).
43 ".*$"
44 // Consume the line wrapping so that the entire line is gone.
45 "[\\r\\n]*",
46 // Replace entire line with empty string.
47 "");
48}
49
[email protected]7173edd02011-02-09 00:31:5750} // namespace
51
52void DataDrivenTest::RunDataDrivenTest(
[email protected]650b2d52013-02-10 03:41:4553 const base::FilePath& input_directory,
54 const base::FilePath& output_directory,
55 const base::FilePath::StringType& file_name_pattern) {
Francois Doraye6fb2d02017-10-18 21:29:1356 base::ScopedAllowBlockingForTesting allow_blocking;
[email protected]0faac1d2013-07-19 23:51:0457 ASSERT_TRUE(base::DirectoryExists(input_directory));
58 ASSERT_TRUE(base::DirectoryExists(output_directory));
Gayane Petrosyan945148b2021-11-04 18:52:0359 base::FileEnumerator input_files(
60 input_directory, false, base::FileEnumerator::FILES, file_name_pattern);
Roger McFarlanebf55e6a2017-11-28 18:55:2361 const bool kIsExpectedToPass = true;
Gayane Petrosyan945148b2021-11-04 18:52:0362 for (base::FilePath input_file = input_files.Next(); !input_file.empty();
[email protected]7173edd02011-02-09 00:31:5763 input_file = input_files.Next()) {
Roger McFarlanebf55e6a2017-11-28 18:55:2364 RunOneDataDrivenTest(input_file, output_directory, kIsExpectedToPass);
[email protected]7173edd02011-02-09 00:31:5765 }
66}
67
Evan Stadef11e8ceb22015-01-08 23:11:2668void DataDrivenTest::RunOneDataDrivenTest(
69 const base::FilePath& test_file_name,
Roger McFarlanebf55e6a2017-11-28 18:55:2370 const base::FilePath& output_directory,
71 bool is_expected_to_pass) {
Francois Doraye6fb2d02017-10-18 21:29:1372 base::ScopedAllowBlockingForTesting allow_blocking;
estade431dfb722015-06-05 22:30:0173 // iOS doesn't get rid of removed test files. TODO(estade): remove this after
74 // all iOS bots are clobbered.
75 if (test_file_name.BaseName().value() == FILE_PATH_LITERAL("multimerge.in"))
76 return;
77
Evan Stadef11e8ceb22015-01-08 23:11:2678 ASSERT_TRUE(base::DirectoryExists(output_directory));
79 SCOPED_TRACE(test_file_name.BaseName().value());
80
81 std::string input;
82 ReadFile(test_file_name, &input);
83
84 std::string output;
Gabriel Charettead330422021-07-21 00:49:2885 {
86 base::ScopedDisallowBlocking disallow_blocking;
87 GenerateResults(input, &output);
88 }
Evan Stadef11e8ceb22015-01-08 23:11:2689
90 base::FilePath output_file = output_directory.Append(
91 test_file_name.BaseName().StripTrailingSeparators().ReplaceExtension(
92 FILE_PATH_LITERAL(".out")));
93
94 std::string output_file_contents;
Roger McFarlanebf55e6a2017-11-28 18:55:2395 if (!ReadFile(output_file, &output_file_contents)) {
Evan Stadef11e8ceb22015-01-08 23:11:2696 ASSERT_TRUE(WriteFile(output_file, output));
Roger McFarlanebf55e6a2017-11-28 18:55:2397 return;
98 }
Dominic Battre50103492019-09-19 08:38:1199 // Remove comment lines (lead by '#' character).
100 StripComments(&output_file_contents);
Roger McFarlanebf55e6a2017-11-28 18:55:23101
102 if (is_expected_to_pass) {
103 EXPECT_EQ(output_file_contents, output);
104 } else {
105 EXPECT_NE(output_file_contents, output);
106 }
Evan Stadef11e8ceb22015-01-08 23:11:26107}
108
Gayane Petrosyan945148b2021-11-04 18:52:03109base::FilePath DataDrivenTest::GetInputDirectory() {
110 return test_data_directory_.Append(feature_directory_)
111 .Append(test_name_)
Roger McFarlanebf55e6a2017-11-28 18:55:23112 .AppendASCII("input");
[email protected]7173edd02011-02-09 00:31:57113}
114
Gayane Petrosyan945148b2021-11-04 18:52:03115base::FilePath DataDrivenTest::GetOutputDirectory() {
116 return test_data_directory_.Append(feature_directory_)
117 .Append(test_name_)
Roger McFarlanebf55e6a2017-11-28 18:55:23118 .AppendASCII("output");
[email protected]7173edd02011-02-09 00:31:57119}
120
Gayane Petrosyan945148b2021-11-04 18:52:03121DataDrivenTest::DataDrivenTest(
122 const base::FilePath& test_data_directory,
123 const base::FilePath::StringType& feature_directory,
124 const base::FilePath::StringType& test_name)
125 : test_data_directory_(test_data_directory),
126 feature_directory_(feature_directory),
127 test_name_(test_name) {}
[email protected]7173edd02011-02-09 00:31:57128
Gayane Petrosyan945148b2021-11-04 18:52:03129DataDrivenTest::~DataDrivenTest() {}
[email protected]e217c5632013-04-12 19:11:48130
Gayane Petrosyan945148b2021-11-04 18:52:03131} // namespace testing