Skip to content

Commit d64aedd

Browse files
authored
Merge pull request #29 from yoshuawuyts/pch/cargo_test
Change to use `cargo test --target wasm32-wasip2` for most testing
2 parents 059c541 + a9527be commit d64aedd

File tree

12 files changed

+76
-36
lines changed

12 files changed

+76
-36
lines changed

.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.wasm32-wasip2]
2+
runner = "wasmtime -Shttp"

.github/workflows/ci.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,27 @@ jobs:
2929
target: wasm32-wasip2
3030
override: true
3131

32+
- name: Install wasmtime
33+
uses: bytecodealliance/actions/wasmtime/setup@v1
34+
3235
- name: check
3336
uses: actions-rs/cargo@v1
3437
with:
3538
command: check
3639
args: --all --bins --examples
3740

38-
- name: tests
41+
- name: wstd tests
42+
uses: actions-rs/cargo@v1
43+
with:
44+
command: test
45+
args: -p wstd --target wasm32-wasip2
46+
47+
- name: example tests
3948
uses: actions-rs/cargo@v1
4049
with:
4150
command: test
42-
args: --all
51+
args: -p test-programs-artifacts
52+
4353

4454
check_fmt_and_docs:
4555
name: Checking fmt and docs

Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ wstd-macro.workspace = true
2626
anyhow.workspace = true
2727
futures-lite.workspace = true
2828
serde_json.workspace = true
29-
test-programs-artifacts.workspace = true
30-
wasmtime.workspace = true
31-
wasmtime-wasi.workspace = true
32-
wasmtime-wasi-http.workspace = true
3329

3430
[workspace]
3531
members = [
@@ -57,6 +53,7 @@ quote = "1.0"
5753
serde_json = "1"
5854
slab = "0.4.9"
5955
syn = "2.0"
56+
test-log = { version = "0.2", features = ["trace"] }
6057
test-programs = { path = "test-programs" }
6158
test-programs-artifacts = { path = "test-programs/artifacts" }
6259
wasi = "0.13.1"

macro/src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,42 @@ pub fn attr_macro_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
4444
}
4545
.into()
4646
}
47+
48+
#[proc_macro_attribute]
49+
pub fn attr_macro_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
50+
let input = parse_macro_input!(item as ItemFn);
51+
52+
if input.sig.asyncness.is_none() {
53+
return quote_spanned! { input.sig.fn_token.span()=>
54+
compile_error!("fn must be `async fn`");
55+
}
56+
.into();
57+
}
58+
59+
let name = input.sig.ident;
60+
61+
if !input.sig.inputs.is_empty() {
62+
return quote_spanned! { input.sig.inputs.span()=>
63+
compile_error!("arguments to main are not supported");
64+
}
65+
.into();
66+
}
67+
let attrs = input.attrs;
68+
let output = input.sig.output;
69+
let block = input.block;
70+
quote! {
71+
#[test]
72+
pub fn #name() #output {
73+
74+
#(#attrs)*
75+
async fn __run() #output {
76+
#block
77+
}
78+
79+
::wstd::runtime::block_on(async {
80+
__run().await
81+
})
82+
}
83+
}
84+
.into()
85+
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//! **HTTP Client**
2424
//!
2525
//! ```rust,no_run
26-
#![doc = include_str!("../examples/http_get.rs")]
26+
#![doc = include_str!("../tests/http_get.rs")]
2727
//! ```
2828
//!
2929
//! # Design Decisions
@@ -59,6 +59,7 @@ pub mod task;
5959
pub mod time;
6060

6161
pub use wstd_macro::attr_macro_main as main;
62+
pub use wstd_macro::attr_macro_test as test;
6263

6364
pub mod prelude {
6465
pub use crate::future::FutureExt as _;

test-programs/artifacts/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ publish = false
77

88
[dependencies]
99

10+
[dev-dependencies]
11+
anyhow.workspace = true
12+
test-log.workspace = true
13+
test-programs-artifacts.workspace = true
14+
wasmtime.workspace = true
15+
wasmtime-wasi.workspace = true
16+
wasmtime-wasi-http.workspace = true
17+
1018
[build-dependencies]
1119
cargo_metadata.workspace = true
1220
heck.workspace = true

tests/test-programs.rs renamed to test-programs/artifacts/tests/tcp_echo_server.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn run_in_wasmtime(wasm: &[u8], stdout: Option<MemoryOutputPipe>) -> Result<()>
7474
Ok(())
7575
}
7676

77-
#[test]
77+
#[test_log::test]
7878
fn tcp_echo_server() -> Result<()> {
7979
use std::io::{Read, Write};
8080
use std::net::TcpStream;
@@ -119,28 +119,3 @@ fn tcp_echo_server() -> Result<()> {
119119
}
120120
Ok(())
121121
}
122-
123-
#[test]
124-
fn http_get() -> Result<()> {
125-
println!("testing {}", test_programs_artifacts::HTTP_GET);
126-
let wasm = std::fs::read(test_programs_artifacts::HTTP_GET).context("read wasm")?;
127-
run_in_wasmtime(&wasm, None)
128-
}
129-
130-
#[test]
131-
fn http_post() -> Result<()> {
132-
println!("testing {}", test_programs_artifacts::HTTP_POST);
133-
let wasm = std::fs::read(test_programs_artifacts::HTTP_POST).context("read wasm")?;
134-
run_in_wasmtime(&wasm, None)
135-
}
136-
137-
#[test]
138-
fn http_first_byte_timeout() -> Result<()> {
139-
println!(
140-
"testing {}",
141-
test_programs_artifacts::HTTP_FIRST_BYTE_TIMEOUT
142-
);
143-
let wasm =
144-
std::fs::read(test_programs_artifacts::HTTP_FIRST_BYTE_TIMEOUT).context("read wasm")?;
145-
run_in_wasmtime(&wasm, None)
146-
}

test-programs/src/bin/http_get.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/http_get.rs renamed to tests/http_get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::error::Error;
22
use wstd::http::{Body, Client, HeaderValue, Method, Request};
33
use wstd::io::AsyncRead;
44

5-
#[wstd::main]
5+
#[wstd::test]
66
async fn main() -> Result<(), Box<dyn Error>> {
77
let mut request = Request::new(Method::GET, "https://postman-echo.com/get".parse()?);
88
request

0 commit comments

Comments
 (0)