Skip to content

Commit

Permalink
Merge pull request #2 from Troublor/forge_clone
Browse files Browse the repository at this point in the history
Test: fix rate limit issue for forge clone tests
  • Loading branch information
ZhangZhuoSJTU authored Mar 27, 2024
2 parents 8432289 + c21f805 commit 1fd2b18
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ globset = "0.4"
paste = "1.0"
path-slash = "0.2"
pretty_assertions.workspace = true
serial_test = "3"
svm = { package = "svm-rs", version = "0.4", default-features = false, features = ["rustls"] }
tempfile = "3"
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
Expand Down
39 changes: 27 additions & 12 deletions crates/forge/bin/cmd/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clap::{Parser, ValueHint};
use eyre::Result;
use foundry_block_explorers::{contract::Metadata, Client};
use foundry_cli::opts::EtherscanOpts;
use foundry_cli::utils::Git;
use foundry_common::fs;
use foundry_compilers::artifacts::Settings;
use foundry_compilers::remappings::{RelativeRemapping, Remapping};
Expand Down Expand Up @@ -67,8 +68,8 @@ impl CloneArgs {
fs::remove_file(root.join("script/Counter.s.sol"))?;

// dump sources and update the remapping in configuration
let remappings = dump_sources(&meta, root.clone())?;
Config::update_at(root.clone(), |config, doc| {
let remappings = dump_sources(&meta, &root)?;
Config::update_at(&root, |config, doc| {
let profile = config.profile.as_str().as_str();
let mut remapping_array = toml_edit::Array::new();
for r in remappings {
Expand All @@ -80,10 +81,15 @@ impl CloneArgs {
})?;

// update configuration
Config::update_at(root, |config, doc| {
Config::update_at(&root, |config, doc| {
update_config_by_metadata(config, doc, &meta).is_ok()
})?;

// Git add and commit the changes
let git = Git::new(&root).quiet(true);
git.add(Some("--all"))?;
git.commit("chore: forge clone")?;

Ok(())
}
}
Expand Down Expand Up @@ -222,13 +228,12 @@ fn update_config_by_metadata(

/// Dump the contract sources to the root directory.
/// The sources are dumped to the `src` directory.
/// The library sources are dumped to the `lib` directory.
/// IO errors may be returned.
fn dump_sources(meta: &Metadata, root: PathBuf) -> Result<Vec<RelativeRemapping>> {
fn dump_sources(meta: &Metadata, root: &PathBuf) -> Result<Vec<RelativeRemapping>> {
// get config
let path_config = ProjectPathsConfig::builder().build_with_root(&root);
let src_dir = root.join(path_config.sources.clone()).canonicalize()?;
let contract_name = meta.contract_name.clone();
let path_config = ProjectPathsConfig::builder().build_with_root(root);
let src_dir = root.join(&path_config.sources).canonicalize()?;
let contract_name = &meta.contract_name;
let source_tree = meta.source_tree();

// first we dump the sources to a temporary directory
Expand All @@ -240,7 +245,7 @@ fn dump_sources(meta: &Metadata, root: PathBuf) -> Result<Vec<RelativeRemapping>
// then we move the sources to the correct directories
// 1. we will first load existing remappings if necessary
// make sure this happens before dumping sources
let mut remappings: Vec<Remapping> = Remapping::find_many(root.clone());
let mut remappings: Vec<Remapping> = Remapping::find_many(root);
// we also load the original remappings from the metadata
remappings.extend(meta.settings()?.remappings);

Expand All @@ -255,7 +260,7 @@ fn dump_sources(meta: &Metadata, root: PathBuf) -> Result<Vec<RelativeRemapping>
for e in read_dir(entry.path())? {
let e = e?;
let dest = src_dir.join(e.file_name());
std::fs::rename(e.path(), dest.clone())?;
std::fs::rename(e.path(), &dest)?;
remappings.push(Remapping {
context: None,
name: e.file_name().to_string_lossy().to_string(),
Expand All @@ -265,7 +270,7 @@ fn dump_sources(meta: &Metadata, root: PathBuf) -> Result<Vec<RelativeRemapping>
} else {
// move the other folders to src
let dest = src_dir.join(entry.file_name());
std::fs::rename(entry.path(), dest.clone())?;
std::fs::rename(entry.path(), &dest)?;
remappings.push(Remapping {
context: None,
name: entry.file_name().to_string_lossy().to_string(),
Expand All @@ -282,17 +287,23 @@ fn dump_sources(meta: &Metadata, root: PathBuf) -> Result<Vec<RelativeRemapping>

#[cfg(test)]
mod tests {
use std::path::PathBuf;
use std::{path::PathBuf, thread::sleep, time::Duration};

use super::CloneArgs;
use foundry_common::compile::ProjectCompiler;
use foundry_compilers::{Artifact, ProjectCompileOutput};
use foundry_config::Config;
use hex::ToHex;
use serial_test::serial;
use tempfile;

fn assert_successful_compilation(root: &PathBuf) -> ProjectCompileOutput {
// wait 5 second to avoid etherscan rate limit
println!("wait for 5 seconds to avoid etherscan rate limit");
sleep(Duration::from_secs(5));

println!("project_root: {:#?}", root);

// change directory to the root
std::env::set_current_dir(root).unwrap();
let config = Config::load();
Expand Down Expand Up @@ -322,6 +333,7 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[serial]
async fn test_clone_single_file_contract() {
let project_root = tempfile::tempdir().unwrap().path().to_path_buf();
let args = CloneArgs {
Expand All @@ -337,6 +349,7 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[serial]
async fn test_clone_contract_with_optimization_details() {
let project_root = tempfile::tempdir().unwrap().path().to_path_buf();
let args = CloneArgs {
Expand All @@ -352,6 +365,7 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[serial]
async fn test_clone_contract_with_libraries() {
let project_root = tempfile::tempdir().unwrap().path().to_path_buf();
let args = CloneArgs {
Expand All @@ -367,6 +381,7 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[serial]
async fn test_clone_contract_with_metadata() {
let project_root = tempfile::tempdir().unwrap().path().to_path_buf();
let args = CloneArgs {
Expand Down

0 comments on commit 1fd2b18

Please sign in to comment.