Skip to content

Commit

Permalink
test(apply): add apply_to_folder_with_nested_gitignore() test to en…
Browse files Browse the repository at this point in the history
…sure the `ignore::Walk` is correctly skipping files based on nested .gitignore files

see #539
  • Loading branch information
Alex-ley-scrub committed Oct 27, 2024
1 parent e12de3b commit a7d5bd3
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cli_bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tracing-subscriber = { version = "0.3", default-features = false, optional = tru
serde_json = "1.0.96"
tempfile = "3.1"
rayon = "1.8.0"
git2 = "0.19.0"
insta = { version = "1.30.0", features = ["yaml", "redactions", "filters"] }
predicates = "3.0.3"
fs_extra = "1.3"
Expand Down
3 changes: 3 additions & 0 deletions crates/cli_bin/fixtures/nested_gitignore/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.rst
*.pyi
file3.py
1 change: 1 addition & 0 deletions crates/cli_bin/fixtures/nested_gitignore/docs/guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### Hello World
2 changes: 2 additions & 0 deletions crates/cli_bin/fixtures/nested_gitignore/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!module1/*.pyi
!module2/*.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!*.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class MyClass(object): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class MyClass(object): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class MyClass(object): ...
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
file1.py
!file3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class MyClass(object): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class MyClass(object): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class MyClass(object): ...
Empty file.
77 changes: 76 additions & 1 deletion crates/cli_bin/tests/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use anyhow::bail;
use anyhow::{anyhow, Result};
use assert_cmd::Command;
use common::get_test_cmd;
use git2::Repository;
use insta::{assert_snapshot, assert_yaml_snapshot};
use marzano_gritmodule::config::{
CONFIG_FILE_NAMES, GRIT_GLOBAL_DIR_ENV, GRIT_MODULE_DIR, REPO_CONFIG_DIR_NAME,
Expand Down Expand Up @@ -2913,6 +2914,80 @@ fn apply_stdin_with_invalid_lang_alias() -> Result<()> {
Ok(())
}

/// test that we can apply to a folder which contains multiple nested .gitignore files
/// see https://github.com/getgrit/gritql/issues/539
#[test]
fn apply_to_folder_with_nested_gitignore() -> Result<()> {
let (_temp_dir, fixture_dir) = get_fixture("nested_gitignore", false)?;
// make sure the .git files are in place which defines the highest level folder to be considered
Repository::init(&fixture_dir)?;

// root .gitignore has *.rst and *.pyi and file3.py
// src .gitignore has !module1/*.pyi and !module2/*.rst
// src/module1 .gitignore has !*.rst
// src/module2 .gitignore has file1.py and !file3.py

// in this folder we should process file1.py and file2.pyi BUT NOT file3.py
let module1 = fixture_dir.join("src/module1");
let mut cmd = get_test_cmd()?;
cmd.arg("apply")
.arg("`object` => ``")
.arg(".")
.arg("--lang=py")
.arg("--force")
.current_dir(&module1);

let result = cmd.output()?;

let stderr = String::from_utf8(result.stderr)?;
println!("stderr: {:?}", stderr);
let stdout = String::from_utf8(result.stdout)?;
println!("stdout: {:?}", stdout);

assert!(result.status.success(), "Command failed");
// Read back the file1.pyi file to ensure it was processed
let target_file = fixture_dir.join("src/module1/file2.pyi");
let content: String = fs_err::read_to_string(target_file)?;
assert_snapshot!(content);

// ensure we process the correct number of files:
assert!(stdout.contains("file1.py"));
assert!(stdout.contains("file2.pyi"));
assert!(!stdout.contains("file3.py"));
assert!(stdout.contains("Processed 2 files and found 2 matches"));

// in this folder we should ONLY process file3.py and NOT file2.pyi NOR file1.py
let module2 = fixture_dir.join("src/module2");
let mut cmd = get_test_cmd()?;
cmd.arg("apply")
.arg("`object` => ``")
.arg(".")
.arg("--lang=py")
.arg("--force")
.current_dir(&module2);

let result = cmd.output()?;

let stderr = String::from_utf8(result.stderr)?;
println!("stderr: {:?}", stderr);
let stdout = String::from_utf8(result.stdout)?;
println!("stdout: {:?}", stdout);

assert!(result.status.success(), "Command failed");
// Read back the file1.pyi file to ensure it was NOT processed
let target_file = fixture_dir.join("src/module2/file2.pyi");
let content: String = fs_err::read_to_string(target_file)?;
assert_snapshot!(content);

// ensure we process the correct number of files:
assert!(!stdout.contains("file1.py"));
assert!(!stdout.contains("file2.pyi"));
assert!(stdout.contains("file3.py"));
assert!(stdout.contains("Processed 1 files and found 1 matches"));

Ok(())
}

/// test that we can apply to a folder which contains valid and invalid python extensions
/// see https://github.com/getgrit/gritql/issues/485
#[test]
Expand All @@ -2935,7 +3010,7 @@ fn apply_to_folder_with_invalid_python_extension() -> Result<()> {
println!("stdout: {:?}", stdout);

assert!(result.status.success(), "Command failed");
// Read back the file3.nopy file to ensure it was processed
// Read back the file3.nopy file to ensure it was NOT processed
let target_file = fixture_dir.join("some_folder/file3.nopy");
let content: String = fs_err::read_to_string(target_file)?;
assert_snapshot!(content);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/cli_bin/tests/apply.rs
expression: content
---
class MyClass(object): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/cli_bin/tests/apply.rs
expression: content
---
class MyClass(): ...

0 comments on commit a7d5bd3

Please sign in to comment.