blob: 33ae9c2e8f30d29755f54f87b177473d5d309be1 [file] [log] [blame]
ccalvarin5305a362018-04-20 13:56:55 -07001// 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
laszlocsomora300ae82019-08-07 06:28:59 -070019#include <memory>
20
ccalvarin5305a362018-04-20 13:56:55 -070021#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
26namespace blaze {
27
28class 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 Csomor0d107492019-03-13 09:04:27 -070037 old_test_tmpdir_ = GetPathEnv("TEST_TMPDIR");
ccalvarin5305a362018-04-20 13:56:55 -070038
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
59TEST_F(BazelStartupOptionsTest, ProductName) {
60 ASSERT_EQ("Bazel", startup_options_->product_name);
61}
62
63TEST_F(BazelStartupOptionsTest, JavaLoggingOptions) {
64 ASSERT_EQ("com.google.devtools.build.lib.util.SingleLineFormatter",
65 startup_options_->java_logging_formatter);
66}
67
68TEST_F(BazelStartupOptionsTest, EmptyFlagsAreInvalid) {
nharmatabe4b2dc2020-03-14 00:52:56 -070069 {
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
ccalvarin5305a362018-04-20 13:56:55 -070084 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.
91TEST_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();
nharmatabe4b2dc2020-03-14 00:52:56 -070096 ExpectValidNullaryOption(options, "batch");
97 ExpectValidNullaryOption(options, "batch_cpu_scheduling");
98 ExpectValidNullaryOption(options, "block_for_lock");
99 ExpectValidNullaryOption(options, "client_debug");
nharmatabe4b2dc2020-03-14 00:52:56 -0700100 ExpectValidNullaryOption(options, "fatal_event_bus_exceptions");
101 ExpectValidNullaryOption(options, "home_rc");
102 ExpectValidNullaryOption(options, "host_jvm_debug");
Austin Schuh07400c02020-12-14 10:12:31 -0800103 ExpectValidNullaryOption(options, "autodetect_server_javabase");
nharmatabe4b2dc2020-03-14 00:52:56 -0700104 ExpectValidNullaryOption(options, "ignore_all_rc_files");
nharmatabe4b2dc2020-03-14 00:52:56 -0700105 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");
ccalvarin5305a362018-04-20 13:56:55 -0700110 ExpectIsUnaryOption(options, "bazelrc");
111 ExpectIsUnaryOption(options, "command_port");
112 ExpectIsUnaryOption(options, "connect_timeout_secs");
ccalvarin78142a62018-08-01 19:25:22 -0700113 ExpectIsUnaryOption(options, "digest_function");
ccalvarin5305a362018-04-20 13:56:55 -0700114 ExpectIsUnaryOption(options, "host_jvm_args");
115 ExpectIsUnaryOption(options, "host_jvm_profile");
jcater6bd0aa42019-07-08 09:10:05 -0700116 ExpectIsUnaryOption(options, "install_base");
ccalvarin5305a362018-04-20 13:56:55 -0700117 ExpectIsUnaryOption(options, "invocation_policy");
118 ExpectIsUnaryOption(options, "io_nice_level");
Ryan Beasleyf1b3a6b2020-10-23 13:00:52 -0700119 ExpectIsUnaryOption(options, "local_startup_timeout_secs");
jmmv3cb1aef2019-03-14 07:38:00 -0700120 ExpectIsUnaryOption(options, "macos_qos_class");
ccalvarin5305a362018-04-20 13:56:55 -0700121 ExpectIsUnaryOption(options, "max_idle_secs");
122 ExpectIsUnaryOption(options, "output_base");
123 ExpectIsUnaryOption(options, "output_user_root");
jcater6bd0aa42019-07-08 09:10:05 -0700124 ExpectIsUnaryOption(options, "server_javabase");
ccalvarin5305a362018-04-20 13:56:55 -0700125}
126
127TEST_F(BazelStartupOptionsTest, BlazercFlagsAreNotAccepted) {
nharmatabe4b2dc2020-03-14 00:52:56 -0700128 {
129 bool result;
130 std::string error;
131 EXPECT_TRUE(startup_options_->MaybeCheckValidNullary("--master_blazerc",
132 &result, &error));
133 EXPECT_FALSE(result);
134 }
135
ccalvarin5305a362018-04-20 13:56:55 -0700136 EXPECT_FALSE(startup_options_->IsUnary("--master_blazerc"));
nharmatabe4b2dc2020-03-14 00:52:56 -0700137
138 {
139 bool result;
140 std::string error;
141 EXPECT_TRUE(
142 startup_options_->MaybeCheckValidNullary("--blazerc", &result, &error));
143 EXPECT_FALSE(result);
144 }
145
ccalvarin5305a362018-04-20 13:56:55 -0700146 EXPECT_FALSE(startup_options_->IsUnary("--blazerc"));
147}
148
ccalvarin90e116c2018-05-15 16:41:19 -0700149TEST_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
156TEST_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
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700163TEST_F(BazelStartupOptionsTest, IgnoredWorkspaceRcFlagWarns) {
ccalvarin90e116c2018-05-15 16:41:19 -0700164 ParseStartupOptionsAndExpectWarning(
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700165 startup_options_.get(), {"--workspace_rc", "--ignore_all_rc_files"},
166 "WARNING: Explicit value of --workspace_rc is ignored, "
ccalvarin90e116c2018-05-15 16:41:19 -0700167 "since --ignore_all_rc_files is on.\n");
168}
169
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700170TEST_F(BazelStartupOptionsTest, IgnoredWorkspaceRcFlagWarnsAfterIgnore) {
ccalvarin90e116c2018-05-15 16:41:19 -0700171 ParseStartupOptionsAndExpectWarning(
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700172 startup_options_.get(), {"--ignore_all_rc_files", "--workspace_rc"},
173 "WARNING: Explicit value of --workspace_rc is ignored, "
ccalvarin90e116c2018-05-15 16:41:19 -0700174 "since --ignore_all_rc_files is on.\n");
175}
176
177TEST_F(BazelStartupOptionsTest, MultipleIgnoredRcFlagsWarnOnceEach) {
178 ParseStartupOptionsAndExpectWarning(
179 startup_options_.get(),
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700180 {"--workspace_rc", "--bazelrc=somefile", "--ignore_all_rc_files",
181 "--bazelrc=thefinalfile", "--workspace_rc"},
ccalvarin90e116c2018-05-15 16:41:19 -0700182 "WARNING: Value of --bazelrc is ignored, "
183 "since --ignore_all_rc_files is on.\n"
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700184 "WARNING: Explicit value of --workspace_rc is ignored, "
ccalvarin90e116c2018-05-15 16:41:19 -0700185 "since --ignore_all_rc_files is on.\n");
186}
187
188TEST_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(
Googler96c8a902022-03-14 08:05:57 -0700192 startup_options_.get(), {"--ignore_all_rc_files"},
ccalvarin90e116c2018-05-15 16:41:19 -0700193 "");
194}
195
196TEST_F(BazelStartupOptionsTest, IgnoreOptionDoesNotWarnOnItsOwn) {
197 ParseStartupOptionsAndExpectWarning(startup_options_.get(),
198 {"--ignore_all_rc_files"}, "");
199}
200
201TEST_F(BazelStartupOptionsTest, NonIgnoredOptionDoesNotWarn) {
202 ParseStartupOptionsAndExpectWarning(startup_options_.get(),
203 {"--bazelrc=somefile"}, "");
204}
205
206TEST_F(BazelStartupOptionsTest, FinalValueOfIgnoreIsUsedForWarning) {
207 ParseStartupOptionsAndExpectWarning(
208 startup_options_.get(),
Googler96c8a902022-03-14 08:05:57 -0700209 {"--ignore_all_rc_files", "--noignore_all_rc_files"},
ccalvarin90e116c2018-05-15 16:41:19 -0700210 "");
211}
212
ccalvarin5305a362018-04-20 13:56:55 -0700213} // namespace blaze