Skip to content

Commit

Permalink
Add a generate_python_tests! macro (#91)
Browse files Browse the repository at this point in the history
The macro generates tests for Python scripts found next to the
integration test.

This saves a bit of boiler-plate code at the point where the test is
created. It also allows us to quickly see if there are gaps in the
test coverage between the sync and async code.

This comes at the expense of more “magic” in the form of the macro.
  • Loading branch information
mgeisler authored Feb 28, 2024
1 parent 3efaea2 commit de125ef
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 52 deletions.
1 change: 0 additions & 1 deletion mls-rs-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ uniffi = "0.26.0"
tokio = { version = "1.36.0", features = ["sync"] }

[dev-dependencies]
tempfile = "3.10.0"
uniffi_bindgen = "0.26.0"
30 changes: 0 additions & 30 deletions mls-rs-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
//! [UniFFI]: https://mozilla.github.io/uniffi-rs/
mod config;
#[cfg(test)]
pub mod test_utils;

use std::sync::Arc;

Expand Down Expand Up @@ -597,31 +595,3 @@ impl Group {
}
}
}

#[cfg(all(test, not(mls_build_async)))]
mod sync_tests {
use crate::test_utils::run_python;

#[test]
fn test_generate_signature_keypair() -> Result<(), Box<dyn std::error::Error>> {
run_python(include_str!("../test_bindings/signatures_sync.py"))
}

#[test]
fn test_simple_scenario() -> Result<(), Box<dyn std::error::Error>> {
run_python(include_str!("../test_bindings/simple_scenario_sync.py"))
}
}

// TODO(mulmarta): it'll break if we use async trait which will be supported in
// the next UniFFI release
#[cfg(all(test, mls_build_async))]
mod async_tests {
use crate::test_utils::run_python;

#[ignore]
#[test]
fn test_simple_scenario() -> Result<(), Box<dyn std::error::Error>> {
run_python(include_str!("../test_bindings/simple_scenario_async.py"))
}
}
21 changes: 0 additions & 21 deletions mls-rs-uniffi/src/test_utils.rs

This file was deleted.

50 changes: 50 additions & 0 deletions mls-rs-uniffi/tests/scenarios.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// Run sync and async Python scenarios.
///
/// The Python scripts are given as identifiers, relative to this
/// file. They can be `None` if the sync or async test variant does
/// not exist.
///
/// The test script can use `import mls_rs_uniffi` to get access to
/// the Python bindings.
macro_rules! generate_python_tests {
($sync_scenario:ident, None) => {
#[cfg(not(mls_build_async))]
generate_python_tests!($sync_scenario);
};

(None, $async_scenario:ident) => {
#[cfg(mls_build_async)]
generate_python_tests!($async_scenario);
};

($sync_scenario:ident, $async_scenario:ident) => {
#[cfg(not(mls_build_async))]
generate_python_tests!($sync_scenario);

#[cfg(mls_build_async)]
generate_python_tests!($async_scenario);
};

($scenario:ident) => {
#[test]
fn $scenario() -> Result<(), Box<dyn std::error::Error>> {
let target_dir = env!("CARGO_TARGET_TMPDIR");
let script_path = format!("tests/{}.py", stringify!($scenario));
uniffi_bindgen::bindings::python::run_script(
&target_dir,
"mls-rs-uniffi",
&script_path,
vec![],
&uniffi_bindgen::bindings::RunScriptOptions::default(),
)
.map_err(Into::into)
}
};
}

generate_python_tests!(generate_signature_keypair, None);

// TODO(mulmarta): it'll break if we use async trait which will be
// supported in the next UniFFI release
// TODO(mgeisler): add back simple_scenario_async
generate_python_tests!(simple_scenario_sync, None);
File renamed without changes.

0 comments on commit de125ef

Please sign in to comment.