Skip to content

Commit f0e55f8

Browse files
committed
updating tests
1 parent 1fdcc53 commit f0e55f8

File tree

9 files changed

+185
-143
lines changed

9 files changed

+185
-143
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
3434
[dev-dependencies]
3535
assert_cmd = "2.0.16"
3636
rusty-hook = "^0.11.2"
37-
predicates = "3.1.2"
37+
predicates = "3.1.3"
3838
pretty_assertions = "1.4.1" # Shows a more readable diff when comparing objects
3939
indoc = "2.0.5"

src/project_builder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ impl<'a> ProjectBuilder<'a> {
6666
builder.filter_entry(move |entry: &DirEntry| {
6767
let path = entry.path();
6868
let file_name = entry.file_name().to_str().unwrap_or("");
69-
if let Some(tracked_files) = &tracked_files {
70-
if let Some(ft) = entry.file_type() {
71-
if ft.is_file() && !tracked_files.contains_key(path) {
72-
return false;
73-
}
74-
}
69+
if let Some(tracked_files) = &tracked_files
70+
&& let Some(ft) = entry.file_type()
71+
&& ft.is_file()
72+
&& !tracked_files.contains_key(path)
73+
{
74+
return false;
7575
}
7676
if let Some(ft) = entry.file_type()
7777
&& ft.is_dir()

src/tracked_files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mod tests {
4242

4343
std::fs::write(tmp_dir.path().join("test.txt"), "test").unwrap();
4444
let tracked = find_tracked_files(tmp_dir.path()).unwrap();
45-
assert!(tracked.len() == 0);
45+
assert!(tracked.is_empty());
4646

4747
std::process::Command::new("git")
4848
.arg("add")

tests/common/mod.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,47 @@
1+
use assert_cmd::prelude::*;
2+
use codeowners::runner::{self, RunConfig};
3+
use predicates::prelude::*;
14
use std::fs;
25
use std::path::Path;
3-
use std::process::Command;
4-
5-
use codeowners::runner::{self, RunConfig};
6+
use std::{error::Error, process::Command};
67
use tempfile::TempDir;
78

9+
#[allow(dead_code)]
10+
pub enum OutputStream {
11+
Stdout,
12+
Stderr,
13+
}
14+
15+
#[allow(dead_code)]
16+
pub fn run_codeowners<I, P>(
17+
relative_fixture_path: &str,
18+
command: &[&str],
19+
success: bool,
20+
stream: OutputStream,
21+
output_predicate: I,
22+
) -> Result<(), Box<dyn Error>>
23+
where
24+
I: assert_cmd::assert::IntoOutputPredicate<P>,
25+
P: Predicate<[u8]>,
26+
{
27+
let fixture_root = Path::new("tests/fixtures").join(relative_fixture_path);
28+
let temp_dir = setup_fixture_repo(&fixture_root);
29+
let project_root = temp_dir.path();
30+
git_add_all_files(project_root);
31+
let mut cmd = Command::cargo_bin("codeowners")?;
32+
let assert = cmd.arg("--project-root").arg(project_root).arg("--no-cache").args(command).assert();
33+
let assert = if success { assert.success() } else { assert.failure() };
34+
match stream {
35+
OutputStream::Stdout => {
36+
assert.stdout(output_predicate);
37+
}
38+
OutputStream::Stderr => {
39+
assert.stderr(output_predicate);
40+
}
41+
}
42+
Ok(())
43+
}
44+
845
#[allow(dead_code)]
946
pub fn teardown() {
1047
glob::glob("tests/fixtures/*/tmp/cache/codeowners")

tests/invalid_project_test.rs

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,73 @@
1-
use assert_cmd::prelude::*;
21
use indoc::indoc;
32
use predicates::prelude::*;
4-
use std::{error::Error, process::Command};
3+
use std::error::Error;
4+
5+
mod common;
6+
use common::OutputStream;
7+
use common::run_codeowners;
58

69
#[test]
710
fn test_validate() -> Result<(), Box<dyn Error>> {
8-
Command::cargo_bin("codeowners")?
9-
.arg("--project-root")
10-
.arg("tests/fixtures/invalid_project")
11-
.arg("--no-cache")
12-
.arg("validate")
13-
.assert()
14-
.failure()
15-
.stdout(predicate::eq(indoc! {"
11+
run_codeowners(
12+
"invalid_project",
13+
&["validate"],
14+
false,
15+
OutputStream::Stdout,
16+
predicate::eq(indoc! {"
1617
17-
CODEOWNERS out of date. Run `codeowners generate` to update the CODEOWNERS file
18+
CODEOWNERS out of date. Run `codeowners generate` to update the CODEOWNERS file
1819
19-
Code ownership should only be defined for each file in one way. The following files have declared ownership in multiple ways
20+
Code ownership should only be defined for each file in one way. The following files have declared ownership in multiple ways
2021
21-
gems/payroll_calculator/calculator.rb
22-
owner: Payments
23-
- Owner annotation at the top of the file
24-
owner: Payroll
25-
- Owner specified in Team YML's `owned_gems`
22+
gems/payroll_calculator/calculator.rb
23+
owner: Payments
24+
- Owner annotation at the top of the file
25+
owner: Payroll
26+
- Owner specified in Team YML's `owned_gems`
2627
27-
ruby/app/services/multi_owned.rb
28-
owner: Payments
29-
- Owner annotation at the top of the file
30-
owner: Payroll
31-
- Owner specified in `ruby/app/services/.codeowner`
28+
ruby/app/services/multi_owned.rb
29+
owner: Payments
30+
- Owner annotation at the top of the file
31+
owner: Payroll
32+
- Owner specified in `ruby/app/services/.codeowner`
3233
33-
Found invalid team annotations
34-
- ruby/app/models/blockchain.rb is referencing an invalid team - 'Web3'
34+
Found invalid team annotations
35+
- ruby/app/models/blockchain.rb is referencing an invalid team - 'Web3'
3536
36-
Some files are missing ownership
37-
- ruby/app/unowned.rb
37+
Some files are missing ownership
38+
- ruby/app/unowned.rb
3839
39-
"}));
40+
"}),
41+
)?;
4042
Ok(())
4143
}
4244

4345
#[test]
4446
fn test_for_file() -> Result<(), Box<dyn Error>> {
45-
Command::cargo_bin("codeowners")?
46-
.arg("--project-root")
47-
.arg("tests/fixtures/invalid_project")
48-
.arg("--no-cache")
49-
.arg("for-file")
50-
.arg("ruby/app/models/blockchain.rb")
51-
.assert()
52-
.success()
53-
.stdout(predicate::eq(indoc! {"
47+
run_codeowners(
48+
"invalid_project",
49+
&["for-file", "ruby/app/models/blockchain.rb"],
50+
true,
51+
OutputStream::Stdout,
52+
predicate::eq(indoc! {"
5453
Team: Unowned
5554
Github Team: Unowned
5655
Team YML:
5756
Description:
5857
- Unowned
59-
"}));
58+
"}),
59+
)?;
6060
Ok(())
6161
}
6262

6363
#[test]
6464
fn test_for_file_multiple_owners() -> Result<(), Box<dyn Error>> {
65-
Command::cargo_bin("codeowners")?
66-
.arg("--project-root")
67-
.arg("tests/fixtures/invalid_project")
68-
.arg("--no-cache")
69-
.arg("for-file")
70-
.arg("ruby/app/services/multi_owned.rb")
71-
.assert()
72-
.failure()
73-
.stdout(predicate::eq(indoc! {"
65+
run_codeowners(
66+
"invalid_project",
67+
&["for-file", "ruby/app/services/multi_owned.rb"],
68+
false,
69+
OutputStream::Stdout,
70+
predicate::eq(indoc! {"
7471
Error: file is owned by multiple teams!
7572
7673
Team: Payments
@@ -84,6 +81,7 @@ fn test_for_file_multiple_owners() -> Result<(), Box<dyn Error>> {
8481
Team YML: config/teams/payroll.yml
8582
Description:
8683
- Owner specified in `ruby/app/services/.codeowner`
87-
"}));
84+
"}),
85+
)?;
8886
Ok(())
8987
}

tests/multiple_directory_owners_test.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
use assert_cmd::prelude::*;
2-
use std::{error::Error, path::Path, process::Command};
1+
use std::{error::Error, path::Path};
2+
3+
use common::OutputStream;
4+
use common::run_codeowners;
5+
use predicates::prelude::*;
6+
7+
mod common;
38

49
#[test]
510
fn test_validate() -> Result<(), Box<dyn Error>> {
6-
Command::cargo_bin("codeowners")?
7-
.arg("--project-root")
8-
.arg("tests/fixtures/multiple-directory-owners")
9-
.arg("--no-cache")
10-
.arg("validate")
11-
.assert()
12-
.success();
11+
run_codeowners(
12+
"multiple-directory-owners",
13+
&["validate"],
14+
true,
15+
OutputStream::Stdout,
16+
predicate::eq(""),
17+
)?;
1318

1419
Ok(())
1520
}
1621

1722
#[test]
1823
fn test_generate() -> Result<(), Box<dyn Error>> {
19-
Command::cargo_bin("codeowners")?
20-
.arg("--project-root")
21-
.arg("tests/fixtures/multiple-directory-owners")
22-
.arg("--codeowners-file-path")
23-
.arg("../../../tmp/CODEOWNERS")
24-
.arg("--no-cache")
25-
.arg("generate")
26-
.assert()
27-
.success();
24+
let codeowners_abs = std::env::current_dir()?.join("tmp/CODEOWNERS");
25+
let codeowners_str = codeowners_abs.to_str().unwrap();
26+
27+
run_codeowners(
28+
"multiple-directory-owners",
29+
&["--codeowners-file-path", codeowners_str, "generate"],
30+
true,
31+
OutputStream::Stdout,
32+
predicate::eq(""),
33+
)?;
2834

2935
let expected_codeowners: String = std::fs::read_to_string(Path::new("tests/fixtures/multiple-directory-owners/.github/CODEOWNERS"))?;
3036
let actual_codeowners: String = std::fs::read_to_string(Path::new("tmp/CODEOWNERS"))?;

0 commit comments

Comments
 (0)