-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfile_generator.rs
More file actions
203 lines (187 loc) · 6.67 KB
/
file_generator.rs
File metadata and controls
203 lines (187 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::cmp::Ordering;
use super::{Entry, Mapper};
pub struct FileGenerator {
pub mappers: Vec<Box<dyn Mapper>>,
}
impl FileGenerator {
pub fn generate_file(&self) -> String {
let mut lines: Vec<String> = Vec::new();
lines.append(&mut Self::disclaimer());
for mapper in &self.mappers {
if mapper.entries().is_empty() {
continue;
}
lines.push(format!("# {}", mapper.name()));
lines.append(&mut Self::to_sorted_lines(&mapper.entries()));
lines.push("".to_owned());
}
lines.join("\n")
}
pub fn disclaimer() -> Vec<String> {
[
"# STOP! - DO NOT EDIT THIS FILE MANUALLY",
"# This file was automatically generated by \"bin/codeownership validate\".",
"#",
"# CODEOWNERS is used for GitHub to suggest code/file owners to various GitHub",
"# teams. This is useful when developers create Pull Requests since the",
"# code/file owner is notified. Reference GitHub docs for more details:",
"# https://help.github.com/en/articles/about-code-owners",
"",
"",
]
.iter()
.map(|line| line.to_string())
.collect()
}
fn to_sorted_lines(entries: &[Entry]) -> Vec<String> {
let mut lines: Vec<String> = entries.iter().map(|entry| entry.to_row()).collect();
lines.sort_by(compare_lines);
lines
}
}
pub fn compare_lines(a: &String, b: &String) -> Ordering {
if let Some((prefix, _)) = a.split_once("**") {
if b.starts_with(prefix) {
return Ordering::Less;
}
}
if let Some((prefix, _)) = b.split_once("**") {
if a.starts_with(prefix) {
return Ordering::Greater;
}
}
a.cmp(b)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_to_sorted_lines_with_special_characters() {
// The `(` character is less than `*` in the default sort order.
let mut illustrate_issue = vec!["*".to_string(), "(".to_string()];
illustrate_issue.sort();
assert_eq!(illustrate_issue, vec!["(".to_string(), "*".to_string()]);
let entries = vec![
Entry {
// Problem is when a dir name starts with `(`.
path: "directory/owner/(my_folder)/**/**".to_string(),
github_team: "@foo".to_string(),
team_name: "footeam".to_string(),
disabled: false,
},
Entry {
// Another example of a dir starting with `(`.
path: "directory/owner/(my_folder)/without_glob".to_string(),
github_team: "@zoo".to_string(),
team_name: "zooteam".to_string(),
disabled: false,
},
Entry {
// And is compared to a glob that starts with `*`.
path: "directory/owner/**".to_string(),
github_team: "@bar".to_string(),
team_name: "barteam".to_string(),
disabled: false,
},
Entry {
path: "directory/owner/my_folder/**".to_string(),
github_team: "@baz".to_string(),
team_name: "bazteam".to_string(),
disabled: false,
},
Entry {
path: "directory/**".to_string(),
github_team: "@bop".to_string(),
team_name: "bopteam".to_string(),
disabled: false,
},
];
let sorted = FileGenerator::to_sorted_lines(&entries);
assert_eq!(
sorted,
vec![
"/directory/** @bop",
"/directory/owner/** @bar",
"/directory/owner/(my_folder)/**/** @foo",
"/directory/owner/(my_folder)/without_glob @zoo",
"/directory/owner/my_folder/** @baz"
]
);
}
#[test]
fn test_basic_sorting() {
let entries = vec![
Entry {
path: "b_directory/owner/**".to_string(),
github_team: "@bar".to_string(),
team_name: "barteam".to_string(),
disabled: false,
},
Entry {
path: "a_directory/owner/**".to_string(),
github_team: "@foo".to_string(),
team_name: "footeam".to_string(),
disabled: false,
},
];
let sorted = FileGenerator::to_sorted_lines(&entries);
assert_eq!(sorted, vec!["/a_directory/owner/** @foo", "/b_directory/owner/** @bar"]);
}
#[test]
fn test_sorting_with_mixed_case() {
let entries = vec![
Entry {
path: "directory/Owner/**".to_string(),
github_team: "@bar".to_string(),
team_name: "barteam".to_string(),
disabled: false,
},
Entry {
path: "directory/owner/**".to_string(),
github_team: "@foo".to_string(),
team_name: "footeam".to_string(),
disabled: false,
},
];
let sorted = FileGenerator::to_sorted_lines(&entries);
assert_eq!(sorted, vec!["/directory/Owner/** @bar", "/directory/owner/** @foo"]);
}
#[test]
fn test_sorting_with_numbers() {
let entries = vec![
Entry {
path: "directory/owner1/**".to_string(),
github_team: "@foo".to_string(),
team_name: "footeam".to_string(),
disabled: false,
},
Entry {
path: "directory/owner2/**".to_string(),
github_team: "@bar".to_string(),
team_name: "barteam".to_string(),
disabled: false,
},
];
let sorted = FileGenerator::to_sorted_lines(&entries);
assert_eq!(sorted, vec!["/directory/owner1/** @foo", "/directory/owner2/** @bar"]);
}
#[test]
fn test_sorting_with_special_characters() {
let entries = vec![
Entry {
path: "directory/owner-1/**".to_string(),
github_team: "@foo".to_string(),
team_name: "footeam".to_string(),
disabled: false,
},
Entry {
path: "directory/owner_2/**".to_string(),
github_team: "@bar".to_string(),
team_name: "barteam".to_string(),
disabled: false,
},
];
let sorted = FileGenerator::to_sorted_lines(&entries);
assert_eq!(sorted, vec!["/directory/owner-1/** @foo", "/directory/owner_2/** @bar"]);
}
}