Skip to content

Commit

Permalink
Combine WorkspaceRcFileSearchPath and FindCandidateBlazercPaths and a…
Browse files Browse the repository at this point in the history
…dd a test.

RELNOTES: None
PiperOrigin-RevId: 184865343
  • Loading branch information
aj-michael authored and Copybara-Service committed Feb 7, 2018
1 parent da4522f commit e1ed133
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/main/cpp/option_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 7 additions & 26 deletions src/main/cpp/workspace_layout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
18 changes: 7 additions & 11 deletions src/main/cpp/workspace_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
27 changes: 27 additions & 0 deletions src/test/cpp/workspace_layout_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e1ed133

Please sign in to comment.