Skip to content

Commit 3d3edf0

Browse files
committed
DRY
1 parent 13bae8c commit 3d3edf0

File tree

8 files changed

+62
-25
lines changed

8 files changed

+62
-25
lines changed

src/crosscheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::Path;
33
use crate::{
44
cache::Cache,
55
config::Config,
6-
ownership::for_file_fast::find_file_owners,
6+
ownership::file_owner_resolver::find_file_owners,
77
project::Project,
88
project_builder::ProjectBuilder,
99
runner::{RunConfig, RunResult, config_from_path, team_for_file_from_codeowners},

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub(crate) mod common_test;
33
pub mod config;
44
pub mod crosscheck;
55
pub mod ownership;
6+
pub mod path_utils;
67
pub(crate) mod project;
78
pub mod project_builder;
89
pub mod project_file_builder;

src/ownership.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) mod codeowners_file_parser;
1313
pub(crate) mod codeowners_query;
1414
mod file_generator;
1515
mod file_owner_finder;
16-
pub mod for_file_fast;
16+
pub mod file_owner_resolver;
1717
pub(crate) mod mapper;
1818
mod validator;
1919

src/ownership/codeowners_query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(crate) fn team_for_file_from_codeowners(
1111
file_path: &Path,
1212
) -> Result<Option<Team>, String> {
1313
let relative_file_path = if file_path.is_absolute() {
14-
file_path.strip_prefix(project_root).unwrap_or(file_path).to_path_buf()
14+
crate::path_utils::relative_to_buf(project_root, file_path)
1515
} else {
1616
PathBuf::from(file_path)
1717
};
@@ -36,7 +36,7 @@ pub(crate) fn teams_for_files_from_codeowners(
3636
.map(Path::new)
3737
.map(|path| {
3838
if path.is_absolute() {
39-
path.strip_prefix(project_root).unwrap_or(path).to_path_buf()
39+
crate::path_utils::relative_to_buf(project_root, path)
4040
} else {
4141
path.to_path_buf()
4242
}
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ pub fn find_file_owners(project_root: &Path, config: &Config, file_path: &Path)
1717
} else {
1818
project_root.join(file_path)
1919
};
20-
let relative_file_path = absolute_file_path
21-
.strip_prefix(project_root)
22-
.unwrap_or(&absolute_file_path)
23-
.to_path_buf();
20+
let relative_file_path = crate::path_utils::relative_to_buf(project_root, &absolute_file_path);
2421

2522
let teams = load_teams(project_root, &config.team_file_glob)?;
2623
let teams_by_name = build_teams_by_name_map(&teams);
@@ -68,7 +65,7 @@ pub fn find_file_owners(project_root: &Path, config: &Config, file_path: &Path)
6865
}
6966

7067
for team in &teams {
71-
let team_rel = team.path.strip_prefix(project_root).unwrap_or(&team.path).to_path_buf();
68+
let team_rel = crate::path_utils::relative_to_buf(project_root, &team.path);
7269
if team_rel == relative_file_path {
7370
sources_by_team.entry(team.name.clone()).or_default().push(Source::TeamYml);
7471
}
@@ -77,10 +74,7 @@ pub fn find_file_owners(project_root: &Path, config: &Config, file_path: &Path)
7774
let mut file_owners: Vec<FileOwner> = Vec::new();
7875
for (team_name, sources) in sources_by_team.into_iter() {
7976
if let Some(team) = teams_by_name.get(&team_name) {
80-
let relative_team_yml_path = team
81-
.path
82-
.strip_prefix(project_root)
83-
.unwrap_or(&team.path)
77+
let relative_team_yml_path = crate::path_utils::relative_to(project_root, &team.path)
8478
.to_string_lossy()
8579
.to_string();
8680
file_owners.push(FileOwner {
@@ -157,9 +151,7 @@ fn most_specific_directory_owner(
157151
if let Ok(owner_str) = fs::read_to_string(&codeowner_path) {
158152
let owner = owner_str.trim();
159153
if let Some(team) = teams_by_name.get(owner) {
160-
let relative_dir = current
161-
.strip_prefix(project_root)
162-
.unwrap_or(current.as_path())
154+
let relative_dir = crate::path_utils::relative_to(project_root, current.as_path())
163155
.to_string_lossy()
164156
.to_string();
165157
let candidate = (team.name.clone(), Source::Directory(relative_dir));
@@ -191,7 +183,7 @@ fn nearest_package_owner(
191183
if !current.pop() {
192184
break;
193185
}
194-
let parent_rel = current.strip_prefix(project_root).unwrap_or(current.as_path());
186+
let parent_rel = crate::path_utils::relative_to(project_root, current.as_path());
195187
if let Some(rel_str) = parent_rel.to_str() {
196188
if glob_list_matches(rel_str, &config.ruby_package_paths) {
197189
let pkg_yml = current.join("package.yml");

src/path_utils.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::path::{Path, PathBuf};
2+
3+
/// Return `path` relative to `root` if possible; otherwise return `path` unchanged.
4+
pub fn relative_to<'a>(root: &'a Path, path: &'a Path) -> &'a Path {
5+
path.strip_prefix(root).unwrap_or(path)
6+
}
7+
8+
/// Like `relative_to`, but returns an owned `PathBuf`.
9+
pub fn relative_to_buf(root: &Path, path: &Path) -> PathBuf {
10+
relative_to(root, path).to_path_buf()
11+
}
12+
13+
#[cfg(test)]
14+
mod tests {
15+
use super::*;
16+
17+
#[test]
18+
fn relative_to_returns_relative_when_under_root() {
19+
let root = Path::new("/a/b");
20+
let path = Path::new("/a/b/c/d.txt");
21+
let rel = relative_to(root, path);
22+
assert_eq!(rel, Path::new("c/d.txt"));
23+
}
24+
25+
#[test]
26+
fn relative_to_returns_input_when_not_under_root() {
27+
let root = Path::new("/a/b");
28+
let path = Path::new("/x/y/z.txt");
29+
let rel = relative_to(root, path);
30+
assert_eq!(rel, path);
31+
}
32+
33+
#[test]
34+
fn relative_to_handles_equal_paths() {
35+
let root = Path::new("/a/b");
36+
let path = Path::new("/a/b");
37+
let rel = relative_to(root, path);
38+
assert_eq!(rel, Path::new(""));
39+
}
40+
41+
#[test]
42+
fn relative_to_buf_matches_relative_to() {
43+
let root = Path::new("/proj");
44+
let path = Path::new("/proj/src/lib.rs");
45+
let rel_ref = relative_to(root, path);
46+
let rel_buf = relative_to_buf(root, path);
47+
assert_eq!(rel_ref, rel_buf.as_path());
48+
}
49+
}

src/runner.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl Runner {
176176
}
177177

178178
pub fn owners_for_file(&self, file_path: &str) -> Result<Vec<FileOwner>, Error> {
179-
use crate::ownership::for_file_fast::find_file_owners;
179+
use crate::ownership::file_owner_resolver::find_file_owners;
180180
let owners = find_file_owners(&self.run_config.project_root, &self.config, std::path::Path::new(file_path)).map_err(Error::Io)?;
181181
Ok(owners)
182182
}
@@ -215,10 +215,7 @@ impl Runner {
215215
pub fn for_file_codeowners_only(&self, file_path: &str) -> RunResult {
216216
match team_for_file_from_codeowners(&self.run_config, file_path) {
217217
Ok(Some(team)) => {
218-
let relative_team_path = team
219-
.path
220-
.strip_prefix(&self.run_config.project_root)
221-
.unwrap_or(team.path.as_path())
218+
let relative_team_path = crate::path_utils::relative_to(&self.run_config.project_root, team.path.as_path())
222219
.to_string_lossy()
223220
.to_string();
224221
RunResult {

src/runner/api.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ pub fn teams_for_files_from_codeowners(
6868
}
6969

7070
pub fn team_for_file_from_codeowners(run_config: &RunConfig, file_path: &str) -> error_stack::Result<Option<Team>, Error> {
71-
let relative_file_path = Path::new(file_path)
72-
.strip_prefix(&run_config.project_root)
73-
.unwrap_or(Path::new(file_path));
71+
let relative_file_path = crate::path_utils::relative_to(&run_config.project_root, Path::new(file_path));
7472

7573
let config = config_from_path(&run_config.config_path)?;
7674
let res = crate::ownership::codeowners_query::team_for_file_from_codeowners(

0 commit comments

Comments
 (0)