2021-10-30 17:59:44 +00:00
|
|
|
//! project tests
|
|
|
|
|
2022-04-28 12:18:24 +00:00
|
|
|
use ethers_core::types::Address;
|
2022-01-05 21:46:57 +00:00
|
|
|
use ethers_solc::{
|
2022-05-19 18:02:10 +00:00
|
|
|
artifacts::{
|
2022-06-08 10:30:41 +00:00
|
|
|
BytecodeHash, DevDoc, ErrorDoc, EventDoc, Libraries, MethodDoc, ModelCheckerEngine::CHC,
|
|
|
|
ModelCheckerSettings, UserDoc, UserDocNotice,
|
2022-05-19 18:02:10 +00:00
|
|
|
},
|
2022-06-15 19:44:20 +00:00
|
|
|
buildinfo::BuildInfo,
|
2022-01-05 21:46:57 +00:00
|
|
|
cache::{SolFilesCache, SOLIDITY_FILES_CACHE_FILENAME},
|
2023-03-19 18:45:30 +00:00
|
|
|
error::SolcError,
|
2022-06-30 16:19:55 +00:00
|
|
|
info::ContractInfo,
|
2022-01-05 21:46:57 +00:00
|
|
|
project_util::*,
|
|
|
|
remappings::Remapping,
|
2022-08-19 21:33:28 +00:00
|
|
|
Artifact, CompilerInput, ConfigurableArtifacts, ExtraOutputValues, Graph, Project,
|
|
|
|
ProjectCompileOutput, ProjectPathsConfig, Solc, TestFileFilter,
|
2022-01-05 21:46:57 +00:00
|
|
|
};
|
2022-02-04 16:20:24 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2022-05-14 23:29:45 +00:00
|
|
|
use semver::Version;
|
2022-07-24 21:39:37 +00:00
|
|
|
use std::{
|
|
|
|
collections::{BTreeMap, HashMap, HashSet},
|
|
|
|
fs, io,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
str::FromStr,
|
|
|
|
};
|
2022-02-04 16:20:24 +00:00
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
fn init_tracing() {
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
|
|
|
.init();
|
|
|
|
}
|
2022-01-05 21:46:57 +00:00
|
|
|
|
2022-02-18 15:48:56 +00:00
|
|
|
#[test]
|
|
|
|
fn can_get_versioned_linkrefs() {
|
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/test-versioned-linkrefs");
|
|
|
|
let paths = ProjectPathsConfig::builder()
|
|
|
|
.sources(root.join("src"))
|
|
|
|
.lib(root.join("lib"))
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let project = Project::builder().paths(paths).ephemeral().no_artifacts().build().unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
#[test]
|
|
|
|
fn can_compile_hardhat_sample() {
|
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/hardhat-sample");
|
|
|
|
let paths = ProjectPathsConfig::builder()
|
|
|
|
.sources(root.join("contracts"))
|
2021-12-12 23:39:28 +00:00
|
|
|
.lib(root.join("node_modules"));
|
2022-02-17 15:31:35 +00:00
|
|
|
let project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
|
2021-10-30 17:59:44 +00:00
|
|
|
|
2021-11-03 08:05:09 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Greeter").is_some());
|
|
|
|
assert!(compiled.find_first("console").is_some());
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
// nothing to compile
|
2021-11-15 23:29:06 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Greeter").is_some());
|
|
|
|
assert!(compiled.find_first("console").is_some());
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
|
|
|
|
// delete artifacts
|
2021-12-12 23:39:28 +00:00
|
|
|
std::fs::remove_dir_all(&project.paths().artifacts).unwrap();
|
2021-11-15 23:29:06 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Greeter").is_some());
|
|
|
|
assert!(compiled.find_first("console").is_some());
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(!compiled.is_unchanged());
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compile_dapp_sample() {
|
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/dapp-sample");
|
2021-12-12 23:39:28 +00:00
|
|
|
let paths = ProjectPathsConfig::builder().sources(root.join("src")).lib(root.join("lib"));
|
2022-02-17 15:31:35 +00:00
|
|
|
let project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
|
2021-10-30 17:59:44 +00:00
|
|
|
|
2021-11-03 08:05:09 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_some());
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
// nothing to compile
|
2021-11-15 23:29:06 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_some());
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
|
2022-02-04 16:20:24 +00:00
|
|
|
let cache = SolFilesCache::read(project.cache_path()).unwrap();
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
// delete artifacts
|
2021-12-12 23:39:28 +00:00
|
|
|
std::fs::remove_dir_all(&project.paths().artifacts).unwrap();
|
2021-11-15 23:29:06 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_some());
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(!compiled.is_unchanged());
|
2022-02-04 16:20:24 +00:00
|
|
|
|
|
|
|
let updated_cache = SolFilesCache::read(project.cache_path()).unwrap();
|
|
|
|
assert_eq!(cache, updated_cache);
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
2021-11-26 16:49:19 +00:00
|
|
|
|
2022-03-10 18:42:02 +00:00
|
|
|
#[test]
|
|
|
|
fn can_compile_yul_sample() {
|
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/yul-sample");
|
|
|
|
let paths = ProjectPathsConfig::builder().sources(root);
|
|
|
|
let project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_some());
|
|
|
|
assert!(compiled.find_first("SimpleStore").is_some());
|
2022-03-10 18:42:02 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
// nothing to compile
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_some());
|
|
|
|
assert!(compiled.find_first("SimpleStore").is_some());
|
2022-03-10 18:42:02 +00:00
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
|
|
|
|
let cache = SolFilesCache::read(project.cache_path()).unwrap();
|
|
|
|
|
|
|
|
// delete artifacts
|
|
|
|
std::fs::remove_dir_all(&project.paths().artifacts).unwrap();
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_some());
|
|
|
|
assert!(compiled.find_first("SimpleStore").is_some());
|
2022-03-10 18:42:02 +00:00
|
|
|
assert!(!compiled.is_unchanged());
|
|
|
|
|
|
|
|
let updated_cache = SolFilesCache::read(project.cache_path()).unwrap();
|
|
|
|
assert_eq!(cache, updated_cache);
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:31:35 +00:00
|
|
|
#[test]
|
|
|
|
fn can_compile_configured() {
|
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/dapp-sample");
|
|
|
|
let paths = ProjectPathsConfig::builder().sources(root.join("src")).lib(root.join("lib"));
|
|
|
|
|
|
|
|
let handler = ConfigurableArtifacts {
|
|
|
|
additional_values: ExtraOutputValues {
|
|
|
|
metadata: true,
|
|
|
|
ir: true,
|
|
|
|
ir_optimized: true,
|
2022-06-30 16:16:47 +00:00
|
|
|
opcodes: true,
|
2022-02-17 15:31:35 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let settings = handler.settings();
|
|
|
|
let project = TempProject::with_artifacts(paths, handler).unwrap().with_settings(settings);
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
let artifact = compiled.find_first("Dapp").unwrap();
|
2022-02-17 15:31:35 +00:00
|
|
|
assert!(artifact.metadata.is_some());
|
2022-06-10 13:49:19 +00:00
|
|
|
assert!(artifact.raw_metadata.is_some());
|
2022-02-17 15:31:35 +00:00
|
|
|
assert!(artifact.ir.is_some());
|
|
|
|
assert!(artifact.ir_optimized.is_some());
|
2022-06-30 16:16:47 +00:00
|
|
|
assert!(artifact.opcodes.is_some());
|
2022-02-17 15:31:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 21:46:57 +00:00
|
|
|
#[test]
|
|
|
|
fn can_compile_dapp_detect_changes_in_libs() {
|
2022-02-17 15:31:35 +00:00
|
|
|
let mut project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();
|
2022-01-05 21:46:57 +00:00
|
|
|
|
|
|
|
let remapping = project.paths().libraries[0].join("remapping");
|
|
|
|
project
|
|
|
|
.paths_mut()
|
|
|
|
.remappings
|
2022-04-28 12:18:24 +00:00
|
|
|
.push(Remapping::from_str(&format!("remapping/={}/", remapping.display())).unwrap());
|
2022-01-05 21:46:57 +00:00
|
|
|
|
|
|
|
let src = project
|
|
|
|
.add_source(
|
|
|
|
"Foo",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "remapping/Bar.sol";
|
|
|
|
|
|
|
|
contract Foo {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let lib = project
|
|
|
|
.add_lib(
|
|
|
|
"remapping/Bar",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract Bar {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let graph = Graph::resolve(project.paths()).unwrap();
|
|
|
|
assert_eq!(graph.files().len(), 2);
|
|
|
|
assert_eq!(graph.files().clone(), HashMap::from([(src, 0), (lib, 1),]));
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Foo").is_some());
|
|
|
|
assert!(compiled.find_first("Bar").is_some());
|
2022-01-05 21:46:57 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
// nothing to compile
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Foo").is_some());
|
2022-01-05 21:46:57 +00:00
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
|
|
|
|
let cache = SolFilesCache::read(&project.paths().cache).unwrap();
|
|
|
|
assert_eq!(cache.files.len(), 2);
|
|
|
|
|
|
|
|
// overwrite lib
|
|
|
|
project
|
|
|
|
.add_lib(
|
|
|
|
"remapping/Bar",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
// changed lib
|
|
|
|
contract Bar {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let graph = Graph::resolve(project.paths()).unwrap();
|
|
|
|
assert_eq!(graph.files().len(), 2);
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Foo").is_some());
|
|
|
|
assert!(compiled.find_first("Bar").is_some());
|
2022-01-05 21:46:57 +00:00
|
|
|
// ensure change is detected
|
|
|
|
assert!(!compiled.is_unchanged());
|
|
|
|
}
|
|
|
|
|
2022-01-11 10:02:57 +00:00
|
|
|
#[test]
|
|
|
|
fn can_compile_dapp_detect_changes_in_sources() {
|
2022-02-17 15:31:35 +00:00
|
|
|
let project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();
|
2022-01-11 10:02:57 +00:00
|
|
|
|
|
|
|
let src = project
|
|
|
|
.add_source(
|
|
|
|
"DssSpell.t",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "./DssSpell.t.base.sol";
|
|
|
|
|
|
|
|
contract DssSpellTest is DssSpellTestBase { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let base = project
|
|
|
|
.add_source(
|
|
|
|
"DssSpell.t.base",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract DssSpellTestBase {
|
|
|
|
address deployed_spell;
|
|
|
|
function setUp() public {
|
|
|
|
deployed_spell = address(0xA867399B43aF7790aC800f2fF3Fa7387dc52Ec5E);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let graph = Graph::resolve(project.paths()).unwrap();
|
|
|
|
assert_eq!(graph.files().len(), 2);
|
|
|
|
assert_eq!(graph.files().clone(), HashMap::from([(base, 0), (src, 1),]));
|
|
|
|
assert_eq!(graph.imported_nodes(1).to_vec(), vec![0]);
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("DssSpellTest").is_some());
|
|
|
|
assert!(compiled.find_first("DssSpellTestBase").is_some());
|
2022-01-11 10:02:57 +00:00
|
|
|
|
|
|
|
// nothing to compile
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.is_unchanged());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("DssSpellTest").is_some());
|
|
|
|
assert!(compiled.find_first("DssSpellTestBase").is_some());
|
2022-01-11 10:02:57 +00:00
|
|
|
|
|
|
|
let cache = SolFilesCache::read(&project.paths().cache).unwrap();
|
|
|
|
assert_eq!(cache.files.len(), 2);
|
|
|
|
|
|
|
|
let mut artifacts = compiled.into_artifacts().collect::<HashMap<_, _>>();
|
|
|
|
|
|
|
|
// overwrite import
|
|
|
|
let _ = project
|
|
|
|
.add_source(
|
|
|
|
"DssSpell.t.base",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract DssSpellTestBase {
|
|
|
|
address deployed_spell;
|
|
|
|
function setUp() public {
|
|
|
|
deployed_spell = address(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let graph = Graph::resolve(project.paths()).unwrap();
|
|
|
|
assert_eq!(graph.files().len(), 2);
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("DssSpellTest").is_some());
|
|
|
|
assert!(compiled.find_first("DssSpellTestBase").is_some());
|
2022-01-11 10:02:57 +00:00
|
|
|
// ensure change is detected
|
|
|
|
assert!(!compiled.is_unchanged());
|
2022-02-04 16:20:24 +00:00
|
|
|
|
2022-01-11 10:02:57 +00:00
|
|
|
// and all recompiled artifacts are different
|
|
|
|
for (p, artifact) in compiled.into_artifacts() {
|
|
|
|
let other = artifacts.remove(&p).unwrap();
|
|
|
|
assert_ne!(artifact, other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:44:20 +00:00
|
|
|
#[test]
|
|
|
|
fn can_emit_build_info() {
|
|
|
|
let mut project = TempProject::dapptools().unwrap();
|
|
|
|
project.project_mut().build_info = true;
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "./B.sol";
|
|
|
|
contract A { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"B",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract B { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
let info_dir = project.project().build_info_path();
|
|
|
|
assert!(info_dir.exists());
|
|
|
|
|
|
|
|
let mut build_info_count = 0;
|
|
|
|
for entry in fs::read_dir(info_dir).unwrap() {
|
|
|
|
let _info = BuildInfo::read(entry.unwrap().path()).unwrap();
|
|
|
|
build_info_count += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(build_info_count, 1);
|
|
|
|
}
|
|
|
|
|
2021-11-26 16:49:19 +00:00
|
|
|
#[test]
|
|
|
|
fn can_compile_dapp_sample_with_cache() {
|
2022-01-10 19:43:34 +00:00
|
|
|
let tmp_dir = tempfile::tempdir().unwrap();
|
2021-11-26 16:49:19 +00:00
|
|
|
let root = tmp_dir.path();
|
|
|
|
let cache = root.join("cache").join(SOLIDITY_FILES_CACHE_FILENAME);
|
|
|
|
let artifacts = root.join("out");
|
|
|
|
|
|
|
|
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
let orig_root = manifest_dir.join("test-data/dapp-sample");
|
2021-11-28 12:33:24 +00:00
|
|
|
let cache_testdata_dir = manifest_dir.join("test-data/cache-sample/");
|
2021-11-26 16:49:19 +00:00
|
|
|
copy_dir_all(orig_root, &tmp_dir).unwrap();
|
|
|
|
let paths = ProjectPathsConfig::builder()
|
|
|
|
.cache(cache)
|
|
|
|
.sources(root.join("src"))
|
|
|
|
.artifacts(artifacts)
|
|
|
|
.lib(root.join("lib"))
|
|
|
|
.root(root)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// first compile
|
|
|
|
let project = Project::builder().paths(paths).build().unwrap();
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_some());
|
2021-11-26 16:49:19 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
// cache is used when nothing to compile
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_some());
|
2021-11-26 16:49:19 +00:00
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
|
|
|
|
// deleted artifacts cause recompile even with cache
|
2022-09-04 17:57:52 +00:00
|
|
|
std::fs::remove_dir_all(project.artifacts_path()).unwrap();
|
2021-11-26 16:49:19 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_some());
|
2021-11-26 16:49:19 +00:00
|
|
|
assert!(!compiled.is_unchanged());
|
|
|
|
|
|
|
|
// new file is compiled even with partial cache
|
2021-11-28 12:33:24 +00:00
|
|
|
std::fs::copy(cache_testdata_dir.join("NewContract.sol"), root.join("src/NewContract.sol"))
|
|
|
|
.unwrap();
|
2021-11-26 16:49:19 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_some());
|
|
|
|
assert!(compiled.find_first("NewContract").is_some());
|
2021-11-26 16:49:19 +00:00
|
|
|
assert!(!compiled.is_unchanged());
|
2021-11-28 12:33:24 +00:00
|
|
|
assert_eq!(
|
2022-02-18 15:48:56 +00:00
|
|
|
compiled.into_artifacts().map(|(artifact_id, _)| artifact_id.name).collect::<HashSet<_>>(),
|
2022-02-04 16:20:24 +00:00
|
|
|
HashSet::from([
|
2022-02-18 15:48:56 +00:00
|
|
|
"Dapp".to_string(),
|
|
|
|
"DappTest".to_string(),
|
|
|
|
"DSTest".to_string(),
|
|
|
|
"NewContract".to_string(),
|
2022-02-04 16:20:24 +00:00
|
|
|
])
|
2021-11-28 12:33:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// old cached artifact is not taken from the cache
|
|
|
|
std::fs::copy(cache_testdata_dir.join("Dapp.sol"), root.join("src/Dapp.sol")).unwrap();
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert_eq!(
|
2022-02-18 15:48:56 +00:00
|
|
|
compiled.into_artifacts().map(|(artifact_id, _)| artifact_id.name).collect::<HashSet<_>>(),
|
2022-02-04 16:20:24 +00:00
|
|
|
HashSet::from([
|
2022-02-18 15:48:56 +00:00
|
|
|
"DappTest".to_string(),
|
|
|
|
"NewContract".to_string(),
|
|
|
|
"DSTest".to_string(),
|
|
|
|
"Dapp".to_string(),
|
2022-02-04 16:20:24 +00:00
|
|
|
])
|
2021-11-28 12:33:24 +00:00
|
|
|
);
|
2021-11-26 16:49:19 +00:00
|
|
|
|
|
|
|
// deleted artifact is not taken from the cache
|
2022-11-07 23:43:11 +00:00
|
|
|
std::fs::remove_file(project.paths.sources.join("Dapp.sol")).unwrap();
|
2022-02-04 16:20:24 +00:00
|
|
|
let compiled: ProjectCompileOutput<_> = project.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Dapp").is_none());
|
2021-11-26 16:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
|
|
|
|
std::fs::create_dir_all(&dst)?;
|
|
|
|
for entry in std::fs::read_dir(src)? {
|
|
|
|
let entry = entry?;
|
|
|
|
let ty = entry.file_type()?;
|
|
|
|
if ty.is_dir() {
|
|
|
|
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
|
|
|
|
} else {
|
|
|
|
std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-01-17 12:27:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_flatten_file() {
|
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/test-contract-libs");
|
|
|
|
let target = root.join("src").join("Foo.sol");
|
|
|
|
let paths = ProjectPathsConfig::builder()
|
|
|
|
.sources(root.join("src"))
|
|
|
|
.lib(root.join("lib1"))
|
|
|
|
.lib(root.join("lib2"));
|
2022-02-17 15:31:35 +00:00
|
|
|
let project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
|
2022-01-17 12:27:40 +00:00
|
|
|
|
|
|
|
let result = project.flatten(&target);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
let result = result.unwrap();
|
2022-02-22 14:22:24 +00:00
|
|
|
assert!(!result.contains("import"));
|
2022-01-20 19:41:19 +00:00
|
|
|
assert!(result.contains("contract Foo"));
|
|
|
|
assert!(result.contains("contract Bar"));
|
2022-01-17 12:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_flatten_file_with_external_lib() {
|
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/hardhat-sample");
|
|
|
|
let paths = ProjectPathsConfig::builder()
|
|
|
|
.sources(root.join("contracts"))
|
|
|
|
.lib(root.join("node_modules"));
|
2022-02-17 15:31:35 +00:00
|
|
|
let project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
|
2022-01-17 12:27:40 +00:00
|
|
|
|
|
|
|
let target = root.join("contracts").join("Greeter.sol");
|
|
|
|
|
|
|
|
let result = project.flatten(&target);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
let result = result.unwrap();
|
2022-02-22 14:22:24 +00:00
|
|
|
assert!(!result.contains("import"));
|
2022-01-20 19:41:19 +00:00
|
|
|
assert!(result.contains("library console"));
|
|
|
|
assert!(result.contains("contract Greeter"));
|
2022-01-17 12:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_flatten_file_in_dapp_sample() {
|
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/dapp-sample");
|
|
|
|
let paths = ProjectPathsConfig::builder().sources(root.join("src")).lib(root.join("lib"));
|
2022-02-17 15:31:35 +00:00
|
|
|
let project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
|
2022-01-17 12:27:40 +00:00
|
|
|
|
|
|
|
let target = root.join("src/Dapp.t.sol");
|
|
|
|
|
|
|
|
let result = project.flatten(&target);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
let result = result.unwrap();
|
2022-02-22 14:22:24 +00:00
|
|
|
assert!(!result.contains("import"));
|
2022-01-20 19:41:19 +00:00
|
|
|
assert!(result.contains("contract DSTest"));
|
|
|
|
assert!(result.contains("contract Dapp"));
|
|
|
|
assert!(result.contains("contract DappTest"));
|
2022-01-17 12:27:40 +00:00
|
|
|
}
|
2022-01-19 16:11:37 +00:00
|
|
|
|
2022-03-08 14:46:04 +00:00
|
|
|
#[test]
|
|
|
|
fn can_flatten_unique() {
|
|
|
|
let project = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
let f = project
|
|
|
|
.add_source(
|
|
|
|
"A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "./C.sol";
|
|
|
|
import "./B.sol";
|
|
|
|
contract A { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"B",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "./C.sol";
|
|
|
|
contract B { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"C",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "./A.sol";
|
|
|
|
contract C { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let result = project.flatten(&f).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
2022-04-23 08:40:34 +00:00
|
|
|
r#"pragma solidity ^0.8.10;
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-03-08 14:46:04 +00:00
|
|
|
contract C { }
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-03-08 14:46:04 +00:00
|
|
|
contract B { }
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-03-08 14:46:04 +00:00
|
|
|
contract A { }
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-07 10:12:25 +00:00
|
|
|
#[test]
|
|
|
|
fn can_flatten_experimental_pragma() {
|
|
|
|
let project = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
let f = project
|
|
|
|
.add_source(
|
|
|
|
"A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
import "./C.sol";
|
|
|
|
import "./B.sol";
|
|
|
|
contract A { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"B",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
import "./C.sol";
|
|
|
|
contract B { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"C",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
import "./A.sol";
|
|
|
|
contract C { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let result = project.flatten(&f).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
2022-04-23 08:40:34 +00:00
|
|
|
r#"pragma solidity ^0.8.10;
|
2022-04-07 10:12:25 +00:00
|
|
|
pragma experimental ABIEncoderV2;
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-04-07 10:12:25 +00:00
|
|
|
contract C { }
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-04-07 10:12:25 +00:00
|
|
|
contract B { }
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-04-07 10:12:25 +00:00
|
|
|
contract A { }
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:11:37 +00:00
|
|
|
#[test]
|
|
|
|
fn can_flatten_file_with_duplicates() {
|
2022-02-22 14:22:24 +00:00
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/test-flatten-duplicates");
|
2022-01-19 16:11:37 +00:00
|
|
|
let paths = ProjectPathsConfig::builder().sources(root.join("contracts"));
|
2022-02-17 15:31:35 +00:00
|
|
|
let project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
|
2022-01-19 16:11:37 +00:00
|
|
|
|
|
|
|
let target = root.join("contracts/FooBar.sol");
|
|
|
|
|
|
|
|
let result = project.flatten(&target);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
let result = result.unwrap();
|
2022-02-22 14:22:24 +00:00
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
r#"//SPDX-License-Identifier: UNLICENSED
|
|
|
|
pragma solidity >=0.6.0;
|
|
|
|
|
|
|
|
contract Bar {}
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-02-22 14:22:24 +00:00
|
|
|
contract Foo {}
|
|
|
|
|
|
|
|
contract FooBar {}
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_flatten_on_solang_failure() {
|
|
|
|
let root =
|
|
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/test-flatten-solang-failure");
|
2022-11-07 23:43:11 +00:00
|
|
|
let paths = ProjectPathsConfig::builder().sources(root.join("contracts"));
|
2022-02-22 14:22:24 +00:00
|
|
|
let project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
|
|
|
|
|
|
|
|
let target = root.join("contracts/Contract.sol");
|
|
|
|
|
|
|
|
let result = project.flatten(&target);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
let result = result.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
r#"// SPDX-License-Identifier: UNLICENSED
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
library Lib {}
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-02-22 14:22:24 +00:00
|
|
|
// Intentionally erroneous code
|
|
|
|
contract Contract {
|
|
|
|
failure();
|
|
|
|
}
|
|
|
|
"#
|
|
|
|
);
|
2022-01-19 16:11:37 +00:00
|
|
|
}
|
2022-01-31 21:54:49 +00:00
|
|
|
|
2022-04-03 15:05:31 +00:00
|
|
|
#[test]
|
|
|
|
fn can_flatten_multiline() {
|
|
|
|
let project = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
let f = project
|
|
|
|
.add_source(
|
|
|
|
"A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "./C.sol";
|
|
|
|
import {
|
|
|
|
IllegalArgument,
|
|
|
|
IllegalState
|
|
|
|
} from "./Errors.sol";
|
|
|
|
contract A { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"Errors",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
error IllegalArgument();
|
|
|
|
error IllegalState();
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"C",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract C { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let result = project.flatten(&f).unwrap();
|
|
|
|
assert_eq!(
|
2022-04-23 08:40:34 +00:00
|
|
|
result,
|
2022-04-03 15:05:31 +00:00
|
|
|
r#"pragma solidity ^0.8.10;
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-04-03 15:05:31 +00:00
|
|
|
contract C { }
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-04-03 15:05:31 +00:00
|
|
|
error IllegalArgument();
|
|
|
|
error IllegalState();
|
2022-04-16 20:09:00 +00:00
|
|
|
|
2022-04-23 08:40:34 +00:00
|
|
|
contract A { }
|
|
|
|
"#
|
2022-04-16 20:09:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_flatten_remove_extra_spacing() {
|
|
|
|
let project = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
let f = project
|
|
|
|
.add_source(
|
|
|
|
"A",
|
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
import "./C.sol";
|
|
|
|
import "./B.sol";
|
|
|
|
contract A { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"B",
|
|
|
|
r#"// This is a B Contract
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
import "./C.sol";
|
|
|
|
|
|
|
|
contract B { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"C",
|
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
contract C { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let result = project.flatten(&f).unwrap();
|
|
|
|
assert_eq!(
|
2022-04-23 08:40:34 +00:00
|
|
|
result,
|
2022-04-16 20:09:00 +00:00
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract C { }
|
|
|
|
|
|
|
|
// This is a B Contract
|
|
|
|
|
|
|
|
contract B { }
|
|
|
|
|
2022-04-23 08:40:34 +00:00
|
|
|
contract A { }
|
|
|
|
"#
|
2022-04-03 15:05:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-04 05:33:25 +00:00
|
|
|
#[test]
|
|
|
|
fn can_flatten_with_alias() {
|
|
|
|
let project = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
let f = project
|
|
|
|
.add_source(
|
|
|
|
"Contract",
|
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
import { ParentContract as Parent } from "./Parent.sol";
|
|
|
|
import { AnotherParentContract as AnotherParent } from "./AnotherParent.sol";
|
|
|
|
import { PeerContract as Peer } from "./Peer.sol";
|
|
|
|
import { MathLibrary as Math } from "./Math.sol";
|
|
|
|
import * as Lib from "./SomeLib.sol";
|
|
|
|
|
|
|
|
contract Contract is Parent,
|
|
|
|
AnotherParent {
|
|
|
|
using Math for uint256;
|
|
|
|
|
|
|
|
string public usingString = "using Math for uint256;";
|
|
|
|
string public inheritanceString = "\"Contract is Parent {\"";
|
|
|
|
string public castString = 'Peer(smth) ';
|
|
|
|
string public methodString = '\' Math.max()';
|
|
|
|
|
|
|
|
Peer public peer;
|
|
|
|
|
|
|
|
error Peer();
|
|
|
|
|
|
|
|
constructor(address _peer) {
|
|
|
|
peer = Peer(_peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Math(uint256 value) external pure returns (uint256) {
|
|
|
|
return Math.minusOne(Math.max() - value.diffMax());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"Parent",
|
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
contract ParentContract { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"AnotherParent",
|
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
contract AnotherParentContract { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"Peer",
|
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
contract PeerContract { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"Math",
|
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
library MathLibrary {
|
|
|
|
function minusOne(uint256 val) internal returns (uint256) {
|
|
|
|
return val - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function max() internal returns (uint256) {
|
|
|
|
return type(uint256).max;
|
|
|
|
}
|
|
|
|
|
|
|
|
function diffMax(uint256 value) internal returns (uint256) {
|
|
|
|
return type(uint256).max - value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"SomeLib",
|
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
library SomeLib { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let result = project.flatten(&f).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract ParentContract { }
|
|
|
|
|
|
|
|
contract AnotherParentContract { }
|
|
|
|
|
|
|
|
contract PeerContract { }
|
|
|
|
|
|
|
|
library MathLibrary {
|
|
|
|
function minusOne(uint256 val) internal returns (uint256) {
|
|
|
|
return val - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function max() internal returns (uint256) {
|
|
|
|
return type(uint256).max;
|
|
|
|
}
|
|
|
|
|
|
|
|
function diffMax(uint256 value) internal returns (uint256) {
|
|
|
|
return type(uint256).max - value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
library SomeLib { }
|
|
|
|
|
|
|
|
contract Contract is ParentContract,
|
|
|
|
AnotherParentContract {
|
|
|
|
using MathLibrary for uint256;
|
|
|
|
|
|
|
|
string public usingString = "using Math for uint256;";
|
|
|
|
string public inheritanceString = "\"Contract is Parent {\"";
|
|
|
|
string public castString = 'Peer(smth) ';
|
|
|
|
string public methodString = '\' Math.max()';
|
|
|
|
|
|
|
|
PeerContract public peer;
|
|
|
|
|
|
|
|
error Peer();
|
|
|
|
|
|
|
|
constructor(address _peer) {
|
|
|
|
peer = PeerContract(_peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Math(uint256 value) external pure returns (uint256) {
|
|
|
|
return MathLibrary.minusOne(MathLibrary.max() - value.diffMax());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-20 20:00:36 +00:00
|
|
|
#[test]
|
|
|
|
fn can_flatten_with_version_pragma_after_imports() {
|
|
|
|
let project = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
let f = project
|
|
|
|
.add_source(
|
|
|
|
"A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
import * as B from "./B.sol";
|
|
|
|
|
|
|
|
contract A { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"B",
|
|
|
|
r#"
|
|
|
|
import D from "./D.sol";
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import * as C from "./C.sol";
|
|
|
|
contract B { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"C",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract C { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"D",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract D { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let result = project.flatten(&f).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
r#"pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract D { }
|
|
|
|
|
|
|
|
contract C { }
|
|
|
|
|
|
|
|
contract B { }
|
|
|
|
|
|
|
|
contract A { }
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:54:49 +00:00
|
|
|
#[test]
|
|
|
|
fn can_detect_type_error() {
|
2022-02-17 15:31:35 +00:00
|
|
|
let project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();
|
2022-01-31 21:54:49 +00:00
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"Contract",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract Contract {
|
|
|
|
function xyz() public {
|
|
|
|
require(address(0), "Error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.has_compiler_errors());
|
|
|
|
}
|
2022-02-19 13:56:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compile_single_files() {
|
|
|
|
let tmp = TempProject::dapptools().unwrap();
|
|
|
|
|
2022-02-22 14:23:23 +00:00
|
|
|
let f = tmp
|
2022-02-19 13:56:34 +00:00
|
|
|
.add_contract(
|
|
|
|
"examples/Foo",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract Foo {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-02-22 14:23:23 +00:00
|
|
|
let compiled = tmp.project().compile_file(f.clone()).unwrap();
|
2022-02-19 13:56:34 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Foo").is_some());
|
2022-02-19 13:56:34 +00:00
|
|
|
|
|
|
|
let bar = tmp
|
|
|
|
.add_contract(
|
|
|
|
"examples/Bar",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract Bar {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-02-22 14:23:23 +00:00
|
|
|
let compiled = tmp.project().compile_files(vec![f, bar]).unwrap();
|
2022-02-19 13:56:34 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Foo").is_some());
|
|
|
|
assert!(compiled.find_first("Bar").is_some());
|
2022-02-19 13:56:34 +00:00
|
|
|
}
|
2022-02-22 14:23:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn consistent_bytecode() {
|
|
|
|
let tmp = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"LinkTest",
|
|
|
|
r#"
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
library LibTest {
|
|
|
|
function foobar(uint256 a) public view returns (uint256) {
|
|
|
|
return a * 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contract LinkTest {
|
|
|
|
function foo() public returns (uint256) {
|
|
|
|
return LibTest.foobar(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
2022-07-04 18:53:49 +00:00
|
|
|
let contract = compiled.find_first("LinkTest").unwrap();
|
2022-02-22 14:23:23 +00:00
|
|
|
let bytecode = &contract.bytecode.as_ref().unwrap().object;
|
|
|
|
assert!(bytecode.is_unlinked());
|
|
|
|
let s = bytecode.as_str().unwrap();
|
|
|
|
assert!(!s.starts_with("0x"));
|
|
|
|
|
|
|
|
let s = serde_json::to_string(&bytecode).unwrap();
|
|
|
|
assert_eq!(bytecode.clone(), serde_json::from_str(&s).unwrap());
|
|
|
|
}
|
2022-03-09 18:52:40 +00:00
|
|
|
|
2022-04-28 12:18:24 +00:00
|
|
|
#[test]
|
|
|
|
fn can_apply_libraries() {
|
|
|
|
let mut tmp = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"LinkTest",
|
|
|
|
r#"
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import "./MyLib.sol";
|
|
|
|
contract LinkTest {
|
|
|
|
function foo() public returns (uint256) {
|
|
|
|
return MyLib.foobar(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let lib = tmp
|
|
|
|
.add_source(
|
|
|
|
"MyLib",
|
|
|
|
r#"
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
library MyLib {
|
|
|
|
function foobar(uint256 a) public view returns (uint256) {
|
|
|
|
return a * 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("MyLib").is_some());
|
|
|
|
let contract = compiled.find_first("LinkTest").unwrap();
|
2022-04-28 12:18:24 +00:00
|
|
|
let bytecode = &contract.bytecode.as_ref().unwrap().object;
|
|
|
|
assert!(bytecode.is_unlinked());
|
|
|
|
|
|
|
|
// provide the library settings to let solc link
|
|
|
|
tmp.project_mut().solc_config.settings.libraries = BTreeMap::from([(
|
|
|
|
lib,
|
|
|
|
BTreeMap::from([("MyLib".to_string(), format!("{:?}", Address::zero()))]),
|
|
|
|
)])
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("MyLib").is_some());
|
|
|
|
let contract = compiled.find_first("LinkTest").unwrap();
|
2022-04-28 12:18:24 +00:00
|
|
|
let bytecode = &contract.bytecode.as_ref().unwrap().object;
|
|
|
|
assert!(!bytecode.is_unlinked());
|
|
|
|
|
|
|
|
let libs = Libraries::parse(&[format!("./src/MyLib.sol:MyLib:{:?}", Address::zero())]).unwrap();
|
|
|
|
// provide the library settings to let solc link
|
|
|
|
tmp.project_mut().solc_config.settings.libraries = libs.with_applied_remappings(tmp.paths());
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("MyLib").is_some());
|
|
|
|
let contract = compiled.find_first("LinkTest").unwrap();
|
2022-04-28 12:18:24 +00:00
|
|
|
let bytecode = &contract.bytecode.as_ref().unwrap().object;
|
|
|
|
assert!(!bytecode.is_unlinked());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_apply_libraries_with_remappings() {
|
|
|
|
let mut tmp = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
let remapping = tmp.paths().libraries[0].join("remapping");
|
|
|
|
tmp.paths_mut()
|
|
|
|
.remappings
|
|
|
|
.push(Remapping::from_str(&format!("remapping/={}/", remapping.display())).unwrap());
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"LinkTest",
|
|
|
|
r#"
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import "remapping/MyLib.sol";
|
|
|
|
contract LinkTest {
|
|
|
|
function foo() public returns (uint256) {
|
|
|
|
return MyLib.foobar(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
tmp.add_lib(
|
|
|
|
"remapping/MyLib",
|
|
|
|
r#"
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
library MyLib {
|
|
|
|
function foobar(uint256 a) public view returns (uint256) {
|
|
|
|
return a * 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("MyLib").is_some());
|
|
|
|
let contract = compiled.find_first("LinkTest").unwrap();
|
2022-04-28 12:18:24 +00:00
|
|
|
let bytecode = &contract.bytecode.as_ref().unwrap().object;
|
|
|
|
assert!(bytecode.is_unlinked());
|
|
|
|
|
|
|
|
let libs =
|
|
|
|
Libraries::parse(&[format!("remapping/MyLib.sol:MyLib:{:?}", Address::zero())]).unwrap(); // provide the library settings to let solc link
|
|
|
|
tmp.project_mut().solc_config.settings.libraries = libs.with_applied_remappings(tmp.paths());
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("MyLib").is_some());
|
|
|
|
let contract = compiled.find_first("LinkTest").unwrap();
|
2022-04-28 12:18:24 +00:00
|
|
|
let bytecode = &contract.bytecode.as_ref().unwrap().object;
|
|
|
|
assert!(!bytecode.is_unlinked());
|
|
|
|
}
|
2023-03-19 18:45:30 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_detect_invalid_version() {
|
|
|
|
let tmp = TempProject::dapptools().unwrap();
|
|
|
|
let content = r#"
|
|
|
|
pragma solidity ^0.100.10;
|
|
|
|
contract A {}
|
|
|
|
"#;
|
|
|
|
tmp.add_source("A", content).unwrap();
|
|
|
|
|
|
|
|
let out = tmp.compile().unwrap_err();
|
|
|
|
match out {
|
|
|
|
SolcError::Message(err) => {
|
|
|
|
assert_eq!(err, "Encountered invalid solc version in src/A.sol: No solc version exists that matches the version requirement: ^0.100.10");
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 18:52:40 +00:00
|
|
|
#[test]
|
|
|
|
fn can_recompile_with_changes() {
|
|
|
|
let mut tmp = TempProject::dapptools().unwrap();
|
2022-08-12 19:11:08 +00:00
|
|
|
tmp.project_mut().allowed_paths = vec![tmp.root().join("modules")].into();
|
2022-03-09 18:52:40 +00:00
|
|
|
|
|
|
|
let content = r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "../modules/B.sol";
|
|
|
|
contract A {}
|
|
|
|
"#;
|
|
|
|
tmp.add_source("A", content).unwrap();
|
|
|
|
|
|
|
|
tmp.add_contract(
|
|
|
|
"modules/B",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract B {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("A").is_some());
|
|
|
|
assert!(compiled.find_first("B").is_some());
|
2022-03-09 18:52:40 +00:00
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("A").is_some());
|
|
|
|
assert!(compiled.find_first("B").is_some());
|
2022-03-09 18:52:40 +00:00
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
|
|
|
|
// modify A.sol
|
2022-11-07 23:43:11 +00:00
|
|
|
tmp.add_source("A", format!("{content}\n")).unwrap();
|
2022-03-09 18:52:40 +00:00
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(!compiled.is_unchanged());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("A").is_some());
|
|
|
|
assert!(compiled.find_first("B").is_some());
|
2022-03-09 18:52:40 +00:00
|
|
|
}
|
2022-03-13 22:36:26 +00:00
|
|
|
|
2022-03-17 08:27:03 +00:00
|
|
|
#[test]
|
|
|
|
fn can_recompile_with_lowercase_names() {
|
|
|
|
let tmp = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"deployProxy.sol",
|
|
|
|
r#"
|
|
|
|
pragma solidity =0.8.12;
|
|
|
|
contract DeployProxy {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let upgrade = r#"
|
|
|
|
pragma solidity =0.8.12;
|
|
|
|
import "./deployProxy.sol";
|
|
|
|
import "./ProxyAdmin.sol";
|
|
|
|
contract UpgradeProxy {}
|
|
|
|
"#;
|
|
|
|
tmp.add_source("upgradeProxy.sol", upgrade).unwrap();
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"ProxyAdmin.sol",
|
|
|
|
r#"
|
|
|
|
pragma solidity =0.8.12;
|
|
|
|
contract ProxyAdmin {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("DeployProxy").is_some());
|
|
|
|
assert!(compiled.find_first("UpgradeProxy").is_some());
|
|
|
|
assert!(compiled.find_first("ProxyAdmin").is_some());
|
2022-03-17 08:27:03 +00:00
|
|
|
|
|
|
|
let artifacts = tmp.artifacts_snapshot().unwrap();
|
|
|
|
assert_eq!(artifacts.artifacts.as_ref().len(), 3);
|
|
|
|
artifacts.assert_artifacts_essentials_present();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("DeployProxy").is_some());
|
|
|
|
assert!(compiled.find_first("UpgradeProxy").is_some());
|
|
|
|
assert!(compiled.find_first("ProxyAdmin").is_some());
|
2022-03-17 08:27:03 +00:00
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
|
|
|
|
// modify upgradeProxy.sol
|
2022-11-07 23:43:11 +00:00
|
|
|
tmp.add_source("upgradeProxy.sol", format!("{upgrade}\n")).unwrap();
|
2022-03-17 08:27:03 +00:00
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(!compiled.is_unchanged());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("DeployProxy").is_some());
|
|
|
|
assert!(compiled.find_first("UpgradeProxy").is_some());
|
|
|
|
assert!(compiled.find_first("ProxyAdmin").is_some());
|
2022-03-17 08:27:03 +00:00
|
|
|
|
|
|
|
let artifacts = tmp.artifacts_snapshot().unwrap();
|
|
|
|
assert_eq!(artifacts.artifacts.as_ref().len(), 3);
|
|
|
|
artifacts.assert_artifacts_essentials_present();
|
|
|
|
}
|
|
|
|
|
2022-03-13 22:36:26 +00:00
|
|
|
#[test]
|
|
|
|
fn can_recompile_unchanged_with_empty_files() {
|
|
|
|
let tmp = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "./B.sol";
|
|
|
|
contract A {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"B",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "./C.sol";
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let c = r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract C {}
|
|
|
|
"#;
|
|
|
|
tmp.add_source("C", c).unwrap();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("A").is_some());
|
|
|
|
assert!(compiled.find_first("C").is_some());
|
2022-03-13 22:36:26 +00:00
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("A").is_some());
|
|
|
|
assert!(compiled.find_first("C").is_some());
|
2022-03-13 22:36:26 +00:00
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
|
|
|
|
// modify C.sol
|
2022-11-07 23:43:11 +00:00
|
|
|
tmp.add_source("C", format!("{c}\n")).unwrap();
|
2022-03-13 22:36:26 +00:00
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(!compiled.is_unchanged());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("A").is_some());
|
|
|
|
assert!(compiled.find_first("C").is_some());
|
2022-03-13 22:36:26 +00:00
|
|
|
}
|
2022-04-04 22:50:10 +00:00
|
|
|
|
2022-05-22 22:19:59 +00:00
|
|
|
#[test]
|
|
|
|
fn can_emit_empty_artifacts() {
|
|
|
|
let tmp = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
let top_level = tmp
|
|
|
|
.add_source(
|
|
|
|
"top_level",
|
|
|
|
r#"
|
|
|
|
function test() {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"Contract",
|
|
|
|
r#"
|
|
|
|
// SPDX-License-Identifier: UNLICENSED
|
|
|
|
pragma solidity 0.8.10;
|
|
|
|
|
|
|
|
import "./top_level.sol";
|
|
|
|
|
|
|
|
contract Contract {
|
|
|
|
function a() public{
|
|
|
|
test();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Contract").is_some());
|
|
|
|
assert!(compiled.find_first("top_level").is_some());
|
2022-05-22 22:19:59 +00:00
|
|
|
let mut artifacts = tmp.artifacts_snapshot().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(artifacts.artifacts.as_ref().len(), 2);
|
|
|
|
|
|
|
|
let mut top_level =
|
|
|
|
artifacts.artifacts.as_mut().remove(top_level.to_string_lossy().as_ref()).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(top_level.len(), 1);
|
|
|
|
|
|
|
|
let artifact = top_level.remove("top_level").unwrap().remove(0);
|
|
|
|
assert!(artifact.artifact.ast.is_some());
|
|
|
|
|
|
|
|
// recompile
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
|
|
|
|
// modify standalone file
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"top_level",
|
|
|
|
r#"
|
|
|
|
error MyError();
|
|
|
|
function test() {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.is_unchanged());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_detect_contract_def_source_files() {
|
|
|
|
let tmp = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
let mylib = tmp
|
|
|
|
.add_source(
|
|
|
|
"MyLib",
|
|
|
|
r#"
|
|
|
|
pragma solidity 0.8.10;
|
|
|
|
library MyLib {
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let myinterface = tmp
|
|
|
|
.add_source(
|
|
|
|
"MyInterface",
|
|
|
|
r#"
|
|
|
|
pragma solidity 0.8.10;
|
|
|
|
interface MyInterface {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mycontract = tmp
|
|
|
|
.add_source(
|
|
|
|
"MyContract",
|
|
|
|
r#"
|
|
|
|
pragma solidity 0.8.10;
|
|
|
|
contract MyContract {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let myabstract_contract = tmp
|
|
|
|
.add_source(
|
|
|
|
"MyAbstractContract",
|
|
|
|
r#"
|
|
|
|
pragma solidity 0.8.10;
|
|
|
|
contract MyAbstractContract {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let myerr = tmp
|
|
|
|
.add_source(
|
|
|
|
"MyError",
|
|
|
|
r#"
|
|
|
|
pragma solidity 0.8.10;
|
|
|
|
error MyError();
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let myfunc = tmp
|
|
|
|
.add_source(
|
|
|
|
"MyFunction",
|
|
|
|
r#"
|
|
|
|
pragma solidity 0.8.10;
|
|
|
|
function abc(){}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
let mut sources = compiled.output().sources;
|
|
|
|
let myfunc = sources.remove_by_path(myfunc.to_string_lossy()).unwrap();
|
|
|
|
assert!(!myfunc.contains_contract_definition());
|
|
|
|
|
|
|
|
let myerr = sources.remove_by_path(myerr.to_string_lossy()).unwrap();
|
|
|
|
assert!(!myerr.contains_contract_definition());
|
|
|
|
|
|
|
|
let mylib = sources.remove_by_path(mylib.to_string_lossy()).unwrap();
|
|
|
|
assert!(mylib.contains_contract_definition());
|
|
|
|
|
|
|
|
let myabstract_contract =
|
|
|
|
sources.remove_by_path(myabstract_contract.to_string_lossy()).unwrap();
|
|
|
|
assert!(myabstract_contract.contains_contract_definition());
|
|
|
|
|
|
|
|
let myinterface = sources.remove_by_path(myinterface.to_string_lossy()).unwrap();
|
|
|
|
assert!(myinterface.contains_contract_definition());
|
|
|
|
|
|
|
|
let mycontract = sources.remove_by_path(mycontract.to_string_lossy()).unwrap();
|
|
|
|
assert!(mycontract.contains_contract_definition());
|
|
|
|
}
|
|
|
|
|
2022-04-04 22:50:10 +00:00
|
|
|
#[test]
|
|
|
|
fn can_compile_sparse_with_link_references() {
|
|
|
|
let tmp = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"ATest.t.sol",
|
|
|
|
r#"
|
|
|
|
pragma solidity =0.8.12;
|
|
|
|
import {MyLib} from "./mylib.sol";
|
|
|
|
contract ATest {
|
|
|
|
function test_mylib() public returns (uint256) {
|
|
|
|
return MyLib.doStuff();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-06-22 17:14:02 +00:00
|
|
|
let my_lib_path = tmp
|
|
|
|
.add_source(
|
|
|
|
"mylib.sol",
|
|
|
|
r#"
|
2022-04-04 22:50:10 +00:00
|
|
|
pragma solidity =0.8.12;
|
|
|
|
library MyLib {
|
|
|
|
function doStuff() external pure returns (uint256) {return 1337;}
|
|
|
|
}
|
|
|
|
"#,
|
2022-06-22 17:14:02 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-04-04 22:50:10 +00:00
|
|
|
|
|
|
|
let mut compiled = tmp.compile_sparse(TestFileFilter::default()).unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
2022-06-22 17:14:02 +00:00
|
|
|
let mut output = compiled.clone().output();
|
|
|
|
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("ATest").is_some());
|
|
|
|
assert!(compiled.find_first("MyLib").is_some());
|
|
|
|
let lib = compiled.remove_first("MyLib").unwrap();
|
2022-04-04 22:50:10 +00:00
|
|
|
assert!(lib.bytecode.is_some());
|
2022-07-04 18:53:49 +00:00
|
|
|
let lib = compiled.remove_first("MyLib");
|
2022-06-22 17:14:02 +00:00
|
|
|
assert!(lib.is_none());
|
|
|
|
|
|
|
|
let mut dup = output.clone();
|
|
|
|
let lib = dup.remove_first("MyLib");
|
|
|
|
assert!(lib.is_some());
|
|
|
|
let lib = dup.remove_first("MyLib");
|
|
|
|
assert!(lib.is_none());
|
|
|
|
|
2022-06-30 16:19:55 +00:00
|
|
|
dup = output.clone();
|
|
|
|
let lib = dup.remove(my_lib_path.to_string_lossy(), "MyLib");
|
2022-06-22 17:14:02 +00:00
|
|
|
assert!(lib.is_some());
|
2022-06-30 16:19:55 +00:00
|
|
|
let lib = dup.remove(my_lib_path.to_string_lossy(), "MyLib");
|
|
|
|
assert!(lib.is_none());
|
|
|
|
|
|
|
|
let info = ContractInfo::new(format!("{}:{}", my_lib_path.to_string_lossy(), "MyLib"));
|
|
|
|
let lib = output.remove_contract(&info);
|
|
|
|
assert!(lib.is_some());
|
|
|
|
let lib = output.remove_contract(&info);
|
2022-06-22 17:14:02 +00:00
|
|
|
assert!(lib.is_none());
|
2022-04-04 22:50:10 +00:00
|
|
|
}
|
2022-04-05 19:00:04 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_sanitize_bytecode_hash() {
|
|
|
|
let mut tmp = TempProject::dapptools().unwrap();
|
|
|
|
tmp.project_mut().solc_config.settings.metadata = Some(BytecodeHash::Ipfs.into());
|
|
|
|
|
|
|
|
tmp.add_source(
|
|
|
|
"A",
|
|
|
|
r#"
|
|
|
|
pragma solidity =0.5.17;
|
|
|
|
contract A {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = tmp.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("A").is_some());
|
2022-04-05 19:00:04 +00:00
|
|
|
}
|
2022-04-11 20:36:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compile_std_json_input() {
|
|
|
|
let tmp = TempProject::dapptools_init().unwrap();
|
|
|
|
tmp.assert_no_errors();
|
2022-04-23 08:39:31 +00:00
|
|
|
let source = tmp.list_source_files().into_iter().find(|p| p.ends_with("Dapp.t.sol")).unwrap();
|
2022-04-11 20:36:06 +00:00
|
|
|
let input = tmp.project().standard_json_input(source).unwrap();
|
|
|
|
|
|
|
|
assert!(input.settings.remappings.contains(&"ds-test/=lib/ds-test/src/".parse().unwrap()));
|
2022-04-23 08:39:31 +00:00
|
|
|
let input: CompilerInput = input.into();
|
2022-04-11 20:36:06 +00:00
|
|
|
assert!(input.sources.contains_key(Path::new("lib/ds-test/src/test.sol")));
|
|
|
|
|
|
|
|
// should be installed
|
|
|
|
if let Some(solc) = Solc::find_svm_installed_version("0.8.10").ok().flatten() {
|
|
|
|
let out = solc.compile(&input).unwrap();
|
|
|
|
assert!(!out.has_error());
|
|
|
|
assert!(out.sources.contains_key("lib/ds-test/src/test.sol"));
|
|
|
|
}
|
|
|
|
}
|
2022-05-13 20:29:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compile_model_checker_sample() {
|
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/model-checker-sample");
|
|
|
|
let paths = ProjectPathsConfig::builder().sources(root);
|
|
|
|
|
|
|
|
let mut project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
|
|
|
|
project.project_mut().solc_config.settings.model_checker = Some(ModelCheckerSettings {
|
|
|
|
contracts: BTreeMap::new(),
|
|
|
|
engine: Some(CHC),
|
|
|
|
targets: None,
|
|
|
|
timeout: Some(10000),
|
2023-02-14 01:09:40 +00:00
|
|
|
show_unproved: None,
|
|
|
|
div_mod_with_slacks: None,
|
|
|
|
solvers: None,
|
2023-02-10 19:51:01 +00:00
|
|
|
invariants: None,
|
2022-05-13 20:29:13 +00:00
|
|
|
});
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Assert").is_some());
|
2022-05-13 20:29:13 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(compiled.has_compiler_warnings());
|
|
|
|
}
|
2022-05-14 23:29:45 +00:00
|
|
|
|
2022-11-10 20:19:43 +00:00
|
|
|
#[test]
|
|
|
|
fn test_compiler_severity_filter() {
|
|
|
|
fn gen_test_data_warning_path() -> ProjectPathsConfig {
|
|
|
|
let root =
|
|
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/test-contract-warnings");
|
2022-11-13 12:49:27 +00:00
|
|
|
|
|
|
|
ProjectPathsConfig::builder().sources(root).build().unwrap()
|
2022-11-10 20:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let project = Project::builder()
|
|
|
|
.no_artifacts()
|
|
|
|
.paths(gen_test_data_warning_path())
|
|
|
|
.ephemeral()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.has_compiler_warnings());
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
let project = Project::builder()
|
|
|
|
.no_artifacts()
|
|
|
|
.paths(gen_test_data_warning_path())
|
|
|
|
.ephemeral()
|
|
|
|
.set_compiler_severity_filter(ethers_solc::artifacts::Severity::Warning)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.has_compiler_warnings());
|
|
|
|
assert!(compiled.has_compiler_errors());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_compiler_severity_filter_and_ignored_error_codes() {
|
|
|
|
fn gen_test_data_licensing_warning() -> ProjectPathsConfig {
|
2022-11-13 12:49:27 +00:00
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.join("test-data/test-contract-warnings/LicenseWarning.sol");
|
|
|
|
|
|
|
|
ProjectPathsConfig::builder().sources(root).build().unwrap()
|
2022-11-10 20:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let missing_license_error_code = 1878;
|
|
|
|
|
|
|
|
let project = Project::builder()
|
2022-11-13 12:49:27 +00:00
|
|
|
.no_artifacts()
|
|
|
|
.paths(gen_test_data_licensing_warning())
|
|
|
|
.ephemeral()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2022-11-10 20:19:43 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.has_compiler_warnings());
|
|
|
|
|
|
|
|
let project = Project::builder()
|
|
|
|
.no_artifacts()
|
|
|
|
.paths(gen_test_data_licensing_warning())
|
|
|
|
.ephemeral()
|
|
|
|
.ignore_error_code(missing_license_error_code)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_warnings());
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
let project = Project::builder()
|
2022-11-13 12:49:27 +00:00
|
|
|
.no_artifacts()
|
|
|
|
.paths(gen_test_data_licensing_warning())
|
|
|
|
.ephemeral()
|
|
|
|
.ignore_error_code(missing_license_error_code)
|
|
|
|
.set_compiler_severity_filter(ethers_solc::artifacts::Severity::Warning)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2022-11-10 20:19:43 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_warnings());
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
}
|
|
|
|
|
2022-05-14 23:29:45 +00:00
|
|
|
fn remove_solc_if_exists(version: &Version) {
|
2022-07-12 18:27:55 +00:00
|
|
|
if Solc::find_svm_installed_version(version.to_string()).unwrap().is_some() {
|
|
|
|
svm::remove_version(version).expect("failed to remove version")
|
|
|
|
}
|
2022-05-14 23:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
async fn can_install_solc_and_compile_version() {
|
|
|
|
let project = TempProject::dapptools().unwrap();
|
|
|
|
let version = Version::new(0, 8, 10);
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"Contract",
|
|
|
|
format!(
|
|
|
|
r#"
|
2022-12-06 06:05:33 +00:00
|
|
|
pragma solidity {version};
|
2022-05-14 23:29:45 +00:00
|
|
|
contract Contract {{ }}
|
2022-12-06 06:05:33 +00:00
|
|
|
"#
|
2022-05-14 23:29:45 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
remove_solc_if_exists(&version);
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
async fn can_install_solc_and_compile_std_json_input_async() {
|
|
|
|
let tmp = TempProject::dapptools_init().unwrap();
|
|
|
|
tmp.assert_no_errors();
|
|
|
|
let source = tmp.list_source_files().into_iter().find(|p| p.ends_with("Dapp.t.sol")).unwrap();
|
|
|
|
let input = tmp.project().standard_json_input(source).unwrap();
|
|
|
|
let solc = &tmp.project().solc;
|
|
|
|
|
|
|
|
assert!(input.settings.remappings.contains(&"ds-test/=lib/ds-test/src/".parse().unwrap()));
|
|
|
|
let input: CompilerInput = input.into();
|
|
|
|
assert!(input.sources.contains_key(Path::new("lib/ds-test/src/test.sol")));
|
|
|
|
|
|
|
|
remove_solc_if_exists(&solc.version().expect("failed to get version"));
|
|
|
|
|
|
|
|
let out = solc.async_compile(&input).await.unwrap();
|
|
|
|
assert!(!out.has_error());
|
|
|
|
assert!(out.sources.contains_key("lib/ds-test/src/test.sol"));
|
|
|
|
}
|
2022-05-16 23:48:47 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_purge_obsolete_artifacts() {
|
|
|
|
let mut project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();
|
|
|
|
project.set_solc("0.8.10");
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"Contract",
|
|
|
|
r#"
|
|
|
|
pragma solidity >=0.8.10;
|
|
|
|
|
|
|
|
contract Contract {
|
|
|
|
function xyz() public {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(!compiled.is_unchanged());
|
|
|
|
assert_eq!(compiled.into_artifacts().count(), 1);
|
|
|
|
|
|
|
|
project.set_solc("0.8.13");
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(!compiled.is_unchanged());
|
|
|
|
assert_eq!(compiled.into_artifacts().count(), 1);
|
|
|
|
}
|
2022-05-19 18:02:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_parse_notice() {
|
|
|
|
let mut project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();
|
|
|
|
project.project_mut().artifacts.additional_values.userdoc = true;
|
|
|
|
project.project_mut().solc_config.settings = project.project_mut().artifacts.settings();
|
|
|
|
|
|
|
|
let contract = r#"
|
|
|
|
pragma solidity $VERSION;
|
|
|
|
|
|
|
|
contract Contract {
|
|
|
|
string greeting;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice hello
|
|
|
|
*/
|
|
|
|
constructor(string memory _greeting) public {
|
|
|
|
greeting = _greeting;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice hello
|
|
|
|
*/
|
|
|
|
function xyz() public {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @notice hello
|
|
|
|
function abc() public {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
project.add_source("Contract", contract.replace("$VERSION", "=0.5.17")).unwrap();
|
|
|
|
|
|
|
|
let mut compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(!compiled.is_unchanged());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Contract").is_some());
|
|
|
|
let userdoc = compiled.remove_first("Contract").unwrap().userdoc;
|
2022-05-19 18:02:10 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
userdoc,
|
|
|
|
Some(UserDoc {
|
|
|
|
version: None,
|
|
|
|
kind: None,
|
|
|
|
methods: BTreeMap::from([
|
2022-06-08 10:30:41 +00:00
|
|
|
("abc()".to_string(), UserDocNotice::Notice { notice: "hello".to_string() }),
|
|
|
|
("xyz()".to_string(), UserDocNotice::Notice { notice: "hello".to_string() }),
|
2022-05-19 18:02:10 +00:00
|
|
|
("constructor".to_string(), UserDocNotice::Constructor("hello".to_string())),
|
|
|
|
]),
|
2022-06-08 10:30:41 +00:00
|
|
|
events: BTreeMap::new(),
|
|
|
|
errors: BTreeMap::new(),
|
2022-05-19 18:02:10 +00:00
|
|
|
notice: None
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
project.add_source("Contract", contract.replace("$VERSION", "^0.8.10")).unwrap();
|
|
|
|
|
|
|
|
let mut compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(!compiled.is_unchanged());
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("Contract").is_some());
|
|
|
|
let userdoc = compiled.remove_first("Contract").unwrap().userdoc;
|
2022-05-19 18:02:10 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
userdoc,
|
|
|
|
Some(UserDoc {
|
|
|
|
version: Some(1),
|
|
|
|
kind: Some("user".to_string()),
|
|
|
|
methods: BTreeMap::from([
|
2022-06-08 10:30:41 +00:00
|
|
|
("abc()".to_string(), UserDocNotice::Notice { notice: "hello".to_string() }),
|
|
|
|
("xyz()".to_string(), UserDocNotice::Notice { notice: "hello".to_string() }),
|
|
|
|
("constructor".to_string(), UserDocNotice::Notice { notice: "hello".to_string() }),
|
2022-05-19 18:02:10 +00:00
|
|
|
]),
|
2022-06-08 10:30:41 +00:00
|
|
|
events: BTreeMap::new(),
|
|
|
|
errors: BTreeMap::new(),
|
2022-05-19 18:02:10 +00:00
|
|
|
notice: None
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2022-05-24 14:51:09 +00:00
|
|
|
|
2022-06-08 10:30:41 +00:00
|
|
|
#[test]
|
|
|
|
fn can_parse_doc() {
|
|
|
|
let mut project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();
|
|
|
|
project.project_mut().artifacts.additional_values.userdoc = true;
|
|
|
|
project.project_mut().artifacts.additional_values.devdoc = true;
|
|
|
|
project.project_mut().solc_config.settings = project.project_mut().artifacts.settings();
|
|
|
|
|
|
|
|
let contract = r#"
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
2023-02-10 19:45:51 +00:00
|
|
|
pragma solidity 0.8.17;
|
2022-06-08 10:30:41 +00:00
|
|
|
|
|
|
|
/// @title Not an ERC20.
|
|
|
|
/// @author Notadev
|
|
|
|
/// @notice Do not use this.
|
|
|
|
/// @dev This is not an ERC20 implementation.
|
|
|
|
/// @custom:experimental This is an experimental contract.
|
|
|
|
interface INotERC20 {
|
|
|
|
/// @notice Transfer tokens.
|
|
|
|
/// @dev Transfer `amount` tokens to account `to`.
|
|
|
|
/// @param to Target account.
|
|
|
|
/// @param amount Transfer amount.
|
|
|
|
/// @return A boolean value indicating whether the operation succeeded.
|
|
|
|
function transfer(address to, uint256 amount) external returns (bool);
|
|
|
|
|
|
|
|
/// @notice Transfer some tokens.
|
|
|
|
/// @dev Emitted when transfer.
|
|
|
|
/// @param from Source account.
|
|
|
|
/// @param to Target account.
|
|
|
|
/// @param value Transfer amount.
|
|
|
|
event Transfer(address indexed from, address indexed to, uint256 value);
|
|
|
|
|
|
|
|
/// @notice Insufficient balance for transfer.
|
|
|
|
/// @dev Needed `required` but only `available` available.
|
|
|
|
/// @param available Balance available.
|
|
|
|
/// @param required Requested amount to transfer.
|
|
|
|
error InsufficientBalance(uint256 available, uint256 required);
|
|
|
|
}
|
|
|
|
|
|
|
|
contract NotERC20 is INotERC20 {
|
|
|
|
/// @inheritdoc INotERC20
|
|
|
|
function transfer(address to, uint256 amount) external returns (bool) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
project.add_source("Contract", contract).unwrap();
|
|
|
|
|
|
|
|
let mut compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(!compiled.is_unchanged());
|
|
|
|
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("INotERC20").is_some());
|
|
|
|
let contract = compiled.remove_first("INotERC20").unwrap();
|
2022-06-08 10:30:41 +00:00
|
|
|
assert_eq!(
|
|
|
|
contract.userdoc,
|
|
|
|
Some(UserDoc {
|
|
|
|
version: Some(1),
|
|
|
|
kind: Some("user".to_string()),
|
|
|
|
notice: Some("Do not use this.".to_string()),
|
|
|
|
methods: BTreeMap::from([(
|
|
|
|
"transfer(address,uint256)".to_string(),
|
|
|
|
UserDocNotice::Notice { notice: "Transfer tokens.".to_string() }
|
|
|
|
),]),
|
|
|
|
events: BTreeMap::from([(
|
|
|
|
"Transfer(address,address,uint256)".to_string(),
|
|
|
|
UserDocNotice::Notice { notice: "Transfer some tokens.".to_string() }
|
|
|
|
),]),
|
|
|
|
errors: BTreeMap::from([(
|
|
|
|
"InsufficientBalance(uint256,uint256)".to_string(),
|
|
|
|
vec![UserDocNotice::Notice {
|
|
|
|
notice: "Insufficient balance for transfer.".to_string()
|
|
|
|
}]
|
|
|
|
),]),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
contract.devdoc,
|
|
|
|
Some(DevDoc {
|
|
|
|
version: Some(1),
|
|
|
|
kind: Some("dev".to_string()),
|
|
|
|
author: Some("Notadev".to_string()),
|
|
|
|
details: Some("This is not an ERC20 implementation.".to_string()),
|
|
|
|
custom_experimental: Some("This is an experimental contract.".to_string()),
|
|
|
|
methods: BTreeMap::from([(
|
|
|
|
"transfer(address,uint256)".to_string(),
|
|
|
|
MethodDoc {
|
|
|
|
details: Some("Transfer `amount` tokens to account `to`.".to_string()),
|
|
|
|
params: BTreeMap::from([
|
|
|
|
("to".to_string(), "Target account.".to_string()),
|
|
|
|
("amount".to_string(), "Transfer amount.".to_string())
|
|
|
|
]),
|
|
|
|
returns: BTreeMap::from([(
|
|
|
|
"_0".to_string(),
|
|
|
|
"A boolean value indicating whether the operation succeeded.".to_string()
|
|
|
|
),])
|
|
|
|
}
|
|
|
|
),]),
|
|
|
|
events: BTreeMap::from([(
|
|
|
|
"Transfer(address,address,uint256)".to_string(),
|
|
|
|
EventDoc {
|
|
|
|
details: Some("Emitted when transfer.".to_string()),
|
|
|
|
params: BTreeMap::from([
|
|
|
|
("from".to_string(), "Source account.".to_string()),
|
|
|
|
("to".to_string(), "Target account.".to_string()),
|
|
|
|
("value".to_string(), "Transfer amount.".to_string()),
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
),]),
|
|
|
|
errors: BTreeMap::from([(
|
|
|
|
"InsufficientBalance(uint256,uint256)".to_string(),
|
|
|
|
vec![ErrorDoc {
|
|
|
|
details: Some("Needed `required` but only `available` available.".to_string()),
|
|
|
|
params: BTreeMap::from([
|
|
|
|
("available".to_string(), "Balance available.".to_string()),
|
|
|
|
("required".to_string(), "Requested amount to transfer.".to_string())
|
|
|
|
]),
|
|
|
|
}]
|
|
|
|
),]),
|
|
|
|
title: Some("Not an ERC20.".to_string())
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2022-07-04 18:53:49 +00:00
|
|
|
assert!(compiled.find_first("NotERC20").is_some());
|
|
|
|
let contract = compiled.remove_first("NotERC20").unwrap();
|
2022-06-08 10:30:41 +00:00
|
|
|
assert_eq!(
|
|
|
|
contract.userdoc,
|
|
|
|
Some(UserDoc {
|
|
|
|
version: Some(1),
|
|
|
|
kind: Some("user".to_string()),
|
|
|
|
notice: None,
|
|
|
|
methods: BTreeMap::from([(
|
|
|
|
"transfer(address,uint256)".to_string(),
|
|
|
|
UserDocNotice::Notice { notice: "Transfer tokens.".to_string() }
|
|
|
|
),]),
|
|
|
|
events: BTreeMap::from([(
|
|
|
|
"Transfer(address,address,uint256)".to_string(),
|
|
|
|
UserDocNotice::Notice { notice: "Transfer some tokens.".to_string() }
|
|
|
|
),]),
|
|
|
|
errors: BTreeMap::from([(
|
|
|
|
"InsufficientBalance(uint256,uint256)".to_string(),
|
|
|
|
vec![UserDocNotice::Notice {
|
|
|
|
notice: "Insufficient balance for transfer.".to_string()
|
|
|
|
}]
|
|
|
|
),]),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
contract.devdoc,
|
|
|
|
Some(DevDoc {
|
|
|
|
version: Some(1),
|
|
|
|
kind: Some("dev".to_string()),
|
|
|
|
author: None,
|
|
|
|
details: None,
|
|
|
|
custom_experimental: None,
|
|
|
|
methods: BTreeMap::from([(
|
|
|
|
"transfer(address,uint256)".to_string(),
|
|
|
|
MethodDoc {
|
|
|
|
details: Some("Transfer `amount` tokens to account `to`.".to_string()),
|
|
|
|
params: BTreeMap::from([
|
|
|
|
("to".to_string(), "Target account.".to_string()),
|
|
|
|
("amount".to_string(), "Transfer amount.".to_string())
|
|
|
|
]),
|
|
|
|
returns: BTreeMap::from([(
|
|
|
|
"_0".to_string(),
|
|
|
|
"A boolean value indicating whether the operation succeeded.".to_string()
|
|
|
|
),])
|
|
|
|
}
|
|
|
|
),]),
|
|
|
|
events: BTreeMap::new(),
|
|
|
|
errors: BTreeMap::from([(
|
|
|
|
"InsufficientBalance(uint256,uint256)".to_string(),
|
|
|
|
vec![ErrorDoc {
|
|
|
|
details: Some("Needed `required` but only `available` available.".to_string()),
|
|
|
|
params: BTreeMap::from([
|
|
|
|
("available".to_string(), "Balance available.".to_string()),
|
|
|
|
("required".to_string(), "Requested amount to transfer.".to_string())
|
|
|
|
]),
|
|
|
|
}]
|
|
|
|
),]),
|
|
|
|
title: None
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-24 14:51:09 +00:00
|
|
|
#[test]
|
|
|
|
fn test_relative_cache_entries() {
|
|
|
|
let project = TempProject::dapptools().unwrap();
|
|
|
|
let _a = project
|
|
|
|
.add_source(
|
|
|
|
"A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract A { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let _b = project
|
|
|
|
.add_source(
|
|
|
|
"B",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract B { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let _c = project
|
|
|
|
.add_source(
|
|
|
|
"C",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract C { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let _d = project
|
|
|
|
.add_source(
|
|
|
|
"D",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract D { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
let cache = SolFilesCache::read(project.cache_path()).unwrap();
|
|
|
|
|
|
|
|
let entries = vec![
|
|
|
|
PathBuf::from("src/A.sol"),
|
|
|
|
PathBuf::from("src/B.sol"),
|
|
|
|
PathBuf::from("src/C.sol"),
|
|
|
|
PathBuf::from("src/D.sol"),
|
|
|
|
];
|
|
|
|
assert_eq!(entries, cache.files.keys().cloned().collect::<Vec<_>>());
|
|
|
|
|
|
|
|
let cache = SolFilesCache::read_joined(project.paths()).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
entries.into_iter().map(|p| project.root().join(p)).collect::<Vec<_>>(),
|
|
|
|
cache.files.keys().cloned().collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
}
|
2022-06-02 17:31:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_failure_after_removing_file() {
|
|
|
|
let project = TempProject::dapptools().unwrap();
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "./B.sol";
|
|
|
|
contract A { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"B",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "./C.sol";
|
|
|
|
contract B { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let c = project
|
|
|
|
.add_source(
|
|
|
|
"C",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract C { }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
fs::remove_file(c).unwrap();
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.has_compiler_errors());
|
|
|
|
}
|
2022-07-24 21:39:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_handle_conflicting_files() {
|
|
|
|
let project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"Greeter",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract Greeter {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"tokens/Greeter",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract Greeter {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
let artifacts = compiled.artifacts().count();
|
|
|
|
assert_eq!(artifacts, 2);
|
|
|
|
|
|
|
|
// nothing to compile
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
let artifacts = compiled.artifacts().count();
|
|
|
|
assert_eq!(artifacts, 2);
|
|
|
|
|
|
|
|
let cache = SolFilesCache::read(project.cache_path()).unwrap();
|
|
|
|
|
|
|
|
let mut source_files = cache.files.keys().cloned().collect::<Vec<_>>();
|
|
|
|
source_files.sort_unstable();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
source_files,
|
|
|
|
vec![PathBuf::from("src/Greeter.sol"), PathBuf::from("src/tokens/Greeter.sol"),]
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut artifacts = project.artifacts_snapshot().unwrap().artifacts;
|
|
|
|
artifacts.strip_prefix_all(&project.paths().artifacts);
|
|
|
|
|
|
|
|
assert_eq!(artifacts.len(), 2);
|
|
|
|
let mut artifact_files = artifacts.artifact_files().map(|f| f.file.clone()).collect::<Vec<_>>();
|
|
|
|
artifact_files.sort_unstable();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
artifact_files,
|
|
|
|
vec![
|
|
|
|
PathBuf::from("Greeter.sol/Greeter.json"),
|
|
|
|
PathBuf::from("tokens/Greeter.sol/Greeter.json"),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2022-08-09 16:21:52 +00:00
|
|
|
|
2022-08-19 21:33:28 +00:00
|
|
|
// <https://github.com/foundry-rs/foundry/issues/2843>
|
|
|
|
#[test]
|
|
|
|
fn can_handle_conflicting_files_recompile() {
|
|
|
|
let project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract A {
|
|
|
|
function foo() public{}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"inner/A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract A {
|
|
|
|
function bar() public{}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
let artifacts = compiled.artifacts().count();
|
|
|
|
assert_eq!(artifacts, 2);
|
|
|
|
|
|
|
|
// nothing to compile
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
let artifacts = compiled.artifacts().count();
|
|
|
|
assert_eq!(artifacts, 2);
|
|
|
|
|
|
|
|
let cache = SolFilesCache::read(project.cache_path()).unwrap();
|
|
|
|
|
|
|
|
let mut source_files = cache.files.keys().cloned().collect::<Vec<_>>();
|
|
|
|
source_files.sort_unstable();
|
|
|
|
|
|
|
|
assert_eq!(source_files, vec![PathBuf::from("src/A.sol"), PathBuf::from("src/inner/A.sol"),]);
|
|
|
|
|
|
|
|
let mut artifacts =
|
|
|
|
project.artifacts_snapshot().unwrap().artifacts.into_stripped_file_prefixes(project.root());
|
|
|
|
artifacts.strip_prefix_all(&project.paths().artifacts);
|
|
|
|
|
|
|
|
assert_eq!(artifacts.len(), 2);
|
|
|
|
let mut artifact_files = artifacts.artifact_files().map(|f| f.file.clone()).collect::<Vec<_>>();
|
|
|
|
artifact_files.sort_unstable();
|
|
|
|
|
|
|
|
let expected_files = vec![PathBuf::from("A.sol/A.json"), PathBuf::from("inner/A.sol/A.json")];
|
|
|
|
assert_eq!(artifact_files, expected_files);
|
|
|
|
|
|
|
|
// overwrite conflicting nested file, effectively changing it
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"inner/A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract A {
|
|
|
|
function bar() public{}
|
|
|
|
function baz() public{}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
let mut recompiled_artifacts =
|
|
|
|
project.artifacts_snapshot().unwrap().artifacts.into_stripped_file_prefixes(project.root());
|
|
|
|
recompiled_artifacts.strip_prefix_all(&project.paths().artifacts);
|
|
|
|
|
|
|
|
assert_eq!(recompiled_artifacts.len(), 2);
|
|
|
|
let mut artifact_files =
|
|
|
|
recompiled_artifacts.artifact_files().map(|f| f.file.clone()).collect::<Vec<_>>();
|
|
|
|
artifact_files.sort_unstable();
|
|
|
|
assert_eq!(artifact_files, expected_files);
|
|
|
|
|
|
|
|
// ensure that `a.sol/A.json` is unchanged
|
|
|
|
let outer = artifacts.find("src/A.sol", "A").unwrap();
|
|
|
|
let outer_recompiled = recompiled_artifacts.find("src/A.sol", "A").unwrap();
|
|
|
|
assert_eq!(outer, outer_recompiled);
|
|
|
|
|
|
|
|
let inner_recompiled = recompiled_artifacts.find("src/inner/A.sol", "A").unwrap();
|
|
|
|
assert!(inner_recompiled.get_abi().unwrap().functions.contains_key("baz"));
|
|
|
|
}
|
|
|
|
|
2022-08-20 22:35:56 +00:00
|
|
|
// <https://github.com/foundry-rs/foundry/issues/2843>
|
|
|
|
#[test]
|
|
|
|
fn can_handle_conflicting_files_case_sensitive_recompile() {
|
|
|
|
let project = TempProject::<ConfigurableArtifacts>::dapptools().unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"a",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract A {
|
|
|
|
function foo() public{}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"inner/A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
contract A {
|
|
|
|
function bar() public{}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
let artifacts = compiled.artifacts().count();
|
|
|
|
assert_eq!(artifacts, 2);
|
|
|
|
|
|
|
|
// nothing to compile
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
let artifacts = compiled.artifacts().count();
|
|
|
|
assert_eq!(artifacts, 2);
|
|
|
|
|
|
|
|
let cache = SolFilesCache::read(project.cache_path()).unwrap();
|
|
|
|
|
|
|
|
let mut source_files = cache.files.keys().cloned().collect::<Vec<_>>();
|
|
|
|
source_files.sort_unstable();
|
|
|
|
|
|
|
|
assert_eq!(source_files, vec![PathBuf::from("src/a.sol"), PathBuf::from("src/inner/A.sol"),]);
|
|
|
|
|
|
|
|
let mut artifacts =
|
|
|
|
project.artifacts_snapshot().unwrap().artifacts.into_stripped_file_prefixes(project.root());
|
|
|
|
artifacts.strip_prefix_all(&project.paths().artifacts);
|
|
|
|
|
|
|
|
assert_eq!(artifacts.len(), 2);
|
|
|
|
let mut artifact_files = artifacts.artifact_files().map(|f| f.file.clone()).collect::<Vec<_>>();
|
|
|
|
artifact_files.sort_unstable();
|
|
|
|
|
|
|
|
let expected_files = vec![PathBuf::from("a.sol/A.json"), PathBuf::from("inner/A.sol/A.json")];
|
|
|
|
assert_eq!(artifact_files, expected_files);
|
|
|
|
|
|
|
|
// overwrite conflicting nested file, effectively changing it
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"inner/A",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract A {
|
|
|
|
function bar() public{}
|
|
|
|
function baz() public{}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
let mut recompiled_artifacts =
|
|
|
|
project.artifacts_snapshot().unwrap().artifacts.into_stripped_file_prefixes(project.root());
|
|
|
|
recompiled_artifacts.strip_prefix_all(&project.paths().artifacts);
|
|
|
|
|
|
|
|
assert_eq!(recompiled_artifacts.len(), 2);
|
|
|
|
let mut artifact_files =
|
|
|
|
recompiled_artifacts.artifact_files().map(|f| f.file.clone()).collect::<Vec<_>>();
|
|
|
|
artifact_files.sort_unstable();
|
|
|
|
assert_eq!(artifact_files, expected_files);
|
|
|
|
|
|
|
|
// ensure that `a.sol/A.json` is unchanged
|
|
|
|
let outer = artifacts.find("src/a.sol", "A").unwrap();
|
|
|
|
let outer_recompiled = recompiled_artifacts.find("src/a.sol", "A").unwrap();
|
|
|
|
assert_eq!(outer, outer_recompiled);
|
|
|
|
|
|
|
|
let inner_recompiled = recompiled_artifacts.find("src/inner/A.sol", "A").unwrap();
|
|
|
|
assert!(inner_recompiled.get_abi().unwrap().functions.contains_key("baz"));
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:24:54 +00:00
|
|
|
#[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();
|
|
|
|
}
|
|
|
|
|
2022-08-11 23:43:35 +00:00
|
|
|
#[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());
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:21:52 +00:00
|
|
|
#[test]
|
|
|
|
fn can_add_basic_contract_and_library() {
|
|
|
|
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());
|
|
|
|
|
|
|
|
let src = project.add_basic_source("Foo.sol", "^0.8.0").unwrap();
|
|
|
|
|
2022-08-09 21:49:17 +00:00
|
|
|
let lib = project.add_basic_source("Bar", "^0.8.0").unwrap();
|
2022-08-09 16:21:52 +00:00
|
|
|
|
|
|
|
let graph = Graph::resolve(project.paths()).unwrap();
|
|
|
|
assert_eq!(graph.files().len(), 2);
|
2022-08-09 21:49:17 +00:00
|
|
|
assert!(graph.files().contains_key(&src));
|
|
|
|
assert!(graph.files().contains_key(&lib));
|
2022-08-09 16:21:52 +00:00
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
2022-08-09 21:49:17 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
2022-08-09 16:21:52 +00:00
|
|
|
assert!(compiled.find_first("Foo").is_some());
|
|
|
|
assert!(compiled.find_first("Bar").is_some());
|
|
|
|
}
|
2022-08-12 19:11:08 +00:00
|
|
|
|
|
|
|
// <https://github.com/foundry-rs/foundry/issues/2706>
|
|
|
|
#[test]
|
|
|
|
fn can_handle_nested_absolute_imports() {
|
|
|
|
let mut project = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
let remapping = project.paths().libraries[0].join("myDepdendency");
|
|
|
|
project
|
|
|
|
.paths_mut()
|
|
|
|
.remappings
|
|
|
|
.push(Remapping::from_str(&format!("myDepdendency/={}/", remapping.display())).unwrap());
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_lib(
|
|
|
|
"myDepdendency/src/interfaces/IConfig.sol",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
|
|
|
|
interface IConfig {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_lib(
|
|
|
|
"myDepdendency/src/Config.sol",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "src/interfaces/IConfig.sol";
|
|
|
|
|
|
|
|
contract Config {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"Greeter",
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
import "myDepdendency/src/Config.sol";
|
|
|
|
|
|
|
|
contract Greeter {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(compiled.find_first("Greeter").is_some());
|
|
|
|
assert!(compiled.find_first("Config").is_some());
|
|
|
|
assert!(compiled.find_first("IConfig").is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_handle_nested_test_absolute_imports() {
|
|
|
|
let project = TempProject::dapptools().unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_source(
|
|
|
|
"Contract.sol",
|
|
|
|
r#"
|
|
|
|
// SPDX-License-Identifier: UNLICENSED
|
|
|
|
pragma solidity =0.8.13;
|
|
|
|
|
|
|
|
library Library {
|
|
|
|
function f(uint256 a, uint256 b) public pure returns (uint256) {
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
contract Contract {
|
|
|
|
uint256 c;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
c = Library.f(1, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
project
|
|
|
|
.add_test(
|
|
|
|
"Contract.t.sol",
|
|
|
|
r#"
|
|
|
|
// SPDX-License-Identifier: UNLICENSED
|
|
|
|
pragma solidity =0.8.13;
|
|
|
|
|
|
|
|
import "src/Contract.sol";
|
|
|
|
|
|
|
|
contract ContractTest {
|
|
|
|
function setUp() public {
|
|
|
|
}
|
|
|
|
|
|
|
|
function test() public {
|
|
|
|
new Contract();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(compiled.find_first("Contract").is_some());
|
|
|
|
}
|