Skip to content

Commit b8cd8ba

Browse files
committed
better formatting for multiple ownership validation
1 parent d14ee3d commit b8cd8ba

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
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: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ fn test_validate() -> Result<(), Box<dyn Error>> {
1616
CODEOWNERS out of date. Run `codeowners generate` to update the CODEOWNERS file
1717
1818
Code ownership should only be defined for each file in one way. The following files have declared ownership in multiple ways
19-
- gems/payroll_calculator/calculator.rb (owner: Payments, source: Owner annotation at the top of the file)
20-
- gems/payroll_calculator/calculator.rb (owner: Payroll, source: Owner specified in Team YML's `owned_gems`)
21-
- ruby/app/services/multi_owned.rb (owner: Payments, source: Owner annotation at the top of the file)
22-
- ruby/app/services/multi_owned.rb (owner: Payroll, source: Owner specified in `ruby/app/services/.codeowner`)
19+
20+
gems/payroll_calculator/calculator.rb
21+
owner: Payments
22+
- Owner annotation at the top of the file
23+
owner: Payroll
24+
- Owner specified in Team YML's `owned_gems`
25+
26+
ruby/app/services/multi_owned.rb
27+
owner: Payments
28+
- Owner annotation at the top of the file
29+
owner: Payroll
30+
- Owner specified in `ruby/app/services/.codeowner`
2331
2432
Found invalid team annotations
2533
- ruby/app/models/blockchain.rb is referencing an invalid team - 'Web3'

tests/valid_project_test.rs

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

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

0 commit comments

Comments
 (0)