2022-03-15 12:27:49 +00:00
|
|
|
//! mocked project tests
|
|
|
|
|
|
|
|
use ethers_solc::{
|
|
|
|
error::Result,
|
|
|
|
project_util::{
|
2022-03-17 10:48:01 +00:00
|
|
|
mock::{MockProjectGenerator, MockProjectSettings, MockProjectSkeleton},
|
2022-03-15 12:27:49 +00:00
|
|
|
TempProject,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// default version to use
|
|
|
|
const DEFAULT_VERSION: &str = "^0.8.10";
|
|
|
|
|
|
|
|
struct MockSettings {
|
|
|
|
settings: MockProjectSettings,
|
|
|
|
version: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<MockProjectSettings> for MockSettings {
|
|
|
|
fn from(settings: MockProjectSettings) -> Self {
|
|
|
|
MockSettings { settings, version: DEFAULT_VERSION }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<(MockProjectSettings, &'static str)> for MockSettings {
|
|
|
|
fn from(input: (MockProjectSettings, &'static str)) -> Self {
|
|
|
|
MockSettings { settings: input.0, version: input.1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function to run a test and report the used generator if the closure failed.
|
|
|
|
fn run_mock(
|
|
|
|
settings: impl Into<MockSettings>,
|
2022-03-15 15:58:33 +00:00
|
|
|
f: impl FnOnce(&mut TempProject, &MockProjectGenerator) -> Result<()>,
|
2022-03-15 12:27:49 +00:00
|
|
|
) -> TempProject {
|
|
|
|
let MockSettings { settings, version } = settings.into();
|
|
|
|
let gen = MockProjectGenerator::new(&settings);
|
|
|
|
let mut project = TempProject::dapptools().unwrap();
|
|
|
|
let remappings = gen.remappings_at(project.root());
|
|
|
|
project.paths_mut().remappings.extend(remappings);
|
|
|
|
project.mock(&gen, version).unwrap();
|
|
|
|
|
2022-03-15 15:58:33 +00:00
|
|
|
if let Err(err) = f(&mut project, &gen) {
|
2022-03-15 12:27:49 +00:00
|
|
|
panic!(
|
|
|
|
"mock failed: `{}` with mock settings:\n {}",
|
|
|
|
err,
|
|
|
|
serde_json::to_string(&gen).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
project
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs a basic set of tests for the given settings
|
|
|
|
fn run_basic(settings: impl Into<MockSettings>) {
|
|
|
|
let settings = settings.into();
|
|
|
|
let version = settings.version;
|
2022-03-15 15:58:33 +00:00
|
|
|
run_mock(settings, |project, _| {
|
2022-03-15 12:27:49 +00:00
|
|
|
project.ensure_no_errors_recompile_unchanged()?;
|
|
|
|
project.add_basic_source("Dummy", version)?;
|
|
|
|
project.ensure_changed()?;
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compile_mocked_random() {
|
|
|
|
run_basic(MockProjectSettings::random());
|
|
|
|
}
|
|
|
|
|
|
|
|
// compile a bunch of random projects
|
|
|
|
#[test]
|
|
|
|
fn can_compile_mocked_multi() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
run_basic(MockProjectSettings::random());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compile_mocked_large() {
|
|
|
|
run_basic(MockProjectSettings::large())
|
|
|
|
}
|
2022-03-15 15:58:33 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compile_mocked_modified() {
|
|
|
|
run_mock(MockProjectSettings::random(), |project, gen| {
|
|
|
|
project.ensure_no_errors_recompile_unchanged()?;
|
|
|
|
// modify a random file
|
2022-03-17 10:48:01 +00:00
|
|
|
gen.modify_file(gen.used_file_ids().count() / 2, project.paths(), DEFAULT_VERSION)?;
|
2022-03-15 15:58:33 +00:00
|
|
|
project.ensure_changed()?;
|
|
|
|
project.artifacts_snapshot()?.assert_artifacts_essentials_present();
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compile_mocked_modified_all() {
|
|
|
|
run_mock(MockProjectSettings::random(), |project, gen| {
|
|
|
|
project.ensure_no_errors_recompile_unchanged()?;
|
|
|
|
// modify a random file
|
2022-03-17 10:48:01 +00:00
|
|
|
for id in gen.used_file_ids() {
|
2022-03-15 15:58:33 +00:00
|
|
|
gen.modify_file(id, project.paths(), DEFAULT_VERSION)?;
|
|
|
|
project.ensure_changed()?;
|
|
|
|
project.artifacts_snapshot()?.assert_artifacts_essentials_present();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
2022-03-17 10:48:01 +00:00
|
|
|
|
|
|
|
// a test useful to manually debug a serialized skeleton
|
|
|
|
#[test]
|
|
|
|
fn can_compile_skeleton() {
|
|
|
|
let mut project = TempProject::dapptools().unwrap();
|
|
|
|
let s = r#"{"files":[{"id":0,"name":"SourceFile0","imports":[{"External":[0,1]},{"External":[3,4]}],"lib_id":null,"emit_artifacts":true},{"id":1,"name":"SourceFile1","imports":[],"lib_id":0,"emit_artifacts":true},{"id":2,"name":"SourceFile2","imports":[],"lib_id":1,"emit_artifacts":true},{"id":3,"name":"SourceFile3","imports":[],"lib_id":2,"emit_artifacts":true},{"id":4,"name":"SourceFile4","imports":[],"lib_id":3,"emit_artifacts":true}],"libraries":[{"name":"Lib0","id":0,"offset":1,"num_files":1},{"name":"Lib1","id":1,"offset":2,"num_files":1},{"name":"Lib2","id":2,"offset":3,"num_files":1},{"name":"Lib3","id":3,"offset":4,"num_files":1}]}"#;
|
|
|
|
let gen: MockProjectGenerator = serde_json::from_str::<MockProjectSkeleton>(s).unwrap().into();
|
|
|
|
let remappings = gen.remappings_at(project.root());
|
|
|
|
project.paths_mut().remappings.extend(remappings);
|
|
|
|
project.mock(&gen, DEFAULT_VERSION).unwrap();
|
|
|
|
|
|
|
|
// mattsse: helper to show what's being generated
|
|
|
|
// gen.write_to(ðers_solc::ProjectPathsConfig::dapptools("./skeleton").unwrap(),
|
|
|
|
// DEFAULT_VERSION).unwrap();
|
|
|
|
|
|
|
|
let compiled = project.compile().unwrap();
|
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
assert!(!compiled.is_unchanged());
|
|
|
|
for id in gen.used_file_ids() {
|
|
|
|
gen.modify_file(id, project.paths(), DEFAULT_VERSION).unwrap();
|
|
|
|
project.ensure_changed().unwrap();
|
|
|
|
project.artifacts_snapshot().unwrap().assert_artifacts_essentials_present();
|
|
|
|
}
|
|
|
|
}
|