-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvalidate_files_test.rs
More file actions
419 lines (361 loc) · 15.3 KB
/
validate_files_test.rs
File metadata and controls
419 lines (361 loc) · 15.3 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
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>> {
// ============================================================================
// THIS TEST CURRENTLY FAILS ON MAIN - IT DEMONSTRATES THE BUG
// ============================================================================
//
// BUG DESCRIPTION:
// When validate is called with a file list, it validates ALL provided files
// without checking if they match owned_globs configuration.
//
// CONFIGURATION:
// valid_project has: owned_globs = "**/*.{rb,tsx,erb}"
// Notice: .rbi files (Sorbet interface files) are NOT in this pattern
//
// EXPECTED BEHAVIOR:
// - .rbi files should be SILENTLY SKIPPED (don't match owned_globs)
// - Only .rb files should be validated against CODEOWNERS
// - Command should SUCCEED because all validated files are owned
//
// ACTUAL BEHAVIOR (BUG):
// - ALL files are validated (including .rbi files)
// - .rbi files are not in CODEOWNERS (correctly excluded during generate)
// - .rbi files are reported as "Unowned"
// - Command FAILS with validation errors
//
// ROOT CAUSE:
// src/runner.rs lines 112-143: validate_files() iterates all file_paths
// without applying the owned_globs/unowned_globs filter that
// project_builder.rs:172 uses when no files are specified
//
// FIX NEEDED:
// Filter file_paths by owned_globs and unowned_globs before validation
// ============================================================================
// 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
// EXPECTED: .rbi files are silently skipped, only .rb files validated, succeeds
// ACTUAL (BUG): All files validated, .rbi reported as unowned, command fails
//
// ============================================================================
// THIS ASSERTION WILL FAIL ON MAIN (proving the bug exists)
// ============================================================================
//
// The command should succeed because:
// 1. .rbi files should be filtered out (don't match owned_globs)
// 2. Only .rb files should be validated
// 3. All .rb files are properly owned in CODEOWNERS
//
// But it currently fails because:
// 1. ALL files (including .rbi) are validated
// 2. .rbi files are not in CODEOWNERS
// 3. Validation error: "Unowned files detected: ruby/app/models/bank_account.rbi ..."
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(())
}