Skip to content

Commit

Permalink
feat: rewrite inline config using figment (foundry-rs#9414)
Browse files Browse the repository at this point in the history
* feat: rewrite inline config using figment

* wip

* wip

* fix: use same GasLimit type

* wip

* fixes

* tests

* test fixes

* fmt

* test update
  • Loading branch information
DaniPopes authored and rplusq committed Nov 29, 2024
1 parent 182a184 commit 88b40d3
Show file tree
Hide file tree
Showing 32 changed files with 969 additions and 1,162 deletions.
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.

5 changes: 3 additions & 2 deletions crates/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ dirs-next = "2"
dunce.workspace = true
eyre.workspace = true
figment = { workspace = true, features = ["toml", "env"] }
globset = "0.4"
glob = "0.3"
globset = "0.4"
Inflector = "0.11"
number_prefix = "0.4"
itertools.workspace = true
mesc.workspace = true
number_prefix = "0.4"
regex.workspace = true
reqwest.workspace = true
semver = { workspace = true, features = ["serde"] }
Expand Down
108 changes: 1 addition & 107 deletions crates/config/src/fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//! Configuration for fuzz testing.
use crate::inline::{
parse_config_bool, parse_config_u32, InlineConfigParser, InlineConfigParserError,
INLINE_CONFIG_FUZZ_KEY,
};
use alloy_primitives::U256;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
Expand Down Expand Up @@ -53,50 +49,13 @@ impl FuzzConfig {
/// Creates fuzz configuration to write failures in `{PROJECT_ROOT}/cache/fuzz` dir.
pub fn new(cache_dir: PathBuf) -> Self {
Self {
runs: 256,
max_test_rejects: 65536,
seed: None,
dictionary: FuzzDictionaryConfig::default(),
gas_report_samples: 256,
failure_persist_dir: Some(cache_dir),
failure_persist_file: Some("failures".to_string()),
show_logs: false,
..Default::default()
}
}
}

impl InlineConfigParser for FuzzConfig {
fn config_key() -> String {
INLINE_CONFIG_FUZZ_KEY.into()
}

fn try_merge(&self, configs: &[String]) -> Result<Option<Self>, InlineConfigParserError> {
let overrides: Vec<(String, String)> = Self::get_config_overrides(configs);

if overrides.is_empty() {
return Ok(None)
}

let mut conf_clone = self.clone();

for pair in overrides {
let key = pair.0;
let value = pair.1;
match key.as_str() {
"runs" => conf_clone.runs = parse_config_u32(key, value)?,
"max-test-rejects" => conf_clone.max_test_rejects = parse_config_u32(key, value)?,
"dictionary-weight" => {
conf_clone.dictionary.dictionary_weight = parse_config_u32(key, value)?
}
"failure-persist-file" => conf_clone.failure_persist_file = Some(value),
"show-logs" => conf_clone.show_logs = parse_config_bool(key, value)?,
_ => Err(InlineConfigParserError::InvalidConfigProperty(key))?,
}
}
Ok(Some(conf_clone))
}
}

/// Contains for fuzz testing
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FuzzDictionaryConfig {
Expand Down Expand Up @@ -132,68 +91,3 @@ impl Default for FuzzDictionaryConfig {
}
}
}

#[cfg(test)]
mod tests {
use crate::{inline::InlineConfigParser, FuzzConfig};

#[test]
fn unrecognized_property() {
let configs = &["forge-config: default.fuzz.unknownprop = 200".to_string()];
let base_config = FuzzConfig::default();
if let Err(e) = base_config.try_merge(configs) {
assert_eq!(e.to_string(), "'unknownprop' is an invalid config property");
} else {
unreachable!()
}
}

#[test]
fn successful_merge() {
let configs = &[
"forge-config: default.fuzz.runs = 42424242".to_string(),
"forge-config: default.fuzz.dictionary-weight = 42".to_string(),
"forge-config: default.fuzz.failure-persist-file = fuzz-failure".to_string(),
];
let base_config = FuzzConfig::default();
let merged: FuzzConfig = base_config.try_merge(configs).expect("No errors").unwrap();
assert_eq!(merged.runs, 42424242);
assert_eq!(merged.dictionary.dictionary_weight, 42);
assert_eq!(merged.failure_persist_file, Some("fuzz-failure".to_string()));
}

#[test]
fn merge_is_none() {
let empty_config = &[];
let base_config = FuzzConfig::default();
let merged = base_config.try_merge(empty_config).expect("No errors");
assert!(merged.is_none());
}

#[test]
fn merge_is_none_unrelated_property() {
let unrelated_configs = &["forge-config: default.invariant.runs = 2".to_string()];
let base_config = FuzzConfig::default();
let merged = base_config.try_merge(unrelated_configs).expect("No errors");
assert!(merged.is_none());
}

#[test]
fn override_detection() {
let configs = &[
"forge-config: default.fuzz.runs = 42424242".to_string(),
"forge-config: ci.fuzz.runs = 666666".to_string(),
"forge-config: default.invariant.runs = 2".to_string(),
"forge-config: default.fuzz.dictionary-weight = 42".to_string(),
];
let variables = FuzzConfig::get_config_overrides(configs);
assert_eq!(
variables,
vec![
("runs".into(), "42424242".into()),
("runs".into(), "666666".into()),
("dictionary-weight".into(), "42".into())
]
);
}
}
169 changes: 0 additions & 169 deletions crates/config/src/inline/conf_parser.rs

This file was deleted.

44 changes: 0 additions & 44 deletions crates/config/src/inline/error.rs

This file was deleted.

Loading

0 comments on commit 88b40d3

Please sign in to comment.