From 601cb1b75e99407aa739cf1e1d7b96734108b84c Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 24 Feb 2022 17:43:35 +0100 Subject: [PATCH] test(solc): add capture test for curly bracket imports (#956) * test(solc): add capture test for curly bracket imports * chore: rustfmt --- ethers-solc/src/resolver/mod.rs | 41 +++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/ethers-solc/src/resolver/mod.rs b/ethers-solc/src/resolver/mod.rs index 104865fd..20b77c96 100644 --- a/ethers-solc/src/resolver/mod.rs +++ b/ethers-solc/src/resolver/mod.rs @@ -807,13 +807,7 @@ fn parse_data(content: &str, file: &Path) -> SolData { .map(|(cap, name)| { SolDataUnit::new(name.as_str().to_owned(), cap.to_owned().into()) }); - imports = - capture_outer_and_inner(content, &utils::RE_SOL_IMPORT, &["p1", "p2", "p3", "p4"]) - .iter() - .map(|(cap, m)| { - SolDataUnit::new(PathBuf::from(m.as_str()), cap.to_owned().into()) - }) - .collect(); + imports = capture_imports(content); } }; let license = content.lines().next().and_then(|line| { @@ -846,10 +840,43 @@ fn capture_outer_and_inner<'a>( .collect() } +fn capture_imports(content: &str) -> Vec> { + capture_outer_and_inner(content, &utils::RE_SOL_IMPORT, &["p1", "p2", "p3", "p4"]) + .iter() + .map(|(cap, m)| SolDataUnit::new(PathBuf::from(m.as_str()), cap.to_owned().into())) + .collect() +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn can_capture_curly_imports() { + let content = r#" +import { T } from "../Test.sol"; +import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; +import {DsTest} from "ds-test/test.sol"; +"#; + + let captured_imports = + capture_imports(content).into_iter().map(|s| s.data).collect::>(); + + let expected = + utils::find_import_paths(content).map(|m| m.as_str().into()).collect::>(); + + assert_eq!(captured_imports, expected); + + assert_eq!( + captured_imports, + vec![ + PathBuf::from("../Test.sol"), + "@openzeppelin/contracts/utils/ReentrancyGuard.sol".into(), + "ds-test/test.sol".into(), + ] + ); + } + #[test] fn can_resolve_hardhat_dependency_graph() { let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/hardhat-sample");