-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvalidate_files_test.rs
More file actions
378 lines (320 loc) · 13.4 KB
/
validate_files_test.rs
File metadata and controls
378 lines (320 loc) · 13.4 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::{error::Error, process::Command};
mod common;
use common::*;
#[test]
fn test_validate_with_owned_files() -> Result<(), Box<dyn Error>> {
run_codeowners(
"valid_project",
&["validate", "ruby/app/models/payroll.rb", "ruby/app/models/bank_account.rb"],
true,
OutputStream::Stdout,
predicate::eq(""),
)?;
Ok(())
}
#[test]
fn test_validate_with_unowned_file() -> Result<(), Box<dyn Error>> {
run_codeowners(
"valid_project",
&["validate", "ruby/app/unowned.rb"],
false,
OutputStream::Stdout,
predicate::str::contains("ruby/app/unowned.rb").and(predicate::str::contains("Unowned")),
)?;
Ok(())
}
#[test]
fn test_validate_with_mixed_files() -> Result<(), Box<dyn Error>> {
run_codeowners(
"valid_project",
&["validate", "ruby/app/models/payroll.rb", "ruby/app/unowned.rb"],
false,
OutputStream::Stdout,
predicate::str::contains("ruby/app/unowned.rb").and(predicate::str::contains("Unowned")),
)?;
Ok(())
}
#[test]
fn test_validate_with_no_files() -> Result<(), Box<dyn Error>> {
// Existing behavior - validates entire project
run_codeowners("valid_project", &["validate"], true, OutputStream::Stdout, predicate::eq(""))?;
Ok(())
}
#[test]
fn test_generate_and_validate_with_owned_files() -> Result<(), Box<dyn Error>> {
let fixture_root = std::path::Path::new("tests/fixtures/valid_project");
let temp_dir = setup_fixture_repo(fixture_root);
let project_root = temp_dir.path();
git_add_all_files(project_root);
let codeowners_path = project_root.join("tmp/CODEOWNERS");
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--codeowners-file-path")
.arg(&codeowners_path)
.arg("--no-cache")
.arg("generate-and-validate")
.arg("ruby/app/models/payroll.rb")
.arg("ruby/app/models/bank_account.rb")
.assert()
.success();
Ok(())
}
#[test]
fn test_generate_and_validate_with_unowned_file() -> Result<(), Box<dyn Error>> {
let fixture_root = std::path::Path::new("tests/fixtures/valid_project");
let temp_dir = setup_fixture_repo(fixture_root);
let project_root = temp_dir.path();
git_add_all_files(project_root);
let codeowners_path = project_root.join("tmp/CODEOWNERS");
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--codeowners-file-path")
.arg(&codeowners_path)
.arg("--no-cache")
.arg("generate-and-validate")
.arg("ruby/app/unowned.rb")
.assert()
.failure()
.stdout(predicate::str::contains("ruby/app/unowned.rb"))
.stdout(predicate::str::contains("Unowned"));
Ok(())
}
#[test]
fn test_validate_with_absolute_path() -> Result<(), Box<dyn Error>> {
let fixture_root = std::path::Path::new("tests/fixtures/valid_project");
let temp_dir = setup_fixture_repo(fixture_root);
let project_root = temp_dir.path();
git_add_all_files(project_root);
let file_absolute_path = project_root.join("ruby/app/models/payroll.rb").canonicalize()?;
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--no-cache")
.arg("validate")
.arg(file_absolute_path.to_str().unwrap())
.assert()
.success();
Ok(())
}
#[test]
fn test_validate_only_checks_codeowners_file() -> Result<(), Box<dyn Error>> {
// This test demonstrates that `validate` with files only checks the CODEOWNERS file
// It does NOT check file annotations or other ownership sources
//
// If a file has an annotation but is missing from CODEOWNERS, `validate` will report it as unowned
// This is why `generate-and-validate` should be used for accuracy
// ruby/app/models/bank_account.rb has @team Payments annotation and is in CODEOWNERS
run_codeowners(
"valid_project",
&["validate", "ruby/app/models/bank_account.rb"],
true,
OutputStream::Stdout,
predicate::eq(""),
)?;
Ok(())
}
#[test]
fn test_validate_files_respects_owned_globs_with_excluded_extensions() -> Result<(), Box<dyn Error>> {
// Validates that files not matching owned_globs are silently skipped when
// validate is called with an explicit file list.
//
// valid_project owned_globs: "{gems,config,javascript,ruby,components}/**/*.{rb,tsx,erb}"
// .rbi files (Sorbet interface files) do NOT match this pattern and should be filtered.
// Setup: Create a temporary copy of valid_project fixture
let fixture_root = std::path::Path::new("tests/fixtures/valid_project");
let temp_dir = setup_fixture_repo(fixture_root);
let project_root = temp_dir.path();
// Create .rbi files (Sorbet interface files) that do NOT match owned_globs
// These files should be ignored by validate when specified in the file list
let bank_account_rbi = project_root.join("ruby/app/models/bank_account.rbi");
let payroll_rbi = project_root.join("ruby/app/models/payroll.rbi");
std::fs::write(
&bank_account_rbi,
"# typed: strict\n# RBI file for BankAccount\nclass BankAccount; end\n",
)?;
std::fs::write(&payroll_rbi, "# typed: strict\n# RBI file for Payroll\nclass Payroll; end\n")?;
git_add_all_files(project_root);
// Step 1: Generate CODEOWNERS
// This should ONLY include .rb files (not .rbi) because .rbi doesn't match owned_globs
let codeowners_path = project_root.join("tmp/CODEOWNERS");
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--codeowners-file-path")
.arg(&codeowners_path)
.arg("--no-cache")
.arg("generate")
.assert()
.success();
// Verify: CODEOWNERS contains .rb files but NOT .rbi files
let codeowners_content = std::fs::read_to_string(&codeowners_path)?;
assert!(
codeowners_content.contains("bank_account.rb"),
"CODEOWNERS should contain .rb files (they match owned_globs)"
);
assert!(
!codeowners_content.contains("bank_account.rbi"),
"CODEOWNERS should NOT contain .rbi files (they don't match owned_globs)"
);
// Step 2: Run validate with BOTH .rb and .rbi files in the list.
// .rbi files should be silently filtered; only .rb files validated; command succeeds.
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--codeowners-file-path")
.arg(&codeowners_path)
.arg("--no-cache")
.arg("validate")
// Mix .rb and .rbi files in the argument list
.arg("ruby/app/models/bank_account.rb") // Should be validated (matches owned_globs)
.arg("ruby/app/models/bank_account.rbi") // Should be SKIPPED (doesn't match)
.arg("ruby/app/models/payroll.rb") // Should be validated (matches owned_globs)
.arg("ruby/app/models/payroll.rbi") // Should be SKIPPED (doesn't match)
.assert()
.success()
.stdout(predicate::eq(""));
Ok(())
}
// ============================================================================
// GLOB FILTERING TESTS: Verify validate with files respects owned_globs
// ============================================================================
//
// These tests ensure that when validate is called with explicit file paths,
// it correctly filters files based on owned_globs configuration. Files that
// don't match owned_globs should be silently skipped, not reported as unowned.
#[test]
fn test_validate_filters_multiple_non_matching_extensions() -> Result<(), Box<dyn Error>> {
// Test that various file types not in owned_globs are filtered out
// valid_project owned_globs: "{gems,config,javascript,ruby,components}/**/*.{rb,tsx,erb}"
let fixture_root = std::path::Path::new("tests/fixtures/valid_project");
let temp_dir = setup_fixture_repo(fixture_root);
let project_root = temp_dir.path();
// Create files with extensions NOT in owned_globs
std::fs::write(project_root.join("ruby/app/models/test.rbi"), "# Sorbet RBI file")?;
std::fs::write(project_root.join("ruby/app/models/test.md"), "# Markdown doc")?;
std::fs::write(project_root.join("ruby/app/models/test.txt"), "Plain text")?;
std::fs::write(project_root.join("ruby/app/models/test.json"), "{}")?;
git_add_all_files(project_root);
let codeowners_path = project_root.join("tmp/CODEOWNERS");
// Generate CODEOWNERS (will only include .rb, .tsx, .erb files)
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--codeowners-file-path")
.arg(&codeowners_path)
.arg("--no-cache")
.arg("generate")
.assert()
.success();
// Validate with a mix of matching and non-matching files
// All non-matching should be filtered, matching ones should succeed
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--codeowners-file-path")
.arg(&codeowners_path)
.arg("--no-cache")
.arg("validate")
.arg("ruby/app/models/payroll.rb") // matches owned_globs, is owned
.arg("ruby/app/models/test.rbi") // doesn't match owned_globs
.arg("ruby/app/models/test.md") // doesn't match owned_globs
.arg("ruby/app/models/test.txt") // doesn't match owned_globs
.arg("ruby/app/models/test.json") // doesn't match owned_globs
.assert()
.success()
.stdout(predicate::eq(""));
Ok(())
}
#[test]
fn test_validate_filters_files_outside_owned_directories() -> Result<(), Box<dyn Error>> {
// Test that files in directories not matching owned_globs are filtered
// valid_project owned_globs: "{gems,config,javascript,ruby,components}/**/*.{rb,tsx,erb}"
let fixture_root = std::path::Path::new("tests/fixtures/valid_project");
let temp_dir = setup_fixture_repo(fixture_root);
let project_root = temp_dir.path();
// Create .rb files OUTSIDE the owned directories
std::fs::create_dir_all(project_root.join("scripts"))?;
std::fs::write(project_root.join("scripts/deploy.rb"), "# Deploy script")?;
std::fs::create_dir_all(project_root.join("bin"))?;
std::fs::write(project_root.join("bin/run.rb"), "# Run script")?;
git_add_all_files(project_root);
let codeowners_path = project_root.join("tmp/CODEOWNERS");
// Generate CODEOWNERS
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--codeowners-file-path")
.arg(&codeowners_path)
.arg("--no-cache")
.arg("generate")
.assert()
.success();
// Validate with files both inside and outside owned directories
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--codeowners-file-path")
.arg(&codeowners_path)
.arg("--no-cache")
.arg("validate")
.arg("ruby/app/models/payroll.rb") // inside ruby/, matches owned_globs
.arg("scripts/deploy.rb") // outside owned dirs, filtered
.arg("bin/run.rb") // outside owned dirs, filtered
.assert()
.success()
.stdout(predicate::eq(""));
Ok(())
}
#[test]
fn test_validate_respects_unowned_globs() -> Result<(), Box<dyn Error>> {
// Test that files matching unowned_globs are filtered out even if they match owned_globs
let fixture_root = std::path::Path::new("tests/fixtures/valid_project");
let temp_dir = setup_fixture_repo(fixture_root);
let project_root = temp_dir.path();
// Read and modify the config to add unowned_globs
let config_path = project_root.join("config/code_ownership.yml");
let config_content = std::fs::read_to_string(&config_path)?;
let updated_config = config_content.replace("unowned_globs:", "unowned_globs:\n - ruby/app/models/ignored_*.rb");
std::fs::write(&config_path, updated_config)?;
// Create a file that matches owned_globs but also matches unowned_globs
std::fs::write(
project_root.join("ruby/app/models/ignored_test.rb"),
"# This file should be ignored via unowned_globs",
)?;
git_add_all_files(project_root);
let codeowners_path = project_root.join("tmp/CODEOWNERS");
// Generate CODEOWNERS (ignored_test.rb should NOT be included)
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--codeowners-file-path")
.arg(&codeowners_path)
.arg("--no-cache")
.arg("generate")
.assert()
.success();
// Verify the ignored file is NOT in CODEOWNERS
let codeowners_content = std::fs::read_to_string(&codeowners_path)?;
assert!(
!codeowners_content.contains("ignored_test.rb"),
"ignored_test.rb should not be in CODEOWNERS"
);
// Validate with the ignored file - should be filtered by unowned_globs
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg(project_root)
.arg("--codeowners-file-path")
.arg(&codeowners_path)
.arg("--no-cache")
.arg("validate")
.arg("ruby/app/models/payroll.rb") // owned, should validate
.arg("ruby/app/models/ignored_test.rb") // matches unowned_globs, should be filtered
.assert()
.success()
.stdout(predicate::eq(""));
Ok(())
}