Skip to content

Commit

Permalink
Keep sierra file path in TestTargetRaw (#2534)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Towards #2403

## Introduced changes

<!-- A brief description of the changes -->

- Keep sierra file path in `TestTargetRaw` so it can be printed later
  • Loading branch information
ddoktorski authored Nov 14, 2024
1 parent e1c0c42 commit d38bc8c
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 42 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/cheatnet/src/forking/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl StateReader for ForkStateReader {
"entry_points_by_type": flattened_class.entry_points_by_type
});

match compile_sierra(&sierra_contract_class, None, &SierraType::Contract) {
match compile_sierra::<String>(&sierra_contract_class, &SierraType::Contract) {
Ok(casm_contract_class_raw) => {
let casm_contract_class: CasmContractClass =
serde_json::from_str(&casm_contract_class_raw)
Expand Down
2 changes: 2 additions & 0 deletions crates/forge-runner/src/package_tests/raw.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use super::TestTargetLocation;
use cairo_lang_sierra::program::ProgramArtifact;
use camino::Utf8PathBuf;

/// these structs are representation of scarb output for `scarb build --test`
/// produced by scarb
pub struct TestTargetRaw {
pub sierra_file_path: Utf8PathBuf,
pub sierra_program: ProgramArtifact,
pub tests_location: TestTargetLocation,
}
7 changes: 4 additions & 3 deletions crates/forge-runner/src/running/with_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use cairo_lang_sierra_type_size::get_type_size_map;
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use starknet_types_core::felt::Felt;
use std::{collections::HashMap, sync::Arc};
use universal_sierra_compiler_api::compile_sierra_to_casm;
use universal_sierra_compiler_api::{compile_sierra_at_path, SierraType};

pub fn test_target_with_config(test_target_raw: TestTargetRaw) -> Result<TestTargetWithConfig> {
macro_rules! by_id {
Expand All @@ -37,8 +37,9 @@ pub fn test_target_with_config(test_target_raw: TestTargetRaw) -> Result<TestTar
let funcs = by_id!(funcs);
let type_declarations = by_id!(type_declarations);

let casm_program = Arc::new(compile_sierra_to_casm(
&test_target_raw.sierra_program.program,
let casm_program = Arc::new(compile_sierra_at_path(
&test_target_raw.sierra_file_path,
&SierraType::Raw,
)?);

let sierra_program_registry =
Expand Down
10 changes: 8 additions & 2 deletions crates/forge/src/run_tests/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ mod tests {
use forge_runner::package_tests::TestTargetLocation;
use forge_runner::{expected_result::ExpectedTestResult, package_tests::TestDetails};
use std::sync::Arc;
use universal_sierra_compiler_api::compile_sierra_to_casm;
use universal_sierra_compiler_api::{compile_sierra, SierraType};
use url::Url;

fn program_for_testing() -> ProgramArtifact {
Expand All @@ -146,7 +146,13 @@ mod tests {
async fn to_runnable_non_existent_id() {
let mocked_tests = TestTargetWithConfig {
sierra_program: program_for_testing(),
casm_program: Arc::new(compile_sierra_to_casm(&program_for_testing().program).unwrap()),
casm_program: Arc::new(
compile_sierra(
&serde_json::to_value(&program_for_testing().program).unwrap(),
&SierraType::Raw,
)
.unwrap(),
),
test_cases: vec![TestCaseWithConfig {
name: "crate1::do_thing".to_string(),
config: TestCaseConfig {
Expand Down
4 changes: 3 additions & 1 deletion crates/forge/src/scarb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ pub fn load_test_artifacts(
};

let target_file = format!("{target_name}.test.sierra.json");
let sierra_file_path = target_dir.join(target_file);

match read_to_string(target_dir.join(target_file)) {
match read_to_string(&sierra_file_path) {
Ok(value) => {
let versioned_program = serde_json::from_str::<VersionedProgram>(&value)?;

Expand All @@ -98,6 +99,7 @@ pub fn load_test_artifacts(
};

let test_target = TestTargetRaw {
sierra_file_path,
sierra_program,
tests_location,
};
Expand Down
42 changes: 36 additions & 6 deletions crates/forge/src/test_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod tests {
};
use forge_runner::package_tests::{TestDetails, TestTargetLocation};
use std::sync::Arc;
use universal_sierra_compiler_api::compile_sierra_to_casm;
use universal_sierra_compiler_api::{compile_sierra, SierraType};

fn program_for_testing() -> ProgramArtifact {
ProgramArtifact {
Expand Down Expand Up @@ -163,7 +163,13 @@ mod tests {
fn filtering_tests() {
let mocked_tests = TestTargetWithResolvedConfig {
sierra_program: program_for_testing(),
casm_program: Arc::new(compile_sierra_to_casm(&program_for_testing().program).unwrap()),
casm_program: Arc::new(
compile_sierra(
&serde_json::to_value(&program_for_testing().program).unwrap(),
&SierraType::Raw,
)
.unwrap(),
),
test_cases: vec![
TestCaseWithResolvedConfig {
name: "crate1::do_thing".to_string(),
Expand Down Expand Up @@ -425,7 +431,13 @@ mod tests {
fn filtering_with_no_tests() {
let mocked_tests = TestTargetWithResolvedConfig {
sierra_program: program_for_testing(),
casm_program: Arc::new(compile_sierra_to_casm(&program_for_testing().program).unwrap()),
casm_program: Arc::new(
compile_sierra(
&serde_json::to_value(&program_for_testing().program).unwrap(),
&SierraType::Raw,
)
.unwrap(),
),
test_cases: vec![],
tests_location: TestTargetLocation::Lib,
};
Expand Down Expand Up @@ -464,7 +476,13 @@ mod tests {
fn filtering_with_exact_match() {
let mocked_tests = TestTargetWithResolvedConfig {
sierra_program: program_for_testing(),
casm_program: Arc::new(compile_sierra_to_casm(&program_for_testing().program).unwrap()),
casm_program: Arc::new(
compile_sierra(
&serde_json::to_value(&program_for_testing().program).unwrap(),
&SierraType::Raw,
)
.unwrap(),
),
test_cases: vec![
TestCaseWithResolvedConfig {
name: "crate1::do_thing".to_string(),
Expand Down Expand Up @@ -649,7 +667,13 @@ mod tests {
fn filtering_with_only_ignored() {
let mocked_tests = TestTargetWithResolvedConfig {
sierra_program: program_for_testing(),
casm_program: Arc::new(compile_sierra_to_casm(&program_for_testing().program).unwrap()),
casm_program: Arc::new(
compile_sierra(
&serde_json::to_value(&program_for_testing().program).unwrap(),
&SierraType::Raw,
)
.unwrap(),
),
test_cases: vec![
TestCaseWithResolvedConfig {
name: "crate1::do_thing".to_string(),
Expand Down Expand Up @@ -744,7 +768,13 @@ mod tests {
fn filtering_with_include_ignored() {
let mocked_tests = TestTargetWithResolvedConfig {
sierra_program: program_for_testing(),
casm_program: Arc::new(compile_sierra_to_casm(&program_for_testing().program).unwrap()),
casm_program: Arc::new(
compile_sierra(
&serde_json::to_value(&program_for_testing().program).unwrap(),
&SierraType::Raw,
)
.unwrap(),
),
test_cases: vec![
TestCaseWithResolvedConfig {
name: "crate1::do_thing".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion crates/scarb-api/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn compile_artifacts(
fn compile_artifact_at_path(path: &Utf8Path) -> Result<StarknetContractArtifacts> {
let sierra = fs::read_to_string(path)?;

let casm = compile_sierra_at_path(path.as_str(), None, &SierraType::Contract)?;
let casm = compile_sierra_at_path(path, &SierraType::Contract)?;

Ok(StarknetContractArtifacts { sierra, casm })
}
1 change: 1 addition & 0 deletions crates/universal-sierra-compiler-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ tempfile.workspace = true
num-bigint.workspace = true
cairo-lang-casm.workspace = true
cairo-lang-sierra.workspace = true
camino.workspace = true
54 changes: 26 additions & 28 deletions crates/universal-sierra-compiler-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use cairo_lang_casm::hints::Hint;
use cairo_lang_sierra::program::Program;
use camino::Utf8Path;
use num_bigint::BigInt;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::Display;
use std::io::Write;
use std::path::Path;
use std::str::from_utf8;
use tempfile::Builder;

Expand Down Expand Up @@ -37,49 +36,48 @@ pub struct AssembledCairoProgramWithSerde {
pub hints: Vec<(usize, Vec<Hint>)>,
}

pub fn compile_sierra_to_casm(sierra_program: &Program) -> Result<AssembledProgramWithDebugInfo> {
let assembled_with_info_raw = compile_sierra(
&serde_json::to_value(sierra_program)?,
None,
&SierraType::Raw,
)?;
let assembled_with_info: AssembledProgramWithDebugInfo =
serde_json::from_str(&assembled_with_info_raw)?;
pub trait UniversalSierraCompilerOutput {
fn convert(output: String) -> Self;
}

impl UniversalSierraCompilerOutput for String {
fn convert(output: String) -> Self {
output
}
}

Ok(assembled_with_info)
impl UniversalSierraCompilerOutput for AssembledProgramWithDebugInfo {
fn convert(output: String) -> Self {
serde_json::from_str(&output).expect("Failed to deserialize casm from json string")
}
}

pub fn compile_sierra(
sierra_contract_class: &Value,
current_dir: Option<&Path>,
pub fn compile_sierra<T: UniversalSierraCompilerOutput>(
sierra_json: &Value,
sierra_type: &SierraType,
) -> Result<String> {
) -> Result<T> {
let mut temp_sierra_file = Builder::new().tempfile()?;
let _ = temp_sierra_file.write(serde_json::to_vec(sierra_contract_class)?.as_slice())?;
temp_sierra_file.write_all(serde_json::to_vec(sierra_json)?.as_slice())?;

compile_sierra_at_path(
temp_sierra_file.path().to_str().unwrap(),
current_dir,
Utf8Path::from_path(temp_sierra_file.path())
.ok_or_else(|| anyhow!("Failed to create Utf8Path from temp file path"))?,
sierra_type,
)
}

pub fn compile_sierra_at_path(
sierra_file_path: &str,
current_dir: Option<&Path>,
pub fn compile_sierra_at_path<T: UniversalSierraCompilerOutput>(
sierra_file_path: &Utf8Path,
sierra_type: &SierraType,
) -> Result<String> {
) -> Result<T> {
let mut usc_command = UniversalSierraCompilerCommand::new();
if let Some(dir) = current_dir {
usc_command.current_dir(dir);
}

let usc_output = usc_command
.inherit_stderr()
.args(vec![
&("compile-".to_string() + &sierra_type.to_string()),
"--sierra-path",
sierra_file_path,
sierra_file_path.as_str(),
])
.command()
.output_checked()
Expand All @@ -89,7 +87,7 @@ pub fn compile_sierra_at_path(
Contact us if it doesn't help",
)?;

Ok(from_utf8(&usc_output.stdout)?.to_string())
Ok(T::convert(from_utf8(&usc_output.stdout)?.to_string()))
}

pub enum SierraType {
Expand Down

0 comments on commit d38bc8c

Please sign in to comment.