Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions extensions/scarb-execute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ pub fn execute(
.then(|| ExecutionResources::try_new(&runner, hint_processor))
.transpose()?;

ui.print(ExecutionSummary {
output: args
let summary = ExecutionSummary {
program_output: args
.run
.print_program_output
.then(|| ExecutionOutput::try_new(&mut runner))
Expand All @@ -343,7 +343,12 @@ pub fn execute(
.print_resource_usage
.then_some(execution_resources.clone())
.flatten(),
});
};
if args.run.print_resource_usage || args.run.print_program_output {
ui.force_print(summary);
} else {
ui.print(summary);
}

if output.is_cairo_pie() {
let output_value = runner.get_cairo_pie()?;
Expand Down
12 changes: 6 additions & 6 deletions extensions/scarb-execute/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use thousands::Separable;

#[derive(Serialize)]
pub struct ExecutionSummary {
pub output: Option<ExecutionOutput>,
pub program_output: Option<ExecutionOutput>,
pub resources: Option<ExecutionResources>,
}

Expand All @@ -26,7 +26,7 @@ impl Message for ExecutionSummary {
where
Self: Sized,
{
if let Some(output) = self.output {
if let Some(output) = self.program_output {
output.print_text();
}
if let Some(resources) = self.resources {
Expand All @@ -43,14 +43,14 @@ impl Message for ExecutionSummary {
}

#[derive(Serialize)]
#[serde(transparent)]
pub struct ExecutionOutput(String);

impl ExecutionOutput {
pub fn try_new(runner: &mut CairoRunner) -> Result<Self> {
let mut output_buffer = "Program output:\n".to_string();
let mut output_buffer = String::new();
runner.vm.write_output(&mut output_buffer)?;
let output = output_buffer.trim_end().to_string();
Ok(Self(output))
Ok(Self(output_buffer.trim_end().to_string()))
}
}

Expand All @@ -59,7 +59,7 @@ impl Message for ExecutionOutput {
where
Self: Sized,
{
println!("{}", self.0);
println!("Program output:\n{}", self.0);
}

fn structured<S: Serializer>(self, ser: S) -> std::result::Result<S::Ok, S::Error>
Expand Down
19 changes: 19 additions & 0 deletions extensions/scarb-execute/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,22 @@ fn allow_syscalls_triggers_layout_warning() {
[..]Executing hello
"#});
}

#[test]
fn can_output_json_format() {
let t = build_executable_project();
Scarb::quick_command().arg("build").current_dir(&t).assert();
Scarb::quick_command()
.arg("execute")
.arg("--json")
.arg("--no-build")
.arg("--output=none")
.arg("--print-program-output")
.current_dir(&t)
.assert()
.success()
.stdout_eq(indoc! {r#"
{"status":"executing","message":"hello"}
{"program_output":"42","resources":null}
"#});
}
Loading