Skip to content

Commit

Permalink
Write to output when no casm_output_path provided (#4)
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

* write to output when casm_output_path not present

* fmt

* use File::create
  • Loading branch information
war-in authored Dec 22, 2023
1 parent c3804c5 commit 9fd2fb3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ cairo-lang-starknet-sierra-1_0_0 = { package = "cairo-lang-starknet", git = "htt
cairo-lang-starknet = "=2.4.0"
serde_json = "1.0.108"
serde = "1.0.193"
test-case = "3.3.1"
clap = "4.4.11"
anyhow = "1.0.75"
console = "0.15.7"
Expand All @@ -20,6 +19,7 @@ snapbox = "0.4.15"
tempfile = "3.8.1"
indoc = "2.0.4"
fs_extra = "1.3.0"
test-case = "3.3.1"

[[bin]]
name = "universal-sierra-compiler"
Expand Down
21 changes: 12 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{Context, Error, Result};
use clap::Parser;
use console::style;
use serde_json::to_writer;
use std::fs::{File, OpenOptions};
use std::fs::File;
use std::path::PathBuf;
use universal_sierra_compiler::compile;

Expand All @@ -15,7 +15,7 @@ struct Args {

/// Path to where casm json file will be saved
#[arg(short, long)]
casm_output_path: PathBuf,
casm_output_path: Option<PathBuf>,
}

fn print_error_message(error: &Error) {
Expand All @@ -34,14 +34,17 @@ fn main_execution() -> Result<bool> {
let casm = compile(sierra_json)?;
let casm_json = serde_json::to_value(casm)?;

let casm_file = OpenOptions::new()
.write(true)
.create(true)
.append(false)
.open(args.casm_output_path)
.context("Unable to open/create casm json file")?;
match args.casm_output_path {
Some(output_path) => {
let casm_file =
File::create(output_path).context("Unable to open/create casm json file")?;

to_writer(casm_file, &casm_json).context("Unable to save casm json file")?;
to_writer(casm_file, &casm_json).context("Unable to save casm json file")?;
}
None => {
println!("{}", serde_json::to_string(&casm_json)?);
}
};

Ok(true)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ fn write_to_existing_file() {
verify_output_file(temp_dir.path().join(casm_file_name));
}

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

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

let output = String::from_utf8(snapbox.assert().success().get_output().stdout.clone()).unwrap();
assert!(output.contains("bytecode"));
}

#[test]
fn wrong_json() {
let sierra_file_name = "wrong_sierra.json";
Expand Down

0 comments on commit 9fd2fb3

Please sign in to comment.