Skip to content

Commit b0afc8f

Browse files
committed
better formatting for multiple ownership validation
1 parent 8e4cafe commit b0afc8f

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
lines changed

src/ownership/validator.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ impl Error {
173173
pub fn messages(&self) -> Vec<String> {
174174
match self {
175175
Error::FileWithoutOwner { path } => vec![format!("- {}", path.to_string_lossy())],
176-
Error::FileWithMultipleOwners { path, owners } => owners
177-
.iter()
178-
.flat_map(|owner| {
179-
owner
180-
.sources
181-
.iter()
182-
.map(|source| format!("- {} (owner: {}, source: {})", path.to_string_lossy(), owner.team_name, &source))
183-
.collect_vec()
184-
})
185-
.collect_vec(),
176+
Error::FileWithMultipleOwners { path, owners } => {
177+
let mut output = vec![format!("\n{}", path.to_string_lossy().to_string())];
178+
for owner in owners.iter().sorted_by_key(|owner| owner.team_name.to_lowercase()) {
179+
output.push(format!(" owner: {}", owner.team_name));
180+
for source in &owner.sources {
181+
output.push(format!(" - {}", source));
182+
}
183+
}
184+
vec![output.join("\n")]
185+
}
186186
Error::CodeownershipFileIsStale => vec![],
187187
Error::InvalidTeam { name, path } => vec![format!("- {} is referencing an invalid team - '{}'", path.to_string_lossy(), name)],
188188
}

tests/invalid_project_test.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,38 @@ use std::{error::Error, process::Command};
44

55
#[test]
66
fn test_validate() -> Result<(), Box<dyn Error>> {
7+
let expected_output = r#"
8+
CODEOWNERS out of date. Run `codeowners generate` to update the CODEOWNERS file
9+
10+
Code ownership should only be defined for each file in one way. The following files have declared ownership in multiple ways
11+
12+
gems/payroll_calculator/calculator.rb
13+
owner: Payments
14+
- Owner annotation at the top of the file
15+
owner: Payroll
16+
- Owner specified in Team YML's `owned_gems`
17+
18+
ruby/app/services/multi_owned.rb
19+
owner: Payments
20+
- Owner annotation at the top of the file
21+
owner: Payroll
22+
- Owner specified in `ruby/app/services/.codeowner`
23+
24+
Found invalid team annotations
25+
- ruby/app/models/blockchain.rb is referencing an invalid team - 'Web3'
26+
27+
Some files are missing ownership
28+
- ruby/app/models/blockchain.rb
29+
- ruby/app/unowned.rb
30+
"#
31+
.trim();
732
Command::cargo_bin("codeowners")?
833
.arg("--project-root")
934
.arg("tests/fixtures/invalid_project")
1035
.arg("validate")
1136
.assert()
1237
.failure()
13-
.stdout(predicate::str::contains(
14-
"CODEOWNERS out of date. Run `codeowners generate` to update the CODEOWNERS file",
15-
))
16-
.stdout(predicate::str::contains(
17-
"Some files are missing ownership\n- ruby/app/models/blockchain.rb\n- ruby/app/unowned.rb",
18-
))
19-
.stdout(predicate::str::contains(
20-
"Found invalid team annotations\n- ruby/app/models/blockchain.rb is referencing an invalid team - 'Web3'",
21-
))
22-
.stdout(predicate::str::contains(
23-
"Code ownership should only be defined for each file in one way. The following files have declared ownership in multiple ways",
24-
))
25-
.stdout(predicate::str::contains(
26-
"gems/payroll_calculator/calculator.rb (owner: Payments, source: Owner annotation at the top of the file)",
27-
))
28-
.stdout(predicate::str::contains(
29-
"gems/payroll_calculator/calculator.rb (owner: Payroll, source: Owner specified in Team YML's `owned_gems`)",
30-
))
31-
.stdout(predicate::str::contains(
32-
"ruby/app/services/multi_owned.rb (owner: Payments, source: Owner annotation at the top of the file)",
33-
))
34-
.stdout(predicate::str::contains(
35-
"ruby/app/services/multi_owned.rb (owner: Payroll, source: Owner specified in `ruby/app/services/.codeowner`",
36-
));
38+
.stdout(predicate::str::contains(expected_output));
3739

3840
Ok(())
3941
}

tests/valid_project_test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ fn test_for_file() -> Result<(), Box<dyn Error>> {
4848
Ok(())
4949
}
5050

51+
#[test]
52+
fn test_for_file_with_2_ownerships() -> Result<(), Box<dyn Error>> {
53+
Command::cargo_bin("codeowners")?
54+
.arg("--project-root")
55+
.arg("tests/fixtures/valid_project")
56+
.arg("for-file")
57+
.arg("javascript/packages/PayrollFlow/index.tsx")
58+
.assert()
59+
.success()
60+
.stdout(predicate::str::contains("Team: Payroll"))
61+
.stdout(predicate::str::contains("Team YML: config/teams/payroll.yml"));
62+
63+
Ok(())
64+
}
65+
5166
#[test]
5267
fn test_for_team() -> Result<(), Box<dyn Error>> {
5368
let expected_stdout = r#"

0 commit comments

Comments
 (0)