2021-10-30 17:59:44 +00:00
|
|
|
//! project tests
|
|
|
|
|
2021-11-26 16:49:19 +00:00
|
|
|
use std::{
|
2022-02-04 16:20:24 +00:00
|
|
|
collections::{HashMap, HashSet},
|
2021-11-26 16:49:19 +00:00
|
|
|
io,
|
|
|
|
path::{Path, PathBuf},
|
2022-01-05 21:46:57 +00:00
|
|
|
str::FromStr,
|
2021-11-26 16:49:19 +00:00
|
|
|
};
|
2022-01-05 21:46:57 +00:00
|
|
|
|
|
|
|
use ethers_solc::{
|
|
|
|
cache::{SolFilesCache, SOLIDITY_FILES_CACHE_FILENAME},
|
|
|
|
project_util::*,
|
|
|
|
remappings::Remapping,
|
2022-02-17 15:31:35 +00:00
|
|
|
ConfigurableArtifacts, ExtraOutputValues, Graph, Project, ProjectCompileOutput,
|
|
|
|
ProjectPathsConfig,
|
2022-01-05 21:46:57 +00:00
|
|
|
};
|
2022-02-04 16:20:24 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
|
|
|
|
#[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();
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(compiled.find("Greeter").is_some());
|
|
|
|
assert!(compiled.find("console").is_some());
|
|
|
|
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();
|
|
|
|
assert!(compiled.find("Greeter").is_some());
|
|
|
|
assert!(compiled.find("console").is_some());
|
|
|
|
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();
|
|
|
|
assert!(compiled.find("Greeter").is_some());
|
|
|
|
assert!(compiled.find("console").is_some());
|
|
|
|
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();
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(compiled.find("Dapp").is_some());
|
|
|
|
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();
|
|
|
|
assert!(compiled.find("Dapp").is_some());
|
|
|
|
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();
|
|
|
|
assert!(compiled.find("Dapp").is_some());
|
|
|
|
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-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,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let settings = handler.settings();
|
|
|
|
let project = TempProject::with_artifacts(paths, handler).unwrap().with_settings(settings);
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
let artifact = compiled.find("Dapp").unwrap();
|
|
|
|
assert!(artifact.metadata.is_some());
|
|
|
|
assert!(artifact.ir.is_some());
|
|
|
|
assert!(artifact.ir_optimized.is_some());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
.push(Remapping::from_str(&format!("remapping={}/", remapping.display())).unwrap());
|
|
|
|
|
|
|
|
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();
|
|
|
|
assert!(compiled.find("Foo").is_some());
|
|
|
|
assert!(compiled.find("Bar").is_some());
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
// nothing to compile
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.find("Foo").is_some());
|
|
|
|
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();
|
|
|
|
assert!(compiled.find("Foo").is_some());
|
|
|
|
assert!(compiled.find("Bar").is_some());
|
|
|
|
// 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());
|
|
|
|
assert!(compiled.find("DssSpellTest").is_some());
|
|
|
|
assert!(compiled.find("DssSpellTestBase").is_some());
|
|
|
|
|
|
|
|
// nothing to compile
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
assert!(compiled.find("DssSpellTest").is_some());
|
|
|
|
assert!(compiled.find("DssSpellTestBase").is_some());
|
|
|
|
|
|
|
|
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();
|
|
|
|
assert!(compiled.find("DssSpellTest").is_some());
|
|
|
|
assert!(compiled.find("DssSpellTestBase").is_some());
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
assert!(compiled.find("Dapp").is_some());
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
|
|
|
|
// cache is used when nothing to compile
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.find("Dapp").is_some());
|
|
|
|
assert!(compiled.is_unchanged());
|
|
|
|
|
|
|
|
// deleted artifacts cause recompile even with cache
|
2021-12-12 23:39:28 +00:00
|
|
|
std::fs::remove_dir_all(&project.artifacts_path()).unwrap();
|
2021-11-26 16:49:19 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(compiled.find("Dapp").is_some());
|
|
|
|
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();
|
|
|
|
assert!(compiled.find("Dapp").is_some());
|
|
|
|
assert!(compiled.find("NewContract").is_some());
|
|
|
|
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
|
|
|
|
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();
|
2021-11-26 16:49:19 +00:00
|
|
|
assert!(compiled.find("Dapp").is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
r#"
|
|
|
|
pragma solidity ^0.8.10;
|
|
|
|
contract C { }
|
|
|
|
contract B { }
|
|
|
|
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 {}
|
|
|
|
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");
|
|
|
|
let paths = ProjectPathsConfig::builder().sources(&root.join("contracts"));
|
|
|
|
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 {}
|
|
|
|
// Intentionally erroneous code
|
|
|
|
contract Contract {
|
|
|
|
failure();
|
|
|
|
}
|
|
|
|
"#
|
|
|
|
);
|
2022-01-19 16:11:37 +00:00
|
|
|
}
|
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());
|
|
|
|
assert!(compiled.find("Foo").is_some());
|
|
|
|
|
|
|
|
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());
|
|
|
|
assert!(compiled.find("Foo").is_some());
|
|
|
|
assert!(compiled.find("Bar").is_some());
|
|
|
|
}
|
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());
|
|
|
|
|
|
|
|
let contract = compiled.find("LinkTest").unwrap();
|
|
|
|
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());
|
|
|
|
}
|