-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon_test.rs
338 lines (311 loc) · 13.1 KB
/
common_test.rs
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#[cfg(test)]
pub mod tests {
use std::{
error::Error,
fs::{self, File},
path::PathBuf,
};
use indoc::indoc;
use tempfile::tempdir;
use crate::{
cache::{Cache, noop::NoopCache},
config::Config,
ownership::Ownership,
project_builder::ProjectBuilder,
};
macro_rules! ownership {
($($test_files:expr),+) => {{
let temp_dir = tempdir()?;
let test_config = TestConfig::new(
temp_dir.path().to_path_buf(),
vec![$($test_files),+]
);
build_ownership(test_config)
}};
}
const DEFAULT_CODE_OWNERSHIP_YML: &str = indoc! {"
---
owned_globs:
- \"{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx,json,yml}\"
unowned_globs:
- config/code_ownership.yml
javascript_package_paths:
- javascript/packages/**
vendored_gems_path: gems
team_file_glob:
- config/teams/**/*.yml
"};
#[derive(Debug)]
pub struct TestConfig {
pub temp_dir_path: PathBuf,
pub team_names: Vec<String>,
pub files: Vec<TestProjectFile>,
pub code_ownership_config_yml: String,
pub relative_code_ownership_config_yml_path: String,
pub relative_teams_path: String,
pub generate_codeowners: bool,
}
impl Default for TestConfig {
fn default() -> Self {
Self {
code_ownership_config_yml: DEFAULT_CODE_OWNERSHIP_YML.to_owned(),
relative_code_ownership_config_yml_path: "config/code_ownership.yml".to_owned(),
relative_teams_path: "config/teams".to_owned(),
generate_codeowners: true,
temp_dir_path: PathBuf::default(),
team_names: vec!["Bar".to_owned(), "Foo".to_owned(), "Baz".to_owned(), "Bam".to_owned()],
files: vec![],
}
}
}
impl TestConfig {
pub fn new(temp_dir_path: PathBuf, files: Vec<TestProjectFile>) -> Self {
Self {
temp_dir_path,
files,
..Default::default()
}
}
}
#[derive(Debug, Default)]
pub struct TestProjectFile {
pub relative_path: String,
pub content: String,
}
pub fn build_ownership(test_config: TestConfig) -> Result<Ownership, Box<dyn Error>> {
fs::create_dir_all(test_config.temp_dir_path.join(".github"))?;
fs::create_dir_all(test_config.temp_dir_path.join(&test_config.relative_teams_path))?;
fs::write(
test_config.temp_dir_path.join(&test_config.relative_code_ownership_config_yml_path),
test_config.code_ownership_config_yml,
)?;
let relative_teams_path = &test_config.relative_teams_path;
for name in test_config.team_names.iter() {
let team_yml = format!("name: {}\ngithub:\n team: \"@{}\"\n members:\n - {}member\n", name, name, name);
fs::write(
test_config
.temp_dir_path
.join(relative_teams_path)
.join(format!("{}.yml", name.to_lowercase())),
team_yml,
)?;
}
for project_file in test_config.files.iter() {
if let Some(parent_dir) = PathBuf::from(&project_file.relative_path).parent() {
fs::create_dir_all(test_config.temp_dir_path.join(parent_dir))?;
}
fs::write(test_config.temp_dir_path.join(&project_file.relative_path), &project_file.content)?;
}
let config_file = File::open(test_config.temp_dir_path.join(test_config.relative_code_ownership_config_yml_path))?;
let config: Config = serde_yaml::from_reader(config_file)?;
let codeowners_file_path = &test_config.temp_dir_path.join(".github/CODEOWNERS");
let cache: Cache = NoopCache::default().into();
let mut builder = ProjectBuilder::new(&config, test_config.temp_dir_path.clone(), codeowners_file_path.clone(), &cache);
let project = builder.build()?;
let ownership = Ownership::build(project);
if test_config.generate_codeowners {
std::fs::write(codeowners_file_path, ownership.generate_file())?;
}
// rebuild project to ensure new codeowners file is read
let mut builder = ProjectBuilder::new(&config, test_config.temp_dir_path.clone(), codeowners_file_path.clone(), &cache);
let project = builder.build()?;
Ok(Ownership::build(project))
}
pub fn build_ownership_with_directory_codeowners() -> Result<Ownership, Box<dyn Error>> {
ownership!(
TestProjectFile {
relative_path: "app/consumers/deep/nesting/nestdir/deep_file.rb".to_owned(),
content: "class DeepFile\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "app/consumers/one_owner.rb".to_owned(),
content: "class OneOwner\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "app/services/service_file.rb".to_owned(),
content: "class ServiceFile\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "app/services/some_other_file.rb".to_owned(),
content: "class SomeOtherFile\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "app/consumers/.codeowner".to_owned(),
content: "Bar\n".to_owned(),
},
TestProjectFile {
relative_path: "app/services/.codeowner".to_owned(),
content: "Foo\n".to_owned(),
},
TestProjectFile {
relative_path: "app/services/exciting/.codeowner".to_owned(),
content: "Bar\n".to_owned(),
}
)
}
pub fn build_ownership_with_directory_codeowners_with_brackets() -> Result<Ownership, Box<dyn Error>> {
ownership!(
TestProjectFile {
relative_path: "app/[consumers]/deep/nesting/[nestdir]/deep_file.rb".to_owned(),
content: "class DeepFile\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "app/[consumers]/one_owner.rb".to_owned(),
content: "class OneOwner\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "app/services/service_file.rb".to_owned(),
content: "class ServiceFile\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "app/services/some_other_file.rb".to_owned(),
content: "class SomeOtherFile\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "app/[consumers]/.codeowner".to_owned(),
content: "Bar\n".to_owned(),
},
TestProjectFile {
relative_path: "app/[consumers]/deep/nesting/[nestdir]/.codeowner".to_owned(),
content: "Foo\n".to_owned(),
}
)
}
pub fn build_ownership_with_all_mappers() -> Result<Ownership, Box<dyn Error>> {
ownership!(
TestProjectFile {
relative_path: "app/consumers/directory_owned.rb".to_owned(),
content: "class DirectoryOwned\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "app/consumers/.codeowner".to_owned(),
content: "Bar\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/foo/package.yml".to_owned(),
content: "owner: Baz\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/foo/app/services/package_owned.rb".to_owned(),
content: "class PackageOwned\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/bar/app/services/team_file_owned.rb".to_owned(),
content: "class GlobMapperOwned\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "config/teams/baz.yml".to_owned(),
content: "name: Baz\ngithub:\n team: \"@Baz\"\n members:\n - Baz member\nowned_globs:\n - \"packs/bar/**\"\n"
.to_owned(),
},
TestProjectFile {
relative_path: "packs/zebra/app/services/team_file_owned.rb".to_owned(),
content: "# @team Foo\nclass TeamFileOwned\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/jscomponents/comp.ts".to_owned(),
content: "// @team Foo\n".to_owned(),
},
TestProjectFile {
relative_path: "gems/taco/sauce.rb".to_owned(),
content: "class Taco::Sauce\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "config/teams/bam.yml".to_owned(),
content: "name: Bam\ngithub:\n team: \"@Bam\"\n members:\n - Bam member\nruby:\n owned_gems:\n - taco\n"
.to_owned(),
}
)
}
pub fn build_ownership_with_team_file_codeowners() -> Result<Ownership, Box<dyn Error>> {
let temp_dir = tempdir()?;
let test_config = TestConfig::new(
temp_dir.path().to_path_buf(),
vec![
TestProjectFile {
relative_path: "packs/jscomponents/comp.ts".to_owned(),
content: "// @team Foo\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/[admin]/comp.ts".to_owned(),
content: "// @team Bar\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/bar/comp.rb".to_owned(),
content: "// @team Bar\n".to_owned(),
},
],
);
build_ownership(test_config)
}
pub fn build_ownership_with_team_gem_codeowners() -> Result<Ownership, Box<dyn Error>> {
ownership!(
TestProjectFile {
relative_path: "gems/globbing/globber.rb".to_owned(),
content: "class Globbing::Globber\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "config/teams/bam.yml".to_owned(),
content: "name: Bam\ngithub:\n team: \"@Bam\"\n members:\n - Bam member\nruby:\n owned_gems:\n - globbing\n"
.to_owned(),
}
)
}
pub fn build_ownership_with_team_glob_codeowners() -> Result<Ownership, Box<dyn Error>> {
ownership!(TestProjectFile {
relative_path: "config/teams/baz.yml".to_owned(),
content: "name: Baz\ngithub:\n team: \"@Baz\"\n members:\n - Baz member\nowned_globs:\n - \"packs/bar/**\"\n".to_owned(),
})
}
pub fn build_ownership_with_subtracted_globs_team_glob_codeowners() -> Result<Ownership, Box<dyn Error>> {
ownership!(TestProjectFile {
relative_path: "config/teams/baz.yml".to_owned(),
content: "name: Baz\ngithub:\n team: \"@Baz\"\n members:\n - Baz member\nowned_globs:\n - \"packs/bar/**\"\nunowned_globs:\n - \"packs/bar/excluded/**\"\n".to_owned(),
})
}
pub fn build_ownership_with_team_yml_codeowners() -> Result<Ownership, Box<dyn Error>> {
let temp_dir = tempdir()?;
let test_config = TestConfig::new(temp_dir.path().to_path_buf(), vec![]);
build_ownership(test_config)
}
pub fn build_ownership_with_package_codeowners() -> Result<Ownership, Box<dyn Error>> {
ownership!(
TestProjectFile {
relative_path: "packs/foo/package.yml".to_owned(),
content: "owner: Baz\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/foo/app/services/package_owned.rb".to_owned(),
content: "class PackageOwned\nend\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/bam/package.yml".to_owned(),
content: "owner: Bam\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/bam/app/services/package_owned.rb".to_owned(),
content: "class PackageOwned\nend\n".to_owned(),
}
)
}
pub fn vecs_match<T: PartialEq + std::fmt::Debug>(a: &Vec<T>, b: &Vec<T>) {
// First check lengths match
assert_eq!(a.len(), b.len(), "Vectors have different lengths");
// Check each element in a exists in b
for elem_a in a {
assert!(
b.contains(elem_a),
"Element {:?} from first vector not found in second vector",
elem_a
);
}
// Check each element in b exists in a
for elem_b in b {
assert!(
a.contains(elem_b),
"Element {:?} from second vector not found in first vector",
elem_b
);
}
}
}