cargo wasi test should preopen the current dir by default
ricochet opened this issue · 2 comments
Related to #17, tests that rely on any kind of test file, like expected results or test data, are unable to run with cargo wasi test
.
When running tests, we should have a sensible default like --dir=.
. We could use the same mechanism proposed in #19, but I would still want to provide a default preopen. This will make converting existing projects to Wasm and WASI as seamless as possible.
git clone https://github.com/image-rs/image-tiff.git
cd image-tiff
cargo build && cargo test # works
cargo wasi build && cargo wasi test # should also "just work"
A workaround is to run with:
CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime --dir=." cargo wasi test
I just hit this, and unless there's a better way to build filenames for test fixtures that I (a very new Rust learner) am unaware of, the solution probably needs to also ensure the CARGO_MANIFEST_DIR
env-var is passed into wasmtime.
My currently-working env-var is (PowerShell)
$Env:CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime --dir=${PWD} --env CARGO_MANIFEST_DIR=${PWD}
which makes the following work with just cargo test
:
let manfest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let fixture = path!(
Path::new(&manfest_dir) / "tests" / "fixtures" / "2024-05-30" / "BlueprintList_info.json"
);
let data = fs::read_to_string(fixture).unwrap();
Without the --env CARGO_MANIFEST_DIR
value, the call to env::var("CARGO_MANIFEST_DIR").unwrap();
panics with called ``Result::unwrap()`` on an ``Err`` value: NotPresent
.
Edit: Playing around a bit,
$Env:CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime --dir=${PWD}::/ --env CARGO_MANIFEST_DIR=/"
seems to work better, as it doesn't get confused by leading backslashes in CLI parameters using cargo wasi run
, i.e., this works with the mapping to /, but fails with my earlier version with no guest mapping.
cargo wasi run .\tests\fixtures\2024-05-30\BlueprintList_info.json
This works in both cases, but tab-completion in PowerShell or CMD will convert it to the above.
cargo wasi run ./tests\fixtures\2024-05-30\BlueprintList_info.json
Might be a good time to clarify that I'm using Windows for this, in case that affects more than just the directory-seperator.