From 41bcb35fd992e2285925c249f6c33618ab3fe08a Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 7 Feb 2022 18:05:29 +0100 Subject: [PATCH] fix(config): parse libraries as string (#691) * fix(config): parse libraries as string * fix failing test --- config/src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- config/src/utils.rs | 20 ++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/config/src/lib.rs b/config/src/lib.rs index f47f9e52a..0ccde5493 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -611,10 +611,12 @@ impl From for Figment { .merge(ForcedSnakeCaseData( Toml::file(Env::var_or("FOUNDRY_CONFIG", Config::FILE_NAME)).nested(), )) - .merge(Env::prefixed("DAPP_").ignore(&["REMAPPINGS"]).global()) + .merge(Env::prefixed("DAPP_").ignore(&["REMAPPINGS", "LIBRARIES"]).global()) .merge(Env::prefixed("DAPP_TEST_").global()) .merge(DappEnvCompatProvider) - .merge(Env::prefixed("FOUNDRY_").ignore(&["PROFILE", "REMAPPINGS"]).global()) + .merge( + Env::prefixed("FOUNDRY_").ignore(&["PROFILE", "REMAPPINGS", "LIBRARIES"]).global(), + ) .select(profile.clone()); // we try to merge remappings after we've merged all other providers, this prevents @@ -845,6 +847,11 @@ impl Provider for DappEnvCompatProvider { dict.insert("optimizer".to_string(), (val == 1).into()); } + // libraries in env vars either as `[..]` or single string separated by comma + if let Ok(val) = env::var("DAPP_LIBRARIES").or_else(|_| env::var("FOUNDRY_LIBRARIES")) { + dict.insert("libraries".to_string(), utils::to_array_value(&val)?); + } + Ok(Map::from([(Config::selected_profile(), dict)])) } } @@ -1373,6 +1380,38 @@ mod tests { }); } + #[test] + fn can_parse_libraries() { + figment::Jail::expect_with(|jail| { + jail.set_env( + "DAPP_LIBRARIES", + "[src/DssSpell.sol:DssExecLib:0x8De6DDbCd5053d32292AAA0D2105A32d108484a6]", + ); + let config = Config::load(); + assert_eq!( + config.libraries, + vec!["src/DssSpell.sol:DssExecLib:0x8De6DDbCd5053d32292AAA0D2105A32d108484a6" + .to_string()] + ); + jail.set_env( + "DAPP_LIBRARIES", + "src/DssSpell.sol:DssExecLib:0x8De6DDbCd5053d32292AAA0D2105A32d108484a6,src/DssSpell.sol:DssExecLib:0x8De6DDbCd5053d32292AAA0D2105A32d108484a6", + ); + let config = Config::load(); + assert_eq!( + config.libraries, + vec![ + "src/DssSpell.sol:DssExecLib:0x8De6DDbCd5053d32292AAA0D2105A32d108484a6" + .to_string(), + "src/DssSpell.sol:DssExecLib:0x8De6DDbCd5053d32292AAA0D2105A32d108484a6" + .to_string() + ] + ); + + Ok(()) + }); + } + #[test] fn config_roundtrip() { figment::Jail::expect_with(|jail| { diff --git a/config/src/utils.rs b/config/src/utils.rs index 6a8e8c484..5945603e1 100644 --- a/config/src/utils.rs +++ b/config/src/utils.rs @@ -7,6 +7,7 @@ use ethers_solc::{ error::SolcError, remappings::{Remapping, RemappingError}, }; +use figment::value::Value; /// Loads the config for the current project workspace pub fn load_config() -> Config { @@ -122,3 +123,22 @@ pub fn parse_libraries( } Ok(libraries) } + +/// Converts the `val` into a `figment::Value::Array` +/// +/// The values should be separated by commas, surrounding brackets are also supported `[a,b,c]` +pub fn to_array_value(val: &str) -> Result { + let value: Value = match Value::from(val) { + Value::String(_, val) => val + .trim_start_matches('[') + .trim_end_matches(']') + .split(',') + .map(|s| s.to_string()) + .collect::>() + .into(), + Value::Empty(_, _) => Vec::::new().into(), + val @ Value::Array(_, _) => val, + _ => return Err(format!("Invalid value `{}`, expected an array", val).into()), + }; + Ok(value) +}