Repository: gakonst/ethers-rs
Pull request #1591 (2022-08-11 22:50:58)

test(solc): ensure trigger rebuild on settings change
<!--
Thank you for your Pull Request. Please provide a description above and review
the requirements below.

Bug fixes and new features should include tests.

Contributors guide: https://github.com/gakonst/ethers-rs/blob/master/CONTRIBUTING.md

The contributors guide includes instructions for running rustfmt and building the
documentation.
-->

## Motivation
Add test for https://github.com/foundry-rs/foundry/issues/2726
<!--
Explain the context and why you're making that change. What is the problem
you're trying to solve? In some cases there is not a problem and this can be
thought of as being the motivation for your change.
-->

## Solution
ensure changing the optimizer triggers rebuild
<!--
Summarize the solution and provide any necessary context needed to understand
the code change.
-->

## PR Checklist

- [ ] Added Tests
- [ ] Added Documentation
- [ ] Updated the changelog



Patch:
diff --git a/ethers-solc/src/project_util/mod.rs b/ethers-solc/src/project_util/mod.rs
index 7d0c8b22..b5a35e5d 100644
--- a/ethers-solc/src/project_util/mod.rs
+++ b/ethers-solc/src/project_util/mod.rs
@@ -200,6 +200,20 @@ contract {} {{}}
         )
     }
 
+    /// Adds a new test file inside the project's test dir
+    pub fn add_test(&self, name: impl AsRef<str>, content: impl AsRef<str>) -> Result<PathBuf> {
+        let name = contract_file_name(name);
+        let tests = self.paths().tests.join(name);
+        create_contract_file(tests, content)
+    }
+
+    /// Adds a new script file inside the project's script dir
+    pub fn add_script(&self, name: impl AsRef<str>, content: impl AsRef<str>) -> Result<PathBuf> {
+        let name = contract_file_name(name);
+        let script = self.paths().scripts.join(name);
+        create_contract_file(script, content)
+    }
+
     /// Adds a new source file inside the project's source dir
     pub fn add_source(&self, name: impl AsRef<str>, content: impl AsRef<str>) -> Result<PathBuf> {
         let name = contract_file_name(name);


Test patch:
diff --git a/ethers-solc/tests/project.rs b/ethers-solc/tests/project.rs
index 0a0a023b..7ce00970 100644
--- a/ethers-solc/tests/project.rs
+++ b/ethers-solc/tests/project.rs
@@ -2138,6 +2138,55 @@ fn can_checkout_repo() {
     let _artifacts = project.artifacts_snapshot().unwrap();
 }
 
+#[test]
+fn can_detect_config_changes() {
+    let mut project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();
+
+    let remapping = project.paths().libraries[0].join("remapping");
+    project
+        .paths_mut()
+        .remappings
+        .push(Remapping::from_str(&format!("remapping/={}/", remapping.display())).unwrap());
+
+    project
+        .add_source(
+            "Foo",
+            r#"
+    pragma solidity ^0.8.10;
+    import "remapping/Bar.sol";
+
+    contract Foo {}
+   "#,
+        )
+        .unwrap();
+    project
+        .add_lib(
+            "remapping/Bar",
+            r#"
+    pragma solidity ^0.8.10;
+
+    contract Bar {}
+    "#,
+        )
+        .unwrap();
+
+    let compiled = project.compile().unwrap();
+    assert!(!compiled.has_compiler_errors());
+
+    let cache = SolFilesCache::read(&project.paths().cache).unwrap();
+    assert_eq!(cache.files.len(), 2);
+
+    // nothing to compile
+    let compiled = project.compile().unwrap();
+    assert!(compiled.is_unchanged());
+
+    project.project_mut().solc_config.settings.optimizer.enabled = Some(true);
+
+    let compiled = project.compile().unwrap();
+    assert!(!compiled.has_compiler_errors());
+    assert!(!compiled.is_unchanged());
+}
+
 #[test]
 fn can_add_basic_contract_and_library() {
     let mut project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();