ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "src/main/cpp/bazel_startup_options.h" |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | |
laszlocsomor | a300ae8 | 2019-08-07 06:28:59 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 21 | #include "src/main/cpp/blaze_util_platform.h" |
| 22 | #include "src/main/cpp/workspace_layout.h" |
| 23 | #include "src/test/cpp/test_util.h" |
| 24 | #include "googletest/include/gtest/gtest.h" |
| 25 | |
| 26 | namespace blaze { |
| 27 | |
| 28 | class BazelStartupOptionsTest : public ::testing::Test { |
| 29 | protected: |
| 30 | BazelStartupOptionsTest() : workspace_layout_(new WorkspaceLayout()) {} |
| 31 | ~BazelStartupOptionsTest() = default; |
| 32 | |
| 33 | void SetUp() override { |
| 34 | // This knowingly ignores the possibility of these environment variables |
| 35 | // being unset because we expect our test runner to set them in all cases. |
| 36 | // Otherwise, we'll crash here, but this keeps our code simpler. |
Laszlo Csomor | 0d10749 | 2019-03-13 09:04:27 -0700 | [diff] [blame] | 37 | old_test_tmpdir_ = GetPathEnv("TEST_TMPDIR"); |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 38 | |
| 39 | ReinitStartupOptions(); |
| 40 | } |
| 41 | |
| 42 | void TearDown() override { SetEnv("TEST_TMPDIR", old_test_tmpdir_); } |
| 43 | |
| 44 | // Recreates startup_options_ after changes to the environment. |
| 45 | void ReinitStartupOptions() { |
| 46 | startup_options_.reset(new BazelStartupOptions(workspace_layout_.get())); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | std::unique_ptr<WorkspaceLayout> workspace_layout_; |
| 51 | |
| 52 | protected: |
| 53 | std::unique_ptr<BazelStartupOptions> startup_options_; |
| 54 | |
| 55 | private: |
| 56 | std::string old_test_tmpdir_; |
| 57 | }; |
| 58 | |
| 59 | TEST_F(BazelStartupOptionsTest, ProductName) { |
| 60 | ASSERT_EQ("Bazel", startup_options_->product_name); |
| 61 | } |
| 62 | |
| 63 | TEST_F(BazelStartupOptionsTest, JavaLoggingOptions) { |
| 64 | ASSERT_EQ("com.google.devtools.build.lib.util.SingleLineFormatter", |
| 65 | startup_options_->java_logging_formatter); |
| 66 | } |
| 67 | |
| 68 | TEST_F(BazelStartupOptionsTest, EmptyFlagsAreInvalid) { |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 69 | { |
| 70 | bool result; |
| 71 | std::string error; |
| 72 | EXPECT_TRUE(startup_options_->MaybeCheckValidNullary("", &result, &error)); |
| 73 | EXPECT_FALSE(result); |
| 74 | } |
| 75 | |
| 76 | { |
| 77 | bool result; |
| 78 | std::string error; |
| 79 | EXPECT_TRUE( |
| 80 | startup_options_->MaybeCheckValidNullary("--", &result, &error)); |
| 81 | EXPECT_FALSE(result); |
| 82 | } |
| 83 | |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 84 | EXPECT_FALSE(startup_options_->IsUnary("")); |
| 85 | EXPECT_FALSE(startup_options_->IsUnary("--")); |
| 86 | } |
| 87 | |
| 88 | // TODO(#4502 related cleanup) This test serves as a catalog of the valid |
| 89 | // options - make this test check that the list is complete, that no options are |
| 90 | // missing. |
| 91 | TEST_F(BazelStartupOptionsTest, ValidStartupFlags) { |
| 92 | // IMPORTANT: Before modifying this test, please contact a Bazel core team |
| 93 | // member that knows the Google-internal procedure for adding/deprecating |
| 94 | // startup flags. |
| 95 | const StartupOptions* options = startup_options_.get(); |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 96 | ExpectValidNullaryOption(options, "batch"); |
| 97 | ExpectValidNullaryOption(options, "batch_cpu_scheduling"); |
| 98 | ExpectValidNullaryOption(options, "block_for_lock"); |
| 99 | ExpectValidNullaryOption(options, "client_debug"); |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 100 | ExpectValidNullaryOption(options, "fatal_event_bus_exceptions"); |
| 101 | ExpectValidNullaryOption(options, "home_rc"); |
| 102 | ExpectValidNullaryOption(options, "host_jvm_debug"); |
Austin Schuh | 07400c0 | 2020-12-14 10:12:31 -0800 | [diff] [blame] | 103 | ExpectValidNullaryOption(options, "autodetect_server_javabase"); |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 104 | ExpectValidNullaryOption(options, "ignore_all_rc_files"); |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 105 | ExpectValidNullaryOption(options, "shutdown_on_low_sys_mem"); |
| 106 | ExpectValidNullaryOption(options, "system_rc"); |
| 107 | ExpectValidNullaryOption(options, "watchfs"); |
| 108 | ExpectValidNullaryOption(options, "workspace_rc"); |
| 109 | ExpectValidNullaryOption(options, "write_command_log"); |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 110 | ExpectIsUnaryOption(options, "bazelrc"); |
| 111 | ExpectIsUnaryOption(options, "command_port"); |
| 112 | ExpectIsUnaryOption(options, "connect_timeout_secs"); |
ccalvarin | 78142a6 | 2018-08-01 19:25:22 -0700 | [diff] [blame] | 113 | ExpectIsUnaryOption(options, "digest_function"); |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 114 | ExpectIsUnaryOption(options, "host_jvm_args"); |
| 115 | ExpectIsUnaryOption(options, "host_jvm_profile"); |
jcater | 6bd0aa4 | 2019-07-08 09:10:05 -0700 | [diff] [blame] | 116 | ExpectIsUnaryOption(options, "install_base"); |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 117 | ExpectIsUnaryOption(options, "invocation_policy"); |
| 118 | ExpectIsUnaryOption(options, "io_nice_level"); |
Ryan Beasley | f1b3a6b | 2020-10-23 13:00:52 -0700 | [diff] [blame] | 119 | ExpectIsUnaryOption(options, "local_startup_timeout_secs"); |
jmmv | 3cb1aef | 2019-03-14 07:38:00 -0700 | [diff] [blame] | 120 | ExpectIsUnaryOption(options, "macos_qos_class"); |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 121 | ExpectIsUnaryOption(options, "max_idle_secs"); |
| 122 | ExpectIsUnaryOption(options, "output_base"); |
| 123 | ExpectIsUnaryOption(options, "output_user_root"); |
jcater | 6bd0aa4 | 2019-07-08 09:10:05 -0700 | [diff] [blame] | 124 | ExpectIsUnaryOption(options, "server_javabase"); |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | TEST_F(BazelStartupOptionsTest, BlazercFlagsAreNotAccepted) { |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 128 | { |
| 129 | bool result; |
| 130 | std::string error; |
| 131 | EXPECT_TRUE(startup_options_->MaybeCheckValidNullary("--master_blazerc", |
| 132 | &result, &error)); |
| 133 | EXPECT_FALSE(result); |
| 134 | } |
| 135 | |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 136 | EXPECT_FALSE(startup_options_->IsUnary("--master_blazerc")); |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 137 | |
| 138 | { |
| 139 | bool result; |
| 140 | std::string error; |
| 141 | EXPECT_TRUE( |
| 142 | startup_options_->MaybeCheckValidNullary("--blazerc", &result, &error)); |
| 143 | EXPECT_FALSE(result); |
| 144 | } |
| 145 | |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 146 | EXPECT_FALSE(startup_options_->IsUnary("--blazerc")); |
| 147 | } |
| 148 | |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 149 | TEST_F(BazelStartupOptionsTest, IgnoredBazelrcFlagWarns) { |
| 150 | ParseStartupOptionsAndExpectWarning( |
| 151 | startup_options_.get(), {"--bazelrc=somefile", "--ignore_all_rc_files"}, |
| 152 | "WARNING: Value of --bazelrc is ignored, since --ignore_all_rc_files is " |
| 153 | "on.\n"); |
| 154 | } |
| 155 | |
| 156 | TEST_F(BazelStartupOptionsTest, IgnoredBazelrcFlagWarnsWhenAfterIgnore) { |
| 157 | ParseStartupOptionsAndExpectWarning( |
| 158 | startup_options_.get(), {"--ignore_all_rc_files", "--bazelrc=somefile"}, |
| 159 | "WARNING: Value of --bazelrc is ignored, since --ignore_all_rc_files is " |
| 160 | "on.\n"); |
| 161 | } |
| 162 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 163 | TEST_F(BazelStartupOptionsTest, IgnoredWorkspaceRcFlagWarns) { |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 164 | ParseStartupOptionsAndExpectWarning( |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 165 | startup_options_.get(), {"--workspace_rc", "--ignore_all_rc_files"}, |
| 166 | "WARNING: Explicit value of --workspace_rc is ignored, " |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 167 | "since --ignore_all_rc_files is on.\n"); |
| 168 | } |
| 169 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 170 | TEST_F(BazelStartupOptionsTest, IgnoredWorkspaceRcFlagWarnsAfterIgnore) { |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 171 | ParseStartupOptionsAndExpectWarning( |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 172 | startup_options_.get(), {"--ignore_all_rc_files", "--workspace_rc"}, |
| 173 | "WARNING: Explicit value of --workspace_rc is ignored, " |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 174 | "since --ignore_all_rc_files is on.\n"); |
| 175 | } |
| 176 | |
| 177 | TEST_F(BazelStartupOptionsTest, MultipleIgnoredRcFlagsWarnOnceEach) { |
| 178 | ParseStartupOptionsAndExpectWarning( |
| 179 | startup_options_.get(), |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 180 | {"--workspace_rc", "--bazelrc=somefile", "--ignore_all_rc_files", |
| 181 | "--bazelrc=thefinalfile", "--workspace_rc"}, |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 182 | "WARNING: Value of --bazelrc is ignored, " |
| 183 | "since --ignore_all_rc_files is on.\n" |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 184 | "WARNING: Explicit value of --workspace_rc is ignored, " |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 185 | "since --ignore_all_rc_files is on.\n"); |
| 186 | } |
| 187 | |
| 188 | TEST_F(BazelStartupOptionsTest, IgnoredNoMasterBazelrcDoesNotWarn) { |
| 189 | // Warning for nomaster would feel pretty spammy - it's redundant, but the |
| 190 | // behavior is as one would expect, so warning is unnecessary. |
| 191 | ParseStartupOptionsAndExpectWarning( |
Googler | 96c8a90 | 2022-03-14 08:05:57 -0700 | [diff] [blame] | 192 | startup_options_.get(), {"--ignore_all_rc_files"}, |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 193 | ""); |
| 194 | } |
| 195 | |
| 196 | TEST_F(BazelStartupOptionsTest, IgnoreOptionDoesNotWarnOnItsOwn) { |
| 197 | ParseStartupOptionsAndExpectWarning(startup_options_.get(), |
| 198 | {"--ignore_all_rc_files"}, ""); |
| 199 | } |
| 200 | |
| 201 | TEST_F(BazelStartupOptionsTest, NonIgnoredOptionDoesNotWarn) { |
| 202 | ParseStartupOptionsAndExpectWarning(startup_options_.get(), |
| 203 | {"--bazelrc=somefile"}, ""); |
| 204 | } |
| 205 | |
| 206 | TEST_F(BazelStartupOptionsTest, FinalValueOfIgnoreIsUsedForWarning) { |
| 207 | ParseStartupOptionsAndExpectWarning( |
| 208 | startup_options_.get(), |
Googler | 96c8a90 | 2022-03-14 08:05:57 -0700 | [diff] [blame] | 209 | {"--ignore_all_rc_files", "--noignore_all_rc_files"}, |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 210 | ""); |
| 211 | } |
| 212 | |
ccalvarin | 5305a36 | 2018-04-20 13:56:55 -0700 | [diff] [blame] | 213 | } // namespace blaze |