Skip to content

Commit

Permalink
Internal: isolate test utilities in a dedicated crate (#9)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
odesenfans authored Nov 20, 2023
1 parent 54aca74 commit 30df4d2
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 3 additions & 0 deletions madara-prover-rpc-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ tokio-stream = "0.1.14"
[build-dependencies]
tonic-build = { workspace = true }

[dev-dependencies]
test-toolkit = { path = "../test-toolkit" }

2 changes: 2 additions & 0 deletions stone-prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ tempfile = "3.8.1"
thiserror = "1.0.50"
tokio = { workspace = true }

[dev-dependencies]
test-toolkit = { path = "../test-toolkit" }
2 changes: 1 addition & 1 deletion stone-prover/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
4 changes: 3 additions & 1 deletion stone-prover/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
17 changes: 0 additions & 17 deletions stone-prover/src/toolkit.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,16 +22,3 @@ pub fn write_json_to_file<T: Serialize, P: AsRef<Path>>(
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")
}
8 changes: 8 additions & 0 deletions test-toolkit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
12 changes: 12 additions & 0 deletions test-toolkit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 30df4d2

Please sign in to comment.