Skip to content

Commit

Permalink
parsing file paths from foundry.toml (#7063)
Browse files Browse the repository at this point in the history
* parsing file paths from foundry.toml

* e2e test

* fix

* review: renaming

* Update crates/forge/tests/cli/cmd.rs

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>

* review: improved test readability

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
loocapro and mattsse authored Feb 9, 2024
1 parent 282b0c3 commit 113ab8b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ foundry-test-utils = { path = "crates/test-utils" }

# solc & compilation utilities
foundry-block-explorers = { version = "0.2.3", default-features = false }
foundry-compilers = { version = "0.3.2", default-features = false }
foundry-compilers = { version = "0.3.4", default-features = false }

## revm
# no default features to avoid c-kzg
Expand Down Expand Up @@ -162,7 +162,7 @@ alloy-transport = { git = "https://github.com/alloy-rs/alloy" }
alloy-transport-http = { git = "https://github.com/alloy-rs/alloy" }
alloy-transport-ipc = { git = "https://github.com/alloy-rs/alloy" }
alloy-transport-ws = { git = "https://github.com/alloy-rs/alloy" }
alloy-primitives = "0.6.2"
alloy-primitives = { version ="0.6.2", features = ["getrandom"] }
alloy-dyn-abi = "0.6.2"
alloy-json-abi = "0.6.2"
alloy-sol-types = "0.6.2"
Expand Down
1 change: 1 addition & 0 deletions crates/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ etherscan_api_key = "YOURETHERSCANAPIKEY"
# known error codes are: ["unreachable", "unused-return", "unused-param", "unused-var", "code-size", "shadowing", "func-mutability", "license", "pragma-solidity", "virtual-interfaces", "same-varname"]
# additional warnings can be added using their numeric error code: ["license", 1337]
ignored_error_codes = ["license", "code-size"]
ignored_warnings_from = ["path_to_ignore"]
deny_warnings = false
match_test = "Foo"
no_match_test = "Bar"
Expand Down
25 changes: 25 additions & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ pub struct Config {
pub etherscan: EtherscanConfigs,
/// list of solidity error codes to always silence in the compiler output
pub ignored_error_codes: Vec<SolidityErrorCode>,
/// list of file paths to ignore
#[serde(rename = "ignored_warnings_from")]
pub ignored_file_paths: Vec<PathBuf>,
/// When true, compiler warnings are treated as errors
pub deny_warnings: bool,
/// Only run test functions matching the specified regex pattern.
Expand Down Expand Up @@ -657,6 +660,7 @@ impl Config {
.include_paths(&self.include_paths)
.solc_config(SolcConfig::builder().settings(self.solc_settings()?).build())
.ignore_error_codes(self.ignored_error_codes.iter().copied().map(Into::into))
.ignore_paths(self.ignored_file_paths.clone())
.set_compiler_severity_filter(if self.deny_warnings {
Severity::Warning
} else {
Expand Down Expand Up @@ -1868,6 +1872,7 @@ impl Default for Config {
SolidityErrorCode::ContractExceeds24576Bytes,
SolidityErrorCode::ContractInitCodeSizeExceeds49152Bytes,
],
ignored_file_paths: vec![],
deny_warnings: false,
via_ir: false,
rpc_storage_caching: Default::default(),
Expand Down Expand Up @@ -3514,6 +3519,7 @@ mod tests {
gas_price = 0
gas_reports = ['*']
ignored_error_codes = [1878]
ignored_warnings_from = ["something"]
deny_warnings = false
initial_balance = '0xffffffffffffffffffffffff'
libraries = []
Expand Down Expand Up @@ -3561,6 +3567,7 @@ mod tests {

let config = Config::load_with_root(jail.directory());

assert_eq!(config.ignored_file_paths, vec![PathBuf::from("something")]);
assert_eq!(config.fuzz.seed, Some(U256::from(1000)));
assert_eq!(
config.remappings,
Expand Down Expand Up @@ -4590,6 +4597,24 @@ mod tests {
});
}

#[test]
fn test_parse_file_paths() {
figment::Jail::expect_with(|jail| {
jail.create_file(
"foundry.toml",
r#"
[default]
ignored_warnings_from = ["something"]
"#,
)?;

let config = Config::load();
assert_eq!(config.ignored_file_paths, vec![Path::new("something").to_path_buf()]);

Ok(())
});
}

#[test]
fn test_parse_optimizer_settings() {
figment::Jail::expect_with(|jail| {
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/verify/etherscan/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl EtherscanFlattenedSource {
if out.has_error() {
let mut o = AggregatedCompilerOutput::default();
o.extend(version, out);
let diags = o.diagnostics(&[], Default::default());
let diags = o.diagnostics(&[], &[], Default::default());

eyre::bail!(
"\
Expand Down
38 changes: 37 additions & 1 deletion crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use foundry_test_utils::{
use semver::Version;
use std::{
env, fs,
path::PathBuf,
path::{Path, PathBuf},
process::{Command, Stdio},
str::FromStr,
};
Expand Down Expand Up @@ -666,6 +666,42 @@ contract ATest is DSTest {
}
);

// test that `forge build` does not print `(with warnings)` if file path is ignored
forgetest!(can_compile_without_warnings_ignored_file_paths, |prj, cmd| {
// Ignoring path and setting empty error_codes as default would set would set some error codes
prj.write_config(Config {
ignored_file_paths: vec![Path::new("src").to_path_buf()],
ignored_error_codes: vec![],
..Default::default()
});

prj.add_raw_source(
"src/example.sol",
r"
pragma solidity *;
contract A {
function testExample() public {}
}
",
)
.unwrap();

cmd.args(["build", "--force"]);
let out = cmd.stdout_lossy();
// expect no warning as path is ignored
assert!(out.contains("Compiler run successful!"));
assert!(!out.contains("Compiler run successful with warnings:"));

// Reconfigure without ignored paths or error codes and check for warnings
// need to reset empty error codes as default would set some error codes
prj.write_config(Config { ignored_error_codes: vec![], ..Default::default() });

let out = cmd.stdout_lossy();
// expect warnings as path is not ignored
assert!(out.contains("Compiler run successful with warnings:"), "{out}");
assert!(out.contains("Warning") && out.contains("SPDX-License-Identifier"), "{out}");
});

// test that `forge build` does not print `(with warnings)` if there arent any
forgetest!(can_compile_without_warnings, |prj, cmd| {
let config = Config {
Expand Down
1 change: 1 addition & 0 deletions crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ forgetest!(can_extract_config_values, |prj, cmd| {
"src/DssSpell.sol:DssExecLib:0x8De6DDbCd5053d32292AAA0D2105A32d108484a6".to_string()
],
ignored_error_codes: vec![],
ignored_file_paths: vec![],
deny_warnings: false,
via_ir: true,
rpc_storage_caching: StorageCachingConfig {
Expand Down

0 comments on commit 113ab8b

Please sign in to comment.