use std::{
fs::File,
path::PathBuf,
process::{Command, Stdio},
};
use crate::SolcCompiler;
pub const SOLIDITY_CONTRACT_PATH: &str = "src/tests/data/solidity/contract.sol";
pub const SOLIDITY_DEPENDENCY_CONTRACT_PATH: &str = "src/tests/data/solidity/dependency.sol";
pub const SOLIDITY_LARGE_DIV_REM_CONTRACT_PATH: &str = "src/tests/data/solidity/large_div_rem.sol";
pub const YUL_CONTRACT_PATH: &str = "src/tests/data/yul/contract.yul";
pub const YUL_MEMSET_CONTRACT_PATH: &str = "src/tests/data/yul/memset.yul";
pub const YUL_RETURN_CONTRACT_PATH: &str = "src/tests/data/yul/return.yul";
pub const YUL_INVALID_HEX_NIBBLES_PATH: &str = "src/tests/data/yul/invalid_hex_nibbles.yul";
pub const YUL_DUPLICATE_FUNCTIONS_SWITCH_PATH: &str =
"src/tests/data/yul/duplicate_functions_switch.yul";
pub const YUL_DUPLICATE_FUNCTIONS_DEEP_NESTING_PATH: &str =
"src/tests/data/yul/duplicate_functions_deep_nesting.yul";
pub const STANDARD_JSON_CONTRACTS_PATH: &str =
"src/tests/data/standard_json/solidity_contracts.json";
pub const STANDARD_JSON_ALL_OUTPUTS_PATH: &str = "src/tests/data/standard_json/all_outputs.json";
pub const STANDARD_JSON_NO_EVM_CODEGEN_PATH: &str =
"src/tests/data/standard_json/no_evm_codegen.json";
pub const STANDARD_JSON_NO_EVM_CODEGEN_COMPLEX_PATH: &str =
"src/tests/data/standard_json/no_evm_codegen_complex.json";
pub const STANDARD_JSON_PVM_CODEGEN_ALL_WILDCARD_PATH: &str =
"src/tests/data/standard_json/pvm_codegen_all_wildcard.json";
pub const STANDARD_JSON_PVM_CODEGEN_PER_FILE_PATH: &str =
"src/tests/data/standard_json/pvm_codegen_per_file.json";
pub const STANDARD_JSON_PVM_CODEGEN_ONE_FILE_PATH: &str =
"src/tests/data/standard_json/pvm_codegen_one_file.json";
pub const STANDARD_JSON_NO_PVM_CODEGEN_PER_FILE_PATH: &str =
"src/tests/data/standard_json/no_pvm_codegen_per_file.json";
pub const STANDARD_JSON_YUL_PVM_CODEGEN_PATH: &str =
"src/tests/data/standard_json/yul_pvm_codegen.json";
pub const STANDARD_JSON_YUL_NO_PVM_CODEGEN_PATH: &str =
"src/tests/data/standard_json/yul_no_pvm_codegen.json";
pub const RESOLC_YUL_FLAG: &str = "--yul";
pub const SOLC_YUL_FLAG: &str = "--strict-assembly";
pub struct ResolcOptSettings;
impl ResolcOptSettings {
pub const NONE: &'static str = "-O0";
pub const PERFORMANCE: &'static str = "-O3";
pub const SIZE: &'static str = "-Oz";
}
pub struct SolcOptSettings;
impl SolcOptSettings {
pub const NONE: &'static str = "0";
pub const PERFORMANCE: &'static str = "20000";
pub const SIZE: &'static str = "1";
}
pub struct CommandResult {
pub stdout: String,
pub stderr: String,
pub success: bool,
pub code: i32,
}
pub fn execute_resolc(arguments: &[&str]) -> CommandResult {
execute_command("resolc", arguments, None)
}
pub fn execute_resolc_with_stdin_input(arguments: &[&str], stdin_file_path: &str) -> CommandResult {
execute_command("resolc", arguments, Some(stdin_file_path))
}
pub fn execute_solc(arguments: &[&str]) -> CommandResult {
execute_command(SolcCompiler::DEFAULT_EXECUTABLE_NAME, arguments, None)
}
pub fn execute_solc_with_stdin_input(arguments: &[&str], stdin_file_path: &str) -> CommandResult {
execute_command(
SolcCompiler::DEFAULT_EXECUTABLE_NAME,
arguments,
Some(stdin_file_path),
)
}
pub fn execute_command(
command: &str,
arguments: &[&str],
stdin_file_path: Option<&str>,
) -> CommandResult {
log::trace!(
"executing command: '{command} {}{}'",
arguments.join(" "),
stdin_file_path
.map(|argument| format!("< {argument}"))
.unwrap_or_default()
);
let stdin_config = match stdin_file_path {
Some(path) => Stdio::from(File::open(path).unwrap()),
None => Stdio::null(),
};
let result = Command::new(command)
.args(arguments)
.stdin(stdin_config)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.unwrap();
CommandResult {
stdout: String::from_utf8_lossy(&result.stdout).to_string(),
stderr: String::from_utf8_lossy(&result.stderr).to_string(),
success: result.status.success(),
code: result.status.code().unwrap(),
}
}
pub fn assert_equal_exit_codes(solc_result: &CommandResult, resolc_result: &CommandResult) {
assert_eq!(solc_result.code, resolc_result.code,);
}
pub fn assert_command_success(result: &CommandResult, error_message_prefix: &str) {
assert!(
result.success,
"{error_message_prefix} should succeed with exit code {}, got {}.\nDetails: {}",
revive_common::EXIT_CODE_SUCCESS,
result.code,
result.stderr
);
}
pub fn assert_command_failure(result: &CommandResult, error_message_prefix: &str) {
assert!(
!result.success,
"{error_message_prefix} should fail with exit code {}, got {}.",
revive_common::EXIT_CODE_FAILURE,
result.code
);
}
pub fn absolute_path(relative_path: &str) -> String {
let absolute_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(relative_path);
if !absolute_path.is_file() {
panic!("expected a file at `{}`", absolute_path.display());
}
absolute_path.to_string_lossy().into_owned()
}