diff options
author | ajmichael <ajmichael@google.com> | 2018-02-07 11:11:03 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-07 11:13:26 -0800 |
commit | e1ed133298cb65189fb7e2339a0920bbc4d42f07 (patch) | |
tree | a53d2b91188a723f4d6397d3086fc8bb5bac7042 | |
parent | da4522fa5bdf251a345d61ca91fb2a8708242572 (diff) |
Combine WorkspaceRcFileSearchPath and FindCandidateBlazercPaths and add a test.
RELNOTES: None
PiperOrigin-RevId: 184865343
-rw-r--r-- | src/main/cpp/option_processor.cc | 6 | ||||
-rw-r--r-- | src/main/cpp/workspace_layout.cc | 33 | ||||
-rw-r--r-- | src/main/cpp/workspace_layout.h | 18 | ||||
-rw-r--r-- | src/test/cpp/workspace_layout_test.cc | 27 |
4 files changed, 44 insertions, 40 deletions
diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc index 83a184cca5..93f1c4cd67 100644 --- a/src/main/cpp/option_processor.cc +++ b/src/main/cpp/option_processor.cc @@ -237,9 +237,9 @@ blaze_exit_code::ExitCode OptionProcessor::ParseOptions( // paths to the rc files. This list may contain duplicates. vector<string> candidate_blazerc_paths; if (use_master_blazerc) { - workspace_layout_->FindCandidateBlazercPaths( - workspace, cwd, cmd_line_->path_to_binary, cmd_line_->startup_args, - &candidate_blazerc_paths); + candidate_blazerc_paths = + workspace_layout_->FindCandidateBlazercPaths( + workspace, cwd, cmd_line_->path_to_binary, cmd_line_->startup_args); } string user_blazerc_path; diff --git a/src/main/cpp/workspace_layout.cc b/src/main/cpp/workspace_layout.cc index cc02c78113..9748cf4309 100644 --- a/src/main/cpp/workspace_layout.cc +++ b/src/main/cpp/workspace_layout.cc @@ -56,21 +56,6 @@ string WorkspaceLayout::GetPrettyWorkspaceName( return blaze_util::Basename(workspace); } -static string FindDepotBlazerc(const blaze::WorkspaceLayout* workspace_layout, - const string& workspace) { - // Package semantics are ignored here, but that's acceptable because - // blaze.blazerc is a configuration file. - vector<string> candidates; - workspace_layout->WorkspaceRcFileSearchPath(&candidates); - for (const auto& candidate : candidates) { - string blazerc = blaze_util::JoinPath(workspace, candidate); - if (blaze_util::CanReadFile(blazerc)) { - return blazerc; - } - } - return ""; -} - static string FindAlongsideBinaryBlazerc(const string& cwd, const string& path_to_binary) { // TODO(b/32115171): This doesn't work on Windows. Fix this together with the @@ -86,20 +71,16 @@ static string FindAlongsideBinaryBlazerc(const string& cwd, return ""; } -void WorkspaceLayout::FindCandidateBlazercPaths( +vector<string> WorkspaceLayout::FindCandidateBlazercPaths( const string& workspace, const string& cwd, const string& path_to_binary, - const vector<string>& startup_args, - std::vector<string>* result) const { - result->push_back(FindDepotBlazerc(this, workspace)); - result->push_back(FindAlongsideBinaryBlazerc(cwd, path_to_binary)); - result->push_back(FindSystemWideBlazerc()); -} - -void WorkspaceLayout::WorkspaceRcFileSearchPath( - vector<string>* candidates) const { - candidates->push_back("tools/bazel.rc"); + const vector<string>& startup_args) const { + return { + blaze_util::JoinPath(workspace, "tools/bazel.rc"), + FindAlongsideBinaryBlazerc(cwd, path_to_binary), + FindSystemWideBlazerc(), + }; } bool WorkspaceLayout::WorkspaceRelativizeRcFilePath(const string &workspace, diff --git a/src/main/cpp/workspace_layout.h b/src/main/cpp/workspace_layout.h index a8dd6d1ace..edb2c292e1 100644 --- a/src/main/cpp/workspace_layout.h +++ b/src/main/cpp/workspace_layout.h @@ -48,23 +48,19 @@ class WorkspaceLayout { // Returns if workspace is a valid build workspace. virtual bool InWorkspace(const std::string& workspace) const; - // Returns the candidate pathnames for the RC files. - virtual void FindCandidateBlazercPaths( + // Returns the candidate master RC file absolute paths. + // All readable files from the result will be used. Empty or nonexistant files + // will be ignored. It is ok if no usable candidate exists. + virtual std::vector<std::string> FindCandidateBlazercPaths( const std::string& workspace, const std::string& cwd, const std::string& path_to_binary, - const std::vector<std::string>& startup_args, - std::vector<std::string>* result) const; - - // Returns the candidate pathnames for the RC file in the workspace, - // the first readable one of which will be chosen. - // It is ok if no usable candidate exists. - virtual void WorkspaceRcFileSearchPath(std::vector<std::string>* candidates) - const; + const std::vector<std::string>& startup_args) const; // Turn a %workspace%-relative import into its true name in the filesystem. // path_fragment is modified in place. - // Unlike WorkspaceRcFileSearchPath, it is an error if no import file exists. + // Unlike FindCandidateBlazercPaths, it is an error if no import file + // exists. virtual bool WorkspaceRelativizeRcFilePath(const std::string& workspace, std::string* path_fragment) const; diff --git a/src/test/cpp/workspace_layout_test.cc b/src/test/cpp/workspace_layout_test.cc index e2a68eef22..781a54ebbe 100644 --- a/src/test/cpp/workspace_layout_test.cc +++ b/src/test/cpp/workspace_layout_test.cc @@ -67,4 +67,31 @@ TEST_F(WorkspaceLayoutTest, GetWorkspace) { ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(cwd)); } +TEST_F(WorkspaceLayoutTest, FindCandidateBlazercPaths) { + const std::string binary_dir = blaze_util::JoinPath(build_root_, "bazeldir"); + const std::string tools_dir = blaze_util::JoinPath(build_root_, "tools"); + const std::string workspace_rc_path = + blaze_util::JoinPath(build_root_, "tools/bazel.rc"); + const std::string binary_rc_path = + blaze_util::JoinPath(binary_dir, "bazel.bazelrc"); + ASSERT_TRUE(blaze_util::MakeDirectories(binary_dir, 0755)); + ASSERT_TRUE(blaze_util::MakeDirectories(tools_dir, 0755)); + ASSERT_TRUE(blaze_util::WriteFile("", workspace_rc_path, 0755)); + ASSERT_TRUE(blaze_util::WriteFile("", binary_rc_path, 0755)); + + std::vector<std::string> expected = {workspace_rc_path, binary_rc_path}; + std::vector<std::string> actual = + workspace_layout_->FindCandidateBlazercPaths( + build_root_, build_root_, "bazeldir/bazel", {}); + // The third entry is the system wide blazerc path, /etc/bazel.bazelrc, which + // we do not mock within this test because it is not within the sandbox. It + // may or may not exist on the system running the test, so we do not check for + // it. + // TODO(https://github.com/bazelbuild/bazel/issues/4502): Make the system-wide + // master bazelrc location configurable and add test coverage for it. + std::vector<std::string> actual_first_two_entries(actual.begin(), + actual.begin() + 2); + ASSERT_EQ(expected, actual_first_two_entries); +} + } // namespace blaze |