ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 1 | // 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 | |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 15 | #if defined(_WIN32) |
| 16 | #ifndef WIN32_LEAN_AND_MEAN |
| 17 | #define WIN32_LEAN_AND_MEAN |
| 18 | #endif |
| 19 | #include <windows.h> |
| 20 | #endif |
| 21 | |
laszlocsomor | a300ae8 | 2019-08-07 06:28:59 -0700 | [diff] [blame] | 22 | #include "src/main/cpp/rc_file.h" |
| 23 | |
| 24 | #include <memory> |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 25 | |
| 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" |
laszlocsomor | a300ae8 | 2019-08-07 06:28:59 -0700 | [diff] [blame] | 30 | #include "src/main/cpp/option_processor.h" |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 31 | #include "src/main/cpp/util/file.h" |
| 32 | #include "src/main/cpp/util/file_platform.h" |
ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 33 | #include "src/main/cpp/util/path.h" |
ccalvarin | bccf9c6 | 2018-06-20 15:05:23 -0700 | [diff] [blame] | 34 | #include "src/main/cpp/util/path_platform.h" |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 35 | #include "src/main/cpp/workspace_layout.h" |
| 36 | #include "googlemock/include/gmock/gmock.h" |
| 37 | #include "googletest/include/gtest/gtest.h" |
| 38 | |
| 39 | namespace blaze { |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 40 | using ::testing::ContainsRegex; |
Googler | 1980fdb | 2020-09-01 13:49:34 -0700 | [diff] [blame] | 41 | using ::testing::ElementsAre; |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 42 | using ::testing::HasSubstr; |
Googler | 1980fdb | 2020-09-01 13:49:34 -0700 | [diff] [blame] | 43 | using ::testing::IsEmpty; |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 44 | using ::testing::MatchesRegex; |
Googler | 1980fdb | 2020-09-01 13:49:34 -0700 | [diff] [blame] | 45 | using ::testing::Pointee; |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 46 | |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 47 | #if defined(_WIN32) || defined(__CYGWIN__) |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 48 | constexpr const char* kNullDevice = "NUL"; |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 49 | #else // Assume POSIX if not Windows. |
| 50 | constexpr const char* kNullDevice = "/dev/null"; |
| 51 | #endif |
| 52 | |
Googler | 1980fdb | 2020-09-01 13:49:34 -0700 | [diff] [blame] | 53 | // Matches an RcFile's canonical source paths list. |
| 54 | MATCHER_P(CanonicalSourcePathsAre, paths_matcher, "") { |
| 55 | return ExplainMatchResult(ElementsAre(paths_matcher), |
| 56 | arg.canonical_source_paths(), result_listener); |
| 57 | } |
| 58 | |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 59 | class RcFileTest : public ::testing::Test { |
| 60 | protected: |
| 61 | RcFileTest() |
Laszlo Csomor | 0d10749 | 2019-03-13 09:04:27 -0700 | [diff] [blame] | 62 | : workspace_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), |
| 63 | "workspace")), |
| 64 | cwd_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "cwd")), |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 65 | home_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "home")), |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 66 | binary_dir_( |
Laszlo Csomor | 0d10749 | 2019-03-13 09:04:27 -0700 | [diff] [blame] | 67 | blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "bazeldir")), |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 68 | binary_path_(blaze_util::JoinPath(binary_dir_, "bazel")), |
Yannic Bonenberger | 45d5200 | 2018-09-14 16:11:36 -0700 | [diff] [blame] | 69 | workspace_layout_(new WorkspaceLayout()) {} |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 70 | |
| 71 | void SetUp() override { |
| 72 | ASSERT_TRUE(blaze_util::MakeDirectories(workspace_, 0755)); |
| 73 | ASSERT_TRUE(blaze_util::MakeDirectories(cwd_, 0755)); |
ccalvarin | bccf9c6 | 2018-06-20 15:05:23 -0700 | [diff] [blame] | 74 | ASSERT_TRUE(blaze_util::ChangeDirectory(cwd_)); |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 75 | 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 |
ccalvarin | bccf9c6 | 2018-06-20 15:05:23 -0700 | [diff] [blame] | 81 | #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 | |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(blaze_util::MakeDirectories(binary_dir_, 0755)); |
| 93 | option_processor_.reset(new OptionProcessor( |
| 94 | workspace_layout_.get(), |
| 95 | std::unique_ptr<StartupOptions>( |
Yannic Bonenberger | 45d5200 | 2018-09-14 16:11:36 -0700 | [diff] [blame] | 96 | new BazelStartupOptions(workspace_layout_.get())), |
| 97 | "bazel.bazelrc")); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 98 | } |
| 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 | } |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 114 | blaze_util::GetAllFilesUnder(home_, &files); |
| 115 | for (const std::string& file : files) { |
| 116 | blaze_util::UnlinkPath(file); |
| 117 | } |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 118 | blaze_util::GetAllFilesUnder(binary_dir_, &files); |
| 119 | for (const std::string& file : files) { |
| 120 | blaze_util::UnlinkPath(file); |
| 121 | } |
| 122 | } |
| 123 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 124 | bool SetUpSystemRcFile(const std::string& contents, |
ccalvarin | bccf9c6 | 2018-06-20 15:05:23 -0700 | [diff] [blame] | 125 | std::string* rcfile_path) const { |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 126 | const std::string system_rc_path = |
ccalvarin | bccf9c6 | 2018-06-20 15:05:23 -0700 | [diff] [blame] | 127 | blaze_util::ConvertPath(blaze_util::JoinPath(cwd_, "bazel.bazelrc")); |
| 128 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 129 | if (blaze_util::WriteFile(contents, system_rc_path, 0755)) { |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 130 | *rcfile_path = blaze_util::MakeCanonical(system_rc_path.c_str()); |
ccalvarin | bccf9c6 | 2018-06-20 15:05:23 -0700 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 136 | 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)) { |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 141 | *rcfile_path = blaze_util::MakeCanonical(workspace_user_rc_path.c_str()); |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 147 | 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 | } |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 156 | |
| 157 | bool SetUpLegacyMasterRcFileInWorkspace(const std::string& contents, |
| 158 | std::string* rcfile_path) const { |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 159 | 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 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 170 | bool SetUpLegacyMasterRcFileAlongsideBinary(const std::string& contents, |
| 171 | std::string* rcfile_path) const { |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 172 | 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 | |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 181 | const std::string workspace_; |
ccalvarin | bccf9c6 | 2018-06-20 15:05:23 -0700 | [diff] [blame] | 182 | std::string cwd_; |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 183 | const std::string home_; |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 184 | const std::string binary_dir_; |
| 185 | const std::string binary_path_; |
| 186 | const std::unique_ptr<WorkspaceLayout> workspace_layout_; |
ccalvarin | bccf9c6 | 2018-06-20 15:05:23 -0700 | [diff] [blame] | 187 | const std::string old_system_bazelrc_path_; |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 188 | std::unique_ptr<OptionProcessor> option_processor_; |
| 189 | }; |
| 190 | |
| 191 | using GetRcFileTest = RcFileTest; |
| 192 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 193 | TEST_F(GetRcFileTest, GetRcFilesLoadsAllDefaultBazelrcs) { |
| 194 | std::string system_rc; |
| 195 | ASSERT_TRUE(SetUpSystemRcFile("", &system_rc)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 196 | std::string workspace_rc; |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 197 | ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc)); |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 198 | std::string home_rc; |
| 199 | ASSERT_TRUE(SetUpHomeRcFile("", &home_rc)); |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 200 | |
| 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. |
Googler | 1980fdb | 2020-09-01 13:49:34 -0700 | [diff] [blame] | 212 | EXPECT_THAT(parsed_rcs, |
| 213 | ElementsAre(Pointee(CanonicalSourcePathsAre(system_rc)), |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 214 | Pointee(CanonicalSourcePathsAre(workspace_rc)), |
| 215 | Pointee(CanonicalSourcePathsAre(home_rc)))); |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | TEST_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)); |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 223 | std::string home_rc; |
| 224 | ASSERT_TRUE(SetUpHomeRcFile("", &home_rc)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 225 | |
| 226 | const CommandLine cmd_line = |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 227 | CommandLine(binary_path_, {"--nosystem_rc"}, "build", {}); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 228 | 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 | |
Googler | 1980fdb | 2020-09-01 13:49:34 -0700 | [diff] [blame] | 236 | EXPECT_THAT(parsed_rcs, |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 237 | ElementsAre(Pointee(CanonicalSourcePathsAre(workspace_rc)), |
| 238 | Pointee(CanonicalSourcePathsAre(home_rc)))); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 239 | } |
| 240 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 241 | TEST_F(GetRcFileTest, GetRcFilesRespectsNoWorkspaceRc) { |
| 242 | std::string system_rc; |
| 243 | ASSERT_TRUE(SetUpSystemRcFile("", &system_rc)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 244 | std::string workspace_rc; |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 245 | ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc)); |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 246 | std::string home_rc; |
| 247 | ASSERT_TRUE(SetUpHomeRcFile("", &home_rc)); |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 248 | |
| 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 | |
Googler | 1980fdb | 2020-09-01 13:49:34 -0700 | [diff] [blame] | 259 | EXPECT_THAT(parsed_rcs, |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 260 | ElementsAre(Pointee(CanonicalSourcePathsAre(system_rc)), |
| 261 | Pointee(CanonicalSourcePathsAre(home_rc)))); |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 262 | } |
| 263 | |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 264 | TEST_F(GetRcFileTest, |
| 265 | GetRcFilesRespectsNoWorkspaceRcAndNoSystemAndNoHomeRcCombined) { |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 266 | std::string system_rc; |
| 267 | ASSERT_TRUE(SetUpSystemRcFile("", &system_rc)); |
| 268 | std::string workspace_rc; |
| 269 | ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc)); |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 270 | std::string home_rc; |
| 271 | ASSERT_TRUE(SetUpHomeRcFile("", &home_rc)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 272 | |
| 273 | const CommandLine cmd_line = CommandLine( |
philwo | 09b135d | 2020-11-25 06:07:26 -0800 | [diff] [blame] | 274 | binary_path_, {"--noworkspace_rc", "--nosystem_rc", "--nohome_rc"}, |
| 275 | "build", {}); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 276 | 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 | |
Googler | 1980fdb | 2020-09-01 13:49:34 -0700 | [diff] [blame] | 284 | EXPECT_THAT(parsed_rcs, IsEmpty()); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 285 | } |
| 286 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 287 | TEST_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); |
ccalvarin | 375d593 | 2018-08-23 14:25:49 -0700 | [diff] [blame] | 301 | const std::string output = testing::internal::GetCapturedStderr(); |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 302 | |
| 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")); |
dslomov | a6c7758 | 2018-10-09 13:56:38 -0700 | [diff] [blame] | 310 | EXPECT_THAT(output, HasSubstr(workspace_rc)); |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 311 | EXPECT_THAT(output, HasSubstr(binary_rc)); |
| 312 | } |
| 313 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 314 | TEST_F(GetRcFileTest, GetRcFilesReadsCommandLineRc) { |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 315 | 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( |
Googler | 96c8a90 | 2022-03-14 08:05:57 -0700 | [diff] [blame] | 322 | binary_path_, {"--bazelrc=" + cmdline_rc_path}, |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 323 | "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()); |
Googler | 1980fdb | 2020-09-01 13:49:34 -0700 | [diff] [blame] | 335 | ASSERT_EQ(1, parsed_rcs[0]->canonical_source_paths().size()); |
| 336 | EXPECT_THAT(parsed_rcs[0]->canonical_source_paths()[0], |
Laszlo Csomor | 9b83bd7 | 2018-12-17 08:42:44 -0800 | [diff] [blame] | 337 | HasSubstr("mybazelrc")); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 338 | } |
| 339 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 340 | TEST_F(GetRcFileTest, GetRcFilesAcceptsNullCommandLineRc) { |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 341 | const CommandLine cmd_line = |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 342 | CommandLine(binary_path_, |
| 343 | {"--nosystem_rc", "--noworkspace_rc", "--nohome_rc", |
| 344 | "--bazelrc=/dev/null"}, |
| 345 | "build", {}); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 346 | 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); |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 351 | // /dev/null is not an error |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 352 | EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code); |
| 353 | EXPECT_EQ("check that this string is not modified", error); |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 354 | // but it does technically count as a file |
Googler | 1980fdb | 2020-09-01 13:49:34 -0700 | [diff] [blame] | 355 | EXPECT_THAT(parsed_rcs, |
| 356 | ElementsAre(Pointee(CanonicalSourcePathsAre(kNullDevice)))); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 357 | } |
| 358 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 359 | class 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 | }; |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 377 | |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 378 | TEST_F(ParseOptionsTest, IgnoreAllRcFilesIgnoresAllMasterAndUserRcFiles) { |
| 379 | // Put fake options in different expected rc files, to check that none of them |
| 380 | // are read. |
ccalvarin | d96f157 | 2018-08-22 11:18:53 -0700 | [diff] [blame] | 381 | std::string workspace_rc; |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 382 | 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)); |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 390 | |
| 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(); |
ccalvarin | 375d593 | 2018-08-23 14:25:49 -0700 | [diff] [blame] | 403 | const std::string output = testing::internal::GetCapturedStderr(); |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 404 | |
| 405 | EXPECT_EQ(output, ""); |
| 406 | } |
| 407 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 408 | TEST_F(ParseOptionsTest, LaterIgnoreAllRcFilesValueWins) { |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 409 | std::string workspace_rc; |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 410 | ASSERT_TRUE(SetUpWorkspaceRcFile("startup --workspacefoo", &workspace_rc)); |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 411 | |
| 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( |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 418 | "Unknown startup option: '--workspacefoo'.\n For more info, run " |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 419 | "'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(); |
ccalvarin | 375d593 | 2018-08-23 14:25:49 -0700 | [diff] [blame] | 426 | const std::string output = testing::internal::GetCapturedStderr(); |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 427 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 428 | EXPECT_THAT( |
| 429 | output, |
| 430 | MatchesRegex("INFO: Reading 'startup' options from .*workspace.*bazelrc: " |
| 431 | "--workspacefoo\n")); |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | TEST_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; |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 438 | ASSERT_TRUE(SetUpWorkspaceRcFile("startup --workspacefoo", &workspace_rc)); |
| 439 | std::string system_rc; |
| 440 | ASSERT_TRUE(SetUpSystemRcFile("startup --systemfoo", &system_rc)); |
| 441 | |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 442 | 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( |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 447 | blaze_util::WriteFile("startup --cmdlinefoo", cmdline_rc_path, 0755)); |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 448 | |
| 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(); |
ccalvarin | 375d593 | 2018-08-23 14:25:49 -0700 | [diff] [blame] | 462 | const std::string output = testing::internal::GetCapturedStderr(); |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 463 | |
| 464 | EXPECT_EQ(output, ""); |
| 465 | } |
| 466 | |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 467 | TEST_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 | |
Googler | 96c8a90 | 2022-03-14 08:05:57 -0700 | [diff] [blame] | 474 | const std::vector<std::string> args = {binary_path_, |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 475 | "--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(); |
ccalvarin | 375d593 | 2018-08-23 14:25:49 -0700 | [diff] [blame] | 491 | const std::string output = testing::internal::GetCapturedStderr(); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 492 | |
| 493 | EXPECT_THAT(output, |
| 494 | MatchesRegex( |
| 495 | "INFO: Reading 'startup' options from .*mybazelrc: --foo\n")); |
| 496 | } |
| 497 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 498 | TEST_F(ParseOptionsTest, BazelrcHasUnknownOption) { |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 499 | std::string workspace_rc; |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 500 | ASSERT_TRUE(SetUpWorkspaceRcFile("startup --foo", &workspace_rc)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 501 | |
| 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(); |
ccalvarin | 375d593 | 2018-08-23 14:25:49 -0700 | [diff] [blame] | 517 | const std::string output = testing::internal::GetCapturedStderr(); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 518 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 519 | EXPECT_THAT(output, MatchesRegex("INFO: Reading 'startup' options from " |
| 520 | ".*workspace.*bazelrc: --foo\n")); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | TEST_F(ParseOptionsTest, |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 524 | IncorrectWorkspaceBazelrcIgnoredWhenNoWorkspaceRcIsPresent) { |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 525 | std::string workspace_rc; |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 526 | ASSERT_TRUE(SetUpWorkspaceRcFile("startup --foo", &workspace_rc)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 527 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 528 | const std::vector<std::string> args = {binary_path_, "--noworkspace_rc", |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 529 | "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(); |
ccalvarin | 375d593 | 2018-08-23 14:25:49 -0700 | [diff] [blame] | 541 | const std::string output = testing::internal::GetCapturedStderr(); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 542 | |
| 543 | EXPECT_EQ(output, ""); |
| 544 | } |
| 545 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 546 | TEST_F(ParseOptionsTest, PositiveOptionOverridesNegativeOption) { |
| 547 | std::string workspace_rc; |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 548 | ASSERT_TRUE( |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 549 | SetUpWorkspaceRcFile("startup --max_idle_secs=123", &workspace_rc)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 550 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 551 | const std::vector<std::string> args = {"bazel", "--noworkspace_rc", |
| 552 | "--workspace_rc", "build"}; |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 553 | ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", ""); |
ccalvarin | 2d3adfb | 2018-08-22 08:56:27 -0700 | [diff] [blame] | 554 | |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 555 | 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(); |
ccalvarin | 375d593 | 2018-08-23 14:25:49 -0700 | [diff] [blame] | 561 | const std::string output = testing::internal::GetCapturedStderr(); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 562 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 563 | EXPECT_THAT(output, |
| 564 | MatchesRegex("INFO: Reading 'startup' options from " |
| 565 | ".*workspace.*bazelrc: --max_idle_secs=123\n")); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | TEST_F(ParseOptionsTest, MultipleStartupArgsInMasterBazelrcWorksCorrectly) { |
| 569 | // Add startup flags to the master bazelrc. |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 570 | std::string workspace_rc; |
| 571 | ASSERT_TRUE(SetUpWorkspaceRcFile( |
| 572 | "startup --max_idle_secs=42\nstartup --io_nice_level=6", &workspace_rc)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 573 | |
| 574 | const std::vector<std::string> args = {binary_path_, "build"}; |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 575 | ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", ""); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 576 | |
| 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(); |
ccalvarin | 375d593 | 2018-08-23 14:25:49 -0700 | [diff] [blame] | 584 | const std::string output = testing::internal::GetCapturedStderr(); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 585 | |
| 586 | EXPECT_THAT( |
| 587 | output, |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 588 | MatchesRegex("INFO: Reading 'startup' options from .*workspace.*bazelrc: " |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 589 | "--max_idle_secs=42 --io_nice_level=6\n")); |
| 590 | } |
| 591 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 592 | TEST_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)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 597 | |
| 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"}; |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 608 | ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", ""); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 609 | |
| 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(); |
ccalvarin | 375d593 | 2018-08-23 14:25:49 -0700 | [diff] [blame] | 617 | const std::string output = testing::internal::GetCapturedStderr(); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 618 | |
| 619 | EXPECT_THAT( |
| 620 | output, |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 621 | MatchesRegex("INFO: Reading 'startup' options from .*workspace.*bazelrc: " |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 622 | "--max_idle_secs=42 --io_nice_level=6\n" |
| 623 | "INFO: Reading 'startup' options from .*mybazelrc: " |
| 624 | "--max_idle_secs=123\n")); |
| 625 | } |
| 626 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 627 | class 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)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 639 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 640 | // 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)); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 647 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 648 | const std::vector<std::string> args = {"bazel", "build"}; |
| 649 | ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", ""); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 650 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 651 | EXPECT_EQ(123, option_processor_->GetParsedStartupOptions()->max_idle_secs); |
| 652 | EXPECT_EQ(6, option_processor_->GetParsedStartupOptions()->io_nice_level); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 653 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 654 | // 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(); |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 659 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 660 | 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 | } |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 670 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 671 | 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, " |
jingwen | 68c57f0 | 2018-11-21 16:17:17 -0800 | [diff] [blame] | 782 | "it is a standard rc file location but must have been unnecessarily " |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 783 | "imported earlier.\n"); |
| 784 | } |
| 785 | }; |
| 786 | |
| 787 | TEST_F(BlazercImportTest, BazelRcImportFailsForMissingFile) { |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 788 | 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 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 800 | TEST_F(BlazercImportTest, BazelRcTryImportDoesNotFailForMissingFile) { |
| 801 | const std::string missing_imported_rc_path = |
| 802 | blaze_util::JoinPath(workspace_, "tryimported.bazelrc"); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 803 | std::string workspace_rc; |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 804 | ASSERT_TRUE(SetUpWorkspaceRcFile("try-import " + missing_imported_rc_path, |
| 805 | &workspace_rc)); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 806 | |
| 807 | const std::vector<std::string> args = {"bazel", "build"}; |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 808 | ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", ""); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 809 | } |
| 810 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 811 | // 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. |
| 816 | TEST_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)); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 821 | std::string workspace_rc; |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 822 | ASSERT_TRUE( |
| 823 | SetUpWorkspaceRcFile("try-import " + unreadable_rc_path, &workspace_rc)); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 824 | |
| 825 | const std::vector<std::string> args = {"bazel", "build"}; |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 826 | ParseOptionsAndCheckOutput(args, blaze_exit_code::SUCCESS, "", ""); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Andrew Katson | 70073fd | 2021-07-28 13:09:21 -0700 | [diff] [blame] | 829 | #if defined(_WIN32) |
| 830 | TEST_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 |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 840 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 841 | TEST_F(BlazercImportTest, BazelRcImportsMaintainsFlagOrdering) { |
| 842 | TestBazelRcImportsMaintainsFlagOrdering("import"); |
| 843 | } |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 844 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 845 | TEST_F(BlazercImportTest, BazelRcTryImportsMaintainsFlagOrdering) { |
| 846 | TestBazelRcImportsMaintainsFlagOrdering("try-import"); |
| 847 | } |
| 848 | |
| 849 | TEST_F(BlazercImportTest, DoubleImportsCauseAWarning) { |
| 850 | TestThatDoubleImportsCauseAWarning("import"); |
| 851 | } |
| 852 | |
| 853 | TEST_F(BlazercImportTest, DoubleTryImportsCauseAWarning) { |
| 854 | TestThatDoubleImportsCauseAWarning("try-import"); |
| 855 | } |
| 856 | |
| 857 | TEST_F(BlazercImportTest, |
| 858 | DoubleImportWithWorkspaceRelativeSyntaxCauseAWarning) { |
| 859 | TestThatDoubleImportWithWorkspaceRelativeSyntaxCauseAWarning("import"); |
| 860 | } |
| 861 | |
| 862 | TEST_F(BlazercImportTest, |
| 863 | DoubleTryImportWithWorkspaceRelativeSyntaxCauseAWarning) { |
| 864 | TestThatDoubleImportWithWorkspaceRelativeSyntaxCauseAWarning("try-import"); |
| 865 | } |
| 866 | |
| 867 | TEST_F(BlazercImportTest, DoubleImportWithExcessPathSyntaxCauseAWarning) { |
| 868 | TestThatDoubleImportWithExcessPathSyntaxCauseAWarning("import"); |
| 869 | } |
| 870 | |
| 871 | TEST_F(BlazercImportTest, DoubleTryImportWithExcessPathSyntaxCauseAWarning) { |
| 872 | TestThatDoubleImportWithExcessPathSyntaxCauseAWarning("try-import"); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | // The following tests unix-path semantics. |
| 876 | #if !defined(_WIN32) && !defined(__CYGWIN__) |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 877 | TEST_F(BlazercImportTest, |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 878 | DoubleImportWithEnclosingDirectorySyntaxCauseAWarning) { |
| 879 | const std::string imported_rc_path = |
| 880 | blaze_util::JoinPath(workspace_, "myimportedbazelrc"); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 881 | 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 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 901 | TEST_F(BlazercImportTest, DeepDoubleImportCausesAWarning) { |
| 902 | TestThatDeepDoubleImportCausesAWarning("import"); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 903 | } |
| 904 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 905 | TEST_F(BlazercImportTest, DeepDoubleTryImportCausesAWarning) { |
| 906 | TestThatDeepDoubleImportCausesAWarning("try-import"); |
| 907 | } |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 908 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 909 | TEST_F(BlazercImportTest, ImportingAFileAndPassingItInCausesAWarning) { |
| 910 | TestThatImportingAFileAndPassingItInCausesAWarning("import"); |
| 911 | } |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 912 | |
ccalvarin | f03acca | 2018-09-06 14:25:27 -0700 | [diff] [blame] | 913 | TEST_F(BlazercImportTest, TryImportingAFileAndPassingItInCausesAWarning) { |
| 914 | TestThatImportingAFileAndPassingItInCausesAWarning("try-import"); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 915 | } |
| 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__) |
| 921 | TEST_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 | |
| 936 | TEST_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 " |
jingwen | 68c57f0 | 2018-11-21 16:17:17 -0800 | [diff] [blame] | 949 | "unnecessarily imported earlier.\n"); |
ccalvarin | 8ceaa65 | 2018-08-24 12:44:31 -0700 | [diff] [blame] | 950 | } |
| 951 | #endif // !defined(_WIN32) && !defined(__CYGWIN__) |
| 952 | |
ccalvarin | 0480894 | 2018-05-01 15:30:28 -0700 | [diff] [blame] | 953 | } // namespace blaze |