feat(solc): add helper to checkout temp projects (#1581)

Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
This commit is contained in:
Matthias Seitz 2022-08-09 18:24:54 +02:00 committed by GitHub
parent 64ac7d01ab
commit 2f0dbad1fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -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<ConfigurableArtifacts> {
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<str>) -> Result<Self> {
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<Path>, target_dir: impl AsRef<Path>) -> Resul
Ok(())
}
/// Clones a remote repository into the specified directory.
pub fn clone_remote(
repo_url: &str,
target_dir: impl AsRef<Path>,
) -> std::io::Result<process::Output> {
Command::new("git")
.args(["clone", "--depth", "1", "--recursive", repo_url])
.arg(target_dir.as_ref())
.output()
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -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::<ConfigurableArtifacts>::dapptools().unwrap();