Skip to content

Commit

Permalink
fix(config): parse libraries as string (gakonst#691)
Browse files Browse the repository at this point in the history
* fix(config): parse libraries as string

* fix failing test
  • Loading branch information
mattsse authored Feb 7, 2022
1 parent 809500f commit 41bcb35
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
43 changes: 41 additions & 2 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,12 @@ impl From<Config> 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
Expand Down Expand Up @@ -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)]))
}
}
Expand Down Expand Up @@ -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| {
Expand Down
20 changes: 20 additions & 0 deletions config/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Value, figment::Error> {
let value: Value = match Value::from(val) {
Value::String(_, val) => val
.trim_start_matches('[')
.trim_end_matches(']')
.split(',')
.map(|s| s.to_string())
.collect::<Vec<_>>()
.into(),
Value::Empty(_, _) => Vec::<Value>::new().into(),
val @ Value::Array(_, _) => val,
_ => return Err(format!("Invalid value `{}`, expected an array", val).into()),
};
Ok(value)
}

0 comments on commit 41bcb35

Please sign in to comment.