Skip to content

Commit 43e2926

Browse files
committed
better for file formatting
1 parent b8cd8ba commit 43e2926

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn cli() -> Result<(), Error> {
133133
_ => {
134134
println!("Error: file is owned by multiple teams!");
135135
for file_owner in file_owners {
136-
println!("\n{}\n", file_owner);
136+
println!("\n{}", file_owner);
137137
}
138138
}
139139
}

src/ownership.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use file_owner_finder::FileOwnerFinder;
2+
use itertools::Itertools;
23
use mapper::{OwnerMatcher, Source, TeamName};
34
use std::{
45
error::Error,
@@ -53,12 +54,13 @@ impl Display for FileOwner {
5354
let sources = self
5455
.sources
5556
.iter()
57+
.sorted_by_key(|source| source.to_string())
5658
.map(|source| source.to_string())
5759
.collect::<Vec<String>>()
58-
.join(", ");
60+
.join("\n- ");
5961
write!(
6062
f,
61-
"Team: {}\nTeam YML: {}\nDescription: {}",
63+
"Team: {}\nTeam YML: {}\nDescription:\n- {}",
6264
self.team_name, self.team_config_file_path, sources
6365
)
6466
}
@@ -123,6 +125,7 @@ impl Ownership {
123125
let owners = file_owner_finder.find(Path::new(file_path));
124126
Ok(owners
125127
.iter()
128+
.sorted_by_key(|owner| owner.team_name.to_lowercase())
126129
.map(|owner| match self.project.get_team(&owner.team_name) {
127130
Some(team) => FileOwner {
128131
team_name: owner.team_name.clone(),

tests/invalid_project_test.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn test_for_file() -> Result<(), Box<dyn Error>> {
5252
.stdout(predicate::eq(indoc! {"
5353
Team: Unowned
5454
Team YML: Unowned
55-
Description: \n"})); // trailing whitespace
55+
Description:\n- \n"})); // trailing whitespace
5656
Ok(())
5757
}
5858

@@ -65,17 +65,18 @@ fn test_for_file_multiple_owners() -> Result<(), Box<dyn Error>> {
6565
.arg("ruby/app/services/multi_owned.rb")
6666
.assert()
6767
.success()
68-
.stdout(predicate::str::starts_with("Error: file is owned by multiple teams!"))
69-
// order not static
70-
.stdout(predicate::str::contains(indoc! {"
71-
Team: Payroll
72-
Team YML: config/teams/payroll.yml
73-
Description: Owner specified in `ruby/app/services/.codeowner`
74-
"}))
75-
.stdout(predicate::str::contains(indoc! {"
68+
.stdout(predicate::eq(indoc! {"
69+
Error: file is owned by multiple teams!
70+
7671
Team: Payments
7772
Team YML: config/teams/payments.yml
78-
Description: Owner annotation at the top of the file
73+
Description:
74+
- Owner annotation at the top of the file
75+
76+
Team: Payroll
77+
Team YML: config/teams/payroll.yml
78+
Description:
79+
- Owner specified in `ruby/app/services/.codeowner`
7980
"}));
8081
Ok(())
81-
}
82+
}

tests/valid_project_test.rs

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

54+
#[test]
55+
fn test_for_file_same_team_multiple_ownerships() -> Result<(), Box<dyn Error>> {
56+
let expected_output = r#"
57+
Team: Payroll
58+
Team YML: config/teams/payroll.yml
59+
Description:
60+
- Owner annotation at the top of the file
61+
- Owner defined in `javascript/packages/PayrollFlow/package.json` with implicity owned glob: `javascript/packages/PayrollFlow/**/**`
62+
"#
63+
.trim();
64+
Command::cargo_bin("codeowners")?
65+
.arg("--project-root")
66+
.arg("tests/fixtures/valid_project")
67+
.arg("for-file")
68+
.arg("javascript/packages/PayrollFlow/index.tsx")
69+
.assert()
70+
.success()
71+
.stdout(predicate::str::contains(expected_output));
72+
Ok(())
73+
}
74+
5475
#[test]
5576
fn test_for_file_with_2_ownerships() -> Result<(), Box<dyn Error>> {
5677
Command::cargo_bin("codeowners")?

0 commit comments

Comments
 (0)