Skip to content

Commit

Permalink
Add e2e tests (#3)
Browse files Browse the repository at this point in the history
* add e2e tests

* move fs_extra to dev-dependencies

* check writing to existing file

* review suggestions

* remove stdout eq

* remove duplicated ci test
  • Loading branch information
war-in authored Dec 22, 2023
1 parent 6f4f50c commit c3804c5
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 31 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jobs:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@3cf7f8cc28d1b4e7d01e3783be10a97d55d483c8
- run: cargo test --release --lib
- run: cargo test --release

rustfmt:
Expand Down
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ clap = "4.4.11"
anyhow = "1.0.75"
console = "0.15.7"

[dev-dependencies]
snapbox = "0.4.15"
tempfile = "3.8.1"
indoc = "2.0.4"
fs_extra = "1.3.0"

[[bin]]
name = "universal-sierra-compiler"
path = "src/main.rs"
30 changes: 0 additions & 30 deletions tests/compilation.rs

This file was deleted.

1 change: 1 addition & 0 deletions tests/data/wrong_sierra.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"sierra_program": "wrong_program_data"}
101 changes: 101 additions & 0 deletions tests/e2e.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use indoc::indoc;
use snapbox::cmd::{cargo_bin, Command};
use std::fs::File;
use std::path::PathBuf;
use tempfile::TempDir;
use test_case::test_case;

#[must_use]
fn runner(args: Vec<&str>, temp_dir: &TempDir) -> Command {
Command::new(cargo_bin!("universal-sierra-compiler"))
.current_dir(temp_dir.path())
.args(args)
}

#[must_use]
fn temp_dir_with_sierra_file(file_name: &str) -> TempDir {
let temp_dir = TempDir::new().expect("Unable to create a temporary directory");

let src_dir = PathBuf::from("tests/data");

fs_extra::file::copy(
src_dir.join(file_name),
temp_dir.path().join(file_name),
&fs_extra::file::CopyOptions::new().overwrite(true),
)
.unwrap_or_else(|_| panic!("Unable to copy {file_name}"));

temp_dir
}

fn verify_output_file(output_path: PathBuf) {
let file = File::open(output_path).unwrap();
let casm_json = serde_json::from_reader(file).unwrap();

assert!(serde_json::from_value::<CasmContractClass>(casm_json).is_ok());
}

#[test]
fn write_to_existing_file() {
let sierra_file_name = "sierra_1_4_0.json";
let casm_file_name = "casm.json";
let args = vec![
"--sierra-input-path",
&sierra_file_name,
"--casm-output-path",
casm_file_name,
];

let temp_dir = temp_dir_with_sierra_file(sierra_file_name);
let _ = File::create(temp_dir.path().join(casm_file_name)).expect("Unable to create file");

let snapbox = runner(args, &temp_dir);

snapbox.assert().success();

verify_output_file(temp_dir.path().join(casm_file_name));
}

#[test]
fn wrong_json() {
let sierra_file_name = "wrong_sierra.json";
let casm_file_name = "casm.json";
let args = vec![
"--sierra-input-path",
&sierra_file_name,
"--casm-output-path",
casm_file_name,
];

let temp_dir = temp_dir_with_sierra_file(sierra_file_name);
let snapbox = runner(args, &temp_dir);

snapbox.assert().failure().stdout_eq(indoc! {r"
[ERROR] Unable to read sierra_program. Make sure it is an array of felts
"});
}

#[test_case("1_4_0"; "sierra 1.4.0")]
#[test_case("1_3_0"; "sierra 1.3.0")]
#[test_case("1_2_0"; "sierra 1.2.0")]
#[test_case("1_1_0"; "sierra 1.1.0")]
#[test_case("1_0_0"; "sierra 1.0.0")]
#[test_case("0_1_0"; "sierra 0.1.0")]
fn test_happy_case(sierra_version: &str) {
let sierra_file_name = "sierra_".to_string() + sierra_version + ".json";
let casm_file_name = "casm.json";
let args = vec![
"--sierra-input-path",
&sierra_file_name,
"--casm-output-path",
casm_file_name,
];

let temp_dir = temp_dir_with_sierra_file(&sierra_file_name);
let snapbox = runner(args, &temp_dir);

snapbox.assert().success();

verify_output_file(temp_dir.path().join(casm_file_name));
}
27 changes: 27 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::fs::File;
use test_case::test_case;
use universal_sierra_compiler::compile;

#[test]
fn wrong_json() {
let sierra_json = serde_json::json!({
"wrong": "data"
});

let casm_class = compile(sierra_json);
assert!(casm_class.is_err());
}

#[test_case("1_4_0"; "sierra 1.4.0")]
#[test_case("1_3_0"; "sierra 1.3.0")]
#[test_case("1_2_0"; "sierra 1.2.0")]
#[test_case("1_1_0"; "sierra 1.1.0")]
#[test_case("1_0_0"; "sierra 1.0.0")]
#[test_case("0_1_0"; "sierra 0.1.0")]
fn compile_sierra(sierra_version: &str) {
let file = File::open("tests/data/sierra_".to_string() + sierra_version + ".json").unwrap();
let sierra_json = serde_json::from_reader(file).unwrap();

let casm_class = compile(sierra_json);
assert!(casm_class.is_ok());
}

0 comments on commit c3804c5

Please sign in to comment.