aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/cpp
diff options
context:
space:
mode:
authorGravatar ccalvarin <ccalvarin@google.com>2018-05-15 16:41:19 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-15 16:43:02 -0700
commit90e116c2358e2d1e71818f7d39ddb609581ee211 (patch)
tree998a51348dfe39c513d44a05bdc0935589fd9102 /src/test/cpp
parent0015d18f57e3f94905b58967b9dd6a1e8b364596 (diff)
Add --ignore_all_rc_files startup options.
This overrides --bazelrc and --[no]master_bazelrc regardless of order. Like --bazelrc and --[no]master_bazelrc, it cannot be mentioned in an rc file, this would be a contradiction. This flag is useful for testing, and for having a version-agnostic way to turn off all rc files, such as in the canonical command line reporting. Now that blazerc and bazelrc are separate, this is necessary. If explicit values for --bazelrc or --master_bazelrc are provided which are now ignored, Bazel will warn the user. #4502 Alternatives considered - We could avoid this flag but would need to have some well-documented, reusable list of the startup flags that effectively come to the same effect. This would be necessary in our integration tests and in the CommandLineEvent and other places where rc files need to be completely disabled for correctness. We decided that this startup option was more straightforward and usable for both users and Bazel devs: it shouldn't be used when more fine-grained control is needed, but provides warnings if users are likely to be confused by the outcome. RELNOTES: None. PiperOrigin-RevId: 196750704
Diffstat (limited to 'src/test/cpp')
-rw-r--r--src/test/cpp/bazel_startup_options_test.cc65
-rw-r--r--src/test/cpp/rc_file_test.cc90
-rw-r--r--src/test/cpp/startup_options_test.cc1
-rw-r--r--src/test/cpp/test_util.cc21
-rw-r--r--src/test/cpp/test_util.h4
5 files changed, 181 insertions, 0 deletions
diff --git a/src/test/cpp/bazel_startup_options_test.cc b/src/test/cpp/bazel_startup_options_test.cc
index 2ac4c7f61b..686f43178d 100644
--- a/src/test/cpp/bazel_startup_options_test.cc
+++ b/src/test/cpp/bazel_startup_options_test.cc
@@ -86,6 +86,7 @@ TEST_F(BazelStartupOptionsTest, ValidStartupFlags) {
ExpectIsNullaryOption(options, "experimental_oom_more_eagerly");
ExpectIsNullaryOption(options, "fatal_event_bus_exceptions");
ExpectIsNullaryOption(options, "host_jvm_debug");
+ ExpectIsNullaryOption(options, "ignore_all_rc_files");
ExpectIsNullaryOption(options, "master_bazelrc");
ExpectIsNullaryOption(options, "watchfs");
ExpectIsNullaryOption(options, "write_command_log");
@@ -111,4 +112,68 @@ TEST_F(BazelStartupOptionsTest, BlazercFlagsAreNotAccepted) {
EXPECT_FALSE(startup_options_->IsUnary("--blazerc"));
}
+TEST_F(BazelStartupOptionsTest, IgnoredBazelrcFlagWarns) {
+ ParseStartupOptionsAndExpectWarning(
+ startup_options_.get(), {"--bazelrc=somefile", "--ignore_all_rc_files"},
+ "WARNING: Value of --bazelrc is ignored, since --ignore_all_rc_files is "
+ "on.\n");
+}
+
+TEST_F(BazelStartupOptionsTest, IgnoredBazelrcFlagWarnsWhenAfterIgnore) {
+ ParseStartupOptionsAndExpectWarning(
+ startup_options_.get(), {"--ignore_all_rc_files", "--bazelrc=somefile"},
+ "WARNING: Value of --bazelrc is ignored, since --ignore_all_rc_files is "
+ "on.\n");
+}
+
+TEST_F(BazelStartupOptionsTest, IgnoredMasterBazelrcFlagWarns) {
+ ParseStartupOptionsAndExpectWarning(
+ startup_options_.get(), {"--master_bazelrc", "--ignore_all_rc_files"},
+ "WARNING: Explicit value of --master_bazelrc is ignored, "
+ "since --ignore_all_rc_files is on.\n");
+}
+
+TEST_F(BazelStartupOptionsTest, IgnoredMasterBazelrcFlagWarnsAfterIgnore) {
+ ParseStartupOptionsAndExpectWarning(
+ startup_options_.get(), {"--ignore_all_rc_files", "--master_bazelrc"},
+ "WARNING: Explicit value of --master_bazelrc is ignored, "
+ "since --ignore_all_rc_files is on.\n");
+}
+
+TEST_F(BazelStartupOptionsTest, MultipleIgnoredRcFlagsWarnOnceEach) {
+ ParseStartupOptionsAndExpectWarning(
+ startup_options_.get(),
+ {"--master_bazelrc", "--bazelrc=somefile", "--ignore_all_rc_files",
+ "--bazelrc=thefinalfile", "--master_bazelrc"},
+ "WARNING: Value of --bazelrc is ignored, "
+ "since --ignore_all_rc_files is on.\n"
+ "WARNING: Explicit value of --master_bazelrc is ignored, "
+ "since --ignore_all_rc_files is on.\n");
+}
+
+TEST_F(BazelStartupOptionsTest, IgnoredNoMasterBazelrcDoesNotWarn) {
+ // Warning for nomaster would feel pretty spammy - it's redundant, but the
+ // behavior is as one would expect, so warning is unnecessary.
+ ParseStartupOptionsAndExpectWarning(
+ startup_options_.get(), {"--ignore_all_rc_files", "--nomaster_bazelrc"},
+ "");
+}
+
+TEST_F(BazelStartupOptionsTest, IgnoreOptionDoesNotWarnOnItsOwn) {
+ ParseStartupOptionsAndExpectWarning(startup_options_.get(),
+ {"--ignore_all_rc_files"}, "");
+}
+
+TEST_F(BazelStartupOptionsTest, NonIgnoredOptionDoesNotWarn) {
+ ParseStartupOptionsAndExpectWarning(startup_options_.get(),
+ {"--bazelrc=somefile"}, "");
+}
+
+TEST_F(BazelStartupOptionsTest, FinalValueOfIgnoreIsUsedForWarning) {
+ ParseStartupOptionsAndExpectWarning(
+ startup_options_.get(),
+ {"--ignore_all_rc_files", "--master_bazelrc", "--noignore_all_rc_files"},
+ "");
+}
+
} // namespace blaze
diff --git a/src/test/cpp/rc_file_test.cc b/src/test/cpp/rc_file_test.cc
index 70b3f931ed..5900db1cd1 100644
--- a/src/test/cpp/rc_file_test.cc
+++ b/src/test/cpp/rc_file_test.cc
@@ -229,6 +229,96 @@ TEST_F(GetRcFileTest, GetRcFilesReadsUserRcInWorkspace) {
using ParseOptionsTest = RcFileTest;
+TEST_F(ParseOptionsTest, IgnoreAllRcFilesIgnoresAllMasterAndUserRcFiles) {
+ // Put fake options in different expected rc files, to check that none of them
+ // are read.
+ std::string user_workspace_rc;
+ ASSERT_TRUE(
+ SetUpUserRcFileInWorkspace("startup --userfoo", &user_workspace_rc));
+ std::string workspace_rc;
+ ASSERT_TRUE(SetUpMasterRcFileInWorkspace("startup --workspacemasterfoo",
+ &workspace_rc));
+ std::string binary_rc;
+ ASSERT_TRUE(SetUpMasterRcFileAlongsideBinary("startup --binarymasterfoo",
+ &binary_rc));
+
+ const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
+ "build"};
+ // Expect no error due to the incorrect options, as non of them should have
+ // been loaded.
+ std::string error;
+ EXPECT_EQ(blaze_exit_code::SUCCESS,
+ option_processor_->ParseOptions(args, workspace_, cwd_, &error));
+ ASSERT_EQ("", error);
+
+ // Check that the startup options' provenance message contains nothing
+ testing::internal::CaptureStderr();
+ option_processor_->PrintStartupOptionsProvenanceMessage();
+ const std::string& output = testing::internal::GetCapturedStderr();
+
+ EXPECT_EQ(output, "");
+}
+
+TEST_F(ParseOptionsTest, LaterIgnoreRcFileValueWins) {
+ std::string workspace_rc;
+ ASSERT_TRUE(SetUpMasterRcFileInWorkspace("startup --workspacemasterfoo",
+ &workspace_rc));
+
+ const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
+ "--noignore_all_rc_files", "build"};
+ std::string error;
+ EXPECT_EQ(blaze_exit_code::BAD_ARGV,
+ option_processor_->ParseOptions(args, workspace_, cwd_, &error));
+ ASSERT_EQ(
+ "Unknown startup option: '--workspacemasterfoo'.\n For more info, run "
+ "'bazel help startup_options'.",
+ error);
+
+ // Check that the startup options' provenance message contains the provenance
+ // of the incorrect option.
+ testing::internal::CaptureStderr();
+ option_processor_->PrintStartupOptionsProvenanceMessage();
+ const std::string& output = testing::internal::GetCapturedStderr();
+
+ EXPECT_THAT(output,
+ MatchesRegex("INFO: Reading 'startup' options from .*bazel.rc: "
+ "--workspacemasterfoo\n"));
+}
+
+TEST_F(ParseOptionsTest, IgnoreAllRcFilesIgnoresCommandLineRcFileToo) {
+ // Put fake options in different expected rc files, to check that none of them
+ // are read.
+ std::string workspace_rc;
+ ASSERT_TRUE(SetUpMasterRcFileInWorkspace("startup --workspacemasterfoo",
+ &workspace_rc));
+ std::string binary_rc;
+ ASSERT_TRUE(SetUpMasterRcFileAlongsideBinary("startup --binarymasterfoo",
+ &binary_rc));
+ const std::string cmdline_rc_path =
+ blaze_util::JoinPath(workspace_, "mybazelrc");
+ ASSERT_TRUE(
+ blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
+ ASSERT_TRUE(
+ blaze_util::WriteFile("startup --userfoo", cmdline_rc_path, 0755));
+
+ const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
+ "--bazelrc=" + cmdline_rc_path,
+ "build"};
+ // Expect no error due to the incorrect options, as non of them should have
+ // been loaded.
+ std::string error;
+ EXPECT_EQ(blaze_exit_code::SUCCESS,
+ option_processor_->ParseOptions(args, workspace_, cwd_, &error));
+ ASSERT_EQ("", error);
+
+ // Check that the startup options' provenance message contains nothing
+ testing::internal::CaptureStderr();
+ option_processor_->PrintStartupOptionsProvenanceMessage();
+ const std::string& output = testing::internal::GetCapturedStderr();
+
+ EXPECT_EQ(output, "");
+}
+
TEST_F(ParseOptionsTest, CommandLineBazelrcHasUnknownOption) {
const std::string cmdline_rc_path =
blaze_util::JoinPath(workspace_, "mybazelrc");
diff --git a/src/test/cpp/startup_options_test.cc b/src/test/cpp/startup_options_test.cc
index ecc7ac1702..9a0b01c311 100644
--- a/src/test/cpp/startup_options_test.cc
+++ b/src/test/cpp/startup_options_test.cc
@@ -34,6 +34,7 @@ class FakeStartupOptions : public StartupOptions {
*is_processed = false;
return blaze_exit_code::SUCCESS;
}
+ void MaybeLogStartupOptionWarnings() const override {}
};
class StartupOptionsTest : public ::testing::Test {
diff --git a/src/test/cpp/test_util.cc b/src/test/cpp/test_util.cc
index b50f74d79c..f56fd8798b 100644
--- a/src/test/cpp/test_util.cc
+++ b/src/test/cpp/test_util.cc
@@ -50,4 +50,25 @@ void ExpectIsUnaryOption(const StartupOptions* options,
EXPECT_FALSE(options->IsNullary("--no" + flag_name));
}
+void ParseStartupOptionsAndExpectWarning(
+ StartupOptions* startup_options,
+ const std::vector<std::string>& options_to_parse,
+ const std::string& expected_warning) {
+ std::vector<RcStartupFlag> flags;
+ for (std::string option : options_to_parse) {
+ flags.push_back(RcStartupFlag("", option));
+ }
+
+ std::string error;
+ EXPECT_EQ(blaze_exit_code::SUCCESS,
+ startup_options->ProcessArgs(flags, &error));
+ ASSERT_EQ("", error);
+
+ testing::internal::CaptureStderr();
+ startup_options->MaybeLogStartupOptionWarnings();
+ const std::string& output = testing::internal::GetCapturedStderr();
+
+ EXPECT_EQ(expected_warning, output);
+}
+
} // namespace blaze
diff --git a/src/test/cpp/test_util.h b/src/test/cpp/test_util.h
index e571f6b134..6c23c2ee15 100644
--- a/src/test/cpp/test_util.h
+++ b/src/test/cpp/test_util.h
@@ -23,6 +23,10 @@ void ExpectIsNullaryOption(const StartupOptions* options,
const std::string& flag_name);
void ExpectIsUnaryOption(const StartupOptions* options,
const std::string& flag_name);
+void ParseStartupOptionsAndExpectWarning(
+ StartupOptions* startup_options,
+ const std::vector<std::string>& options_to_parse,
+ const std::string& expected_warning);
} // namespace blaze