From 2f0dbad1fd8b79fcc2000646e61ef2e43b5f8b79 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 9 Aug 2022 18:24:54 +0200 Subject: [PATCH] feat(solc): add helper to checkout temp projects (#1581) Co-authored-by: Georgios Konstantopoulos --- ethers-solc/src/project_util/mod.rs | 33 +++++++++++++++++++++++++++++ ethers-solc/tests/project.rs | 9 ++++++++ 2 files changed, 42 insertions(+) diff --git a/ethers-solc/src/project_util/mod.rs b/ethers-solc/src/project_util/mod.rs index 107dc360..5b2b95d8 100644 --- a/ethers-solc/src/project_util/mod.rs +++ b/ethers-solc/src/project_util/mod.rs @@ -16,6 +16,8 @@ use fs_extra::{dir, file}; use std::{ fmt, path::{Path, PathBuf}, + process, + process::Command, }; use tempfile::TempDir; @@ -407,6 +409,26 @@ impl TempProject { Ok(project) } + /// Clones the given repo into a temp dir, initializes it recursively and configures it. + /// + /// # Example + /// + /// ``` + /// use ethers_solc::project_util::TempProject; + /// # fn t() { + /// let project = TempProject::checkout("transmissions11/solmate").unwrap(); + /// # } + /// ``` + pub fn checkout(repo: impl AsRef) -> Result { + let tmp_dir = tempdir("tmp_checkout")?; + clone_remote(&format!("https://github.com/{}", repo.as_ref()), tmp_dir.path()) + .map_err(|err| SolcIoError::new(err, tmp_dir.path()))?; + let paths = ProjectPathsConfig::dapptools(tmp_dir.path())?; + + let inner = Project::builder().paths(paths).build()?; + Ok(Self::create_new(tmp_dir, inner)?) + } + /// Create a new temporary project and populate it with mock files /// /// ```no_run @@ -507,6 +529,17 @@ pub fn copy_dir(source: impl AsRef, target_dir: impl AsRef) -> Resul Ok(()) } +/// Clones a remote repository into the specified directory. +pub fn clone_remote( + repo_url: &str, + target_dir: impl AsRef, +) -> std::io::Result { + Command::new("git") + .args(["clone", "--depth", "1", "--recursive", repo_url]) + .arg(target_dir.as_ref()) + .output() +} + #[cfg(test)] mod tests { use super::*; diff --git a/ethers-solc/tests/project.rs b/ethers-solc/tests/project.rs index a468c43c..cb3cbdfd 100644 --- a/ethers-solc/tests/project.rs +++ b/ethers-solc/tests/project.rs @@ -2129,6 +2129,15 @@ fn can_handle_conflicting_files() { ); } +#[test] +fn can_checkout_repo() { + let project = TempProject::checkout("transmissions11/solmate").unwrap(); + + let compiled = project.compile().unwrap(); + assert!(!compiled.has_compiler_errors()); + let _artifacts = project.artifacts_snapshot().unwrap(); +} + #[test] fn can_add_basic_contract_and_library() { let mut project = TempProject::::dapptools().unwrap();