From b8a11aa296aa24f3a33e6eb2f614e084557e9e8c Mon Sep 17 00:00:00 2001 From: Olivier Desenfans Date: Mon, 20 Nov 2023 13:31:28 +0100 Subject: [PATCH] Internal: isolate test utilities in a dedicated crate Problem: test fixture utils are not available to the client/server crates. Solution: extract these functions from the stone-prover crate and add a new project-wide crate as dev dependency for all crates in the project. --- Cargo.toml | 2 +- madara-prover-rpc-server/Cargo.toml | 3 +++ stone-prover/Cargo.toml | 2 ++ stone-prover/src/models.rs | 2 +- stone-prover/src/prover.rs | 4 +++- stone-prover/src/toolkit.rs | 17 ----------------- test-toolkit/Cargo.toml | 8 ++++++++ test-toolkit/src/lib.rs | 12 ++++++++++++ 8 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 test-toolkit/Cargo.toml create mode 100644 test-toolkit/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 3820594..1a14f8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] resolver = "2" -members = ["madara-prover-rpc-client", "madara-prover-rpc-server", "stone-prover"] +members = ["madara-prover-rpc-client", "madara-prover-rpc-server", "stone-prover", "test-toolkit"] [workspace.dependencies] prost = "0.12.1" diff --git a/madara-prover-rpc-server/Cargo.toml b/madara-prover-rpc-server/Cargo.toml index 570f895..cab5eb1 100644 --- a/madara-prover-rpc-server/Cargo.toml +++ b/madara-prover-rpc-server/Cargo.toml @@ -16,3 +16,6 @@ tokio-stream = "0.1.14" [build-dependencies] tonic-build = { workspace = true } +[dev-dependencies] +test-toolkit = { path = "../test-toolkit" } + diff --git a/stone-prover/Cargo.toml b/stone-prover/Cargo.toml index e9a94a7..088afdc 100644 --- a/stone-prover/Cargo.toml +++ b/stone-prover/Cargo.toml @@ -11,3 +11,5 @@ tempfile = "3.8.1" thiserror = "1.0.50" tokio = { workspace = true } +[dev-dependencies] +test-toolkit = { path = "../test-toolkit" } \ No newline at end of file diff --git a/stone-prover/src/models.rs b/stone-prover/src/models.rs index 80c59b4..9b91228 100644 --- a/stone-prover/src/models.rs +++ b/stone-prover/src/models.rs @@ -110,8 +110,8 @@ pub struct Proof { #[cfg(test)] mod tests { - use crate::toolkit::load_fixture; use std::path::Path; + use test_toolkit::load_fixture; use super::*; diff --git a/stone-prover/src/prover.rs b/stone-prover/src/prover.rs index 28f4ccf..a204a8e 100644 --- a/stone-prover/src/prover.rs +++ b/stone-prover/src/prover.rs @@ -225,8 +225,10 @@ pub async fn run_prover_async( mod test { use tempfile::NamedTempFile; + use test_toolkit::get_fixture_path; + use crate::models::{PrivateInput, Proof}; - use crate::toolkit::{get_fixture_path, read_json_from_file}; + use crate::toolkit::read_json_from_file; use super::*; diff --git a/stone-prover/src/toolkit.rs b/stone-prover/src/toolkit.rs index 081c0db..b900299 100644 --- a/stone-prover/src/toolkit.rs +++ b/stone-prover/src/toolkit.rs @@ -1,9 +1,5 @@ -#[cfg(test)] -use std::fs; use std::fs::File; use std::path::Path; -#[cfg(test)] -use std::path::PathBuf; use serde::de::DeserializeOwned; use serde::Serialize; @@ -26,16 +22,3 @@ pub fn write_json_to_file>( serde_json::to_writer(&mut file, &obj)?; Ok(()) } - -#[cfg(test)] -pub fn get_fixture_path(filename: &str) -> PathBuf { - Path::new(env!("CARGO_MANIFEST_DIR")) - .join("tests/fixtures") - .join(filename) -} - -#[cfg(test)] -pub fn load_fixture(filename: &str) -> String { - let fixture_path = get_fixture_path(filename); - fs::read_to_string(fixture_path).expect("Failed to read the fixture file") -} diff --git a/test-toolkit/Cargo.toml b/test-toolkit/Cargo.toml new file mode 100644 index 0000000..f1b55bd --- /dev/null +++ b/test-toolkit/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "test-toolkit" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/test-toolkit/src/lib.rs b/test-toolkit/src/lib.rs new file mode 100644 index 0000000..2142a6c --- /dev/null +++ b/test-toolkit/src/lib.rs @@ -0,0 +1,12 @@ +use std::path::{Path, PathBuf}; + +pub fn get_fixture_path(filename: &str) -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../stone-prover/tests/fixtures") + .join(filename) +} + +pub fn load_fixture(filename: &str) -> String { + let fixture_path = get_fixture_path(filename); + std::fs::read_to_string(fixture_path).expect("Failed to read the fixture file") +}