use crate::{
artifacts::Settings,
config::ProjectPathsConfigBuilder,
error::{bail, Result, SolcError},
hh::HardhatArtifacts,
project_util::mock::{MockProjectGenerator, MockProjectSettings},
remappings::Remapping,
utils,
utils::tempdir,
Artifact, ArtifactOutput, Artifacts, ConfigurableArtifacts, ConfigurableContractArtifact,
FileFilter, PathStyle, Project, ProjectCompileOutput, ProjectPathsConfig, SolFilesCache,
SolcIoError,
};
use fs_extra::{dir, file};
use std::{
fmt,
path::{Path, PathBuf},
process,
process::Command,
};
use tempfile::TempDir;
pub mod mock;
pub struct TempProject<T: ArtifactOutput = ConfigurableArtifacts> {
_root: TempDir,
inner: Project<T>,
}
impl<T: ArtifactOutput> TempProject<T> {
pub fn create_new(root: TempDir, inner: Project<T>) -> std::result::Result<Self, SolcIoError> {
let mut project = Self { _root: root, inner };
project.paths().create_all()?;
project.inner.ignored_error_codes.push(1878);
Ok(project)
}
pub fn with_artifacts(paths: ProjectPathsConfigBuilder, artifacts: T) -> Result<Self> {
Self::prefixed_with_artifacts("temp-project", paths, artifacts)
}
pub fn prefixed_with_artifacts(
prefix: &str,
paths: ProjectPathsConfigBuilder,
artifacts: T,
) -> Result<Self> {
let tmp_dir = tempdir(prefix)?;
let paths = paths.build_with_root(tmp_dir.path());
let inner = Project::builder().artifacts(artifacts).paths(paths).build()?;
Ok(Self::create_new(tmp_dir, inner)?)
}
pub fn with_settings(mut self, settings: impl Into<Settings>) -> Self {
self.inner.solc_config.settings = settings.into();
self
}
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
pub fn set_solc(&mut self, solc: impl AsRef<str>) -> &mut Self {
self.inner.solc = crate::Solc::find_or_install_svm_version(solc).unwrap();
self.inner.auto_detect = false;
self
}
pub fn project(&self) -> &Project<T> {
&self.inner
}
pub fn compile(&self) -> Result<ProjectCompileOutput<T>> {
self.project().compile()
}
pub fn compile_sparse<F: FileFilter + 'static>(
&self,
filter: F,
) -> Result<ProjectCompileOutput<T>> {
self.project().compile_sparse(filter)
}
pub fn flatten(&self, target: &Path) -> Result<String> {
self.project().flatten(target)
}
pub fn project_mut(&mut self) -> &mut Project<T> {
&mut self.inner
}
pub fn paths(&self) -> &ProjectPathsConfig {
&self.project().paths
}
pub fn paths_mut(&mut self) -> &mut ProjectPathsConfig {
&mut self.project_mut().paths
}
pub fn artifacts_path(&self) -> &PathBuf {
&self.paths().artifacts
}
pub fn sources_path(&self) -> &PathBuf {
&self.paths().sources
}
pub fn cache_path(&self) -> &PathBuf {
&self.paths().cache
}
pub fn root(&self) -> &Path {
self.project().paths.root.as_path()
}
pub fn copy_source(&self, source: impl AsRef<Path>) -> Result<()> {
copy_file(source, &self.paths().sources)
}
pub fn copy_sources<I, S>(&self, sources: I) -> Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<Path>,
{
for path in sources {
self.copy_source(path)?;
}
Ok(())
}
fn get_lib(&self) -> Result<PathBuf> {
self.paths()
.libraries
.first()
.cloned()
.ok_or_else(|| SolcError::msg("No libraries folders configured"))
}
pub fn copy_lib(&self, lib: impl AsRef<Path>) -> Result<()> {
let lib_dir = self.get_lib()?;
copy_file(lib, lib_dir)
}
pub fn copy_libs<I, S>(&self, libs: I) -> Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<Path>,
{
for path in libs {
self.copy_lib(path)?;
}
Ok(())
}
pub fn add_lib(&self, name: impl AsRef<str>, content: impl AsRef<str>) -> Result<PathBuf> {
let name = contract_file_name(name);
let lib_dir = self.get_lib()?;
let lib = lib_dir.join(name);
create_contract_file(lib, content)
}
pub fn add_basic_lib(
&self,
name: impl AsRef<str>,
version: impl AsRef<str>,
) -> Result<PathBuf> {
let name = name.as_ref();
let name = name.strip_suffix(".sol").unwrap_or(name);
self.add_lib(
name,
format!(
r#"
// SPDX-License-Identifier: UNLICENSED
pragma solidity {};
contract {} {{}}
"#,
version.as_ref(),
name,
),
)
}
pub fn add_test(&self, name: impl AsRef<str>, content: impl AsRef<str>) -> Result<PathBuf> {
let name = contract_file_name(name);
let tests = self.paths().tests.join(name);
create_contract_file(tests, content)
}
pub fn add_script(&self, name: impl AsRef<str>, content: impl AsRef<str>) -> Result<PathBuf> {
let name = contract_file_name(name);
let script = self.paths().scripts.join(name);
create_contract_file(script, content)
}
pub fn add_source(&self, name: impl AsRef<str>, content: impl AsRef<str>) -> Result<PathBuf> {
let name = contract_file_name(name);
let source = self.paths().sources.join(name);
create_contract_file(source, content)
}
pub fn add_basic_source(
&self,
name: impl AsRef<str>,
version: impl AsRef<str>,
) -> Result<PathBuf> {
let name = name.as_ref();
let name = name.strip_suffix(".sol").unwrap_or(name);
self.add_source(
name,
format!(
r#"
// SPDX-License-Identifier: UNLICENSED
pragma solidity {};
contract {} {{}}
"#,
version.as_ref(),
name,
),
)
}
pub fn add_contract(&self, name: impl AsRef<str>, content: impl AsRef<str>) -> Result<PathBuf> {
let name = contract_file_name(name);
let source = self.root().join(name);
create_contract_file(source, content)
}
pub fn artifacts_snapshot(&self) -> Result<ArtifactsSnapshot<T::Artifact>> {
let cache = self.project().read_cache_file()?;
let artifacts = cache.read_artifacts::<T::Artifact>()?;
Ok(ArtifactsSnapshot { cache, artifacts })
}
pub fn mock(&self, gen: &MockProjectGenerator, version: impl AsRef<str>) -> Result<()> {
gen.write_to(self.paths(), version)
}
pub fn ensure_no_errors(&self) -> Result<&Self> {
let compiled = self.compile().unwrap();
if compiled.has_compiler_errors() {
bail!("Compiled with errors {}", compiled)
}
Ok(self)
}
pub fn ensure_unchanged(&self) -> Result<&Self> {
let compiled = self.compile().unwrap();
if !compiled.is_unchanged() {
bail!("Compiled with detected changes {}", compiled)
}
Ok(self)
}
pub fn ensure_changed(&self) -> Result<&Self> {
let compiled = self.compile().unwrap();
if compiled.is_unchanged() {
bail!("Compiled without detecting changes {}", compiled)
}
Ok(self)
}
pub fn ensure_no_errors_recompile_unchanged(&self) -> Result<&Self> {
self.ensure_no_errors()?.ensure_unchanged()
}
pub fn assert_no_errors_recompile_unchanged(&self) -> &Self {
self.assert_no_errors().assert_unchanged()
}
pub fn assert_no_errors(&self) -> &Self {
let compiled = self.compile().unwrap();
compiled.assert_success();
self
}
pub fn assert_unchanged(&self) -> &Self {
let compiled = self.compile().unwrap();
assert!(compiled.is_unchanged());
self
}
pub fn assert_changed(&self) -> &Self {
let compiled = self.compile().unwrap();
assert!(!compiled.is_unchanged());
self
}
pub fn list_source_files(&self) -> Vec<PathBuf> {
utils::source_files(self.project().sources_path())
}
}
impl<T: ArtifactOutput + Default> TempProject<T> {
pub fn prefixed(prefix: &str, paths: ProjectPathsConfigBuilder) -> Result<Self> {
Self::prefixed_with_artifacts(prefix, paths, T::default())
}
pub fn with_style(prefix: &str, style: PathStyle) -> Result<Self> {
let tmp_dir = tempdir(prefix)?;
let paths = style.paths(tmp_dir.path())?;
let inner = Project::builder().artifacts(T::default()).paths(paths).build()?;
Ok(Self::create_new(tmp_dir, inner)?)
}
pub fn new(paths: ProjectPathsConfigBuilder) -> Result<Self> {
Self::prefixed("temp-project", paths)
}
}
impl<T: ArtifactOutput> fmt::Debug for TempProject<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TempProject").field("paths", self.paths()).finish()
}
}
pub(crate) fn create_contract_file(path: PathBuf, content: impl AsRef<str>) -> Result<PathBuf> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.map_err(|err| SolcIoError::new(err, parent.to_path_buf()))?;
}
std::fs::write(&path, content.as_ref()).map_err(|err| SolcIoError::new(err, path.clone()))?;
Ok(path)
}
fn contract_file_name(name: impl AsRef<str>) -> String {
let name = name.as_ref().trim();
if name.ends_with(".sol") {
name.to_string()
} else {
format!("{name}.sol")
}
}
impl TempProject<HardhatArtifacts> {
pub fn hardhat() -> Result<Self> {
let tmp_dir = tempdir("tmp_hh")?;
let paths = ProjectPathsConfig::hardhat(tmp_dir.path())?;
let inner =
Project::builder().artifacts(HardhatArtifacts::default()).paths(paths).build()?;
Ok(Self::create_new(tmp_dir, inner)?)
}
}
impl TempProject<ConfigurableArtifacts> {
pub fn dapptools() -> Result<Self> {
let tmp_dir = tempdir("tmp_dapp")?;
let paths = ProjectPathsConfig::dapptools(tmp_dir.path())?;
let inner = Project::builder().paths(paths).build()?;
Ok(Self::create_new(tmp_dir, inner)?)
}
pub fn dapptools_init() -> Result<Self> {
let mut project = Self::dapptools()?;
let orig_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/dapp-sample");
copy_dir(orig_root, project.root())?;
project.project_mut().paths.remappings = Remapping::find_many(project.root());
Ok(project)
}
pub fn checkout(repo: impl AsRef<str>) -> Result<Self> {
let tmp_dir = tempdir("tmp_checkout")?;
clone_remote(&format!("https://github.com/{}", repo.as_ref()), tmp_dir.path())
.map_err(|err| SolcIoError::new(err, tmp_dir.path()))?;
let paths = ProjectPathsConfig::dapptools(tmp_dir.path())?;
let inner = Project::builder().paths(paths).build()?;
Ok(Self::create_new(tmp_dir, inner)?)
}
pub fn mocked(settings: &MockProjectSettings, version: impl AsRef<str>) -> Result<Self> {
let mut tmp = Self::dapptools()?;
let gen = MockProjectGenerator::new(settings);
tmp.mock(&gen, version)?;
let remappings = gen.remappings_at(tmp.root());
tmp.paths_mut().remappings.extend(remappings);
Ok(tmp)
}
pub fn mocked_random(version: impl AsRef<str>) -> Result<Self> {
Self::mocked(&MockProjectSettings::random(), version)
}
}
impl<T: ArtifactOutput> AsRef<Project<T>> for TempProject<T> {
fn as_ref(&self) -> &Project<T> {
self.project()
}
}
#[derive(Debug, Clone)]
pub struct ArtifactsSnapshot<T> {
pub cache: SolFilesCache,
pub artifacts: Artifacts<T>,
}
impl ArtifactsSnapshot<ConfigurableContractArtifact> {
pub fn assert_artifacts_essentials_present(&self) {
for artifact in self.artifacts.artifact_files() {
let c = artifact.artifact.clone().into_compact_contract();
assert!(c.abi.is_some());
assert!(c.bin.is_some());
assert!(c.bin_runtime.is_some());
}
}
}
fn dir_copy_options() -> dir::CopyOptions {
dir::CopyOptions {
overwrite: true,
skip_exist: false,
buffer_size: 64000, copy_inside: true,
content_only: true,
depth: 0,
}
}
fn file_copy_options() -> file::CopyOptions {
file::CopyOptions {
overwrite: true,
skip_exist: false,
buffer_size: 64000, }
}
pub fn copy_file(source: impl AsRef<Path>, target_dir: impl AsRef<Path>) -> Result<()> {
let source = source.as_ref();
let target = target_dir.as_ref().join(
source
.file_name()
.ok_or_else(|| SolcError::msg(format!("No file name for {}", source.display())))?,
);
fs_extra::file::copy(source, target, &file_copy_options())?;
Ok(())
}
pub fn copy_dir(source: impl AsRef<Path>, target_dir: impl AsRef<Path>) -> Result<()> {
fs_extra::dir::copy(source, target_dir, &dir_copy_options())?;
Ok(())
}
pub fn clone_remote(
repo_url: &str,
target_dir: impl AsRef<Path>,
) -> std::io::Result<process::Output> {
Command::new("git")
.args(["clone", "--depth", "1", "--recursive", repo_url])
.arg(target_dir.as_ref())
.output()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_mock_project() {
let _prj = TempProject::mocked(&Default::default(), "^0.8.11").unwrap();
let _prj = TempProject::mocked_random("^0.8.11").unwrap();
}
}