blob: ee8880bfab3acb293789900437b259dc3523c7ed [file] [log] [blame]
ccalvarin04808942018-05-01 15:30:28 -07001// Copyright 2017 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
philwo09b135d2020-11-25 06:07:26 -080015#if defined(_WIN32)
16#ifndef WIN32_LEAN_AND_MEAN
17#define WIN32_LEAN_AND_MEAN
18#endif
19#include <windows.h>
20#endif
21
laszlocsomora300ae82019-08-07 06:28:59 -070022#include "src/main/cpp/rc_file.h"
23
24#include <memory>
ccalvarin04808942018-05-01 15:30:28 -070025
26#include "src/main/cpp/bazel_startup_options.h"
27#include "src/main/cpp/blaze_util.h"
28#include "src/main/cpp/blaze_util_platform.h"
29#include "src/main/cpp/option_processor-internal.h"
laszlocsomora300ae82019-08-07 06:28:59 -070030#include "src/main/cpp/option_processor.h"
ccalvarin04808942018-05-01 15:30:28 -070031#include "src/main/cpp/util/file.h"
32#include "src/main/cpp/util/file_platform.h"
ccalvarinac69da02018-06-05 15:27:26 -070033#include "src/main/cpp/util/path.h"
ccalvarinbccf9c62018-06-20 15:05:23 -070034#include "src/main/cpp/util/path_platform.h"
ccalvarin04808942018-05-01 15:30:28 -070035#include "src/main/cpp/workspace_layout.h"
36#include "googlemock/include/gmock/gmock.h"
37#include "googletest/include/gtest/gtest.h"
38
39namespace blaze {
ccalvarin8ceaa652018-08-24 12:44:31 -070040using ::testing::ContainsRegex;
Googler1980fdb2020-09-01 13:49:34 -070041using ::testing::ElementsAre;
ccalvarin04808942018-05-01 15:30:28 -070042using ::testing::HasSubstr;
Googler1980fdb2020-09-01 13:49:34 -070043using ::testing::IsEmpty;
ccalvarin04808942018-05-01 15:30:28 -070044using ::testing::MatchesRegex;
Googler1980fdb2020-09-01 13:49:34 -070045using ::testing::Pointee;
ccalvarin04808942018-05-01 15:30:28 -070046
Loo Rong Jie4022bac2018-06-11 02:04:52 -070047#if defined(_WIN32) || defined(__CYGWIN__)
ccalvarin8ceaa652018-08-24 12:44:31 -070048constexpr const char* kNullDevice = "NUL";
ccalvarin04808942018-05-01 15:30:28 -070049#else // Assume POSIX if not Windows.
50constexpr const char* kNullDevice = "/dev/null";
51#endif
52
Googler1980fdb2020-09-01 13:49:34 -070053// Matches an RcFile's canonical source paths list.
54MATCHER_P(CanonicalSourcePathsAre, paths_matcher, "") {
55 return ExplainMatchResult(ElementsAre(paths_matcher),
56 arg.canonical_source_paths(), result_listener);
57}
58
ccalvarin04808942018-05-01 15:30:28 -070059class RcFileTest : public ::testing::Test {
60 protected:
61 RcFileTest()
Laszlo Csomor0d107492019-03-13 09:04:27 -070062 : workspace_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"),
63 "workspace")),
64 cwd_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "cwd")),
philwo09b135d2020-11-25 06:07:26 -080065 home_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "home")),
ccalvarin04808942018-05-01 15:30:28 -070066 binary_dir_(
Laszlo Csomor0d107492019-03-13 09:04:27 -070067 blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "bazeldir")),
ccalvarin04808942018-05-01 15:30:28 -070068 binary_path_(blaze_util::JoinPath(binary_dir_, "bazel")),
Yannic Bonenberger45d52002018-09-14 16:11:36 -070069 workspace_layout_(new WorkspaceLayout()) {}
ccalvarin04808942018-05-01 15:30:28 -070070
71 void SetUp() override {
72 ASSERT_TRUE(blaze_util::MakeDirectories(workspace_, 0755));
73 ASSERT_TRUE(blaze_util::MakeDirectories(cwd_, 0755));
ccalvarinbccf9c62018-06-20 15:05:23 -070074 ASSERT_TRUE(blaze_util::ChangeDirectory(cwd_));
philwo09b135d2020-11-25 06:07:26 -080075 ASSERT_TRUE(blaze_util::MakeDirectories(home_, 0755));
76#if defined(_WIN32)
77 ASSERT_NE(::SetEnvironmentVariable("HOME", home_.c_str()), 0);
78#else
79 ASSERT_EQ(setenv("HOME", home_.c_str(), 1), 0);
80#endif
ccalvarinbccf9c62018-06-20 15:05:23 -070081#if defined(_WIN32) || defined(__CYGWIN__)
82 // GetCwd returns a short path on Windows, so we store this expectation now
83 // to keep assertions sane in the tests.
84 std::string short_cwd;
85 std::string error;
86 ASSERT_TRUE(blaze_util::AsShortWindowsPath(cwd_, &short_cwd, &error))
87 << error;
88 cwd_ = short_cwd;
89
90#endif
91
ccalvarin04808942018-05-01 15:30:28 -070092 ASSERT_TRUE(blaze_util::MakeDirectories(binary_dir_, 0755));
93 option_processor_.reset(new OptionProcessor(
94 workspace_layout_.get(),
95 std::unique_ptr<StartupOptions>(
Yannic Bonenberger45d52002018-09-14 16:11:36 -070096 new BazelStartupOptions(workspace_layout_.get())),
97 "bazel.bazelrc"));
ccalvarin04808942018-05-01 15:30:28 -070098 }
99
100 void TearDown() override {
101 // TODO(bazel-team): The code below deletes all the files in the workspace
102 // and other rc-related directories, but it intentionally skips directories.
103 // As a consequence, there may be empty directories from test to test.
104 // Remove this once blaze_util::DeleteDirectories(path) exists.
105 std::vector<std::string> files;
106 blaze_util::GetAllFilesUnder(workspace_, &files);
107 for (const std::string& file : files) {
108 blaze_util::UnlinkPath(file);
109 }
110 blaze_util::GetAllFilesUnder(cwd_, &files);
111 for (const std::string& file : files) {
112 blaze_util::UnlinkPath(file);
113 }
philwo09b135d2020-11-25 06:07:26 -0800114 blaze_util::GetAllFilesUnder(home_, &files);
115 for (const std::string& file : files) {
116 blaze_util::UnlinkPath(file);
117 }
ccalvarin04808942018-05-01 15:30:28 -0700118 blaze_util::GetAllFilesUnder(binary_dir_, &files);
119 for (const std::string& file : files) {
120 blaze_util::UnlinkPath(file);
121 }
122 }
123
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700124 bool SetUpSystemRcFile(const std::string& contents,
ccalvarinbccf9c62018-06-20 15:05:23 -0700125 std::string* rcfile_path) const {
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700126 const std::string system_rc_path =
ccalvarinbccf9c62018-06-20 15:05:23 -0700127 blaze_util::ConvertPath(blaze_util::JoinPath(cwd_, "bazel.bazelrc"));
128
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700129 if (blaze_util::WriteFile(contents, system_rc_path, 0755)) {
ccalvarin8ceaa652018-08-24 12:44:31 -0700130 *rcfile_path = blaze_util::MakeCanonical(system_rc_path.c_str());
ccalvarinbccf9c62018-06-20 15:05:23 -0700131 return true;
132 }
133 return false;
134 }
135
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700136 bool SetUpWorkspaceRcFile(const std::string& contents,
137 std::string* rcfile_path) const {
138 const std::string workspace_user_rc_path =
139 blaze_util::JoinPath(workspace_, ".bazelrc");
140 if (blaze_util::WriteFile(contents, workspace_user_rc_path, 0755)) {
ccalvarin8ceaa652018-08-24 12:44:31 -0700141 *rcfile_path = blaze_util::MakeCanonical(workspace_user_rc_path.c_str());
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700142 return true;
143 }
144 return false;
145 }
146
philwo09b135d2020-11-25 06:07:26 -0800147 bool SetUpHomeRcFile(const std::string& contents,
148 std::string* rcfile_path) const {
149 const std::string home_rc_path = blaze_util::JoinPath(home_, ".bazelrc");
150 if (blaze_util::WriteFile(contents, home_rc_path, 0755)) {
151 *rcfile_path = blaze_util::MakeCanonical(home_rc_path.c_str());
152 return true;
153 }
154 return false;
155 }
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700156
157 bool SetUpLegacyMasterRcFileInWorkspace(const std::string& contents,
158 std::string* rcfile_path) const {
ccalvarin04808942018-05-01 15:30:28 -0700159 const std::string tools_dir = blaze_util::JoinPath(workspace_, "tools");
160 const std::string workspace_rc_path =
161 blaze_util::JoinPath(tools_dir, "bazel.rc");
162 if (blaze_util::MakeDirectories(tools_dir, 0755) &&
163 blaze_util::WriteFile(contents, workspace_rc_path, 0755)) {
164 *rcfile_path = workspace_rc_path;
165 return true;
166 }
167 return false;
168 }
169
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700170 bool SetUpLegacyMasterRcFileAlongsideBinary(const std::string& contents,
171 std::string* rcfile_path) const {
ccalvarin04808942018-05-01 15:30:28 -0700172 const std::string binary_rc_path =
173 blaze_util::JoinPath(binary_dir_, "bazel.bazelrc");
174 if (blaze_util::WriteFile(contents, binary_rc_path, 0755)) {
175 *rcfile_path = binary_rc_path;
176 return true;
177 }
178 return false;
179 }
180
ccalvarin04808942018-05-01 15:30:28 -0700181 const std::string workspace_;
ccalvarinbccf9c62018-06-20 15:05:23 -0700182 std::string cwd_;
philwo09b135d2020-11-25 06:07:26 -0800183 const std::string home_;
ccalvarin04808942018-05-01 15:30:28 -0700184 const std::string binary_dir_;
185 const std::string binary_path_;
186 const std::unique_ptr<WorkspaceLayout> workspace_layout_;
ccalvarinbccf9c62018-06-20 15:05:23 -0700187 const std::string old_system_bazelrc_path_;
ccalvarin04808942018-05-01 15:30:28 -0700188 std::unique_ptr<OptionProcessor> option_processor_;
189};
190
191using GetRcFileTest = RcFileTest;
192
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700193TEST_F(GetRcFileTest, GetRcFilesLoadsAllDefaultBazelrcs) {
194 std::string system_rc;
195 ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
ccalvarin04808942018-05-01 15:30:28 -0700196 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700197 ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
philwo09b135d2020-11-25 06:07:26 -0800198 std::string home_rc;
199 ASSERT_TRUE(SetUpHomeRcFile("", &home_rc));
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700200
201 const CommandLine cmd_line = CommandLine(binary_path_, {}, "build", {});
202 std::string error = "check that this string is not modified";
203 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
204 const blaze_exit_code::ExitCode exit_code =
205 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
206 &cmd_line, &parsed_rcs, &error);
207 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
208 EXPECT_EQ("check that this string is not modified", error);
209
210 // There should be 2 rc files: the system one and the workspace one. --bazelrc
211 // is not passed and therefore is not relevant.
Googler1980fdb2020-09-01 13:49:34 -0700212 EXPECT_THAT(parsed_rcs,
213 ElementsAre(Pointee(CanonicalSourcePathsAre(system_rc)),
philwo09b135d2020-11-25 06:07:26 -0800214 Pointee(CanonicalSourcePathsAre(workspace_rc)),
215 Pointee(CanonicalSourcePathsAre(home_rc))));
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700216}
217
218TEST_F(GetRcFileTest, GetRcFilesRespectsNoSystemRc) {
219 std::string system_rc;
220 ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
221 std::string workspace_rc;
222 ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
philwo09b135d2020-11-25 06:07:26 -0800223 std::string home_rc;
224 ASSERT_TRUE(SetUpHomeRcFile("", &home_rc));
ccalvarin04808942018-05-01 15:30:28 -0700225
226 const CommandLine cmd_line =
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700227 CommandLine(binary_path_, {"--nosystem_rc"}, "build", {});
ccalvarin04808942018-05-01 15:30:28 -0700228 std::string error = "check that this string is not modified";
229 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
230 const blaze_exit_code::ExitCode exit_code =
231 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
232 &cmd_line, &parsed_rcs, &error);
233 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
234 EXPECT_EQ("check that this string is not modified", error);
235
Googler1980fdb2020-09-01 13:49:34 -0700236 EXPECT_THAT(parsed_rcs,
philwo09b135d2020-11-25 06:07:26 -0800237 ElementsAre(Pointee(CanonicalSourcePathsAre(workspace_rc)),
238 Pointee(CanonicalSourcePathsAre(home_rc))));
ccalvarin04808942018-05-01 15:30:28 -0700239}
240
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700241TEST_F(GetRcFileTest, GetRcFilesRespectsNoWorkspaceRc) {
242 std::string system_rc;
243 ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
ccalvarin04808942018-05-01 15:30:28 -0700244 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700245 ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
philwo09b135d2020-11-25 06:07:26 -0800246 std::string home_rc;
247 ASSERT_TRUE(SetUpHomeRcFile("", &home_rc));
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700248
249 const CommandLine cmd_line =
250 CommandLine(binary_path_, {"--noworkspace_rc"}, "build", {});
251 std::string error = "check that this string is not modified";
252 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
253 const blaze_exit_code::ExitCode exit_code =
254 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
255 &cmd_line, &parsed_rcs, &error);
256 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
257 EXPECT_EQ("check that this string is not modified", error);
258
Googler1980fdb2020-09-01 13:49:34 -0700259 EXPECT_THAT(parsed_rcs,
philwo09b135d2020-11-25 06:07:26 -0800260 ElementsAre(Pointee(CanonicalSourcePathsAre(system_rc)),
261 Pointee(CanonicalSourcePathsAre(home_rc))));
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700262}
263
philwo09b135d2020-11-25 06:07:26 -0800264TEST_F(GetRcFileTest,
265 GetRcFilesRespectsNoWorkspaceRcAndNoSystemAndNoHomeRcCombined) {
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700266 std::string system_rc;
267 ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
268 std::string workspace_rc;
269 ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
philwo09b135d2020-11-25 06:07:26 -0800270 std::string home_rc;
271 ASSERT_TRUE(SetUpHomeRcFile("", &home_rc));
ccalvarin04808942018-05-01 15:30:28 -0700272
273 const CommandLine cmd_line = CommandLine(
philwo09b135d2020-11-25 06:07:26 -0800274 binary_path_, {"--noworkspace_rc", "--nosystem_rc", "--nohome_rc"},
275 "build", {});
ccalvarin04808942018-05-01 15:30:28 -0700276 std::string error = "check that this string is not modified";
277 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
278 const blaze_exit_code::ExitCode exit_code =
279 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
280 &cmd_line, &parsed_rcs, &error);
281 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
282 EXPECT_EQ("check that this string is not modified", error);
283
Googler1980fdb2020-09-01 13:49:34 -0700284 EXPECT_THAT(parsed_rcs, IsEmpty());
ccalvarin04808942018-05-01 15:30:28 -0700285}
286
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700287TEST_F(GetRcFileTest, GetRcFilesWarnsAboutIgnoredMasterRcFiles) {
288 std::string workspace_rc;
289 ASSERT_TRUE(SetUpLegacyMasterRcFileInWorkspace("", &workspace_rc));
290 std::string binary_rc;
291 ASSERT_TRUE(SetUpLegacyMasterRcFileAlongsideBinary("", &binary_rc));
292
293 const CommandLine cmd_line = CommandLine(binary_path_, {}, "build", {});
294 std::string error = "check that this string is not modified";
295 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
296
297 testing::internal::CaptureStderr();
298 const blaze_exit_code::ExitCode exit_code =
299 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
300 &cmd_line, &parsed_rcs, &error);
ccalvarin375d5932018-08-23 14:25:49 -0700301 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700302
303 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
304 EXPECT_EQ("check that this string is not modified", error);
305
306 // Expect that GetRcFiles outputs a warning about these files that are not
307 // read as expected.
308 EXPECT_THAT(output,
309 HasSubstr("The following rc files are no longer being read"));
dslomova6c77582018-10-09 13:56:38 -0700310 EXPECT_THAT(output, HasSubstr(workspace_rc));
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700311 EXPECT_THAT(output, HasSubstr(binary_rc));
312}
313
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700314TEST_F(GetRcFileTest, GetRcFilesReadsCommandLineRc) {
ccalvarin04808942018-05-01 15:30:28 -0700315 const std::string cmdline_rc_path =
316 blaze_util::JoinPath(workspace_, "mybazelrc");
317 ASSERT_TRUE(
318 blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
319 ASSERT_TRUE(blaze_util::WriteFile("", cmdline_rc_path, 0755));
320
321 const CommandLine cmd_line = CommandLine(
Googler96c8a902022-03-14 08:05:57 -0700322 binary_path_, {"--bazelrc=" + cmdline_rc_path},
ccalvarin04808942018-05-01 15:30:28 -0700323 "build", {});
324 std::string error = "check that this string is not modified";
325 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
326 const blaze_exit_code::ExitCode exit_code =
327 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
328 &cmd_line, &parsed_rcs, &error);
329 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
330 EXPECT_EQ("check that this string is not modified", error);
331
332 // Because of the variety of path representations in windows, this
333 // equality test does not attempt to check the entire path.
334 ASSERT_EQ(1, parsed_rcs.size());
Googler1980fdb2020-09-01 13:49:34 -0700335 ASSERT_EQ(1, parsed_rcs[0]->canonical_source_paths().size());
336 EXPECT_THAT(parsed_rcs[0]->canonical_source_paths()[0],
Laszlo Csomor9b83bd72018-12-17 08:42:44 -0800337 HasSubstr("mybazelrc"));
ccalvarin04808942018-05-01 15:30:28 -0700338}
339
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700340TEST_F(GetRcFileTest, GetRcFilesAcceptsNullCommandLineRc) {
ccalvarin04808942018-05-01 15:30:28 -0700341 const CommandLine cmd_line =
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700342 CommandLine(binary_path_,
343 {"--nosystem_rc", "--noworkspace_rc", "--nohome_rc",
344 "--bazelrc=/dev/null"},
345 "build", {});
ccalvarin04808942018-05-01 15:30:28 -0700346 std::string error = "check that this string is not modified";
347 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
348 const blaze_exit_code::ExitCode exit_code =
349 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
350 &cmd_line, &parsed_rcs, &error);
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700351 // /dev/null is not an error
ccalvarin04808942018-05-01 15:30:28 -0700352 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
353 EXPECT_EQ("check that this string is not modified", error);
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700354 // but it does technically count as a file
Googler1980fdb2020-09-01 13:49:34 -0700355 EXPECT_THAT(parsed_rcs,
356 ElementsAre(Pointee(CanonicalSourcePathsAre(kNullDevice))));
ccalvarin04808942018-05-01 15:30:28 -0700357}
358
ccalvarinf03acca2018-09-06 14:25:27 -0700359class ParseOptionsTest : public RcFileTest {
360 protected:
361 void ParseOptionsAndCheckOutput(
362 const std::vector<std::string>& args,
363 const blaze_exit_code::ExitCode expected_exit_code,
364 const std::string& expected_error_regex,
365 const std::string& expected_output_regex) {
366 std::string error;
367 testing::internal::CaptureStderr();
368 const blaze_exit_code::ExitCode exit_code =
369 option_processor_->ParseOptions(args, workspace_, cwd_, &error);
370 const std::string output = testing::internal::GetCapturedStderr();
371
372 ASSERT_EQ(expected_exit_code, exit_code) << error;
373 ASSERT_THAT(error, ContainsRegex(expected_error_regex));
374 ASSERT_THAT(output, ContainsRegex(expected_output_regex));
375 }
376};
ccalvarin04808942018-05-01 15:30:28 -0700377
ccalvarin90e116c2018-05-15 16:41:19 -0700378TEST_F(ParseOptionsTest, IgnoreAllRcFilesIgnoresAllMasterAndUserRcFiles) {
379 // Put fake options in different expected rc files, to check that none of them
380 // are read.
ccalvarind96f1572018-08-22 11:18:53 -0700381 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700382 ASSERT_TRUE(SetUpWorkspaceRcFile("startup --workspacefoo", &workspace_rc));
383 std::string system_rc;
384 ASSERT_TRUE(SetUpSystemRcFile("startup --systemfoo", &system_rc));
385 const std::string cmdline_rc_path =
386 blaze_util::JoinPath(workspace_, "mybazelrc");
387 ASSERT_TRUE(
388 blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
389 ASSERT_TRUE(blaze_util::WriteFile("startup --myfoo", cmdline_rc_path, 0755));
ccalvarin90e116c2018-05-15 16:41:19 -0700390
391 const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
392 "build"};
393 // Expect no error due to the incorrect options, as non of them should have
394 // been loaded.
395 std::string error;
396 EXPECT_EQ(blaze_exit_code::SUCCESS,
397 option_processor_->ParseOptions(args, workspace_, cwd_, &error));
398 ASSERT_EQ("", error);
399
400 // Check that the startup options' provenance message contains nothing
401 testing::internal::CaptureStderr();
402 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700403 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin90e116c2018-05-15 16:41:19 -0700404
405 EXPECT_EQ(output, "");
406}
407
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700408TEST_F(ParseOptionsTest, LaterIgnoreAllRcFilesValueWins) {
ccalvarin90e116c2018-05-15 16:41:19 -0700409 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700410 ASSERT_TRUE(SetUpWorkspaceRcFile("startup --workspacefoo", &workspace_rc));
ccalvarin90e116c2018-05-15 16:41:19 -0700411
412 const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
413 "--noignore_all_rc_files", "build"};
414 std::string error;
415 EXPECT_EQ(blaze_exit_code::BAD_ARGV,
416 option_processor_->ParseOptions(args, workspace_, cwd_, &error));
417 ASSERT_EQ(
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700418 "Unknown startup option: '--workspacefoo'.\n For more info, run "
ccalvarin90e116c2018-05-15 16:41:19 -0700419 "'bazel help startup_options'.",
420 error);
421
422 // Check that the startup options' provenance message contains the provenance
423 // of the incorrect option.
424 testing::internal::CaptureStderr();
425 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700426 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin90e116c2018-05-15 16:41:19 -0700427
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700428 EXPECT_THAT(
429 output,
430 MatchesRegex("INFO: Reading 'startup' options from .*workspace.*bazelrc: "
431 "--workspacefoo\n"));
ccalvarin90e116c2018-05-15 16:41:19 -0700432}
433
434TEST_F(ParseOptionsTest, IgnoreAllRcFilesIgnoresCommandLineRcFileToo) {
435 // Put fake options in different expected rc files, to check that none of them
436 // are read.
437 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700438 ASSERT_TRUE(SetUpWorkspaceRcFile("startup --workspacefoo", &workspace_rc));
439 std::string system_rc;
440 ASSERT_TRUE(SetUpSystemRcFile("startup --systemfoo", &system_rc));
441
ccalvarin90e116c2018-05-15 16:41:19 -0700442 const std::string cmdline_rc_path =
443 blaze_util::JoinPath(workspace_, "mybazelrc");
444 ASSERT_TRUE(
445 blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
446 ASSERT_TRUE(
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700447 blaze_util::WriteFile("startup --cmdlinefoo", cmdline_rc_path, 0755));
ccalvarin90e116c2018-05-15 16:41:19 -0700448
449 const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
450 "--bazelrc=" + cmdline_rc_path,
451 "build"};
452 // Expect no error due to the incorrect options, as non of them should have
453 // been loaded.
454 std::string error;
455 EXPECT_EQ(blaze_exit_code::SUCCESS,
456 option_processor_->ParseOptions(args, workspace_, cwd_, &error));
457 ASSERT_EQ("", error);
458
459 // Check that the startup options' provenance message contains nothing
460 testing::internal::CaptureStderr();
461 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700462 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin90e116c2018-05-15 16:41:19 -0700463
464 EXPECT_EQ(output, "");
465}
466
ccalvarin04808942018-05-01 15:30:28 -0700467TEST_F(ParseOptionsTest, CommandLineBazelrcHasUnknownOption) {
468 const std::string cmdline_rc_path =
469 blaze_util::JoinPath(workspace_, "mybazelrc");
470 ASSERT_TRUE(
471 blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
472 ASSERT_TRUE(blaze_util::WriteFile("startup --foo", cmdline_rc_path, 0755));
473
Googler96c8a902022-03-14 08:05:57 -0700474 const std::vector<std::string> args = {binary_path_,
ccalvarin04808942018-05-01 15:30:28 -0700475 "--bazelrc=" + cmdline_rc_path,
476 "build"};
477 const std::string expected_error =
478 "Unknown startup option: '--foo'.\n"
479 " For more info, run 'bazel help startup_options'.";
480 std::string error;
481 ASSERT_NE(blaze_exit_code::SUCCESS,
482 option_processor_->ParseOptions(args, workspace_, cwd_, &error))
483 << error;
484 ASSERT_EQ(expected_error, error);
485
486 // Check that the startup options' provenance message contains the correct
487 // information for the incorrect flag, and does not print the command-line
488 // provided startup flags.
489 testing::internal::CaptureStderr();
490 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700491 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700492
493 EXPECT_THAT(output,
494 MatchesRegex(
495 "INFO: Reading 'startup' options from .*mybazelrc: --foo\n"));
496}
497
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700498TEST_F(ParseOptionsTest, BazelrcHasUnknownOption) {
ccalvarin04808942018-05-01 15:30:28 -0700499 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700500 ASSERT_TRUE(SetUpWorkspaceRcFile("startup --foo", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700501
502 const std::vector<std::string> args = {binary_path_, "build"};
503
504 // Expect no error due to the incorrect --foo.
505 std::string error;
506 EXPECT_EQ(blaze_exit_code::BAD_ARGV,
507 option_processor_->ParseOptions(args, workspace_, cwd_, &error));
508 ASSERT_EQ(
509 "Unknown startup option: '--foo'.\n"
510 " For more info, run 'bazel help startup_options'.",
511 error);
512
513 // Check that the startup options' provenance message contains nothing for the
514 // master bazelrc.
515 testing::internal::CaptureStderr();
516 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700517 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700518
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700519 EXPECT_THAT(output, MatchesRegex("INFO: Reading 'startup' options from "
520 ".*workspace.*bazelrc: --foo\n"));
ccalvarin04808942018-05-01 15:30:28 -0700521}
522
523TEST_F(ParseOptionsTest,
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700524 IncorrectWorkspaceBazelrcIgnoredWhenNoWorkspaceRcIsPresent) {
ccalvarin04808942018-05-01 15:30:28 -0700525 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700526 ASSERT_TRUE(SetUpWorkspaceRcFile("startup --foo", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700527
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700528 const std::vector<std::string> args = {binary_path_, "--noworkspace_rc",
ccalvarin04808942018-05-01 15:30:28 -0700529 "build"};
530
531 // Expect no error due to the incorrect --foo.
532 std::string error;
533 EXPECT_EQ(blaze_exit_code::SUCCESS,
534 option_processor_->ParseOptions(args, workspace_, cwd_, &error));
535 ASSERT_EQ("", error);
536
537 // Check that the startup options' provenance message contains nothing for the
538 // master bazelrc.
539 testing::internal::CaptureStderr();
540 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700541 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700542
543 EXPECT_EQ(output, "");
544}
545
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700546TEST_F(ParseOptionsTest, PositiveOptionOverridesNegativeOption) {
547 std::string workspace_rc;
ccalvarin04808942018-05-01 15:30:28 -0700548 ASSERT_TRUE(
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700549 SetUpWorkspaceRcFile("startup --max_idle_secs=123", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700550
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700551 const std::vector<std::string> args = {"bazel", "--noworkspace_rc",
552 "--workspace_rc", "build"};
ccalvarin8ceaa652018-08-24 12:44:31 -0700553 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin2d3adfb2018-08-22 08:56:27 -0700554
ccalvarin04808942018-05-01 15:30:28 -0700555 EXPECT_EQ(123, option_processor_->GetParsedStartupOptions()->max_idle_secs);
556
557 // Check that the startup options' provenance message contains the correct
558 // information for the master bazelrc.
559 testing::internal::CaptureStderr();
560 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700561 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700562
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700563 EXPECT_THAT(output,
564 MatchesRegex("INFO: Reading 'startup' options from "
565 ".*workspace.*bazelrc: --max_idle_secs=123\n"));
ccalvarin04808942018-05-01 15:30:28 -0700566}
567
568TEST_F(ParseOptionsTest, MultipleStartupArgsInMasterBazelrcWorksCorrectly) {
569 // Add startup flags to the master bazelrc.
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700570 std::string workspace_rc;
571 ASSERT_TRUE(SetUpWorkspaceRcFile(
572 "startup --max_idle_secs=42\nstartup --io_nice_level=6", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700573
574 const std::vector<std::string> args = {binary_path_, "build"};
ccalvarin8ceaa652018-08-24 12:44:31 -0700575 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin04808942018-05-01 15:30:28 -0700576
577 EXPECT_EQ(42, option_processor_->GetParsedStartupOptions()->max_idle_secs);
578 EXPECT_EQ(6, option_processor_->GetParsedStartupOptions()->io_nice_level);
579
580 // Check that the startup options get grouped together properly in the output
581 // message.
582 testing::internal::CaptureStderr();
583 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700584 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700585
586 EXPECT_THAT(
587 output,
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700588 MatchesRegex("INFO: Reading 'startup' options from .*workspace.*bazelrc: "
ccalvarin04808942018-05-01 15:30:28 -0700589 "--max_idle_secs=42 --io_nice_level=6\n"));
590}
591
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700592TEST_F(ParseOptionsTest, CommandLineBazelrcHasPriorityOverDefaultBazelrc) {
593 // Add startup flags to the workspace bazelrc.
594 std::string workspace_rc;
595 ASSERT_TRUE(SetUpWorkspaceRcFile(
596 "startup --max_idle_secs=42\nstartup --io_nice_level=6", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700597
598 // Override one of the master bazelrc's flags in the commandline rc.
599 const std::string cmdline_rc_path =
600 blaze_util::JoinPath(workspace_, "mybazelrc");
601 ASSERT_TRUE(
602 blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
603 ASSERT_TRUE(blaze_util::WriteFile("startup --max_idle_secs=123",
604 cmdline_rc_path, 0755));
605
606 const std::vector<std::string> args = {
607 "bazel", "--bazelrc=" + cmdline_rc_path, "build"};
ccalvarin8ceaa652018-08-24 12:44:31 -0700608 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin04808942018-05-01 15:30:28 -0700609
610 EXPECT_EQ(123, option_processor_->GetParsedStartupOptions()->max_idle_secs);
611 EXPECT_EQ(6, option_processor_->GetParsedStartupOptions()->io_nice_level);
612
613 // Check that the options are reported in the correct order in the provenance
614 // message.
615 testing::internal::CaptureStderr();
616 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700617 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700618
619 EXPECT_THAT(
620 output,
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700621 MatchesRegex("INFO: Reading 'startup' options from .*workspace.*bazelrc: "
ccalvarin04808942018-05-01 15:30:28 -0700622 "--max_idle_secs=42 --io_nice_level=6\n"
623 "INFO: Reading 'startup' options from .*mybazelrc: "
624 "--max_idle_secs=123\n"));
625}
626
ccalvarinf03acca2018-09-06 14:25:27 -0700627class BlazercImportTest : public ParseOptionsTest {
628 protected:
629 void TestBazelRcImportsMaintainsFlagOrdering(const std::string& import_type) {
630 // Override one of the master bazelrc's flags in the custom bazelrc.
631 const std::string imported_rc_path =
632 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
633 ASSERT_TRUE(blaze_util::MakeDirectories(
634 blaze_util::Dirname(imported_rc_path), 0755));
635 ASSERT_TRUE(blaze_util::WriteFile(
636 "startup --max_idle_secs=123\n"
637 "startup --io_nice_level=4",
638 imported_rc_path, 0755));
ccalvarin04808942018-05-01 15:30:28 -0700639
ccalvarinf03acca2018-09-06 14:25:27 -0700640 // Add startup flags the imported bazelrc.
641 std::string workspace_rc;
642 ASSERT_TRUE(SetUpWorkspaceRcFile(
643 "startup --max_idle_secs=42\n" +
644 import_type + " " + imported_rc_path + "\n"
645 "startup --io_nice_level=6",
646 &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700647
ccalvarinf03acca2018-09-06 14:25:27 -0700648 const std::vector<std::string> args = {"bazel", "build"};
649 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin04808942018-05-01 15:30:28 -0700650
ccalvarinf03acca2018-09-06 14:25:27 -0700651 EXPECT_EQ(123, option_processor_->GetParsedStartupOptions()->max_idle_secs);
652 EXPECT_EQ(6, option_processor_->GetParsedStartupOptions()->io_nice_level);
ccalvarin04808942018-05-01 15:30:28 -0700653
ccalvarinf03acca2018-09-06 14:25:27 -0700654 // Check that the options are reported in the correct order in the
655 // provenance message, the imported file between the two master flags
656 testing::internal::CaptureStderr();
657 option_processor_->PrintStartupOptionsProvenanceMessage();
658 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700659
ccalvarinf03acca2018-09-06 14:25:27 -0700660 EXPECT_THAT(
661 output,
662 MatchesRegex(
663 "INFO: Reading 'startup' options from .*workspace.*bazelrc: "
664 "--max_idle_secs=42\n"
665 "INFO: Reading 'startup' options from .*myimportedbazelrc: "
666 "--max_idle_secs=123 --io_nice_level=4\n"
667 "INFO: Reading 'startup' options from .*workspace.*bazelrc: "
668 "--io_nice_level=6\n"));
669 }
ccalvarin04808942018-05-01 15:30:28 -0700670
ccalvarinf03acca2018-09-06 14:25:27 -0700671 void TestThatDoubleImportsCauseAWarning(const std::string& import_type) {
672 const std::string imported_rc_path =
673 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
674 ASSERT_TRUE(blaze_util::WriteFile("", imported_rc_path, 0755));
675
676 // Import the custom location twice.
677 std::string workspace_rc;
678 ASSERT_TRUE(SetUpWorkspaceRcFile(
679 import_type + " " + imported_rc_path + "\n" +
680 import_type + " " + imported_rc_path + "\n",
681 &workspace_rc));
682
683 const std::vector<std::string> args = {"bazel", "build"};
684 ParseOptionsAndCheckOutput(
685 args, blaze_exit_code::SUCCESS, "",
686 "WARNING: Duplicate rc file: .*myimportedbazelrc is imported multiple "
687 "times from .*workspace.*bazelrc\n");
688 }
689
690 void TestThatDoubleImportWithWorkspaceRelativeSyntaxCauseAWarning(
691 const std::string& import_type) {
692 const std::string imported_rc_path =
693 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
694 ASSERT_TRUE(blaze_util::WriteFile("", imported_rc_path, 0755));
695
696 // Import the custom location twice.
697 std::string workspace_rc;
698 ASSERT_TRUE(
699 SetUpWorkspaceRcFile(
700 import_type + " " + imported_rc_path + "\n" +
701 import_type + " %workspace%/myimportedbazelrc\n",
702 &workspace_rc));
703
704 const std::vector<std::string> args = {"bazel", "build"};
705 ParseOptionsAndCheckOutput(
706 args, blaze_exit_code::SUCCESS, "",
707 "WARNING: Duplicate rc file: .*myimportedbazelrc is imported multiple "
708 "times from .*workspace.*bazelrc\n");
709 }
710
711 void TestThatDoubleImportWithExcessPathSyntaxCauseAWarning(
712 const std::string& import_type) {
713 const std::string imported_rc_path =
714 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
715 ASSERT_TRUE(blaze_util::WriteFile("", imported_rc_path, 0755));
716
717 // Import the custom location twice.
718 std::string workspace_rc;
719 ASSERT_TRUE(
720 SetUpWorkspaceRcFile(
721 import_type + " " + imported_rc_path + "\n" +
722 import_type + " %workspace%///.//myimportedbazelrc\n",
723 &workspace_rc));
724
725 const std::vector<std::string> args = {"bazel", "build"};
726 ParseOptionsAndCheckOutput(
727 args, blaze_exit_code::SUCCESS, "",
728 "WARNING: Duplicate rc file: .*myimportedbazelrc is imported multiple "
729 "times from .*workspace.*bazelrc\n");
730 }
731
732 void TestThatDeepDoubleImportCausesAWarning(const std::string& import_type) {
733 const std::string dual_imported_rc_path =
734 blaze_util::JoinPath(workspace_, "dual_imported.bazelrc");
735 ASSERT_TRUE(blaze_util::WriteFile("", dual_imported_rc_path, 0755));
736
737 const std::string intermediate_import_1 =
738 blaze_util::JoinPath(workspace_, "intermediate_import_1");
739 ASSERT_TRUE(blaze_util::WriteFile(
740 import_type + " " + dual_imported_rc_path,
741 intermediate_import_1, 0755));
742
743 const std::string intermediate_import_2 =
744 blaze_util::JoinPath(workspace_, "intermediate_import_2");
745 ASSERT_TRUE(blaze_util::WriteFile(
746 import_type + " " + dual_imported_rc_path,
747 intermediate_import_2, 0755));
748
749 // Import the custom location twice.
750 std::string workspace_rc;
751 ASSERT_TRUE(SetUpWorkspaceRcFile(
752 import_type + " " + intermediate_import_1 + "\n" +
753 import_type + " " + intermediate_import_2 + "\n",
754 &workspace_rc));
755
756 const std::vector<std::string> args = {"bazel", "build"};
757 ParseOptionsAndCheckOutput(
758 args, blaze_exit_code::SUCCESS, "",
759 "WARNING: Duplicate rc file: .*dual_imported.bazelrc is imported "
760 "multiple times from .*workspace.*bazelrc\n");
761 }
762
763 void TestThatImportingAFileAndPassingItInCausesAWarning(
764 const std::string& import_type) {
765 const std::string imported_rc_path =
766 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
767 ASSERT_TRUE(blaze_util::WriteFile("", imported_rc_path, 0755));
768
769 // Import the custom location, and pass it in by flag.
770 std::string workspace_rc;
771 ASSERT_TRUE(
772 SetUpWorkspaceRcFile(
773 import_type + " " + imported_rc_path,
774 &workspace_rc));
775
776 const std::vector<std::string> args = {
777 "bazel", "--bazelrc=" + imported_rc_path, "build"};
778 ParseOptionsAndCheckOutput(
779 args, blaze_exit_code::SUCCESS, "",
780 "WARNING: Duplicate rc file: .*myimportedbazelrc is read multiple "
781 "times, "
jingwen68c57f02018-11-21 16:17:17 -0800782 "it is a standard rc file location but must have been unnecessarily "
ccalvarinf03acca2018-09-06 14:25:27 -0700783 "imported earlier.\n");
784 }
785};
786
787TEST_F(BlazercImportTest, BazelRcImportFailsForMissingFile) {
ccalvarin8ceaa652018-08-24 12:44:31 -0700788 const std::string missing_imported_rc_path =
789 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
790 std::string workspace_rc;
791 ASSERT_TRUE(SetUpWorkspaceRcFile("import " + missing_imported_rc_path,
792 &workspace_rc));
793
794 const std::vector<std::string> args = {"bazel", "build"};
795 ParseOptionsAndCheckOutput(
796 args, blaze_exit_code::INTERNAL_ERROR,
797 "Unexpected error reading .blazerc file '.*myimportedbazelrc'", "");
798}
799
ccalvarinf03acca2018-09-06 14:25:27 -0700800TEST_F(BlazercImportTest, BazelRcTryImportDoesNotFailForMissingFile) {
801 const std::string missing_imported_rc_path =
802 blaze_util::JoinPath(workspace_, "tryimported.bazelrc");
ccalvarin8ceaa652018-08-24 12:44:31 -0700803 std::string workspace_rc;
ccalvarinf03acca2018-09-06 14:25:27 -0700804 ASSERT_TRUE(SetUpWorkspaceRcFile("try-import " + missing_imported_rc_path,
805 &workspace_rc));
ccalvarin8ceaa652018-08-24 12:44:31 -0700806
807 const std::vector<std::string> args = {"bazel", "build"};
ccalvarinf03acca2018-09-06 14:25:27 -0700808 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin8ceaa652018-08-24 12:44:31 -0700809}
810
ccalvarinf03acca2018-09-06 14:25:27 -0700811// rc_file does not differentiate between non-existent and unreadable files. We
812// don't necessarily want try-import to ignore unreadable files, but this test
813// exists to make sure we don't change the behavior by accident. Any change that
814// makes existent but unreadable files a failure with try-import should inform
815// users.
816TEST_F(BlazercImportTest, BazelRcTryImportDoesNotFailForUnreadableFile) {
817 const std::string unreadable_rc_path =
818 blaze_util::JoinPath(workspace_, "tryimported.bazelrc");
819 ASSERT_TRUE(blaze_util::WriteFile("startup --max_idle_secs=123",
820 unreadable_rc_path, 222));
ccalvarin8ceaa652018-08-24 12:44:31 -0700821 std::string workspace_rc;
ccalvarinf03acca2018-09-06 14:25:27 -0700822 ASSERT_TRUE(
823 SetUpWorkspaceRcFile("try-import " + unreadable_rc_path, &workspace_rc));
ccalvarin8ceaa652018-08-24 12:44:31 -0700824
825 const std::vector<std::string> args = {"bazel", "build"};
ccalvarinf03acca2018-09-06 14:25:27 -0700826 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin8ceaa652018-08-24 12:44:31 -0700827}
828
Andrew Katson70073fd2021-07-28 13:09:21 -0700829#if defined(_WIN32)
830TEST_F(BlazercImportTest,
831 BazelRcTryImportDoesNotFailForInvalidPosixPathOnWindows) {
832 std::string workspace_rc;
833 ASSERT_TRUE(SetUpWorkspaceRcFile("try-import /mnt/shared/defaults.bazelrc",
834 &workspace_rc));
835
836 const std::vector<std::string> args = {"bazel", "build"};
837 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
838}
839#endif
ccalvarin8ceaa652018-08-24 12:44:31 -0700840
ccalvarinf03acca2018-09-06 14:25:27 -0700841TEST_F(BlazercImportTest, BazelRcImportsMaintainsFlagOrdering) {
842 TestBazelRcImportsMaintainsFlagOrdering("import");
843}
ccalvarin8ceaa652018-08-24 12:44:31 -0700844
ccalvarinf03acca2018-09-06 14:25:27 -0700845TEST_F(BlazercImportTest, BazelRcTryImportsMaintainsFlagOrdering) {
846 TestBazelRcImportsMaintainsFlagOrdering("try-import");
847}
848
849TEST_F(BlazercImportTest, DoubleImportsCauseAWarning) {
850 TestThatDoubleImportsCauseAWarning("import");
851}
852
853TEST_F(BlazercImportTest, DoubleTryImportsCauseAWarning) {
854 TestThatDoubleImportsCauseAWarning("try-import");
855}
856
857TEST_F(BlazercImportTest,
858 DoubleImportWithWorkspaceRelativeSyntaxCauseAWarning) {
859 TestThatDoubleImportWithWorkspaceRelativeSyntaxCauseAWarning("import");
860}
861
862TEST_F(BlazercImportTest,
863 DoubleTryImportWithWorkspaceRelativeSyntaxCauseAWarning) {
864 TestThatDoubleImportWithWorkspaceRelativeSyntaxCauseAWarning("try-import");
865}
866
867TEST_F(BlazercImportTest, DoubleImportWithExcessPathSyntaxCauseAWarning) {
868 TestThatDoubleImportWithExcessPathSyntaxCauseAWarning("import");
869}
870
871TEST_F(BlazercImportTest, DoubleTryImportWithExcessPathSyntaxCauseAWarning) {
872 TestThatDoubleImportWithExcessPathSyntaxCauseAWarning("try-import");
ccalvarin8ceaa652018-08-24 12:44:31 -0700873}
874
875// The following tests unix-path semantics.
876#if !defined(_WIN32) && !defined(__CYGWIN__)
ccalvarinf03acca2018-09-06 14:25:27 -0700877TEST_F(BlazercImportTest,
ccalvarin8ceaa652018-08-24 12:44:31 -0700878 DoubleImportWithEnclosingDirectorySyntaxCauseAWarning) {
879 const std::string imported_rc_path =
880 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
ccalvarin8ceaa652018-08-24 12:44:31 -0700881 ASSERT_TRUE(blaze_util::WriteFile("", imported_rc_path, 0755));
882
883 ASSERT_TRUE(blaze_util::MakeDirectories(
884 blaze_util::JoinPath(workspace_, "extra"), 0755));
885
886 // Import the custom location twice.
887 std::string workspace_rc;
888 ASSERT_TRUE(SetUpWorkspaceRcFile(
889 "import " + imported_rc_path + "\n"
890 "import %workspace%/extra/../myimportedbazelrc\n",
891 &workspace_rc));
892
893 const std::vector<std::string> args = {"bazel", "build"};
894 ParseOptionsAndCheckOutput(
895 args, blaze_exit_code::SUCCESS, "",
896 "WARNING: Duplicate rc file: .*myimportedbazelrc is imported multiple "
897 "times from .*workspace.*bazelrc\n");
898}
899#endif // !defined(_WIN32) && !defined(__CYGWIN__)
900
ccalvarinf03acca2018-09-06 14:25:27 -0700901TEST_F(BlazercImportTest, DeepDoubleImportCausesAWarning) {
902 TestThatDeepDoubleImportCausesAWarning("import");
ccalvarin8ceaa652018-08-24 12:44:31 -0700903}
904
ccalvarinf03acca2018-09-06 14:25:27 -0700905TEST_F(BlazercImportTest, DeepDoubleTryImportCausesAWarning) {
906 TestThatDeepDoubleImportCausesAWarning("try-import");
907}
ccalvarin8ceaa652018-08-24 12:44:31 -0700908
ccalvarinf03acca2018-09-06 14:25:27 -0700909TEST_F(BlazercImportTest, ImportingAFileAndPassingItInCausesAWarning) {
910 TestThatImportingAFileAndPassingItInCausesAWarning("import");
911}
ccalvarin8ceaa652018-08-24 12:44:31 -0700912
ccalvarinf03acca2018-09-06 14:25:27 -0700913TEST_F(BlazercImportTest, TryImportingAFileAndPassingItInCausesAWarning) {
914 TestThatImportingAFileAndPassingItInCausesAWarning("try-import");
ccalvarin8ceaa652018-08-24 12:44:31 -0700915}
916
917// TODO(b/112908763): Somehow, in the following tests, we end with a relative
918// path written in the import line on Windows. Figure out what's going on and
919// reinstate these tests
920#if !defined(_WIN32) && !defined(__CYGWIN__)
921TEST_F(ParseOptionsTest, ImportingAPreviouslyLoadedStandardRcCausesAWarning) {
922 std::string system_rc;
923 ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
924
925 // Import the system_rc extraneously.
926 std::string workspace_rc;
927 ASSERT_TRUE(SetUpWorkspaceRcFile("import " + system_rc, &workspace_rc));
928
929 const std::vector<std::string> args = {"bazel", "build"};
930 ParseOptionsAndCheckOutput(
931 args, blaze_exit_code::SUCCESS, "",
932 "WARNING: Duplicate rc file: .*bazel.bazelrc is read multiple "
933 "times, most recently imported from .*workspace.*bazelrc\n");
934}
935
936TEST_F(ParseOptionsTest, ImportingStandardRcBeforeItIsLoadedCausesAWarning) {
937 std::string workspace_rc;
938 ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
939
940 // Import the workspace_rc extraneously.
941 std::string system_rc;
942 ASSERT_TRUE(SetUpSystemRcFile("import " + workspace_rc, &system_rc));
943
944 const std::vector<std::string> args = {"bazel", "build"};
945 ParseOptionsAndCheckOutput(
946 args, blaze_exit_code::SUCCESS, "",
947 "WARNING: Duplicate rc file: .*workspace.*bazelrc is read multiple "
948 "times, it is a standard rc file location but must have been "
jingwen68c57f02018-11-21 16:17:17 -0800949 "unnecessarily imported earlier.\n");
ccalvarin8ceaa652018-08-24 12:44:31 -0700950}
951#endif // !defined(_WIN32) && !defined(__CYGWIN__)
952
ccalvarin04808942018-05-01 15:30:28 -0700953} // namespace blaze