Skip to content

Commit

Permalink
cover more output options
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgar2017 committed Dec 17, 2024
1 parent b6f884e commit edb9033
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 20 deletions.
13 changes: 5 additions & 8 deletions src/build/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,11 @@ impl Contract {
"Refusing to overwrite an existing file {binary_file_path:?} (use --overwrite to force).",
);
}
File::create(&binary_file_path)
.map_err(|error| {
anyhow::anyhow!("File {:?} creating error: {}", binary_file_path, error)
})?
.write_all(format!("0x{}", hex::encode(self.build.bytecode.as_slice())).as_bytes())
.map_err(|error| {
anyhow::anyhow!("File {:?} writing error: {}", binary_file_path, error)
})?;
std::fs::write(
&binary_file_path,
format!("0x{}", hex::encode(self.build.bytecode.as_slice())).as_bytes(),
)
.map_err(|error| anyhow::anyhow!("File {binary_file_path:?} writing error: {error}"))?;

if selection.is_empty() {
return Ok(());
Expand Down
8 changes: 0 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ pub fn llvm_ir(
suppressed_warnings: Vec<WarningType>,
debug_config: Option<era_compiler_llvm_context::DebugConfig>,
) -> anyhow::Result<Build> {
if input_paths.is_empty() {
writeln!(std::io::stderr(), "No input sources provided").expect("Stderr writing error");
}

let paths: Vec<&Path> = input_paths.iter().map(|path| path.as_path()).collect();
let project = Project::try_from_llvm_ir_paths(paths.as_slice(), output_selection)?;

Expand All @@ -90,10 +86,6 @@ pub fn eravm_assembly(
suppressed_warnings: Vec<WarningType>,
debug_config: Option<era_compiler_llvm_context::DebugConfig>,
) -> anyhow::Result<Build> {
if input_paths.is_empty() {
writeln!(std::io::stderr(), "No input sources provided").expect("Stderr writing error");
}

let paths: Vec<&Path> = input_paths.iter().map(|path| path.as_path()).collect();
let project = Project::try_from_eravm_assembly_paths(paths.as_slice(), output_selection)?;

Expand Down
42 changes: 42 additions & 0 deletions tests/cli/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,48 @@ fn default() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn with_proxy() -> anyhow::Result<()> {
let _ = common::setup();

let args = &[common::TEST_CREATE_MINIMAL_PROXY_TO_CONTRACT_PATH];

let result = common::execute_zkvyper(args)?;
let zkvyper_status = result
.success()
.stdout(predicate::str::contains("0x"))
.get_output()
.status
.code()
.expect("No exit code.");

let vyper_result = common::execute_vyper(args)?;
vyper_result.code(zkvyper_status);

Ok(())
}

#[test]
fn with_warnings() -> anyhow::Result<()> {
let _ = common::setup();

let args = &[common::TEST_TX_ORIGIN_CONTRACT_PATH];

let result = common::execute_zkvyper(args)?;
let zkvyper_status = result
.success()
.stdout(predicate::str::contains("0x"))
.get_output()
.status
.code()
.expect("No exit code.");

let vyper_result = common::execute_vyper(args)?;
vyper_result.code(zkvyper_status);

Ok(())
}

#[test]
fn without_input_files() -> anyhow::Result<()> {
let _ = common::setup();
Expand Down
34 changes: 32 additions & 2 deletions tests/cli/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use predicates::prelude::*;
use test_case::test_case;

use era_compiler_vyper::VyperSelection;

use crate::common;

#[test_case("combined_json")]
Expand All @@ -13,10 +15,38 @@ use crate::common;
#[test_case("devdoc")]
#[test_case("eravm_assembly")]
#[test_case("project_metadata")]
fn default(selector: &str) -> anyhow::Result<()> {
fn default(format: &str) -> anyhow::Result<()> {
let _ = common::setup();

let args = &[common::TEST_GREETER_CONTRACT_PATH, "-f", format];

let result = common::execute_zkvyper(args)?;
result.success();

Ok(())
}

#[test]
fn all() -> anyhow::Result<()> {
let _ = common::setup();

let args = &[common::TEST_GREETER_CONTRACT_PATH, "-f", selector];
let format = [
VyperSelection::IRJson,
VyperSelection::AST,
VyperSelection::ABI,
VyperSelection::MethodIdentifiers,
VyperSelection::Layout,
VyperSelection::UserDocumentation,
VyperSelection::DeveloperDocumentation,
VyperSelection::EraVMAssembly,
VyperSelection::ProjectMetadata,
]
.into_iter()
.map(|selection| selection.to_string())
.collect::<Vec<String>>()
.join(",");

let args = &[common::TEST_GREETER_CONTRACT_PATH, "-f", format.as_str()];

let result = common::execute_zkvyper(args)?;
result.success();
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod output_dir;
mod overwrite;
mod recursive_process;
mod search_paths;
mod suppress;
mod suppress_warnings;
mod threads;
mod version;
mod vyper;
71 changes: 70 additions & 1 deletion tests/cli/output_dir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use predicates::prelude::*;
use tempfile::TempDir;

use era_compiler_vyper::VyperSelection;

use crate::common;

#[test]
Expand Down Expand Up @@ -68,7 +70,7 @@ fn combined_json() -> anyhow::Result<()> {
}

#[test]
fn with_eravm_assembly() -> anyhow::Result<()> {
fn eravm_assembly_only() -> anyhow::Result<()> {
let _ = common::setup();

let tmp_dir_zk_vyper = TempDir::new().expect("Failed to create temp dir");
Expand Down Expand Up @@ -110,3 +112,70 @@ fn with_eravm_assembly() -> anyhow::Result<()> {

Ok(())
}

#[test]
fn all_output() -> anyhow::Result<()> {
let _ = common::setup();

let tmp_dir_zk_vyper = TempDir::new().expect("Failed to create temp dir");
let tmp_dir_path_zk_vyper = tmp_dir_zk_vyper.path().to_str().unwrap();

let format = [
VyperSelection::IRJson,
VyperSelection::AST,
VyperSelection::ABI,
VyperSelection::MethodIdentifiers,
VyperSelection::Layout,
VyperSelection::UserDocumentation,
VyperSelection::DeveloperDocumentation,
VyperSelection::EraVMAssembly,
VyperSelection::ProjectMetadata,
]
.into_iter()
.map(|selection| selection.to_string())
.collect::<Vec<String>>()
.join(",");

// Check if output is empty and exit code
let args = &[
common::TEST_GREETER_CONTRACT_PATH,
"-o",
tmp_dir_path_zk_vyper,
"-f",
format.as_str(),
];

let result = common::execute_zkvyper(args)?;
result
.success()
.stderr(predicate::str::contains("Refusing to overwrite").not())
.get_output()
.status
.code()
.expect("No exit code.");

// Verify output directory and file creation
assert_eq!(
false,
common::is_file_empty(&format!(
"{tmp_dir_path_zk_vyper}/{}",
common::VYPER_BIN_OUTPUT_NAME
))?
);
assert_eq!(
false,
common::is_file_empty(&format!(
"{tmp_dir_path_zk_vyper}/{}",
common::VYPER_ASM_OUTPUT_NAME
))?
);
assert_eq!(
false,
common::is_file_empty(&format!(
"{tmp_dir_path_zk_vyper}/{}",
common::TEST_GREETER_CONTRACT_NAME
))?
);

Ok(())
}
18 changes: 18 additions & 0 deletions tests/cli/suppress.rs → tests/cli/suppress_warnings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,21 @@ fn default(warning_type: WarningType) -> anyhow::Result<()> {

Ok(())
}

#[test]
fn invalid_type() -> anyhow::Result<()> {
let _ = common::setup();

let args = &[
common::TEST_TX_ORIGIN_CONTRACT_PATH,
"--suppress-warnings",
"unknown",
];

let result = common::execute_zkvyper(args)?;
result
.failure()
.stderr(predicate::str::contains("invalid warning type: unknown"));

Ok(())
}
4 changes: 4 additions & 0 deletions tests/common/const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub const TEST_SELFDESTRUCT_CONTRACT_PATH: &'static str =
pub const TEST_CREATE_COPY_OF_CONTRACT_PATH: &'static str =
"tests/data/contracts/vyper/create_copy_of.vy";

/// A test input file.
pub const TEST_CREATE_MINIMAL_PROXY_TO_CONTRACT_PATH: &'static str =
"tests/data/contracts/vyper/create_minimal_proxy_to.vy";

/// A test input file.
pub const TEST_ERAVM_ASSEMBLY_CONTRACT_PATH: &'static str =
"tests/data/contracts/eravm/default.zasm";
Expand Down
4 changes: 4 additions & 0 deletions tests/data/contracts/vyper/create_minimal_proxy_to.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@external
def f():
result: address = create_minimal_proxy_to(convert(0x42, address))
return

0 comments on commit edb9033

Please sign in to comment.