Stream: wasmtime

Topic: test runner


view this post on Zulip matt rice (Mar 24 2025 at 19:12):

I've been using wasmtime as a test runner by setting in .cargo/config.toml,
Sorry if this is all written down somewhere, I've found it difficult to search for.

runner = "wasmtime run --dir ."

This has generally worked fine until now where I have added a test that reads some stuff in $OUT_DIR and $CARGO_MANIFEST_DIR/../examples.

I was mainly curious if anyone has written a test runner for cargo that works out all the right environment variables for a workspace and adds them as --dir args, afaict don't think cargo supports env var usage in the runners line.

It seems likely this might be something someone has already done though, so I wanted to ask if there was already a wrapper for doing this.

view this post on Zulip Alex Crichton (Mar 24 2025 at 19:46):

Since .cargo/config.toml is local to a project, or at least can be, could you do that and add the --dir options you'd like?

view this post on Zulip matt rice (Mar 24 2025 at 19:51):

It is currently committed to the repo, so I can't seem to just set it to an absolute path, or know the right $OUT_DIR, I think ideally, instead of manifest_dir/.. It's best to add the whole workspace, not sure if there is an environment variable for that (I don't think CARGO_WORKSPACE_DIR got beyond discussion?)

view this post on Zulip Alex Crichton (Mar 24 2025 at 19:54):

ah yeah that I don't think can be done right now

view this post on Zulip matt rice (Mar 24 2025 at 19:55):

Anyhow I'll probably just look for the top-level cargo.toml from the manifest_dir/.. via negative**

view this post on Zulip matt rice (Mar 24 2025 at 19:55):

and write a hacky script

view this post on Zulip matt rice (Mar 24 2025 at 20:51):

well, that almost works but unfortunately it seems like OUT_DIR isn't actually set when running the test runner, thereby indirectly disabling the test :cry:

view this post on Zulip matt rice (Mar 24 2025 at 21:31):

That itself is a bit weird though because I see the variable set for non-wasi targets... but that like a normal cargo issue...

view this post on Zulip matt rice (Mar 24 2025 at 21:32):

If I get something working, I'll update this...

view this post on Zulip matt rice (Mar 24 2025 at 21:35):

But until then I may just end up cfg out this test on wasi.

view this post on Zulip matt rice (Mar 24 2025 at 22:09):

matt rice said:

That itself is a bit weird though because I see the variable set for non-wasi targets... but that like a normal cargo issue...

oh... it just depends upon whether the crate running the test has a build.rsfile or not whether the runner runs with that environment variable set...

view this post on Zulip matt rice (Mar 25 2025 at 18:09):

So here is a pretty terrible hack of what I managed to get working with my tests... https://github.com/ratmice/workspace_time_test_runner

A hacked up wasmtime test runner that adds `--dir` flags for the rest of your workspace, and `--env` flags for cargo variables - ratmice/workspace_time_test_runner

view this post on Zulip matt rice (Mar 25 2025 at 18:12):

Dont try and runner = "cargo run -p workspace_time_test_runner" because that overwrites a bunch of environment variables on the nested cargo invocations (who would try that?)

view this post on Zulip matt rice (Mar 25 2025 at 18:13):

Anyhow it is probably pretty fragile, and the workspace detection is bad/regretable, but it is a start...

view this post on Zulip matt rice (Mar 25 2025 at 22:11):

Oh it occurred to me that I can get rid of the worst of the hacks by using cargo metadata workspace_root field


Last updated: Apr 07 2025 at 18:04 UTC