Skip to content

Commit

Permalink
Rework component testing support to make test updates easier (#395)
Browse files Browse the repository at this point in the history
Add a macro for generating the core wasm and component versions of a test, and
modify the .adapt_component builder function on Test to accept a bool. This
simplifies modifying existing tests to support components by doing the work of
abstracting out the body in a macro.
  • Loading branch information
elliottt authored Jun 28, 2024
1 parent c75ee9d commit 7b8c222
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
2 changes: 1 addition & 1 deletion cli/tests/integration/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn empty_ok_response_by_default_after_args() -> TestResult {
#[should_panic]
async fn empty_ok_response_by_default_after_args_component() {
let resp = Test::using_fixture("args.wasm")
.adapt_component()
.adapt_component(true)
.against_empty()
.await
.unwrap();
Expand Down
27 changes: 25 additions & 2 deletions cli/tests/integration/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ pub use self::backends::TestBackends;

mod backends;

#[macro_export]
macro_rules! viceroy_test {
($name:ident, |$is_component:ident| $body:block) => {
mod $name {
use super::*;

async fn test_impl($is_component: bool) -> TestResult {
$body
}

#[tokio::test(flavor = "multi_thread")]
async fn core_wasm() -> TestResult {
test_impl(false).await
}

#[tokio::test(flavor = "multi_thread")]
async fn component() -> TestResult {
test_impl(true).await
}
}
};
}

/// A shorthand for the path to our test fixtures' build artifacts for Rust tests.
///
/// This value can be appended with the name of a fixture's `.wasm` in a test program, using the
Expand Down Expand Up @@ -238,8 +261,8 @@ impl Test {
}

/// Automatically adapt the wasm to a component before running.
pub fn adapt_component(mut self) -> Self {
self.adapt_component = true;
pub fn adapt_component(mut self, adapt: bool) -> Self {
self.adapt_component = adapt;
self
}

Expand Down
19 changes: 7 additions & 12 deletions cli/tests/integration/request.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
use {
crate::common::{Test, TestResult},
crate::{
common::{Test, TestResult},
viceroy_test,
},
hyper::StatusCode,
};

#[tokio::test(flavor = "multi_thread")]
async fn request_works() -> TestResult {
let resp = Test::using_fixture("request.wasm").against_empty().await?;
assert_eq!(resp.status(), StatusCode::OK);
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn request_works_component() -> TestResult {
viceroy_test!(request_works, |is_component| {
let resp = Test::using_fixture("request.wasm")
.adapt_component()
.adapt_component(is_component)
.against_empty()
.await?;
assert_eq!(resp.status(), StatusCode::OK);
Ok(())
}
});
19 changes: 7 additions & 12 deletions cli/tests/integration/response.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
use {
crate::common::{Test, TestResult},
crate::{
common::{Test, TestResult},
viceroy_test,
},
hyper::StatusCode,
};

#[tokio::test(flavor = "multi_thread")]
async fn response_works() -> TestResult {
let resp = Test::using_fixture("response.wasm").against_empty().await?;
assert_eq!(resp.status(), StatusCode::OK);
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn response_works_component() -> TestResult {
viceroy_test!(response_works, |is_component| {
let resp = Test::using_fixture("response.wasm")
.adapt_component()
.adapt_component(is_component)
.against_empty()
.await?;
assert_eq!(resp.status(), StatusCode::OK);
Ok(())
}
});

0 comments on commit 7b8c222

Please sign in to comment.