Skip to content

Commit 2fdee9d

Browse files
authored
escaping brackets in team file CODEOWNERS entries (#37)
1 parent 210252a commit 2fdee9d

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

src/common_test.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,20 @@ team_file_glob:
231231

232232
let test_config = TestConfig::new(
233233
temp_dir.path().to_path_buf(),
234-
vec![TestProjectFile {
235-
relative_path: "config/teams/baz.yml".to_owned(),
236-
content: "name: Baz\ngithub:\n team: \"@Baz\"\n members:\n - Baz member\nowned_globs:\n - \"packs/bar/**\"\n"
237-
.to_owned(),
238-
}],
234+
vec![
235+
TestProjectFile {
236+
relative_path: "packs/jscomponents/comp.ts".to_owned(),
237+
content: "// @team Foo\n".to_owned(),
238+
},
239+
TestProjectFile {
240+
relative_path: "packs/[admin]/comp.ts".to_owned(),
241+
content: "// @team Bar\n".to_owned(),
242+
},
243+
TestProjectFile {
244+
relative_path: "packs/bar/comp.rb".to_owned(),
245+
content: "// @team Bar\n".to_owned(),
246+
},
247+
],
239248
);
240249
build_ownership(test_config)
241250
}

src/ownership/mapper.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
};
77

88
pub(crate) mod directory_mapper;
9+
mod escaper;
910
mod package_mapper;
1011
mod team_file_mapper;
1112
mod team_gem_mapper;

src/ownership/mapper/directory_mapper.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::sync::Arc;
22

3+
use super::escaper::escape_brackets;
34
use super::{Entry, Source};
45
use super::{Mapper, OwnerMatcher};
56
use crate::project::Project;
@@ -54,10 +55,6 @@ impl Mapper for DirectoryMapper {
5455
}
5556
}
5657

57-
fn escape_brackets(path: &str) -> String {
58-
path.replace("[", "\\[").replace("]", "\\]")
59-
}
60-
6158
#[cfg(test)]
6259
mod tests {
6360
use std::error::Error;

src/ownership/mapper/escaper.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn escape_brackets(path: &str) -> String {
2+
path.replace("[", "\\[").replace("]", "\\]")
3+
}

src/ownership/mapper/team_file_mapper.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashMap;
22
use std::path::PathBuf;
33
use std::sync::Arc;
44

5+
use super::escaper::escape_brackets;
56
use super::Entry;
67
use super::{Mapper, OwnerMatcher};
78
use crate::ownership::mapper::Source;
@@ -30,7 +31,7 @@ impl Mapper for TeamFileMapper {
3031
let relative_path = self.project.relative_path(&owned_file.path);
3132

3233
entries.push(Entry {
33-
path: relative_path.to_string_lossy().to_string(),
34+
path: escape_brackets(&relative_path.to_string_lossy()),
3435
github_team: team.github_team.to_owned(),
3536
team_name: team.name.to_owned(),
3637
disabled: team.avoid_ownership,
@@ -70,26 +71,33 @@ impl Mapper for TeamFileMapper {
7071
mod tests {
7172
use std::error::Error;
7273

73-
use crate::common_test::tests::{build_ownership_with_all_mappers, build_ownership_with_team_file_codeowners, vecs_match};
74+
use crate::common_test::tests::{build_ownership_with_team_file_codeowners, vecs_match};
7475

7576
use super::*;
7677
#[test]
7778
fn test_entries() -> Result<(), Box<dyn Error>> {
78-
let ownership = build_ownership_with_all_mappers()?;
79+
let ownership = build_ownership_with_team_file_codeowners()?;
7980
let mapper = TeamFileMapper::build(ownership.project.clone());
81+
dbg!(&mapper.entries());
8082
vecs_match(
8183
&mapper.entries(),
8284
&vec![
85+
Entry {
86+
path: "packs/\\[admin\\]/comp.ts".to_owned(),
87+
github_team: "@Bar".to_owned(),
88+
team_name: "Bar".to_owned(),
89+
disabled: false,
90+
},
8391
Entry {
8492
path: "packs/jscomponents/comp.ts".to_owned(),
8593
github_team: "@Foo".to_owned(),
8694
team_name: "Foo".to_owned(),
8795
disabled: false,
8896
},
8997
Entry {
90-
path: "packs/zebra/app/services/team_file_owned.rb".to_owned(),
91-
github_team: "@Foo".to_owned(),
92-
team_name: "Foo".to_owned(),
98+
path: "packs/bar/comp.rb".to_owned(),
99+
github_team: "@Bar".to_owned(),
100+
team_name: "Bar".to_owned(),
93101
disabled: false,
94102
},
95103
],
@@ -102,7 +110,14 @@ mod tests {
102110
let ownership = build_ownership_with_team_file_codeowners()?;
103111
let mapper = TeamFileMapper::build(ownership.project.clone());
104112
let owner_matchers = mapper.owner_matchers();
105-
let expected_owner_matchers = vec![OwnerMatcher::ExactMatches(HashMap::new(), Source::TeamFile)];
113+
let expected_owner_matchers = vec![OwnerMatcher::ExactMatches(
114+
HashMap::from([
115+
(PathBuf::from("packs/[admin]/comp.ts"), "Bar".to_owned()),
116+
(PathBuf::from("packs/bar/comp.rb"), "Bar".to_owned()),
117+
(PathBuf::from("packs/jscomponents/comp.ts"), "Foo".to_owned()),
118+
]),
119+
Source::TeamFile,
120+
)];
106121
assert_eq!(owner_matchers, expected_owner_matchers);
107122
Ok(())
108123
}

0 commit comments

Comments
 (0)