blob: 675176831dbd54372436cdb9ff8d3f09abb6f95f [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
laszlocsomora300ae82019-08-07 06:28:59 -070015#include "src/main/cpp/rc_file.h"
16
17#include <memory>
ccalvarin04808942018-05-01 15:30:28 -070018
19#include "src/main/cpp/bazel_startup_options.h"
20#include "src/main/cpp/blaze_util.h"
21#include "src/main/cpp/blaze_util_platform.h"
22#include "src/main/cpp/option_processor-internal.h"
laszlocsomora300ae82019-08-07 06:28:59 -070023#include "src/main/cpp/option_processor.h"
ccalvarin04808942018-05-01 15:30:28 -070024#include "src/main/cpp/util/file.h"
25#include "src/main/cpp/util/file_platform.h"
ccalvarinac69da02018-06-05 15:27:26 -070026#include "src/main/cpp/util/path.h"
ccalvarinbccf9c62018-06-20 15:05:23 -070027#include "src/main/cpp/util/path_platform.h"
ccalvarin04808942018-05-01 15:30:28 -070028#include "src/main/cpp/workspace_layout.h"
29#include "googlemock/include/gmock/gmock.h"
30#include "googletest/include/gtest/gtest.h"
31
32namespace blaze {
ccalvarin8ceaa652018-08-24 12:44:31 -070033using ::testing::ContainsRegex;
ccalvarin04808942018-05-01 15:30:28 -070034using ::testing::HasSubstr;
35using ::testing::MatchesRegex;
36
Loo Rong Jie4022bac2018-06-11 02:04:52 -070037#if defined(_WIN32) || defined(__CYGWIN__)
ccalvarin8ceaa652018-08-24 12:44:31 -070038constexpr const char* kNullDevice = "NUL";
ccalvarin04808942018-05-01 15:30:28 -070039#else // Assume POSIX if not Windows.
40constexpr const char* kNullDevice = "/dev/null";
41#endif
42
43class RcFileTest : public ::testing::Test {
44 protected:
45 RcFileTest()
Laszlo Csomor0d107492019-03-13 09:04:27 -070046 : workspace_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"),
47 "workspace")),
48 cwd_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "cwd")),
ccalvarin04808942018-05-01 15:30:28 -070049 binary_dir_(
Laszlo Csomor0d107492019-03-13 09:04:27 -070050 blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "bazeldir")),
ccalvarin04808942018-05-01 15:30:28 -070051 binary_path_(blaze_util::JoinPath(binary_dir_, "bazel")),
Yannic Bonenberger45d52002018-09-14 16:11:36 -070052 workspace_layout_(new WorkspaceLayout()) {}
ccalvarin04808942018-05-01 15:30:28 -070053
54 void SetUp() override {
55 ASSERT_TRUE(blaze_util::MakeDirectories(workspace_, 0755));
56 ASSERT_TRUE(blaze_util::MakeDirectories(cwd_, 0755));
ccalvarinbccf9c62018-06-20 15:05:23 -070057 ASSERT_TRUE(blaze_util::ChangeDirectory(cwd_));
58#if defined(_WIN32) || defined(__CYGWIN__)
59 // GetCwd returns a short path on Windows, so we store this expectation now
60 // to keep assertions sane in the tests.
61 std::string short_cwd;
62 std::string error;
63 ASSERT_TRUE(blaze_util::AsShortWindowsPath(cwd_, &short_cwd, &error))
64 << error;
65 cwd_ = short_cwd;
66
67#endif
68
ccalvarin04808942018-05-01 15:30:28 -070069 ASSERT_TRUE(blaze_util::MakeDirectories(binary_dir_, 0755));
70 option_processor_.reset(new OptionProcessor(
71 workspace_layout_.get(),
72 std::unique_ptr<StartupOptions>(
Yannic Bonenberger45d52002018-09-14 16:11:36 -070073 new BazelStartupOptions(workspace_layout_.get())),
74 "bazel.bazelrc"));
ccalvarin04808942018-05-01 15:30:28 -070075 }
76
77 void TearDown() override {
78 // TODO(bazel-team): The code below deletes all the files in the workspace
79 // and other rc-related directories, but it intentionally skips directories.
80 // As a consequence, there may be empty directories from test to test.
81 // Remove this once blaze_util::DeleteDirectories(path) exists.
82 std::vector<std::string> files;
83 blaze_util::GetAllFilesUnder(workspace_, &files);
84 for (const std::string& file : files) {
85 blaze_util::UnlinkPath(file);
86 }
87 blaze_util::GetAllFilesUnder(cwd_, &files);
88 for (const std::string& file : files) {
89 blaze_util::UnlinkPath(file);
90 }
91 blaze_util::GetAllFilesUnder(binary_dir_, &files);
92 for (const std::string& file : files) {
93 blaze_util::UnlinkPath(file);
94 }
95 }
96
ccalvarin5e5ee0d2018-08-23 08:56:01 -070097 bool SetUpSystemRcFile(const std::string& contents,
ccalvarinbccf9c62018-06-20 15:05:23 -070098 std::string* rcfile_path) const {
ccalvarin5e5ee0d2018-08-23 08:56:01 -070099 const std::string system_rc_path =
ccalvarinbccf9c62018-06-20 15:05:23 -0700100 blaze_util::ConvertPath(blaze_util::JoinPath(cwd_, "bazel.bazelrc"));
101
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700102 if (blaze_util::WriteFile(contents, system_rc_path, 0755)) {
ccalvarin8ceaa652018-08-24 12:44:31 -0700103 *rcfile_path = blaze_util::MakeCanonical(system_rc_path.c_str());
ccalvarinbccf9c62018-06-20 15:05:23 -0700104 return true;
105 }
106 return false;
107 }
108
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700109 bool SetUpWorkspaceRcFile(const std::string& contents,
110 std::string* rcfile_path) const {
111 const std::string workspace_user_rc_path =
112 blaze_util::JoinPath(workspace_, ".bazelrc");
113 if (blaze_util::WriteFile(contents, workspace_user_rc_path, 0755)) {
ccalvarin8ceaa652018-08-24 12:44:31 -0700114 *rcfile_path = blaze_util::MakeCanonical(workspace_user_rc_path.c_str());
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700115 return true;
116 }
117 return false;
118 }
119
120 // TODO(b/36168162): Make it possible to configure the home directory so we
121 // can test --home_rc as well.
122
123 bool SetUpLegacyMasterRcFileInWorkspace(const std::string& contents,
124 std::string* rcfile_path) const {
ccalvarin04808942018-05-01 15:30:28 -0700125 const std::string tools_dir = blaze_util::JoinPath(workspace_, "tools");
126 const std::string workspace_rc_path =
127 blaze_util::JoinPath(tools_dir, "bazel.rc");
128 if (blaze_util::MakeDirectories(tools_dir, 0755) &&
129 blaze_util::WriteFile(contents, workspace_rc_path, 0755)) {
130 *rcfile_path = workspace_rc_path;
131 return true;
132 }
133 return false;
134 }
135
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700136 bool SetUpLegacyMasterRcFileAlongsideBinary(const std::string& contents,
137 std::string* rcfile_path) const {
ccalvarin04808942018-05-01 15:30:28 -0700138 const std::string binary_rc_path =
139 blaze_util::JoinPath(binary_dir_, "bazel.bazelrc");
140 if (blaze_util::WriteFile(contents, binary_rc_path, 0755)) {
141 *rcfile_path = binary_rc_path;
142 return true;
143 }
144 return false;
145 }
146
ccalvarin04808942018-05-01 15:30:28 -0700147 const std::string workspace_;
ccalvarinbccf9c62018-06-20 15:05:23 -0700148 std::string cwd_;
ccalvarin04808942018-05-01 15:30:28 -0700149 const std::string binary_dir_;
150 const std::string binary_path_;
151 const std::unique_ptr<WorkspaceLayout> workspace_layout_;
ccalvarinbccf9c62018-06-20 15:05:23 -0700152 const std::string old_system_bazelrc_path_;
ccalvarin04808942018-05-01 15:30:28 -0700153 std::unique_ptr<OptionProcessor> option_processor_;
154};
155
156using GetRcFileTest = RcFileTest;
157
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700158TEST_F(GetRcFileTest, GetRcFilesLoadsAllDefaultBazelrcs) {
159 std::string system_rc;
160 ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
ccalvarin04808942018-05-01 15:30:28 -0700161 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700162 ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
163
164 const CommandLine cmd_line = CommandLine(binary_path_, {}, "build", {});
165 std::string error = "check that this string is not modified";
166 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
167 const blaze_exit_code::ExitCode exit_code =
168 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
169 &cmd_line, &parsed_rcs, &error);
170 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
171 EXPECT_EQ("check that this string is not modified", error);
172
173 // There should be 2 rc files: the system one and the workspace one. --bazelrc
174 // is not passed and therefore is not relevant.
175 ASSERT_EQ(2, parsed_rcs.size());
176 const std::deque<std::string> expected_system_rc_que = {system_rc};
177 const std::deque<std::string> expected_workspace_rc_que = {workspace_rc};
Laszlo Csomor9b83bd72018-12-17 08:42:44 -0800178 EXPECT_EQ(expected_system_rc_que,
179 parsed_rcs[0].get()->canonical_source_paths());
180 EXPECT_EQ(expected_workspace_rc_que,
181 parsed_rcs[1].get()->canonical_source_paths());
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700182}
183
184TEST_F(GetRcFileTest, GetRcFilesRespectsNoSystemRc) {
185 std::string system_rc;
186 ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
187 std::string workspace_rc;
188 ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700189
190 const CommandLine cmd_line =
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700191 CommandLine(binary_path_, {"--nosystem_rc"}, "build", {});
ccalvarin04808942018-05-01 15:30:28 -0700192 std::string error = "check that this string is not modified";
193 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
194 const blaze_exit_code::ExitCode exit_code =
195 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
196 &cmd_line, &parsed_rcs, &error);
197 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
198 EXPECT_EQ("check that this string is not modified", error);
199
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700200 ASSERT_EQ(1, parsed_rcs.size());
ccalvarin04808942018-05-01 15:30:28 -0700201 const std::deque<std::string> expected_workspace_rc_que = {workspace_rc};
Laszlo Csomor9b83bd72018-12-17 08:42:44 -0800202 EXPECT_EQ(expected_workspace_rc_que,
203 parsed_rcs[0].get()->canonical_source_paths());
ccalvarin04808942018-05-01 15:30:28 -0700204}
205
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700206TEST_F(GetRcFileTest, GetRcFilesRespectsNoWorkspaceRc) {
207 std::string system_rc;
208 ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
ccalvarin04808942018-05-01 15:30:28 -0700209 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700210 ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
211
212 const CommandLine cmd_line =
213 CommandLine(binary_path_, {"--noworkspace_rc"}, "build", {});
214 std::string error = "check that this string is not modified";
215 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
216 const blaze_exit_code::ExitCode exit_code =
217 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
218 &cmd_line, &parsed_rcs, &error);
219 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
220 EXPECT_EQ("check that this string is not modified", error);
221
222 ASSERT_EQ(1, parsed_rcs.size());
223 const std::deque<std::string> expected_system_rc_que = {system_rc};
Laszlo Csomor9b83bd72018-12-17 08:42:44 -0800224 EXPECT_EQ(expected_system_rc_que,
225 parsed_rcs[0].get()->canonical_source_paths());
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700226}
227
228TEST_F(GetRcFileTest, GetRcFilesRespectsNoWorkspaceRcAndNoSystemCombined) {
229 std::string system_rc;
230 ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
231 std::string workspace_rc;
232 ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700233
234 const CommandLine cmd_line = CommandLine(
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700235 binary_path_, {"--noworkspace_rc", "--nosystem_rc"}, "build", {});
ccalvarin04808942018-05-01 15:30:28 -0700236 std::string error = "check that this string is not modified";
237 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
238 const blaze_exit_code::ExitCode exit_code =
239 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
240 &cmd_line, &parsed_rcs, &error);
241 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
242 EXPECT_EQ("check that this string is not modified", error);
243
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700244 ASSERT_EQ(0, parsed_rcs.size());
ccalvarin04808942018-05-01 15:30:28 -0700245}
246
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700247TEST_F(GetRcFileTest, GetRcFilesWarnsAboutIgnoredMasterRcFiles) {
248 std::string workspace_rc;
249 ASSERT_TRUE(SetUpLegacyMasterRcFileInWorkspace("", &workspace_rc));
250 std::string binary_rc;
251 ASSERT_TRUE(SetUpLegacyMasterRcFileAlongsideBinary("", &binary_rc));
252
253 const CommandLine cmd_line = CommandLine(binary_path_, {}, "build", {});
254 std::string error = "check that this string is not modified";
255 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
256
257 testing::internal::CaptureStderr();
258 const blaze_exit_code::ExitCode exit_code =
259 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
260 &cmd_line, &parsed_rcs, &error);
ccalvarin375d5932018-08-23 14:25:49 -0700261 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700262
263 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
264 EXPECT_EQ("check that this string is not modified", error);
265
266 // Expect that GetRcFiles outputs a warning about these files that are not
267 // read as expected.
268 EXPECT_THAT(output,
269 HasSubstr("The following rc files are no longer being read"));
dslomova6c77582018-10-09 13:56:38 -0700270 EXPECT_THAT(output, HasSubstr(workspace_rc));
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700271 EXPECT_THAT(output, HasSubstr(binary_rc));
272}
273
274TEST_F(
275 GetRcFileTest,
276 GetRcFilesDoesNotWarnAboutIgnoredMasterRcFilesWhenNoMasterBazelrcIsPassed) {
277 std::string workspace_rc;
278 ASSERT_TRUE(SetUpLegacyMasterRcFileInWorkspace("", &workspace_rc));
279 std::string binary_rc;
280 ASSERT_TRUE(SetUpLegacyMasterRcFileAlongsideBinary("", &binary_rc));
281
282 const CommandLine cmd_line =
283 CommandLine(binary_path_, {"--nomaster_bazelrc"}, "build", {});
284 std::string error = "check that this string is not modified";
285 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
286
287 testing::internal::CaptureStderr();
288 const blaze_exit_code::ExitCode exit_code =
289 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
290 &cmd_line, &parsed_rcs, &error);
ccalvarin375d5932018-08-23 14:25:49 -0700291 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700292
293 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
294 EXPECT_EQ("check that this string is not modified", error);
295
296 // Expect that nothing is logged to stderr about ignored rc files when these
297 // files are disabled.
298 EXPECT_THAT(
299 output,
300 Not(HasSubstr("The following rc files are no longer being read")));
301 EXPECT_THAT(output, Not(HasSubstr(workspace_rc)));
302 EXPECT_THAT(output, Not(HasSubstr(binary_rc)));
303}
304
305TEST_F(GetRcFileTest, GetRcFilesReadsCommandLineRc) {
ccalvarin04808942018-05-01 15:30:28 -0700306 const std::string cmdline_rc_path =
307 blaze_util::JoinPath(workspace_, "mybazelrc");
308 ASSERT_TRUE(
309 blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
310 ASSERT_TRUE(blaze_util::WriteFile("", cmdline_rc_path, 0755));
311
312 const CommandLine cmd_line = CommandLine(
313 binary_path_, {"--nomaster_bazelrc", "--bazelrc=" + cmdline_rc_path},
314 "build", {});
315 std::string error = "check that this string is not modified";
316 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
317 const blaze_exit_code::ExitCode exit_code =
318 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
319 &cmd_line, &parsed_rcs, &error);
320 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
321 EXPECT_EQ("check that this string is not modified", error);
322
323 // Because of the variety of path representations in windows, this
324 // equality test does not attempt to check the entire path.
325 ASSERT_EQ(1, parsed_rcs.size());
Laszlo Csomor9b83bd72018-12-17 08:42:44 -0800326 ASSERT_EQ(1, parsed_rcs[0].get()->canonical_source_paths().size());
327 EXPECT_THAT(parsed_rcs[0].get()->canonical_source_paths().front(),
328 HasSubstr("mybazelrc"));
ccalvarin04808942018-05-01 15:30:28 -0700329}
330
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700331TEST_F(GetRcFileTest, GetRcFilesAcceptsNullCommandLineRc) {
ccalvarin04808942018-05-01 15:30:28 -0700332 const CommandLine cmd_line =
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700333 CommandLine(binary_path_,
334 {"--nosystem_rc", "--noworkspace_rc", "--nohome_rc",
335 "--bazelrc=/dev/null"},
336 "build", {});
ccalvarin04808942018-05-01 15:30:28 -0700337 std::string error = "check that this string is not modified";
338 std::vector<std::unique_ptr<RcFile>> parsed_rcs;
339 const blaze_exit_code::ExitCode exit_code =
340 option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
341 &cmd_line, &parsed_rcs, &error);
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700342 // /dev/null is not an error
ccalvarin04808942018-05-01 15:30:28 -0700343 EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
344 EXPECT_EQ("check that this string is not modified", error);
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700345 // but it does technically count as a file
ccalvarin04808942018-05-01 15:30:28 -0700346 ASSERT_EQ(1, parsed_rcs.size());
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700347 const std::deque<std::string> expected_rc_que = {kNullDevice};
Laszlo Csomor9b83bd72018-12-17 08:42:44 -0800348 EXPECT_EQ(expected_rc_que, parsed_rcs[0].get()->canonical_source_paths());
ccalvarin04808942018-05-01 15:30:28 -0700349}
350
ccalvarinf03acca2018-09-06 14:25:27 -0700351class ParseOptionsTest : public RcFileTest {
352 protected:
353 void ParseOptionsAndCheckOutput(
354 const std::vector<std::string>& args,
355 const blaze_exit_code::ExitCode expected_exit_code,
356 const std::string& expected_error_regex,
357 const std::string& expected_output_regex) {
358 std::string error;
359 testing::internal::CaptureStderr();
360 const blaze_exit_code::ExitCode exit_code =
361 option_processor_->ParseOptions(args, workspace_, cwd_, &error);
362 const std::string output = testing::internal::GetCapturedStderr();
363
364 ASSERT_EQ(expected_exit_code, exit_code) << error;
365 ASSERT_THAT(error, ContainsRegex(expected_error_regex));
366 ASSERT_THAT(output, ContainsRegex(expected_output_regex));
367 }
368};
ccalvarin04808942018-05-01 15:30:28 -0700369
ccalvarin90e116c2018-05-15 16:41:19 -0700370TEST_F(ParseOptionsTest, IgnoreAllRcFilesIgnoresAllMasterAndUserRcFiles) {
371 // Put fake options in different expected rc files, to check that none of them
372 // are read.
ccalvarind96f1572018-08-22 11:18:53 -0700373 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700374 ASSERT_TRUE(SetUpWorkspaceRcFile("startup --workspacefoo", &workspace_rc));
375 std::string system_rc;
376 ASSERT_TRUE(SetUpSystemRcFile("startup --systemfoo", &system_rc));
377 const std::string cmdline_rc_path =
378 blaze_util::JoinPath(workspace_, "mybazelrc");
379 ASSERT_TRUE(
380 blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
381 ASSERT_TRUE(blaze_util::WriteFile("startup --myfoo", cmdline_rc_path, 0755));
ccalvarin90e116c2018-05-15 16:41:19 -0700382
383 const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
384 "build"};
385 // Expect no error due to the incorrect options, as non of them should have
386 // been loaded.
387 std::string error;
388 EXPECT_EQ(blaze_exit_code::SUCCESS,
389 option_processor_->ParseOptions(args, workspace_, cwd_, &error));
390 ASSERT_EQ("", error);
391
392 // Check that the startup options' provenance message contains nothing
393 testing::internal::CaptureStderr();
394 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700395 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin90e116c2018-05-15 16:41:19 -0700396
397 EXPECT_EQ(output, "");
398}
399
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700400TEST_F(ParseOptionsTest, LaterIgnoreAllRcFilesValueWins) {
ccalvarin90e116c2018-05-15 16:41:19 -0700401 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700402 ASSERT_TRUE(SetUpWorkspaceRcFile("startup --workspacefoo", &workspace_rc));
ccalvarin90e116c2018-05-15 16:41:19 -0700403
404 const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
405 "--noignore_all_rc_files", "build"};
406 std::string error;
407 EXPECT_EQ(blaze_exit_code::BAD_ARGV,
408 option_processor_->ParseOptions(args, workspace_, cwd_, &error));
409 ASSERT_EQ(
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700410 "Unknown startup option: '--workspacefoo'.\n For more info, run "
ccalvarin90e116c2018-05-15 16:41:19 -0700411 "'bazel help startup_options'.",
412 error);
413
414 // Check that the startup options' provenance message contains the provenance
415 // of the incorrect option.
416 testing::internal::CaptureStderr();
417 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700418 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin90e116c2018-05-15 16:41:19 -0700419
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700420 EXPECT_THAT(
421 output,
422 MatchesRegex("INFO: Reading 'startup' options from .*workspace.*bazelrc: "
423 "--workspacefoo\n"));
ccalvarin90e116c2018-05-15 16:41:19 -0700424}
425
426TEST_F(ParseOptionsTest, IgnoreAllRcFilesIgnoresCommandLineRcFileToo) {
427 // Put fake options in different expected rc files, to check that none of them
428 // are read.
429 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700430 ASSERT_TRUE(SetUpWorkspaceRcFile("startup --workspacefoo", &workspace_rc));
431 std::string system_rc;
432 ASSERT_TRUE(SetUpSystemRcFile("startup --systemfoo", &system_rc));
433
ccalvarin90e116c2018-05-15 16:41:19 -0700434 const std::string cmdline_rc_path =
435 blaze_util::JoinPath(workspace_, "mybazelrc");
436 ASSERT_TRUE(
437 blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
438 ASSERT_TRUE(
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700439 blaze_util::WriteFile("startup --cmdlinefoo", cmdline_rc_path, 0755));
ccalvarin90e116c2018-05-15 16:41:19 -0700440
441 const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
442 "--bazelrc=" + cmdline_rc_path,
443 "build"};
444 // Expect no error due to the incorrect options, as non of them should have
445 // been loaded.
446 std::string error;
447 EXPECT_EQ(blaze_exit_code::SUCCESS,
448 option_processor_->ParseOptions(args, workspace_, cwd_, &error));
449 ASSERT_EQ("", error);
450
451 // Check that the startup options' provenance message contains nothing
452 testing::internal::CaptureStderr();
453 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700454 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin90e116c2018-05-15 16:41:19 -0700455
456 EXPECT_EQ(output, "");
457}
458
ccalvarin04808942018-05-01 15:30:28 -0700459TEST_F(ParseOptionsTest, CommandLineBazelrcHasUnknownOption) {
460 const std::string cmdline_rc_path =
461 blaze_util::JoinPath(workspace_, "mybazelrc");
462 ASSERT_TRUE(
463 blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
464 ASSERT_TRUE(blaze_util::WriteFile("startup --foo", cmdline_rc_path, 0755));
465
466 const std::vector<std::string> args = {binary_path_, "--nomaster_bazelrc",
467 "--bazelrc=" + cmdline_rc_path,
468 "build"};
469 const std::string expected_error =
470 "Unknown startup option: '--foo'.\n"
471 " For more info, run 'bazel help startup_options'.";
472 std::string error;
473 ASSERT_NE(blaze_exit_code::SUCCESS,
474 option_processor_->ParseOptions(args, workspace_, cwd_, &error))
475 << error;
476 ASSERT_EQ(expected_error, error);
477
478 // Check that the startup options' provenance message contains the correct
479 // information for the incorrect flag, and does not print the command-line
480 // provided startup flags.
481 testing::internal::CaptureStderr();
482 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700483 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700484
485 EXPECT_THAT(output,
486 MatchesRegex(
487 "INFO: Reading 'startup' options from .*mybazelrc: --foo\n"));
488}
489
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700490TEST_F(ParseOptionsTest, BazelrcHasUnknownOption) {
ccalvarin04808942018-05-01 15:30:28 -0700491 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700492 ASSERT_TRUE(SetUpWorkspaceRcFile("startup --foo", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700493
494 const std::vector<std::string> args = {binary_path_, "build"};
495
496 // Expect no error due to the incorrect --foo.
497 std::string error;
498 EXPECT_EQ(blaze_exit_code::BAD_ARGV,
499 option_processor_->ParseOptions(args, workspace_, cwd_, &error));
500 ASSERT_EQ(
501 "Unknown startup option: '--foo'.\n"
502 " For more info, run 'bazel help startup_options'.",
503 error);
504
505 // Check that the startup options' provenance message contains nothing for the
506 // master bazelrc.
507 testing::internal::CaptureStderr();
508 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700509 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700510
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700511 EXPECT_THAT(output, MatchesRegex("INFO: Reading 'startup' options from "
512 ".*workspace.*bazelrc: --foo\n"));
ccalvarin04808942018-05-01 15:30:28 -0700513}
514
515TEST_F(ParseOptionsTest,
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700516 IncorrectWorkspaceBazelrcIgnoredWhenNoWorkspaceRcIsPresent) {
ccalvarin04808942018-05-01 15:30:28 -0700517 std::string workspace_rc;
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700518 ASSERT_TRUE(SetUpWorkspaceRcFile("startup --foo", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700519
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700520 const std::vector<std::string> args = {binary_path_, "--noworkspace_rc",
ccalvarin04808942018-05-01 15:30:28 -0700521 "build"};
522
523 // Expect no error due to the incorrect --foo.
524 std::string error;
525 EXPECT_EQ(blaze_exit_code::SUCCESS,
526 option_processor_->ParseOptions(args, workspace_, cwd_, &error));
527 ASSERT_EQ("", error);
528
529 // Check that the startup options' provenance message contains nothing for the
530 // master bazelrc.
531 testing::internal::CaptureStderr();
532 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700533 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700534
535 EXPECT_EQ(output, "");
536}
537
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700538TEST_F(ParseOptionsTest, PositiveOptionOverridesNegativeOption) {
539 std::string workspace_rc;
ccalvarin04808942018-05-01 15:30:28 -0700540 ASSERT_TRUE(
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700541 SetUpWorkspaceRcFile("startup --max_idle_secs=123", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700542
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700543 const std::vector<std::string> args = {"bazel", "--noworkspace_rc",
544 "--workspace_rc", "build"};
ccalvarin8ceaa652018-08-24 12:44:31 -0700545 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin2d3adfb2018-08-22 08:56:27 -0700546
ccalvarin04808942018-05-01 15:30:28 -0700547 EXPECT_EQ(123, option_processor_->GetParsedStartupOptions()->max_idle_secs);
548
549 // Check that the startup options' provenance message contains the correct
550 // information for the master bazelrc.
551 testing::internal::CaptureStderr();
552 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700553 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700554
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700555 EXPECT_THAT(output,
556 MatchesRegex("INFO: Reading 'startup' options from "
557 ".*workspace.*bazelrc: --max_idle_secs=123\n"));
ccalvarin04808942018-05-01 15:30:28 -0700558}
559
560TEST_F(ParseOptionsTest, MultipleStartupArgsInMasterBazelrcWorksCorrectly) {
561 // Add startup flags to the master bazelrc.
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700562 std::string workspace_rc;
563 ASSERT_TRUE(SetUpWorkspaceRcFile(
564 "startup --max_idle_secs=42\nstartup --io_nice_level=6", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700565
566 const std::vector<std::string> args = {binary_path_, "build"};
ccalvarin8ceaa652018-08-24 12:44:31 -0700567 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin04808942018-05-01 15:30:28 -0700568
569 EXPECT_EQ(42, option_processor_->GetParsedStartupOptions()->max_idle_secs);
570 EXPECT_EQ(6, option_processor_->GetParsedStartupOptions()->io_nice_level);
571
572 // Check that the startup options get grouped together properly in the output
573 // message.
574 testing::internal::CaptureStderr();
575 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700576 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700577
578 EXPECT_THAT(
579 output,
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700580 MatchesRegex("INFO: Reading 'startup' options from .*workspace.*bazelrc: "
ccalvarin04808942018-05-01 15:30:28 -0700581 "--max_idle_secs=42 --io_nice_level=6\n"));
582}
583
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700584TEST_F(ParseOptionsTest, CommandLineBazelrcHasPriorityOverDefaultBazelrc) {
585 // Add startup flags to the workspace bazelrc.
586 std::string workspace_rc;
587 ASSERT_TRUE(SetUpWorkspaceRcFile(
588 "startup --max_idle_secs=42\nstartup --io_nice_level=6", &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700589
590 // Override one of the master bazelrc's flags in the commandline rc.
591 const std::string cmdline_rc_path =
592 blaze_util::JoinPath(workspace_, "mybazelrc");
593 ASSERT_TRUE(
594 blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
595 ASSERT_TRUE(blaze_util::WriteFile("startup --max_idle_secs=123",
596 cmdline_rc_path, 0755));
597
598 const std::vector<std::string> args = {
599 "bazel", "--bazelrc=" + cmdline_rc_path, "build"};
ccalvarin8ceaa652018-08-24 12:44:31 -0700600 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin04808942018-05-01 15:30:28 -0700601
602 EXPECT_EQ(123, option_processor_->GetParsedStartupOptions()->max_idle_secs);
603 EXPECT_EQ(6, option_processor_->GetParsedStartupOptions()->io_nice_level);
604
605 // Check that the options are reported in the correct order in the provenance
606 // message.
607 testing::internal::CaptureStderr();
608 option_processor_->PrintStartupOptionsProvenanceMessage();
ccalvarin375d5932018-08-23 14:25:49 -0700609 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700610
611 EXPECT_THAT(
612 output,
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700613 MatchesRegex("INFO: Reading 'startup' options from .*workspace.*bazelrc: "
ccalvarin04808942018-05-01 15:30:28 -0700614 "--max_idle_secs=42 --io_nice_level=6\n"
615 "INFO: Reading 'startup' options from .*mybazelrc: "
616 "--max_idle_secs=123\n"));
617}
618
ccalvarinf03acca2018-09-06 14:25:27 -0700619class BlazercImportTest : public ParseOptionsTest {
620 protected:
621 void TestBazelRcImportsMaintainsFlagOrdering(const std::string& import_type) {
622 // Override one of the master bazelrc's flags in the custom bazelrc.
623 const std::string imported_rc_path =
624 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
625 ASSERT_TRUE(blaze_util::MakeDirectories(
626 blaze_util::Dirname(imported_rc_path), 0755));
627 ASSERT_TRUE(blaze_util::WriteFile(
628 "startup --max_idle_secs=123\n"
629 "startup --io_nice_level=4",
630 imported_rc_path, 0755));
ccalvarin04808942018-05-01 15:30:28 -0700631
ccalvarinf03acca2018-09-06 14:25:27 -0700632 // Add startup flags the imported bazelrc.
633 std::string workspace_rc;
634 ASSERT_TRUE(SetUpWorkspaceRcFile(
635 "startup --max_idle_secs=42\n" +
636 import_type + " " + imported_rc_path + "\n"
637 "startup --io_nice_level=6",
638 &workspace_rc));
ccalvarin04808942018-05-01 15:30:28 -0700639
ccalvarinf03acca2018-09-06 14:25:27 -0700640 const std::vector<std::string> args = {"bazel", "build"};
641 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin04808942018-05-01 15:30:28 -0700642
ccalvarinf03acca2018-09-06 14:25:27 -0700643 EXPECT_EQ(123, option_processor_->GetParsedStartupOptions()->max_idle_secs);
644 EXPECT_EQ(6, option_processor_->GetParsedStartupOptions()->io_nice_level);
ccalvarin04808942018-05-01 15:30:28 -0700645
ccalvarinf03acca2018-09-06 14:25:27 -0700646 // Check that the options are reported in the correct order in the
647 // provenance message, the imported file between the two master flags
648 testing::internal::CaptureStderr();
649 option_processor_->PrintStartupOptionsProvenanceMessage();
650 const std::string output = testing::internal::GetCapturedStderr();
ccalvarin04808942018-05-01 15:30:28 -0700651
ccalvarinf03acca2018-09-06 14:25:27 -0700652 EXPECT_THAT(
653 output,
654 MatchesRegex(
655 "INFO: Reading 'startup' options from .*workspace.*bazelrc: "
656 "--max_idle_secs=42\n"
657 "INFO: Reading 'startup' options from .*myimportedbazelrc: "
658 "--max_idle_secs=123 --io_nice_level=4\n"
659 "INFO: Reading 'startup' options from .*workspace.*bazelrc: "
660 "--io_nice_level=6\n"));
661 }
ccalvarin04808942018-05-01 15:30:28 -0700662
ccalvarinf03acca2018-09-06 14:25:27 -0700663 void TestThatDoubleImportsCauseAWarning(const std::string& import_type) {
664 const std::string imported_rc_path =
665 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
666 ASSERT_TRUE(blaze_util::WriteFile("", imported_rc_path, 0755));
667
668 // Import the custom location twice.
669 std::string workspace_rc;
670 ASSERT_TRUE(SetUpWorkspaceRcFile(
671 import_type + " " + imported_rc_path + "\n" +
672 import_type + " " + imported_rc_path + "\n",
673 &workspace_rc));
674
675 const std::vector<std::string> args = {"bazel", "build"};
676 ParseOptionsAndCheckOutput(
677 args, blaze_exit_code::SUCCESS, "",
678 "WARNING: Duplicate rc file: .*myimportedbazelrc is imported multiple "
679 "times from .*workspace.*bazelrc\n");
680 }
681
682 void TestThatDoubleImportWithWorkspaceRelativeSyntaxCauseAWarning(
683 const std::string& import_type) {
684 const std::string imported_rc_path =
685 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
686 ASSERT_TRUE(blaze_util::WriteFile("", imported_rc_path, 0755));
687
688 // Import the custom location twice.
689 std::string workspace_rc;
690 ASSERT_TRUE(
691 SetUpWorkspaceRcFile(
692 import_type + " " + imported_rc_path + "\n" +
693 import_type + " %workspace%/myimportedbazelrc\n",
694 &workspace_rc));
695
696 const std::vector<std::string> args = {"bazel", "build"};
697 ParseOptionsAndCheckOutput(
698 args, blaze_exit_code::SUCCESS, "",
699 "WARNING: Duplicate rc file: .*myimportedbazelrc is imported multiple "
700 "times from .*workspace.*bazelrc\n");
701 }
702
703 void TestThatDoubleImportWithExcessPathSyntaxCauseAWarning(
704 const std::string& import_type) {
705 const std::string imported_rc_path =
706 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
707 ASSERT_TRUE(blaze_util::WriteFile("", imported_rc_path, 0755));
708
709 // Import the custom location twice.
710 std::string workspace_rc;
711 ASSERT_TRUE(
712 SetUpWorkspaceRcFile(
713 import_type + " " + imported_rc_path + "\n" +
714 import_type + " %workspace%///.//myimportedbazelrc\n",
715 &workspace_rc));
716
717 const std::vector<std::string> args = {"bazel", "build"};
718 ParseOptionsAndCheckOutput(
719 args, blaze_exit_code::SUCCESS, "",
720 "WARNING: Duplicate rc file: .*myimportedbazelrc is imported multiple "
721 "times from .*workspace.*bazelrc\n");
722 }
723
724 void TestThatDeepDoubleImportCausesAWarning(const std::string& import_type) {
725 const std::string dual_imported_rc_path =
726 blaze_util::JoinPath(workspace_, "dual_imported.bazelrc");
727 ASSERT_TRUE(blaze_util::WriteFile("", dual_imported_rc_path, 0755));
728
729 const std::string intermediate_import_1 =
730 blaze_util::JoinPath(workspace_, "intermediate_import_1");
731 ASSERT_TRUE(blaze_util::WriteFile(
732 import_type + " " + dual_imported_rc_path,
733 intermediate_import_1, 0755));
734
735 const std::string intermediate_import_2 =
736 blaze_util::JoinPath(workspace_, "intermediate_import_2");
737 ASSERT_TRUE(blaze_util::WriteFile(
738 import_type + " " + dual_imported_rc_path,
739 intermediate_import_2, 0755));
740
741 // Import the custom location twice.
742 std::string workspace_rc;
743 ASSERT_TRUE(SetUpWorkspaceRcFile(
744 import_type + " " + intermediate_import_1 + "\n" +
745 import_type + " " + intermediate_import_2 + "\n",
746 &workspace_rc));
747
748 const std::vector<std::string> args = {"bazel", "build"};
749 ParseOptionsAndCheckOutput(
750 args, blaze_exit_code::SUCCESS, "",
751 "WARNING: Duplicate rc file: .*dual_imported.bazelrc is imported "
752 "multiple times from .*workspace.*bazelrc\n");
753 }
754
755 void TestThatImportingAFileAndPassingItInCausesAWarning(
756 const std::string& import_type) {
757 const std::string imported_rc_path =
758 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
759 ASSERT_TRUE(blaze_util::WriteFile("", imported_rc_path, 0755));
760
761 // Import the custom location, and pass it in by flag.
762 std::string workspace_rc;
763 ASSERT_TRUE(
764 SetUpWorkspaceRcFile(
765 import_type + " " + imported_rc_path,
766 &workspace_rc));
767
768 const std::vector<std::string> args = {
769 "bazel", "--bazelrc=" + imported_rc_path, "build"};
770 ParseOptionsAndCheckOutput(
771 args, blaze_exit_code::SUCCESS, "",
772 "WARNING: Duplicate rc file: .*myimportedbazelrc is read multiple "
773 "times, "
jingwen68c57f02018-11-21 16:17:17 -0800774 "it is a standard rc file location but must have been unnecessarily "
ccalvarinf03acca2018-09-06 14:25:27 -0700775 "imported earlier.\n");
776 }
777};
778
779TEST_F(BlazercImportTest, BazelRcImportFailsForMissingFile) {
ccalvarin8ceaa652018-08-24 12:44:31 -0700780 const std::string missing_imported_rc_path =
781 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
782 std::string workspace_rc;
783 ASSERT_TRUE(SetUpWorkspaceRcFile("import " + missing_imported_rc_path,
784 &workspace_rc));
785
786 const std::vector<std::string> args = {"bazel", "build"};
787 ParseOptionsAndCheckOutput(
788 args, blaze_exit_code::INTERNAL_ERROR,
789 "Unexpected error reading .blazerc file '.*myimportedbazelrc'", "");
790}
791
ccalvarinf03acca2018-09-06 14:25:27 -0700792TEST_F(BlazercImportTest, BazelRcTryImportDoesNotFailForMissingFile) {
793 const std::string missing_imported_rc_path =
794 blaze_util::JoinPath(workspace_, "tryimported.bazelrc");
ccalvarin8ceaa652018-08-24 12:44:31 -0700795 std::string workspace_rc;
ccalvarinf03acca2018-09-06 14:25:27 -0700796 ASSERT_TRUE(SetUpWorkspaceRcFile("try-import " + missing_imported_rc_path,
797 &workspace_rc));
ccalvarin8ceaa652018-08-24 12:44:31 -0700798
799 const std::vector<std::string> args = {"bazel", "build"};
ccalvarinf03acca2018-09-06 14:25:27 -0700800 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin8ceaa652018-08-24 12:44:31 -0700801}
802
ccalvarinf03acca2018-09-06 14:25:27 -0700803// rc_file does not differentiate between non-existent and unreadable files. We
804// don't necessarily want try-import to ignore unreadable files, but this test
805// exists to make sure we don't change the behavior by accident. Any change that
806// makes existent but unreadable files a failure with try-import should inform
807// users.
808TEST_F(BlazercImportTest, BazelRcTryImportDoesNotFailForUnreadableFile) {
809 const std::string unreadable_rc_path =
810 blaze_util::JoinPath(workspace_, "tryimported.bazelrc");
811 ASSERT_TRUE(blaze_util::WriteFile("startup --max_idle_secs=123",
812 unreadable_rc_path, 222));
ccalvarin8ceaa652018-08-24 12:44:31 -0700813 std::string workspace_rc;
ccalvarinf03acca2018-09-06 14:25:27 -0700814 ASSERT_TRUE(
815 SetUpWorkspaceRcFile("try-import " + unreadable_rc_path, &workspace_rc));
ccalvarin8ceaa652018-08-24 12:44:31 -0700816
817 const std::vector<std::string> args = {"bazel", "build"};
ccalvarinf03acca2018-09-06 14:25:27 -0700818 ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", "");
ccalvarin8ceaa652018-08-24 12:44:31 -0700819}
820
ccalvarin8ceaa652018-08-24 12:44:31 -0700821
ccalvarinf03acca2018-09-06 14:25:27 -0700822TEST_F(BlazercImportTest, BazelRcImportsMaintainsFlagOrdering) {
823 TestBazelRcImportsMaintainsFlagOrdering("import");
824}
ccalvarin8ceaa652018-08-24 12:44:31 -0700825
ccalvarinf03acca2018-09-06 14:25:27 -0700826TEST_F(BlazercImportTest, BazelRcTryImportsMaintainsFlagOrdering) {
827 TestBazelRcImportsMaintainsFlagOrdering("try-import");
828}
829
830TEST_F(BlazercImportTest, DoubleImportsCauseAWarning) {
831 TestThatDoubleImportsCauseAWarning("import");
832}
833
834TEST_F(BlazercImportTest, DoubleTryImportsCauseAWarning) {
835 TestThatDoubleImportsCauseAWarning("try-import");
836}
837
838TEST_F(BlazercImportTest,
839 DoubleImportWithWorkspaceRelativeSyntaxCauseAWarning) {
840 TestThatDoubleImportWithWorkspaceRelativeSyntaxCauseAWarning("import");
841}
842
843TEST_F(BlazercImportTest,
844 DoubleTryImportWithWorkspaceRelativeSyntaxCauseAWarning) {
845 TestThatDoubleImportWithWorkspaceRelativeSyntaxCauseAWarning("try-import");
846}
847
848TEST_F(BlazercImportTest, DoubleImportWithExcessPathSyntaxCauseAWarning) {
849 TestThatDoubleImportWithExcessPathSyntaxCauseAWarning("import");
850}
851
852TEST_F(BlazercImportTest, DoubleTryImportWithExcessPathSyntaxCauseAWarning) {
853 TestThatDoubleImportWithExcessPathSyntaxCauseAWarning("try-import");
ccalvarin8ceaa652018-08-24 12:44:31 -0700854}
855
856// The following tests unix-path semantics.
857#if !defined(_WIN32) && !defined(__CYGWIN__)
ccalvarinf03acca2018-09-06 14:25:27 -0700858TEST_F(BlazercImportTest,
ccalvarin8ceaa652018-08-24 12:44:31 -0700859 DoubleImportWithEnclosingDirectorySyntaxCauseAWarning) {
860 const std::string imported_rc_path =
861 blaze_util::JoinPath(workspace_, "myimportedbazelrc");
ccalvarin8ceaa652018-08-24 12:44:31 -0700862 ASSERT_TRUE(blaze_util::WriteFile("", imported_rc_path, 0755));
863
864 ASSERT_TRUE(blaze_util::MakeDirectories(
865 blaze_util::JoinPath(workspace_, "extra"), 0755));
866
867 // Import the custom location twice.
868 std::string workspace_rc;
869 ASSERT_TRUE(SetUpWorkspaceRcFile(
870 "import " + imported_rc_path + "\n"
871 "import %workspace%/extra/../myimportedbazelrc\n",
872 &workspace_rc));
873
874 const std::vector<std::string> args = {"bazel", "build"};
875 ParseOptionsAndCheckOutput(
876 args, blaze_exit_code::SUCCESS, "",
877 "WARNING: Duplicate rc file: .*myimportedbazelrc is imported multiple "
878 "times from .*workspace.*bazelrc\n");
879}
880#endif // !defined(_WIN32) && !defined(__CYGWIN__)
881
ccalvarinf03acca2018-09-06 14:25:27 -0700882TEST_F(BlazercImportTest, DeepDoubleImportCausesAWarning) {
883 TestThatDeepDoubleImportCausesAWarning("import");
ccalvarin8ceaa652018-08-24 12:44:31 -0700884}
885
ccalvarinf03acca2018-09-06 14:25:27 -0700886TEST_F(BlazercImportTest, DeepDoubleTryImportCausesAWarning) {
887 TestThatDeepDoubleImportCausesAWarning("try-import");
888}
ccalvarin8ceaa652018-08-24 12:44:31 -0700889
ccalvarinf03acca2018-09-06 14:25:27 -0700890TEST_F(BlazercImportTest, ImportingAFileAndPassingItInCausesAWarning) {
891 TestThatImportingAFileAndPassingItInCausesAWarning("import");
892}
ccalvarin8ceaa652018-08-24 12:44:31 -0700893
ccalvarinf03acca2018-09-06 14:25:27 -0700894TEST_F(BlazercImportTest, TryImportingAFileAndPassingItInCausesAWarning) {
895 TestThatImportingAFileAndPassingItInCausesAWarning("try-import");
ccalvarin8ceaa652018-08-24 12:44:31 -0700896}
897
898// TODO(b/112908763): Somehow, in the following tests, we end with a relative
899// path written in the import line on Windows. Figure out what's going on and
900// reinstate these tests
901#if !defined(_WIN32) && !defined(__CYGWIN__)
902TEST_F(ParseOptionsTest, ImportingAPreviouslyLoadedStandardRcCausesAWarning) {
903 std::string system_rc;
904 ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
905
906 // Import the system_rc extraneously.
907 std::string workspace_rc;
908 ASSERT_TRUE(SetUpWorkspaceRcFile("import " + system_rc, &workspace_rc));
909
910 const std::vector<std::string> args = {"bazel", "build"};
911 ParseOptionsAndCheckOutput(
912 args, blaze_exit_code::SUCCESS, "",
913 "WARNING: Duplicate rc file: .*bazel.bazelrc is read multiple "
914 "times, most recently imported from .*workspace.*bazelrc\n");
915}
916
917TEST_F(ParseOptionsTest, ImportingStandardRcBeforeItIsLoadedCausesAWarning) {
918 std::string workspace_rc;
919 ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
920
921 // Import the workspace_rc extraneously.
922 std::string system_rc;
923 ASSERT_TRUE(SetUpSystemRcFile("import " + workspace_rc, &system_rc));
924
925 const std::vector<std::string> args = {"bazel", "build"};
926 ParseOptionsAndCheckOutput(
927 args, blaze_exit_code::SUCCESS, "",
928 "WARNING: Duplicate rc file: .*workspace.*bazelrc is read multiple "
929 "times, it is a standard rc file location but must have been "
jingwen68c57f02018-11-21 16:17:17 -0800930 "unnecessarily imported earlier.\n");
ccalvarin8ceaa652018-08-24 12:44:31 -0700931}
932#endif // !defined(_WIN32) && !defined(__CYGWIN__)
933
ccalvarin04808942018-05-01 15:30:28 -0700934} // namespace blaze