From 61164c59fe034fcf8d2a86ad75f7813ea307cfeb Mon Sep 17 00:00:00 2001 From: varex83 Date: Tue, 24 Oct 2023 13:34:19 +0300 Subject: [PATCH 1/5] add support of scarb test --- api/src/handlers/cairo_version.rs | 1 - api/src/handlers/compile_casm.rs | 1 + api/src/handlers/mod.rs | 6 +++ api/src/handlers/process.rs | 2 +- api/src/handlers/scarb_compile.rs | 2 +- api/src/handlers/scarb_test.rs | 90 +++++++++++++++++++++++++++++++ api/src/handlers/types.rs | 10 ++++ api/src/main.rs | 3 ++ 8 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 api/src/handlers/scarb_test.rs diff --git a/api/src/handlers/cairo_version.rs b/api/src/handlers/cairo_version.rs index c9d324ce..e5b1a36b 100644 --- a/api/src/handlers/cairo_version.rs +++ b/api/src/handlers/cairo_version.rs @@ -30,7 +30,6 @@ pub async fn cairo_version_async( #[instrument] #[get("/cairo_version_result/")] pub async fn get_cairo_version_result(process_id: String, engine: &State) -> String { - info!("/cairo_version_result/{:?}", process_id); fetch_process_result(process_id, engine, |result| match result { ApiCommandResult::CairoVersion(version) => version.to_string(), _ => String::from("Result not available"), diff --git a/api/src/handlers/compile_casm.rs b/api/src/handlers/compile_casm.rs index df22c81a..8b96ae17 100644 --- a/api/src/handlers/compile_casm.rs +++ b/api/src/handlers/compile_casm.rs @@ -14,6 +14,7 @@ use std::process::{Command, Stdio}; use tracing::info; use tracing::instrument; +#[instrument] #[get("/compile-to-casm//")] pub async fn compile_to_casm( version: String, diff --git a/api/src/handlers/mod.rs b/api/src/handlers/mod.rs index 92188415..6ddfcea7 100644 --- a/api/src/handlers/mod.rs +++ b/api/src/handlers/mod.rs @@ -5,12 +5,14 @@ pub mod compile_sierra; pub mod process; pub mod save_code; pub mod scarb_compile; +pub mod scarb_test; pub mod types; use crate::handlers::cairo_version::do_cairo_version; use crate::handlers::compile_casm::do_compile_to_casm; use crate::handlers::compile_sierra::do_compile_to_sierra; use crate::handlers::scarb_compile::do_scarb_compile; +use crate::handlers::scarb_test::do_scarb_test; use crate::handlers::types::{ApiCommand, ApiCommandResult, FileContentMap}; use crate::types::{ApiError, Result}; use rocket::serde::json::Json; @@ -61,6 +63,10 @@ pub async fn dispatch_command(command: ApiCommand) -> Result { Err(e) => Err(e), }, ApiCommand::Shutdown => Ok(ApiCommandResult::Shutdown), + ApiCommand::ScarbTest { remix_file_path } => match do_scarb_test(remix_file_path).await { + Ok(result) => Ok(ApiCommandResult::ScarbTest(result.into_inner())), + Err(e) => Err(e), + }, } } diff --git a/api/src/handlers/process.rs b/api/src/handlers/process.rs index 4deb2395..906a4027 100644 --- a/api/src/handlers/process.rs +++ b/api/src/handlers/process.rs @@ -1,7 +1,7 @@ use crate::handlers::types::{ApiCommand, ApiCommandResult}; use crate::worker::{ProcessState, WorkerEngine}; use rocket::State; -use tracing::{info, instrument}; +use tracing::instrument; use uuid::Uuid; #[instrument] diff --git a/api/src/handlers/scarb_compile.rs b/api/src/handlers/scarb_compile.rs index edb15028..2bd686c5 100644 --- a/api/src/handlers/scarb_compile.rs +++ b/api/src/handlers/scarb_compile.rs @@ -10,7 +10,7 @@ use rocket::serde::json::Json; use rocket::State; use std::path::PathBuf; use std::process::{Command, Stdio}; -use tracing::{info, instrument}; +use tracing::instrument; #[instrument] #[get("/compile-scarb/")] diff --git a/api/src/handlers/scarb_test.rs b/api/src/handlers/scarb_test.rs new file mode 100644 index 00000000..4cd36993 --- /dev/null +++ b/api/src/handlers/scarb_test.rs @@ -0,0 +1,90 @@ +use crate::handlers::process::{do_process_command, fetch_process_result}; +use crate::handlers::types::{ApiCommand, ApiCommandResult, ScarbTestResponse}; +use crate::rate_limiter::RateLimited; +use crate::types::ApiError; +use crate::types::Result; +use crate::utils::lib::get_file_path; +use crate::worker::WorkerEngine; +use rocket::serde::json; +use rocket::serde::json::Json; +use rocket::State; +use std::path::PathBuf; +use std::process::{Command, Stdio}; +use tracing::instrument; + +#[instrument] +#[get("/scarb-test-async/")] +pub async fn scarb_test_async( + remix_file_path: PathBuf, + engine: &State, + _rate_limited: RateLimited, +) -> String { + info!("/scarb-test-async/{:?}", remix_file_path); + do_process_command(ApiCommand::ScarbTest { remix_file_path }, engine) +} + +#[instrument] +#[get("/scarb-test-result/")] +pub async fn get_scarb_test_result(process_id: String, engine: &State) -> String { + info!("/scarb-test-result/{:?}", process_id); + fetch_process_result(process_id, engine, |result| match result { + ApiCommandResult::ScarbTest(scarb_result) => json::to_string(&scarb_result) + .unwrap_or_else(|e| format!("Failed to fetch result: {:?}", e)), + _ => String::from("Result not available"), + }) +} + +/// Run Scarb to test a project +/// +pub async fn do_scarb_test(remix_file_path: PathBuf) -> Result> { + let remix_file_path = remix_file_path + .to_str() + .ok_or(ApiError::FailedToParseString)? + .to_string(); + + let file_path = get_file_path(&remix_file_path); + + let mut compile = Command::new("scarb"); + compile.current_dir(&file_path); + + let result = compile + .arg("test") + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .map_err(ApiError::FailedToExecuteCommand)?; + + debug!("LOG: ran command:{:?}", compile); + + let output = result + .wait_with_output() + .map_err(ApiError::FailedToReadOutput)?; + + let message = String::from_utf8(output.stdout) + .map_err(ApiError::UTF8Error)? + .replace( + &file_path + .to_str() + .ok_or(ApiError::FailedToParseString)? + .to_string(), + &remix_file_path, + ) + + &String::from_utf8(output.stderr) + .map_err(ApiError::UTF8Error)? + .replace( + &file_path + .to_str() + .ok_or(ApiError::FailedToParseString)? + .to_string(), + &remix_file_path, + ); + + let status = match output.status.code() { + Some(0) => "Success", + Some(_) => "SierraCompilationFailed", + None => "UnknownError", + } + .to_string(); + + Ok(Json(ScarbTestResponse { message, status })) +} diff --git a/api/src/handlers/types.rs b/api/src/handlers/types.rs index 223e58fa..39f5750b 100644 --- a/api/src/handlers/types.rs +++ b/api/src/handlers/types.rs @@ -23,6 +23,12 @@ pub struct ScarbCompileResponse { pub file_content_map_array: Vec, } +#[derive(Debug, Serialize, Deserialize)] +pub struct ScarbTestResponse { + pub status: String, + pub message: String, +} + #[derive(Debug)] pub enum ApiCommand { CairoVersion, @@ -37,6 +43,9 @@ pub enum ApiCommand { ScarbCompile { remix_file_path: PathBuf, }, + ScarbTest { + remix_file_path: PathBuf, + }, #[allow(dead_code)] Shutdown, } @@ -47,6 +56,7 @@ pub enum ApiCommandResult { CasmCompile(CompileResponse), SierraCompile(CompileResponse), ScarbCompile(ScarbCompileResponse), + ScarbTest(ScarbTestResponse), #[allow(dead_code)] Shutdown, } diff --git a/api/src/main.rs b/api/src/main.rs index 03c0a1e5..6202941c 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -22,6 +22,7 @@ use handlers::compile_sierra::{ use handlers::process::get_process_status; use handlers::save_code::save_code; use handlers::scarb_compile::{get_scarb_compile_result, scarb_compile, scarb_compile_async}; +use handlers::scarb_test::{get_scarb_test_result, scarb_test_async}; use handlers::{health, who_is_this}; use tracing::info; @@ -75,6 +76,8 @@ async fn rocket() -> _ { get_process_status, health, who_is_this, + get_scarb_test_result, + scarb_test_async, ], ) } From 13f021465c602efc90c88c532d89398b63eb1fdb Mon Sep 17 00:00:00 2001 From: varex83 Date: Tue, 24 Oct 2023 14:44:17 +0300 Subject: [PATCH 2/5] add github action to check rust linting --- .github/workflows/rust-linting.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .github/workflows/rust-linting.yml diff --git a/.github/workflows/rust-linting.yml b/.github/workflows/rust-linting.yml new file mode 100644 index 00000000..5ffa611e --- /dev/null +++ b/.github/workflows/rust-linting.yml @@ -0,0 +1,29 @@ +on: + pull_request: + branches: + - develop + +jobs: + rustfmt: + name: Rustfmt + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + - run: cd api && cargo +nightly fmt --all -- --check + + clippy: + name: Clippy + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + - run: cd api && cargo +nightly clippy --all -- -D warnings From 661147a2323ce73b6db031e03550b1b32025880c Mon Sep 17 00:00:00 2001 From: varex83 Date: Tue, 24 Oct 2023 14:46:32 +0300 Subject: [PATCH 3/5] add nightly rust component --- .github/workflows/rust-linting.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/rust-linting.yml b/.github/workflows/rust-linting.yml index 5ffa611e..9400e268 100644 --- a/.github/workflows/rust-linting.yml +++ b/.github/workflows/rust-linting.yml @@ -14,6 +14,7 @@ jobs: with: toolchain: nightly override: true + - run: rustup component add rustfmt --toolchain nightly - run: cd api && cargo +nightly fmt --all -- --check clippy: @@ -26,4 +27,5 @@ jobs: with: toolchain: nightly override: true + - run: rustup component add rustfmt --toolchain nightly - run: cd api && cargo +nightly clippy --all -- -D warnings From a67f251df8d4a2cb5632f66759ad350dfcfec0f3 Mon Sep 17 00:00:00 2001 From: varex83 Date: Tue, 24 Oct 2023 14:48:17 +0300 Subject: [PATCH 4/5] add nightly rust component --- .github/workflows/rust-linting.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust-linting.yml b/.github/workflows/rust-linting.yml index 9400e268..286cafb6 100644 --- a/.github/workflows/rust-linting.yml +++ b/.github/workflows/rust-linting.yml @@ -27,5 +27,5 @@ jobs: with: toolchain: nightly override: true - - run: rustup component add rustfmt --toolchain nightly + - run: rustup component add clippy --toolchain nightly - run: cd api && cargo +nightly clippy --all -- -D warnings From e1564d2bb52faeeeb39bafa511c232b8db4084f7 Mon Sep 17 00:00:00 2001 From: varex83 Date: Tue, 24 Oct 2023 14:53:35 +0300 Subject: [PATCH 5/5] fix clippy warnings --- api/src/handlers/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/handlers/process.rs b/api/src/handlers/process.rs index 906a4027..ff6574dd 100644 --- a/api/src/handlers/process.rs +++ b/api/src/handlers/process.rs @@ -22,7 +22,7 @@ pub async fn get_process_status(process_id: String, engine: &State ) } else { // TODO can we return HTTP status code here? - format!("Process id not found") + format!("Process with id={} not found", process_id) } } Err(e) => {