12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
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
+
15
22
#include " src/main/cpp/rc_file.h"
16
23
17
24
#include < memory>
@@ -55,6 +62,7 @@ class RcFileTest : public ::testing::Test {
55
62
: workspace_(blaze_util::JoinPath(blaze::GetPathEnv(" TEST_TMPDIR" ),
56
63
" workspace" )),
57
64
cwd_ (blaze_util::JoinPath(blaze::GetPathEnv(" TEST_TMPDIR" ), "cwd")),
65
+ home_(blaze_util::JoinPath(blaze::GetPathEnv(" TEST_TMPDIR" ), "home")),
58
66
binary_dir_(
59
67
blaze_util::JoinPath (blaze::GetPathEnv(" TEST_TMPDIR" ), "bazeldir")),
60
68
binary_path_(blaze_util::JoinPath(binary_dir_, " bazel" )),
@@ -64,6 +72,12 @@ class RcFileTest : public ::testing::Test {
64
72
ASSERT_TRUE (blaze_util::MakeDirectories (workspace_, 0755 ));
65
73
ASSERT_TRUE (blaze_util::MakeDirectories (cwd_, 0755 ));
66
74
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
67
81
#if defined(_WIN32) || defined(__CYGWIN__)
68
82
// GetCwd returns a short path on Windows, so we store this expectation now
69
83
// to keep assertions sane in the tests.
@@ -97,6 +111,10 @@ class RcFileTest : public ::testing::Test {
97
111
for (const std::string& file : files) {
98
112
blaze_util::UnlinkPath (file);
99
113
}
114
+ blaze_util::GetAllFilesUnder (home_, &files);
115
+ for (const std::string& file : files) {
116
+ blaze_util::UnlinkPath (file);
117
+ }
100
118
blaze_util::GetAllFilesUnder (binary_dir_, &files);
101
119
for (const std::string& file : files) {
102
120
blaze_util::UnlinkPath (file);
@@ -126,8 +144,16 @@ class RcFileTest : public ::testing::Test {
126
144
return false ;
127
145
}
128
146
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
+ }
131
157
132
158
bool SetUpLegacyMasterRcFileInWorkspace (const std::string& contents,
133
159
std::string* rcfile_path) const {
@@ -155,6 +181,7 @@ class RcFileTest : public ::testing::Test {
155
181
156
182
const std::string workspace_;
157
183
std::string cwd_;
184
+ const std::string home_;
158
185
const std::string binary_dir_;
159
186
const std::string binary_path_;
160
187
const std::unique_ptr<WorkspaceLayout> workspace_layout_;
@@ -169,6 +196,8 @@ TEST_F(GetRcFileTest, GetRcFilesLoadsAllDefaultBazelrcs) {
169
196
ASSERT_TRUE (SetUpSystemRcFile (" " , &system_rc));
170
197
std::string workspace_rc;
171
198
ASSERT_TRUE (SetUpWorkspaceRcFile (" " , &workspace_rc));
199
+ std::string home_rc;
200
+ ASSERT_TRUE (SetUpHomeRcFile (" " , &home_rc));
172
201
173
202
const CommandLine cmd_line = CommandLine (binary_path_, {}, " build" , {});
174
203
std::string error = " check that this string is not modified" ;
@@ -183,14 +212,17 @@ TEST_F(GetRcFileTest, GetRcFilesLoadsAllDefaultBazelrcs) {
183
212
// is not passed and therefore is not relevant.
184
213
EXPECT_THAT (parsed_rcs,
185
214
ElementsAre (Pointee (CanonicalSourcePathsAre (system_rc)),
186
- Pointee (CanonicalSourcePathsAre (workspace_rc))));
215
+ Pointee (CanonicalSourcePathsAre (workspace_rc)),
216
+ Pointee (CanonicalSourcePathsAre (home_rc))));
187
217
}
188
218
189
219
TEST_F (GetRcFileTest, GetRcFilesRespectsNoSystemRc) {
190
220
std::string system_rc;
191
221
ASSERT_TRUE (SetUpSystemRcFile (" " , &system_rc));
192
222
std::string workspace_rc;
193
223
ASSERT_TRUE (SetUpWorkspaceRcFile (" " , &workspace_rc));
224
+ std::string home_rc;
225
+ ASSERT_TRUE (SetUpHomeRcFile (" " , &home_rc));
194
226
195
227
const CommandLine cmd_line =
196
228
CommandLine (binary_path_, {" --nosystem_rc" }, " build" , {});
@@ -203,14 +235,17 @@ TEST_F(GetRcFileTest, GetRcFilesRespectsNoSystemRc) {
203
235
EXPECT_EQ (" check that this string is not modified" , error);
204
236
205
237
EXPECT_THAT (parsed_rcs,
206
- ElementsAre (Pointee (CanonicalSourcePathsAre (workspace_rc))));
238
+ ElementsAre (Pointee (CanonicalSourcePathsAre (workspace_rc)),
239
+ Pointee (CanonicalSourcePathsAre (home_rc))));
207
240
}
208
241
209
242
TEST_F (GetRcFileTest, GetRcFilesRespectsNoWorkspaceRc) {
210
243
std::string system_rc;
211
244
ASSERT_TRUE (SetUpSystemRcFile (" " , &system_rc));
212
245
std::string workspace_rc;
213
246
ASSERT_TRUE (SetUpWorkspaceRcFile (" " , &workspace_rc));
247
+ std::string home_rc;
248
+ ASSERT_TRUE (SetUpHomeRcFile (" " , &home_rc));
214
249
215
250
const CommandLine cmd_line =
216
251
CommandLine (binary_path_, {" --noworkspace_rc" }, " build" , {});
@@ -223,17 +258,20 @@ TEST_F(GetRcFileTest, GetRcFilesRespectsNoWorkspaceRc) {
223
258
EXPECT_EQ (" check that this string is not modified" , error);
224
259
225
260
EXPECT_THAT (parsed_rcs,
226
- ElementsAre (Pointee (CanonicalSourcePathsAre (system_rc))));
261
+ ElementsAre (Pointee (CanonicalSourcePathsAre (system_rc)),
262
+ Pointee (CanonicalSourcePathsAre (home_rc))));
227
263
}
228
264
229
- TEST_F (GetRcFileTest, GetRcFilesRespectsNoWorkspaceRcAndNoSystemCombined ) {
265
+ TEST_F (GetRcFileTest, GetRcFilesRespectsNoWorkspaceRcAndNoSystemAndNoHomeRcCombined ) {
230
266
std::string system_rc;
231
267
ASSERT_TRUE (SetUpSystemRcFile (" " , &system_rc));
232
268
std::string workspace_rc;
233
269
ASSERT_TRUE (SetUpWorkspaceRcFile (" " , &workspace_rc));
270
+ std::string home_rc;
271
+ ASSERT_TRUE (SetUpHomeRcFile (" " , &home_rc));
234
272
235
273
const CommandLine cmd_line = CommandLine (
236
- binary_path_, {" --noworkspace_rc" , " --nosystem_rc" }, " build" , {});
274
+ binary_path_, {" --noworkspace_rc" , " --nosystem_rc" , " --nohome_rc " }, " build" , {});
237
275
std::string error = " check that this string is not modified" ;
238
276
std::vector<std::unique_ptr<RcFile>> parsed_rcs;
239
277
const blaze_exit_code::ExitCode exit_code =
0 commit comments