Skip to content

Commit

Permalink
feat: add tests for the EVM target
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgar2017 committed Nov 7, 2024
1 parent 3ce6099 commit b0c824d
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 72 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
/.vscode/

# Test artifacts
**/cli-tests/node_modules
**/cli-tests/artifacts
*.xml
/**/tests/data/temp/

# Accidentally created backup files
*.bak
Expand Down
62 changes: 35 additions & 27 deletions era-compiler-solidity/src/build_evm/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ impl Contract {
///
/// Writes the contract text assembly and bytecode to files.
///
/// TODO: output assembly
///
pub fn write_to_directory(
self,
output_path: &Path,
_output_assembly: bool,
output_assembly: bool,
output_binary: bool,
overwrite: bool,
) -> anyhow::Result<()> {
Expand All @@ -98,33 +96,43 @@ impl Contract {
output_path.push(file_name);
std::fs::create_dir_all(output_path.as_path())?;

if output_assembly {
let output_name = format!(
"{}.{}",
self.name.name.as_deref().unwrap_or(file_name),
"asm"
);
let mut output_path = output_path.clone();
output_path.push(output_name.as_str());

if output_path.exists() && !overwrite {
anyhow::bail!(
"Refusing to overwrite an existing file {output_path:?} (use --overwrite to force)."
);
} else {
std::fs::write(output_path.as_path(), [])
.map_err(|error| anyhow::anyhow!("File {output_path:?} writing: {error}"))?;
}
}

if output_binary {
for (code_segment, bytecode) in [
era_compiler_common::CodeSegment::Deploy,
era_compiler_common::CodeSegment::Runtime,
]
.into_iter()
.zip([self.deploy_build, self.runtime_build].into_iter())
{
let output_name = format!(
"{}.{code_segment}.{}",
self.name.name.as_deref().unwrap_or(file_name),
era_compiler_common::EXTENSION_EVM_BINARY
let output_name = format!(
"{}.{}",
self.name.name.as_deref().unwrap_or(file_name),
era_compiler_common::EXTENSION_EVM_BINARY
);
let mut output_path = output_path.clone();
output_path.push(output_name.as_str());

if output_path.exists() && !overwrite {
anyhow::bail!(
"Refusing to overwrite an existing file {output_path:?} (use --overwrite to force)."
);
let mut output_path = output_path.clone();
output_path.push(output_name.as_str());

if output_path.exists() && !overwrite {
anyhow::bail!(
"Refusing to overwrite an existing file {output_path:?} (use --overwrite to force)."
);
} else {
std::fs::write(
output_path.as_path(),
hex::encode(bytecode.as_slice()).as_bytes(),
)
} else {
let mut bytecode_hexadecimal = hex::encode(self.deploy_build.as_slice());
bytecode_hexadecimal.push_str(hex::encode(self.runtime_build.as_slice()).as_str());
std::fs::write(output_path.as_path(), bytecode_hexadecimal.as_bytes())
.map_err(|error| anyhow::anyhow!("File {output_path:?} writing: {error}"))?;
}
}
}

Expand Down
30 changes: 18 additions & 12 deletions era-compiler-solidity/tests/cli/allow_paths.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::{cli, common};
use era_compiler_common::Target;
use predicates::prelude::*;
use test_case::test_case;

#[test]
fn with_allow_paths() -> anyhow::Result<()> {
#[test_case(Target::EraVM)]
#[test_case(Target::EVM)]
fn with_allow_paths(target: Target) -> anyhow::Result<()> {
common::setup()?;

let args = &[
Expand All @@ -12,16 +15,17 @@ fn with_allow_paths() -> anyhow::Result<()> {
cli::TEST_SOLIDITY_CONTRACT_PATH,
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result
.success()
.stdout(predicate::str::contains("Binary:\n"));

Ok(())
}

#[test]
fn with_allow_paths_yul_mode() -> anyhow::Result<()> {
#[test_case(Target::EraVM)]
#[test_case(Target::EVM)]
fn with_allow_paths_yul_mode(target: Target) -> anyhow::Result<()> {
common::setup()?;

let args = &[
Expand All @@ -32,16 +36,17 @@ fn with_allow_paths_yul_mode() -> anyhow::Result<()> {
cli::TEST_YUL_CONTRACT_PATH,
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result.failure().stderr(predicate::str::contains(
"`allow-paths` is only allowed in Solidity mode",
));

Ok(())
}

#[test]
fn with_allow_paths_llvm_ir_mode() -> anyhow::Result<()> {
#[test_case(Target::EraVM)]
#[test_case(Target::EVM)]
fn with_allow_paths_llvm_ir_mode(target: Target) -> anyhow::Result<()> {
common::setup()?;

let args = &[
Expand All @@ -52,16 +57,17 @@ fn with_allow_paths_llvm_ir_mode() -> anyhow::Result<()> {
cli::TEST_LLVM_IR_CONTRACT_PATH,
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result.failure().stderr(predicate::str::contains(
"`allow-paths` is only allowed in Solidity mode",
));

Ok(())
}

#[test]
fn with_allow_paths_eravm_assembly_mode() -> anyhow::Result<()> {
#[test_case(Target::EraVM)]
#[test_case(Target::EVM)]
fn with_allow_paths_eravm_assembly_mode(target: Target) -> anyhow::Result<()> {
common::setup()?;

let args = &[
Expand All @@ -72,7 +78,7 @@ fn with_allow_paths_eravm_assembly_mode() -> anyhow::Result<()> {
cli::TEST_ERAVM_ASSEMBLY_CONTRACT_PATH,
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result.failure().stderr(predicate::str::contains(
"`allow-paths` is only allowed in Solidity mode",
));
Expand Down
30 changes: 18 additions & 12 deletions era-compiler-solidity/tests/cli/base_path.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::{cli, common};
use era_compiler_common::Target;
use predicates::prelude::*;
use test_case::test_case;

#[test]
fn with_base_path() -> anyhow::Result<()> {
#[test_case(Target::EraVM)]
#[test_case(Target::EVM)]
fn with_base_path(target: Target) -> anyhow::Result<()> {
common::setup()?;

let args = &[
Expand All @@ -12,16 +15,17 @@ fn with_base_path() -> anyhow::Result<()> {
cli::TEST_SOLIDITY_CONTRACT_PATH,
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result
.success()
.stdout(predicate::str::contains("Binary:\n"));

Ok(())
}

#[test]
fn with_base_path_yul_mode() -> anyhow::Result<()> {
#[test_case(Target::EraVM)]
#[test_case(Target::EVM)]
fn with_base_path_yul_mode(target: Target) -> anyhow::Result<()> {
common::setup()?;

let args = &[
Expand All @@ -32,16 +36,17 @@ fn with_base_path_yul_mode() -> anyhow::Result<()> {
cli::TEST_YUL_CONTRACT_PATH,
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result.failure().stderr(predicate::str::contains(
"`base-path` is only allowed in Solidity mode",
));

Ok(())
}

#[test]
fn with_base_path_llvm_ir_mode() -> anyhow::Result<()> {
#[test_case(Target::EraVM)]
#[test_case(Target::EVM)]
fn with_base_path_llvm_ir_mode(target: Target) -> anyhow::Result<()> {
common::setup()?;

let args = &[
Expand All @@ -52,16 +57,17 @@ fn with_base_path_llvm_ir_mode() -> anyhow::Result<()> {
cli::TEST_LLVM_IR_CONTRACT_PATH,
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result.failure().stderr(predicate::str::contains(
"`base-path` is only allowed in Solidity mode",
));

Ok(())
}

#[test]
fn with_base_path_eravm_assembly_mode() -> anyhow::Result<()> {
#[test_case(Target::EraVM)]
#[test_case(Target::EVM)]
fn with_base_path_eravm_assembly_mode(target: Target) -> anyhow::Result<()> {
common::setup()?;

let args = &[
Expand All @@ -72,7 +78,7 @@ fn with_base_path_eravm_assembly_mode() -> anyhow::Result<()> {
cli::TEST_ERAVM_ASSEMBLY_CONTRACT_PATH,
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result.failure().stderr(predicate::str::contains(
"`base-path` is only allowed in Solidity mode",
));
Expand Down
50 changes: 36 additions & 14 deletions era-compiler-solidity/tests/cli/basic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::{cli, common};
use era_compiler_common::Target;
use predicates::prelude::*;
use tempfile::TempDir;
use test_case::test_case;

#[test]
fn without_any_args() -> anyhow::Result<()> {
common::setup()?;

let args: &[&str] = &[];

let result = cli::execute_zksolc(args)?;
Expand All @@ -24,9 +27,11 @@ fn without_any_args() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn with_args_from_help_example() -> anyhow::Result<()> {
#[test_case(Target::EraVM, cli::SOLIDITY_BIN_OUTPUT_NAME_ERAVM)]
#[test_case(Target::EVM, cli::SOLIDITY_BIN_OUTPUT_NAME_EVM)]
fn with_args_from_help_example(target: Target, bin_output_file_name: &str) -> anyhow::Result<()> {
common::setup()?;

let tmp_dir = TempDir::new()?;
let args = &[
cli::TEST_SOLIDITY_CONTRACT_PATH,
Expand All @@ -36,7 +41,7 @@ fn with_args_from_help_example() -> anyhow::Result<()> {
tmp_dir.path().to_str().unwrap(),
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result
.success()
.stderr(predicate::str::contains("Compiler run successful."));
Expand All @@ -46,16 +51,29 @@ fn with_args_from_help_example() -> anyhow::Result<()> {
let bin_output_file = tmp_dir
.path()
.join(cli::TEST_SOLIDITY_CONTRACT_NAME)
.join(cli::SOLIDITY_BIN_OUTPUT_NAME);
.join(bin_output_file_name);

assert!(bin_output_file.exists());
assert!(!cli::is_file_empty(bin_output_file.to_str().unwrap())?);

Ok(())
}

#[test]
fn with_multiple_output_options() -> anyhow::Result<()> {
#[test_case(
Target::EraVM,
cli::SOLIDITY_BIN_OUTPUT_NAME_ERAVM,
cli::SOLIDITY_ASM_OUTPUT_NAME_ERAVM
)]
// #[test_case(
// Target::EVM,
// cli::SOLIDITY_BIN_OUTPUT_NAME_EVM,
// cli::SOLIDITY_ASM_OUTPUT_NAME_EVM
// )] TODO: assembly
fn with_multiple_output_options(
target: Target,
bin_output_file_name: &str,
asm_output_file_name: &str,
) -> anyhow::Result<()> {
common::setup()?;
let tmp_dir = TempDir::new()?;
let args = &[
Expand All @@ -67,7 +85,7 @@ fn with_multiple_output_options() -> anyhow::Result<()> {
tmp_dir.path().to_str().unwrap(),
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result
.success()
.stderr(predicate::str::contains("Compiler run successful."));
Expand All @@ -77,11 +95,11 @@ fn with_multiple_output_options() -> anyhow::Result<()> {
let bin_output_file = tmp_dir
.path()
.join(cli::TEST_SOLIDITY_CONTRACT_NAME)
.join(cli::SOLIDITY_BIN_OUTPUT_NAME);
.join(bin_output_file_name);
let asm_output_file = tmp_dir
.path()
.join(cli::TEST_SOLIDITY_CONTRACT_NAME)
.join(cli::SOLIDITY_ASM_OUTPUT_NAME);
.join(asm_output_file_name);

assert!(bin_output_file.exists());
assert!(asm_output_file.exists());
Expand All @@ -91,8 +109,12 @@ fn with_multiple_output_options() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn with_bin_output_same_file_and_cli() -> anyhow::Result<()> {
#[test_case(Target::EraVM, cli::SOLIDITY_BIN_OUTPUT_NAME_ERAVM)]
#[test_case(Target::EVM, cli::SOLIDITY_BIN_OUTPUT_NAME_EVM)]
fn with_bin_output_same_file_and_cli(
target: Target,
bin_output_file_name: &str,
) -> anyhow::Result<()> {
common::setup()?;

let tmp_dir = TempDir::new()?;
Expand All @@ -104,19 +126,19 @@ fn with_bin_output_same_file_and_cli() -> anyhow::Result<()> {
tmp_dir.path().to_str().unwrap(),
];

let result = cli::execute_zksolc(args)?;
let result = cli::execute_zksolc_with_target(args, target)?;
result
.success()
.stderr(predicate::str::contains("Compiler run successful."));

let bin_output_file = tmp_dir
.path()
.join(cli::TEST_SOLIDITY_CONTRACT_NAME)
.join(cli::SOLIDITY_BIN_OUTPUT_NAME);
.join(bin_output_file_name);
assert!(bin_output_file.exists());

let cli_args = &[cli::TEST_SOLIDITY_CONTRACT_PATH, "-O3", "--bin"];
let cli_result = cli::execute_zksolc(cli_args)?;
let cli_result = cli::execute_zksolc_with_target(cli_args, target)?;

let stdout = String::from_utf8_lossy(cli_result.get_output().stdout.as_slice());

Expand Down
Loading

0 comments on commit b0c824d

Please sign in to comment.