Skip to content

Commit

Permalink
feat: support absolute paths when matching paths (#7362)
Browse files Browse the repository at this point in the history
* added support for absolute paths when running forge test --match-path (#7350)

* Changed how canonicalize() is called

Co-authored-by: Arsenii Kulikov <[email protected]>

* optimize

* upgrade tests to use snapbox

* Update test_cmd.rs

---------

Co-authored-by: Arsenii Kulikov <[email protected]>
Co-authored-by: zerosnacks <[email protected]>
Co-authored-by: zerosnacks <[email protected]>
  • Loading branch information
4 people committed Jul 16, 2024
1 parent 3cbe211 commit 4345e3e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
32 changes: 29 additions & 3 deletions crates/config/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ impl GlobMatcher {
return self.matcher.is_match(format!("./{}", path.display()));
}

if path.is_relative() && Path::new(self.glob().glob()).is_absolute() {
if let Ok(canonicalized_path) = dunce::canonicalize(path) {
return self.matcher.is_match(&canonicalized_path);
} else {
return false;
}
}

false
}

Expand Down Expand Up @@ -218,9 +226,27 @@ mod tests {
}

#[test]
fn can_match_glob_paths() {
fn can_match_relative_glob_paths() {
let matcher: GlobMatcher = "./test/*".parse().unwrap();
assert!(matcher.is_match(Path::new("test/Contract.sol")));
assert!(matcher.is_match(Path::new("./test/Contract.sol")));

// Absolute path that should match the pattern
assert!(matcher.is_match(Path::new("test/Contract.t.sol")));

// Relative path that should match the pattern
assert!(matcher.is_match(Path::new("./test/Contract.t.sol")));
}

#[test]
fn can_match_absolute_glob_paths() {
let matcher: GlobMatcher = "/home/user/projects/project/test/*".parse().unwrap();

// Absolute path that should match the pattern
assert!(matcher.is_match(Path::new("/home/user/projects/project/test/Contract.t.sol")));

// Absolute path that should not match the pattern
assert!(!matcher.is_match(Path::new("/home/user/other/project/test/Contract.t.sol")));

// Relative path that should not match an absolute pattern
assert!(!matcher.is_match(Path::new("projects/project/test/Contract.t.sol")));
}
}
55 changes: 52 additions & 3 deletions crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ forgetest!(can_test_with_match_path, |prj, cmd| {
r#"
import "./test.sol";
contract ATest is DSTest {
function testArray(uint64[2] calldata values) external {
function testPass() external {
assertTrue(true);
}
}
Expand All @@ -176,8 +176,57 @@ contract FailTest is DSTest {
)
.unwrap();

cmd.args(["test", "--match-path", "*src/ATest.t.sol"]);
assert!(cmd.stdout_lossy().contains("[PASS]") && !cmd.stdout_lossy().contains("[FAIL]"));
cmd.args(["test", "--match-path", "*src/ATest.t.sol"]).assert_success().stdout_eq(str![[r#"
...
Ran 1 test for src/ATest.t.sol:ATest
[PASS] testPass() (gas: 190)
...
Ran 1 test suite in [..] 1 tests passed, 0 failed, 0 skipped (1 total tests)
...
"#]]);
});

// tests that using the --match-path option works with absolute paths
forgetest!(can_test_with_match_path_absolute, |prj, cmd| {
prj.insert_ds_test();

prj.add_source(
"ATest.t.sol",
r#"
import "./test.sol";
contract ATest is DSTest {
function testPass() external {
assertTrue(true);
}
}
"#,
)
.unwrap();

prj.add_source(
"FailTest.t.sol",
r#"
import "./test.sol";
contract FailTest is DSTest {
function testNothing() external {
assertTrue(false);
}
}
"#,
)
.unwrap();

let test_path = prj.root().join("src/ATest.t.sol");
let test_path = test_path.to_string_lossy();

cmd.args(["test", "--match-path", test_path.as_ref()]).assert_success().stdout_eq(str![[r#"
...
Ran 1 test for src/ATest.t.sol:ATest
[PASS] testPass() (gas: 190)
...
Ran 1 test suite in [..] 1 tests passed, 0 failed, 0 skipped (1 total tests)
...
"#]]);
});

// tests that `forge test` will pick up tests that are stored in the `test = <path>` config value
Expand Down

0 comments on commit 4345e3e

Please sign in to comment.