Skip to content

Commit 52d61f3

Browse files
committed
Improve rc_file_test to correctly handle --home_rc.
This also fixes an issue where the test would fail when $HOME/.bazelrc would exist and the user ran the test with --test_env=HOME.
1 parent 0c249d5 commit 52d61f3

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

src/test/cpp/rc_file_test.cc

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

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+
1522
#include "src/main/cpp/rc_file.h"
1623

1724
#include <memory>
@@ -55,6 +62,7 @@ class RcFileTest : public ::testing::Test {
5562
: workspace_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"),
5663
"workspace")),
5764
cwd_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "cwd")),
65+
home_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "home")),
5866
binary_dir_(
5967
blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "bazeldir")),
6068
binary_path_(blaze_util::JoinPath(binary_dir_, "bazel")),
@@ -64,6 +72,12 @@ class RcFileTest : public ::testing::Test {
6472
ASSERT_TRUE(blaze_util::MakeDirectories(workspace_, 0755));
6573
ASSERT_TRUE(blaze_util::MakeDirectories(cwd_, 0755));
6674
ASSERT_TRUE(blaze_util::ChangeDirectory(cwd_));
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
6781
#if defined(_WIN32) || defined(__CYGWIN__)
6882
// GetCwd returns a short path on Windows, so we store this expectation now
6983
// to keep assertions sane in the tests.
@@ -97,6 +111,10 @@ class RcFileTest : public ::testing::Test {
97111
for (const std::string& file : files) {
98112
blaze_util::UnlinkPath(file);
99113
}
114+
blaze_util::GetAllFilesUnder(home_, &files);
115+
for (const std::string& file : files) {
116+
blaze_util::UnlinkPath(file);
117+
}
100118
blaze_util::GetAllFilesUnder(binary_dir_, &files);
101119
for (const std::string& file : files) {
102120
blaze_util::UnlinkPath(file);
@@ -126,8 +144,16 @@ class RcFileTest : public ::testing::Test {
126144
return false;
127145
}
128146

129-
// TODO(b/36168162): Make it possible to configure the home directory so we
130-
// can test --home_rc as well.
147+
bool SetUpHomeRcFile(const std::string& contents,
148+
std::string* rcfile_path) const {
149+
const std::string home_rc_path =
150+
blaze_util::JoinPath(home_, ".bazelrc");
151+
if (blaze_util::WriteFile(contents, home_rc_path, 0755)) {
152+
*rcfile_path = blaze_util::MakeCanonical(home_rc_path.c_str());
153+
return true;
154+
}
155+
return false;
156+
}
131157

132158
bool SetUpLegacyMasterRcFileInWorkspace(const std::string& contents,
133159
std::string* rcfile_path) const {
@@ -155,6 +181,7 @@ class RcFileTest : public ::testing::Test {
155181

156182
const std::string workspace_;
157183
std::string cwd_;
184+
const std::string home_;
158185
const std::string binary_dir_;
159186
const std::string binary_path_;
160187
const std::unique_ptr<WorkspaceLayout> workspace_layout_;
@@ -169,6 +196,8 @@ TEST_F(GetRcFileTest, GetRcFilesLoadsAllDefaultBazelrcs) {
169196
ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
170197
std::string workspace_rc;
171198
ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
199+
std::string home_rc;
200+
ASSERT_TRUE(SetUpHomeRcFile("", &home_rc));
172201

173202
const CommandLine cmd_line = CommandLine(binary_path_, {}, "build", {});
174203
std::string error = "check that this string is not modified";
@@ -183,14 +212,17 @@ TEST_F(GetRcFileTest, GetRcFilesLoadsAllDefaultBazelrcs) {
183212
// is not passed and therefore is not relevant.
184213
EXPECT_THAT(parsed_rcs,
185214
ElementsAre(Pointee(CanonicalSourcePathsAre(system_rc)),
186-
Pointee(CanonicalSourcePathsAre(workspace_rc))));
215+
Pointee(CanonicalSourcePathsAre(workspace_rc)),
216+
Pointee(CanonicalSourcePathsAre(home_rc))));
187217
}
188218

189219
TEST_F(GetRcFileTest, GetRcFilesRespectsNoSystemRc) {
190220
std::string system_rc;
191221
ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
192222
std::string workspace_rc;
193223
ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
224+
std::string home_rc;
225+
ASSERT_TRUE(SetUpHomeRcFile("", &home_rc));
194226

195227
const CommandLine cmd_line =
196228
CommandLine(binary_path_, {"--nosystem_rc"}, "build", {});
@@ -203,14 +235,17 @@ TEST_F(GetRcFileTest, GetRcFilesRespectsNoSystemRc) {
203235
EXPECT_EQ("check that this string is not modified", error);
204236

205237
EXPECT_THAT(parsed_rcs,
206-
ElementsAre(Pointee(CanonicalSourcePathsAre(workspace_rc))));
238+
ElementsAre(Pointee(CanonicalSourcePathsAre(workspace_rc)),
239+
Pointee(CanonicalSourcePathsAre(home_rc))));
207240
}
208241

209242
TEST_F(GetRcFileTest, GetRcFilesRespectsNoWorkspaceRc) {
210243
std::string system_rc;
211244
ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
212245
std::string workspace_rc;
213246
ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
247+
std::string home_rc;
248+
ASSERT_TRUE(SetUpHomeRcFile("", &home_rc));
214249

215250
const CommandLine cmd_line =
216251
CommandLine(binary_path_, {"--noworkspace_rc"}, "build", {});
@@ -223,17 +258,20 @@ TEST_F(GetRcFileTest, GetRcFilesRespectsNoWorkspaceRc) {
223258
EXPECT_EQ("check that this string is not modified", error);
224259

225260
EXPECT_THAT(parsed_rcs,
226-
ElementsAre(Pointee(CanonicalSourcePathsAre(system_rc))));
261+
ElementsAre(Pointee(CanonicalSourcePathsAre(system_rc)),
262+
Pointee(CanonicalSourcePathsAre(home_rc))));
227263
}
228264

229-
TEST_F(GetRcFileTest, GetRcFilesRespectsNoWorkspaceRcAndNoSystemCombined) {
265+
TEST_F(GetRcFileTest, GetRcFilesRespectsNoWorkspaceRcAndNoSystemAndNoHomeRcCombined) {
230266
std::string system_rc;
231267
ASSERT_TRUE(SetUpSystemRcFile("", &system_rc));
232268
std::string workspace_rc;
233269
ASSERT_TRUE(SetUpWorkspaceRcFile("", &workspace_rc));
270+
std::string home_rc;
271+
ASSERT_TRUE(SetUpHomeRcFile("", &home_rc));
234272

235273
const CommandLine cmd_line = CommandLine(
236-
binary_path_, {"--noworkspace_rc", "--nosystem_rc"}, "build", {});
274+
binary_path_, {"--noworkspace_rc", "--nosystem_rc", "--nohome_rc"}, "build", {});
237275
std::string error = "check that this string is not modified";
238276
std::vector<std::unique_ptr<RcFile>> parsed_rcs;
239277
const blaze_exit_code::ExitCode exit_code =

0 commit comments

Comments
 (0)