Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions src/github/codeowners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ impl TryFrom<String> for Record {
type Error = CodeOwnersEntryError;

fn try_from(value: String) -> Result<Self, Self::Error> {
let value = if let Some((i, _)) = value.chars().enumerate().find(|(_, c)| c == &'#') {
&value[0..i]
} else {
&value[..]
};
let value = value
.split_once('#')
.map_or(value.as_str(), |(line, _)| line);

let mut iter = value.split_whitespace();
if let Some(pat) = iter.next() {
Expand Down Expand Up @@ -93,6 +91,41 @@ mod tests {
owners: vec!["@foo".to_string(), "@bar".to_string()],
}),
),
(
"é# comment",
Ok(Record {
pattern: "é".to_string(),
owners: vec![],
}),
),
(
"日本語/ @owner # comment",
Ok(Record {
pattern: "日本語/".to_string(),
owners: vec!["@owner".to_string()],
}),
),
(
"* @日本語 @😀 # comment",
Ok(Record {
pattern: "*".to_string(),
owners: vec!["@日本語".to_string(), "@😀".to_string()],
}),
),
(
"😀/ # comment",
Ok(Record {
pattern: "😀/".to_string(),
owners: vec![],
}),
),
(
"日本語/ @日本語",
Ok(Record {
pattern: "日本語/".to_string(),
owners: vec!["@日本語".to_string()],
}),
),
];

for (i, (input, want)) in test_cases.into_iter().enumerate() {
Expand Down
18 changes: 18 additions & 0 deletions tests/codeowners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,21 @@ fn codeowner_debug_marks_only_last_match_effective() {
assert!(matches[2].is_effective());
assert_eq!(matches[2].owners(), &vec![String::from("@lib-team")]);
}

#[test]
fn codeowner_unicode_before_inline_comment_preserves_owners_and_precedence() {
let data = "* @default\n日本語/ @team @日本語 # comment\n日本語/非公開/ # no owners\n";
let co = CodeOwners::<()>::try_from_bufread(data.as_bytes()).unwrap();

let owners = vec![String::from("@team"), String::from("@日本語")];
assert_eq!(co.find_owners("日本語/file.rs"), Some(&owners));
assert_eq!(co.find_owners("日本語/非公開/file.rs"), Some(&vec![]));

let matches = co.debug("日本語/非公開/file.rs").collect::<Vec<_>>();
assert_eq!(matches.len(), 3);
assert!(!matches[0].is_effective());
assert!(!matches[1].is_effective());
assert_eq!(matches[1].owners(), &owners);
assert!(matches[2].is_effective());
assert!(matches[2].owners().is_empty());
}
Loading