Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: avoid cloning CompileOutput to parse inline config #5683

Merged
merged 6 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Cargo.lock

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

7 changes: 2 additions & 5 deletions crates/config/src/inline/conf_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,8 @@ where
fn validate_configs(natspec: &NatSpec) -> Result<(), InlineConfigError> {
let config_key = Self::config_key();

let configs = natspec
.config_lines()
.into_iter()
.filter(|l| l.contains(&config_key))
.collect::<Vec<String>>();
let configs =
natspec.config_lines().filter(|l| l.contains(&config_key)).collect::<Vec<String>>();

Self::default().try_merge(&configs).map_err(|e| {
let line = natspec.debug_context();
Expand Down
59 changes: 26 additions & 33 deletions crates/config/src/inline/natspec.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::{collections::BTreeMap, path::Path};

use super::{remove_whitespaces, INLINE_CONFIG_PREFIX, INLINE_CONFIG_PREFIX_SELECTED_PROFILE};
use ethers_solc::{
artifacts::{ast::NodeType, Node},
ProjectCompileOutput,
};
use serde_json::Value;

use super::{remove_whitespaces, INLINE_CONFIG_PREFIX, INLINE_CONFIG_PREFIX_SELECTED_PROFILE};
use std::{collections::BTreeMap, path::Path};

/// Convenient struct to hold in-line per-test configurations
pub struct NatSpec {
Expand All @@ -26,21 +24,19 @@ impl NatSpec {
/// Factory function that extracts a vector of [`NatSpec`] instances from
/// a solc compiler output. The root path is to express contract base dirs.
/// That is essential to match per-test configs at runtime.
pub fn parse<P>(output: &ProjectCompileOutput, root: &P) -> Vec<Self>
where
P: AsRef<Path>,
{
pub fn parse(output: &ProjectCompileOutput, root: &Path) -> Vec<Self> {
let mut natspecs: Vec<Self> = vec![];

let output = output.clone();
for artifact in output.with_stripped_file_prefixes(root).into_artifacts() {
if let Some(ast) = artifact.1.ast.as_ref() {
let contract: String = artifact.0.identifier();
if let Some(node) = contract_root_node(&ast.nodes, &contract) {
apply(&mut natspecs, &contract, node)
}
}
for (id, artifact) in output.artifact_ids() {
let Some(ast) = &artifact.ast else { continue };
let path = id.source.as_path();
let path = path.strip_prefix(root).unwrap_or(path);
// id.identifier
let contract = format!("{}:{}", path.display(), id.name);
let Some(node) = contract_root_node(&ast.nodes, &contract) else { continue };
apply(&mut natspecs, &contract, node)
}

natspecs
}

Expand All @@ -52,24 +48,21 @@ impl NatSpec {
}

/// Returns a list of configuration lines that match the current profile
pub fn current_profile_configs(&self) -> Vec<String> {
let prefix: &str = INLINE_CONFIG_PREFIX_SELECTED_PROFILE.as_ref();
self.config_lines_with_prefix(prefix)
pub fn current_profile_configs(&self) -> impl Iterator<Item = String> + '_ {
self.config_lines_with_prefix(INLINE_CONFIG_PREFIX_SELECTED_PROFILE.as_str())
}

/// Returns a list of configuration lines that match a specific string prefix
pub fn config_lines_with_prefix<S: Into<String>>(&self, prefix: S) -> Vec<String> {
let prefix: String = prefix.into();
self.config_lines().into_iter().filter(|l| l.starts_with(&prefix)).collect()
pub fn config_lines_with_prefix<'a>(
&'a self,
prefix: &'a str,
) -> impl Iterator<Item = String> + 'a {
self.config_lines().filter(move |l| l.starts_with(prefix))
}

/// Returns a list of all the configuration lines available in the natspec
pub fn config_lines(&self) -> Vec<String> {
self.docs
.split('\n')
.map(remove_whitespaces)
.filter(|line| line.contains(INLINE_CONFIG_PREFIX))
.collect::<Vec<String>>()
pub fn config_lines(&self) -> impl Iterator<Item = String> + '_ {
self.docs.lines().map(remove_whitespaces).filter(|line| line.contains(INLINE_CONFIG_PREFIX))
}
}

Expand Down Expand Up @@ -137,7 +130,7 @@ fn get_fn_docs(fn_data: &BTreeMap<String, Value>) -> Option<(String, String)> {
let mut src_line = fn_docs
.get("src")
.map(|src| src.to_string())
.unwrap_or(String::from("<no-src-line-available>"));
.unwrap_or_else(|| String::from("<no-src-line-available>"));

src_line.retain(|c| c != '"');
return Some((comment.into(), src_line))
Expand All @@ -158,7 +151,7 @@ mod tests {
let natspec = natspec();
let config_lines = natspec.config_lines();
assert_eq!(
config_lines,
config_lines.collect::<Vec<_>>(),
vec![
"forge-config:default.fuzz.runs=600".to_string(),
"forge-config:ci.fuzz.runs=500".to_string(),
Expand All @@ -173,7 +166,7 @@ mod tests {
let config_lines = natspec.current_profile_configs();

assert_eq!(
config_lines,
config_lines.collect::<Vec<_>>(),
vec![
"forge-config:default.fuzz.runs=600".to_string(),
"forge-config:default.invariant.runs=1".to_string()
Expand All @@ -186,9 +179,9 @@ mod tests {
use super::INLINE_CONFIG_PREFIX;
let natspec = natspec();
let prefix = format!("{INLINE_CONFIG_PREFIX}:default");
let config_lines = natspec.config_lines_with_prefix(prefix);
let config_lines = natspec.config_lines_with_prefix(&prefix);
assert_eq!(
config_lines,
config_lines.collect::<Vec<_>>(),
vec![
"forge-config:default.fuzz.runs=600".to_string(),
"forge-config:default.invariant.runs=1".to_string()
Expand Down
3 changes: 1 addition & 2 deletions crates/forge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ impl TestArgs {
let test_options: TestOptions = TestOptionsBuilder::default()
.fuzz(config.fuzz)
.invariant(config.invariant)
.compile_output(&output)
.profiles(profiles)
.build(project_root)?;
.build(&output, project_root)?;

// Determine print verbosity and executor verbosity
let verbosity = evm_opts.verbosity;
Expand Down
Loading