image-rs / jpeg-decoder

JPEG decoder written in Rust
Apache License 2.0
150 stars 87 forks source link

An investigation into testing with wasm #241

Closed HeroicKatora closed 2 years ago

HeroicKatora commented 2 years ago

wasm testing:

As per wasm-bindgen, cargo test --target wasm32-unknown-unknown tries to execute the .wasm file which it can't. We need a runner, something that spins up the host platform which runs the sandbox. They suggest wasm-bindgen-test/wasm-bindgen-test-runner which uses node under the hood, and provides web-sys host bindings for things such as actual output. (There's certainly a possible to provide output in some ring-buffer without host requirements but web-sys works and is established, so who's to blame for doing it that way. Except people that want perfect reproducibility of course).

  1. cargo install --git https://github.com/rustwasm/wasm-bindgen wasm-bindgen-cli --bin wasm-bindgen-test-runner

  2. (1.1) CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-bindgen-test-runner cargo +stable test --target wasm32-unknown-unknown:

    Running tests/rayon-0.rs (target/wasm32-unknown-unknown/debug/deps/rayon_0-39b62c7e345d87d8.wasm) Set timeout to 20 seconds... Error: failed to execute node

  3. Install node, ugh

  4. Yay, tests are being ran

    panicked at 'called Result::unwrap() on an Err value: ThreadPoolBuildError { kind: IOError(Error { kind: Unsupported, message: "operation not supported on this platform" }) }', tests/rayon-0.rs:13:10

  5. as expected, until #240 or further is resolved

HeroicKatora commented 2 years ago

Making sure not to use any file-API:

Diff for this test ```diff diff --git a/Cargo.toml b/Cargo.toml index 7d52882..cf20f66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ rayon = { version = "1.5.1", optional = true } png = "0.16" walkdir = "2.0" criterion = "0.3" +wasm-bindgen-test = "0.3" [features] default = ["rayon"] diff --git a/tests/lib.rs b/tests/lib.rs index 2d49e4e..b6a87c1 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -9,6 +9,27 @@ mod common; mod crashtest; mod reftest; +#[test] +#[wasm_bindgen_test::wasm_bindgen_test] +fn included_file() { + const FILE: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/reftest/images/mozilla/jpg-progressive.jpg")); + + let mut data = FILE; + let mut decoder = jpeg::Decoder::new(&mut data); + let ref_data = decoder.decode().unwrap(); + let ref_info = decoder.info().unwrap(); + + let mut data = FILE; + decoder = jpeg::Decoder::new(&mut data); + decoder.read_info().unwrap(); + let info = decoder.info().unwrap(); + let data = decoder.decode().unwrap(); + + assert_eq!(info, decoder.info().unwrap()); + assert_eq!(info, ref_info); + assert_eq!(data, ref_data); +} + #[test] fn read_info() { let path = Path::new("tests").join("reftest").join("images").join("mozilla").join("jpg-progressive.jpg"); ```
Set timeout to 20 seconds...
running 1 test                                    

test lib::included_file ... ok

test result: ok. 1 passed; 0 failed; 0 ignored

:tada:

PR coming soon.