Skip to content

Commit

Permalink
Expand forge test --match interface (again) (#662)
Browse files Browse the repository at this point in the history
* Added test_path method to TestFilter

* Added path regex to test interface

* Added source path filtering to MultiContractRunner

* Updated test Filter and reorganiezed test_helpers

* Updated tests to use new filter

* Fixed test filter

* Use new into_artifacts

* Path filtering requires absolute path

* Formatting

* Fixed warnings

* Minor refactoring

* Minor refactoring

* Bumped semver to 1.0.5 for dev compatibility with ethers-rs

* Added passing test for foundry_utils::link

* Renamed test

* chore: bump ethers for latest artifacts update
gakonst/ethers-rs#882

driveby fixes:
gakonst/ethers-rs#930
gakonst/ethers-rs#928

Co-authored-by: Georgios Konstantopoulos <[email protected]>
  • Loading branch information
charisma98 and gakonst committed Feb 18, 2022
1 parent 3300f41 commit 4f6844e
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 84 deletions.
55 changes: 28 additions & 27 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ serde = "1.0.133"
sputnik = { package = "evm", git = "https://github.com/rust-blockchain/evm", optional = true }
proptest = "1.0.0"
glob = "0.3.0"
semver = "1.0.4"
semver = "1.0.5"
once_cell = "1.9.0"
locate-cargo-manifest = "0.2.2"
walkdir = "2.3.2"
Expand Down
12 changes: 2 additions & 10 deletions cli/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,8 @@ fn get_artifact_from_name(
let mut has_found_contract = false;
let mut contract_artifact = None;

for (name, artifact) in compiled.into_artifacts() {
// if the contract name
let mut split = name.split(':');
let mut artifact_contract_name =
split.next().ok_or_else(|| eyre::Error::msg("no contract name provided"))?;
if let Some(new_name) = split.next() {
artifact_contract_name = new_name;
};

if artifact_contract_name == contract.name {
for (artifact_id, artifact) in compiled.into_artifacts() {
if artifact_id.name == contract.name {
if has_found_contract {
eyre::bail!("contract with duplicate name. pass path")
}
Expand Down
41 changes: 37 additions & 4 deletions cli/src/cmd/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ use evm_adapters::{
};
use forge::{MultiContractRunnerBuilder, TestFilter};
use foundry_config::{figment::Figment, Config};
use std::collections::BTreeMap;
use regex::Regex;
use std::{collections::BTreeMap, str::FromStr};

#[derive(Debug, Clone, Parser)]
pub struct Filter {
#[clap(
long = "match",
short = 'm',
help = "only run test methods matching regex (deprecated, see --match-test, --match-contract)"
help = "only run test methods matching regex (deprecated, see --match-test)"
)]
pattern: Option<regex::Regex>,

Expand Down Expand Up @@ -54,11 +55,28 @@ pub struct Filter {
conflicts_with = "pattern"
)]
contract_pattern_inverse: Option<regex::Regex>,

#[clap(
long = "match-path",
alias = "mp",
help = "only run test methods in source files at path matching regex. Requires absolute path",
conflicts_with = "pattern"
)]
path_pattern: Option<regex::Regex>,

#[clap(
long = "no-match-path",
alias = "nmp",
help = "only run test methods in source files at path not matching regex. Requires absolute path",
conflicts_with = "pattern"
)]
path_pattern_inverse: Option<regex::Regex>,
}

impl TestFilter for Filter {
fn matches_test(&self, test_name: &str) -> bool {
fn matches_test(&self, test_name: impl AsRef<str>) -> bool {
let mut ok = true;
let test_name = test_name.as_ref();
// Handle the deprecated option match
if let Some(re) = &self.pattern {
ok &= re.is_match(test_name);
Expand All @@ -72,8 +90,9 @@ impl TestFilter for Filter {
ok
}

fn matches_contract(&self, contract_name: &str) -> bool {
fn matches_contract(&self, contract_name: impl AsRef<str>) -> bool {
let mut ok = true;
let contract_name = contract_name.as_ref();
if let Some(re) = &self.contract_pattern {
ok &= re.is_match(contract_name);
}
Expand All @@ -82,6 +101,20 @@ impl TestFilter for Filter {
}
ok
}

fn matches_path(&self, path: impl AsRef<str>) -> bool {
let mut ok = true;
let path = path.as_ref();
if let Some(re) = &self.path_pattern {
let re = Regex::from_str(&format!("^{}", re.as_str())).unwrap();
ok &= re.is_match(path);
}
if let Some(re) = &self.path_pattern_inverse {
let re = Regex::from_str(&format!("^{}", re.as_str())).unwrap();
ok &= !re.is_match(path);
}
ok
}
}

// Loads project's figment and merges the build cli arguments into it
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ forgetest_init!(can_emit_extra_output, |prj: TestProject, mut cmd: TestCommand|
cmd.assert_non_empty_stdout();

let metadata_path = prj.paths().artifacts.join("Contract.sol/Contract.metadata.json");
let artifact: Metadata = ethers::solc::utils::read_json_file(metadata_path).unwrap();
let _artifact: Metadata = ethers::solc::utils::read_json_file(metadata_path).unwrap();
});

// test against a local checkout, useful to debug with local ethers-rs patch
Expand Down
2 changes: 1 addition & 1 deletion config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readme = "README.md"

[dependencies]
dirs-next = "2.0.0"
semver = { version = "1.0.4", features = ["serde"] }
semver = { version = "1.0.5", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.73"
toml = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ evm-adapters = { path = "./../evm-adapters", features = ["sputnik", "sputnik-hel

ethers = { git = "https://github.com/gakonst/ethers-rs", default-features = false, features = ["solc-full"] }
eyre = "0.6.5"
semver = "1.0.4"
semver = "1.0.5"
serde_json = "1.0.67"
serde = "1.0.130"
regex = { version = "1.5.4", default-features = false }
Expand Down
Loading

0 comments on commit 4f6844e

Please sign in to comment.