rustrust-cargo

Pass environment variables to tests but not the cargo binary


For example, I want to run the test with MALLOC_CONF="prof:true,prof_active:false", so I could just do

LD_PRELOAD=jemalloc-5.2.1/lib/libjemalloc.so.2 MALLOC_CONF="prof:true,prof_active:false" LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib cargo test --package my_package --test my_mod test_jemalloc_prof

However, because cargo test itself does't support jemalloc heap profiling, so we could get error messages like the following while compiling

<jemalloc>: Invalid conf pair: prof_active:false
<jemalloc>: Invalid conf pair: prof:true

My question is how to avoid this, and set MALLOC_CONF only in running stage.


Solution

  • This was considered for Cargo but rejected. As a workaround, you can run the test binary directly. Cargo output its path when running cargo test. For example:

         Running unittests src/lib.rs (target/debug/deps/my_test-c11821121b337a71)