Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maksym-arutyunyan committed Aug 8, 2024
1 parent 4c449d7 commit 6493891
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 55 deletions.
26 changes: 2 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fs::{self, File};
use std::io::{self, BufWriter, Write};
use std::path::{Component, Path};
use std::path::Path;

pub mod strategies;

Expand Down Expand Up @@ -80,27 +80,5 @@ fn is_gitignore(path: &Path) -> bool {
}

fn is_codeowners(path: &Path) -> bool {
// Check if the path is a file and has the name 'CODEOWNERS'
if !path.is_file() || path.file_name().map_or(true, |name| name != "CODEOWNERS") {
return false;
}

// Check if any of the parent directories is '.github' or '.gitlab'
let mut components = path.components().rev(); // Reverse to process from the end (file -> directories)

// Skip the file component, process only directories
if components.next().is_none() {
// No components found, path is just a file name
return false;
}

for component in components {
if let Component::Normal(name) = component {
if name == ".github" || name == ".gitlab" {
return true; // Found one of the target directories
}
}
}

false // None of the directories matched
path.is_file() && path.file_name() == Some(std::ffi::OsStr::new("CODEOWNERS"))
}
64 changes: 33 additions & 31 deletions tests/e2e-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use tempfile::tempdir;
fn run_test(input_file_path: &str, expected_file_path: &str, features: &str) {
// Read the input and expected output files
let input_content = fs::read_to_string(input_file_path).expect("Failed to read input file");
let expected_content =
fs::read_to_string(expected_file_path).expect("Failed to read expected file");
let expected_content = fs::read_to_string(expected_file_path)
.unwrap_or_else(|_| panic!("Failed to read expected file: {}", expected_file_path));

// Create a temporary directory
let temp_dir = tempdir().expect("Failed to create temporary directory");
Expand Down Expand Up @@ -56,65 +56,67 @@ fn run_test(input_file_path: &str, expected_file_path: &str, features: &str) {
);
}

fn dir(path: &str) -> String {
format!("./tests/e2e-tests/{path}")
}

#[test]
fn test_e2e_bazel_1() {
run_test(
"./tests/e2e-tests/bazel/1_in.bazel",
"./tests/e2e-tests/bazel/1_out.bazel",
"",
);
run_test(&dir("bazel/1_in.bazel"), &dir("bazel/1_out.bazel"), "");
}

#[test]
fn test_e2e_bazel_2() {
run_test(
"./tests/e2e-tests/bazel/2_in.bazel",
"./tests/e2e-tests/bazel/2_out.bazel",
"",
);
run_test(&dir("bazel/2_in.bazel"), &dir("bazel/2_out.bazel"), "");
}

#[test]
fn test_e2e_generic_1() {
run_test(
"./tests/e2e-tests/generic/1_in.txt",
"./tests/e2e-tests/generic/1_out.txt",
"",
);
run_test(&dir("generic/1_in.txt"), &dir("generic/1_out.txt"), "");
}

#[test]
fn test_e2e_generic_2() {
run_test(
"./tests/e2e-tests/generic/2_in.txt",
"./tests/e2e-tests/generic/2_out.txt",
"",
);
run_test(&dir("generic/2_in.txt"), &dir("generic/2_out.txt"), "");
}

#[test]
fn test_e2e_generic_3() {
run_test(
"./tests/e2e-tests/generic/3_in.txt",
"./tests/e2e-tests/generic/3_out.txt",
"",
);
run_test(&dir("generic/3_in.txt"), &dir("generic/3_out.txt"), "");
}

#[test]
fn test_e2e_cargo_toml_1() {
run_test(
"./tests/e2e-tests/cargo_toml/1/Cargo.toml",
"./tests/e2e-tests/cargo_toml/1/Cargo_out.toml",
&dir("cargo_toml/1/Cargo.toml"),
&dir("cargo_toml/1/Cargo_out.toml"),
"cargo_toml",
);
}

#[test]
fn test_e2e_cargo_toml_2() {
run_test(
"./tests/e2e-tests/cargo_toml/2/Cargo.toml",
"./tests/e2e-tests/cargo_toml/2/Cargo_out.toml",
&dir("cargo_toml/2/Cargo.toml"),
&dir("cargo_toml/2/Cargo_out.toml"),
"cargo_toml",
);
}

#[test]
fn test_e2e_gitignore_1() {
run_test(
&dir("gitignore/.gitignore"),
&dir("gitignore/.gitignore_out"),
"gitignore",
);
}

#[test]
fn test_e2e_codeowners_1() {
run_test(
&dir("codeowners/.github/CODEOWNERS"),
&dir("codeowners/.github/CODEOWNERS_out"),
"codeowners",
);
}
10 changes: 10 additions & 0 deletions tests/e2e-tests/codeowners/.github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
b
a

# [Bazel]
b
a

# [Rust]
b
a
10 changes: 10 additions & 0 deletions tests/e2e-tests/codeowners/.github/CODEOWNERS_out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
a
b

# [Bazel]
a
b

# [Rust]
a
b
10 changes: 10 additions & 0 deletions tests/e2e-tests/gitignore/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/b
/a

# [Bazel]
/b
/a

# [Rust]
/b
/a
10 changes: 10 additions & 0 deletions tests/e2e-tests/gitignore/.gitignore_out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/a
/b

# [Bazel]
/a
/b

# [Rust]
/a
/b

0 comments on commit 6493891

Please sign in to comment.