Rollforward of https://github.com/bazelbuild/bazel/commit/2d3adfb93c023f9752f00d4bf3440ee9869f64f2: Add a warning if the same rc file is read multiple times.
Prerequisite for fixing #5765, to minimize the risk that multiple rc files import the same file, which is slightly more likely with try-import.
Added extra test for double-load rcs imported using different syntax, fixing an issue where paths were not canonicalized for reliable comparison.
RELNOTES: None.
PiperOrigin-RevId: 210140725
diff --git a/src/test/cpp/rc_options_test.cc b/src/test/cpp/rc_options_test.cc
index 58251af..41684a5 100644
--- a/src/test/cpp/rc_options_test.cc
+++ b/src/test/cpp/rc_options_test.cc
@@ -22,12 +22,14 @@
#include "src/main/cpp/util/path.h"
#include "src/main/cpp/util/strings.h"
#include "src/main/cpp/workspace_layout.h"
+#include "googlemock/include/gmock/gmock.h"
#include "googletest/include/gtest/gtest.h"
namespace blaze {
using std::string;
using std::unordered_map;
using std::vector;
+using ::testing::MatchesRegex;
class RcOptionsTest : public ::testing::Test {
protected:
@@ -328,13 +330,12 @@
std::unique_ptr<RcFile> rc =
Parse("import_cycle_1.bazelrc", &error, &error_text);
EXPECT_EQ(error, RcFile::ParseError::IMPORT_LOOP);
- string expected_error;
- blaze_util::StringPrintf(
- &expected_error, "Import loop detected:\n %s\n %s\n %s\n",
- blaze_util::JoinPath(test_file_dir_, "import_cycle_1.bazelrc").c_str(),
- blaze_util::JoinPath(test_file_dir_, "import_cycle_2.bazelrc").c_str(),
- blaze_util::JoinPath(test_file_dir_, "import_cycle_1.bazelrc").c_str());
- ASSERT_EQ(error_text, expected_error);
+ ASSERT_THAT(
+ error_text,
+ MatchesRegex("Import loop detected:\n"
+ " .*import_cycle_1.bazelrc\n"
+ " .*import_cycle_2.bazelrc\n"
+ " .*import_cycle_1.bazelrc\n"));
}
TEST_F(RcOptionsTest, LongImportCycleFails) {
@@ -356,18 +357,16 @@
std::unique_ptr<RcFile> rc =
Parse("chain_to_cycle_1.bazelrc", &error, &error_text);
EXPECT_EQ(error, RcFile::ParseError::IMPORT_LOOP);
- string expected_error;
- blaze_util::StringPrintf(
- &expected_error,
- "Import loop detected:\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
- blaze_util::JoinPath(test_file_dir_, "chain_to_cycle_1.bazelrc").c_str(),
- blaze_util::JoinPath(test_file_dir_, "chain_to_cycle_2.bazelrc").c_str(),
- blaze_util::JoinPath(test_file_dir_, "chain_to_cycle_3.bazelrc").c_str(),
- blaze_util::JoinPath(test_file_dir_, "chain_to_cycle_4.bazelrc").c_str(),
- blaze_util::JoinPath(test_file_dir_, "import_cycle_1.bazelrc").c_str(),
- blaze_util::JoinPath(test_file_dir_, "import_cycle_2.bazelrc").c_str(),
- blaze_util::JoinPath(test_file_dir_, "import_cycle_1.bazelrc").c_str());
- ASSERT_EQ(error_text, expected_error);
+ ASSERT_THAT(
+ error_text,
+ MatchesRegex("Import loop detected:\n"
+ " .*chain_to_cycle_1.bazelrc\n"
+ " .*chain_to_cycle_2.bazelrc\n"
+ " .*chain_to_cycle_3.bazelrc\n"
+ " .*chain_to_cycle_4.bazelrc\n"
+ " .*import_cycle_1.bazelrc\n"
+ " .*import_cycle_2.bazelrc\n"
+ " .*import_cycle_1.bazelrc\n"));
}
TEST_F(RcOptionsTest, FileDoesNotExist) {
@@ -375,11 +374,10 @@
string error_text;
std::unique_ptr<RcFile> rc = Parse("not_a_file.bazelrc", &error, &error_text);
EXPECT_EQ(error, RcFile::ParseError::UNREADABLE_FILE);
- string expected_error;
- blaze_util::StringPrintf(
- &expected_error, "Unexpected error reading .blazerc file '%s'",
- blaze_util::JoinPath(test_file_dir_, "not_a_file.bazelrc").c_str());
- ASSERT_EQ(error_text, expected_error);
+ ASSERT_THAT(
+ error_text,
+ MatchesRegex(
+ "Unexpected error reading .blazerc file '.*not_a_file.bazelrc'"));
}
TEST_F(RcOptionsTest, ImportedFileDoesNotExist) {
@@ -402,14 +400,11 @@
string error_text;
std::unique_ptr<RcFile> rc = Parse("bad_import.bazelrc", &error, &error_text);
EXPECT_EQ(error, RcFile::ParseError::INVALID_FORMAT);
-
- string expected_error;
- blaze_util::StringPrintf(
- &expected_error,
- "Invalid import declaration in .blazerc file '%s': "
- "'import somefile bar' (are you in your source checkout/WORKSPACE?)",
- blaze_util::JoinPath(test_file_dir_, "bad_import.bazelrc").c_str());
- ASSERT_EQ(error_text, expected_error);
+ ASSERT_THAT(
+ error_text,
+ MatchesRegex("Invalid import declaration in .blazerc file "
+ "'.*bad_import.bazelrc': 'import somefile bar' \\(are you "
+ "in your source checkout/WORKSPACE\\?\\)"));
}
// TODO(b/34811299) The tests below identify ways that '\' used as a line