From 5ebc441dc93ea39b9ba875b3d459056d11207c79 Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Tue, 18 Mar 2025 09:55:09 -0700 Subject: make the test_vectors tests WASM-compatible --- .cargo/config.toml | 1 - test_vectors/src/lib.rs | 14 ++++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 52e3106..8e7c3af 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,2 @@ [target.wasm32-wasip1] runner = "wasmtime" - diff --git a/test_vectors/src/lib.rs b/test_vectors/src/lib.rs index 6a4c798..543a31d 100644 --- a/test_vectors/src/lib.rs +++ b/test_vectors/src/lib.rs @@ -1,6 +1,10 @@ use blake3::guts::{BLOCK_LEN, CHUNK_LEN}; use serde::{Deserialize, Serialize}; +// Reading files at runtime requires special configuration under WASM/WASI, so including this at +// compile time is simpler. +const TEST_VECTORS_JSON: &str = include_str!("../test_vectors.json"); + // A non-multiple of 4 is important, since one possible bug is to fail to emit // partial words. pub const OUTPUT_LEN: usize = 2 * BLOCK_LEN + 3; @@ -129,14 +133,8 @@ pub fn generate_json() -> String { json } -pub fn read_test_vectors_file() -> String { - let test_vectors_file_path = "./test_vectors.json"; - std::fs::read_to_string(test_vectors_file_path).expect("failed to read test_vectors.json") -} - pub fn parse_test_cases() -> Cases { - let json = read_test_vectors_file(); - serde_json::from_str(&json).expect("failed to parse test_vectors.json") + serde_json::from_str(TEST_VECTORS_JSON).expect("failed to parse test_vectors.json") } #[cfg(test)] @@ -344,7 +342,7 @@ mod tests { fn test_checked_in_vectors_up_to_date() { // Replace Windows newlines, in case Git is configured to alter // newlines when files are checked out. - let json = read_test_vectors_file().replace("\r\n", "\n"); + let json = TEST_VECTORS_JSON.replace("\r\n", "\n"); if generate_json() != json { panic!("Checked-in test_vectors.json is not up to date. Regenerate with `cargo run --bin generate > ./test_vectors.json`."); } -- cgit v1.2.3