Repository: gakonst/ethers-rs
Pull request #1589 (2022-08-11 21:04:18)

chore(etherscan): add set cache helper
<!--
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 smol helper to adjust cache path
<!--
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

<!--
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-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs
index b73a4849..8b89f05e 100644
--- a/ethers-etherscan/src/lib.rs
+++ b/ethers-etherscan/src/lib.rs
@@ -117,6 +117,12 @@ impl Client {
         Self::new(chain, api_key)
     }
 
+    /// Sets the root to the cache dir and the ttl to use
+    pub fn set_cache(&mut self, root: impl Into<PathBuf>, ttl: Duration) -> &mut Self {
+        self.cache = Some(Cache { root: root.into(), ttl });
+        self
+    }
+
     pub fn etherscan_api_url(&self) -> &Url {
         &self.etherscan_api_url
     }
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();