Skip to content

Commit

Permalink
better for file formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
perryqh committed Nov 8, 2024
1 parent b8cd8ba commit 43e2926
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn cli() -> Result<(), Error> {
_ => {
println!("Error: file is owned by multiple teams!");
for file_owner in file_owners {
println!("\n{}\n", file_owner);
println!("\n{}", file_owner);
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/ownership.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use file_owner_finder::FileOwnerFinder;
use itertools::Itertools;
use mapper::{OwnerMatcher, Source, TeamName};
use std::{
error::Error,
Expand Down Expand Up @@ -53,12 +54,13 @@ impl Display for FileOwner {
let sources = self
.sources
.iter()
.sorted_by_key(|source| source.to_string())
.map(|source| source.to_string())
.collect::<Vec<String>>()
.join(", ");
.join("\n- ");
write!(
f,
"Team: {}\nTeam YML: {}\nDescription: {}",
"Team: {}\nTeam YML: {}\nDescription:\n- {}",
self.team_name, self.team_config_file_path, sources
)
}
Expand Down Expand Up @@ -123,6 +125,7 @@ impl Ownership {
let owners = file_owner_finder.find(Path::new(file_path));
Ok(owners
.iter()
.sorted_by_key(|owner| owner.team_name.to_lowercase())
.map(|owner| match self.project.get_team(&owner.team_name) {
Some(team) => FileOwner {
team_name: owner.team_name.clone(),
Expand Down
23 changes: 12 additions & 11 deletions tests/invalid_project_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn test_for_file() -> Result<(), Box<dyn Error>> {
.stdout(predicate::eq(indoc! {"
Team: Unowned
Team YML: Unowned
Description: \n"})); // trailing whitespace
Description:\n- \n"})); // trailing whitespace
Ok(())
}

Expand All @@ -65,17 +65,18 @@ fn test_for_file_multiple_owners() -> Result<(), Box<dyn Error>> {
.arg("ruby/app/services/multi_owned.rb")
.assert()
.success()
.stdout(predicate::str::starts_with("Error: file is owned by multiple teams!"))
// order not static
.stdout(predicate::str::contains(indoc! {"
Team: Payroll
Team YML: config/teams/payroll.yml
Description: Owner specified in `ruby/app/services/.codeowner`
"}))
.stdout(predicate::str::contains(indoc! {"
.stdout(predicate::eq(indoc! {"
Error: file is owned by multiple teams!
Team: Payments
Team YML: config/teams/payments.yml
Description: Owner annotation at the top of the file
Description:
- Owner annotation at the top of the file
Team: Payroll
Team YML: config/teams/payroll.yml
Description:
- Owner specified in `ruby/app/services/.codeowner`
"}));
Ok(())
}
}
21 changes: 21 additions & 0 deletions tests/valid_project_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ fn test_for_file() -> Result<(), Box<dyn Error>> {
Ok(())
}

#[test]
fn test_for_file_same_team_multiple_ownerships() -> Result<(), Box<dyn Error>> {
let expected_output = r#"
Team: Payroll
Team YML: config/teams/payroll.yml
Description:
- Owner annotation at the top of the file
- Owner defined in `javascript/packages/PayrollFlow/package.json` with implicity owned glob: `javascript/packages/PayrollFlow/**/**`
"#
.trim();
Command::cargo_bin("codeowners")?
.arg("--project-root")
.arg("tests/fixtures/valid_project")
.arg("for-file")
.arg("javascript/packages/PayrollFlow/index.tsx")
.assert()
.success()
.stdout(predicate::str::contains(expected_output));
Ok(())
}

#[test]
fn test_for_file_with_2_ownerships() -> Result<(), Box<dyn Error>> {
Command::cargo_bin("codeowners")?
Expand Down

0 comments on commit 43e2926

Please sign in to comment.