I expect to reference A's test tool code in B, but that doesn't seem to work, and the test tag won't be passed to A
crate when cargo test is performed.
crate a:
#[cfg!(test)]
pub mod test_utils {
use std::path::PathBuf;
pub fn test_dir() -> PathBuf {
todo!()
}
}
crate b
#[test]
fn test_xxx() {
let dir = a::test_utils::test_dir();
todo!()
}
So I started using for-test feature instead of the test tag, but this has another problem. Suppose A implements the for-test feature, B can make it take effect in dev-dependencies, but A cannot make the for-test feature take effect in tests.
crate B can use [dev-dependencies]
[dev-dependencies]
A = { workspace = true, features = ["for-test"] }
crate A can't
Cargo.toml
[features]
for-test = []
src
test_util.rs // cfg for-test
tests
xxx_test.rs // Unable to use test_util
jofas
April 4, 2023, 8:51am
3
Would it be possible for you to abstract the test utility functionality you share across workspace members into its own workspace member? Like create a new crate C
, put test_dir
in it and add C
as dev-dependency to A
and B
?
My usage scenario does not allow abstracting a C, because it involves the code of B. In actual code, I've put what I can abstract into C (test-utils).
jofas
April 6, 2023, 10:46am
5
I think I found a workaround for this:
opened 07:46AM - 24 Jul 16 UTC
A-features
Given a library crate, I want to have optional features which are available to i… ntegration-level tests and examples so that 'cargo test' tests them.
Here's what I have:
```
[package]
name = "rustls"
version = "0.1.0"
(etc)
[features]
default = []
clientauth = []
[dependencies]
untrusted = "0.2.0"
(etc)
[dev-dependencies]
env_logger = "0.3.3"
(etc)
```
Here's what I've tried so far. Attempt one: add the crate into dev-dependencies with the feature, ie:
```
[dev-dependencies.rustls]
path = "."
version = "0.1.0"
features = ["clientauth"]
```
I kind of expected that to work, but it panics:
```
jbp@debian:~/rustls$ RUST_BACKTRACE=1 cargo test
thread '<main>' panicked at 'assertion failed: my_dependencies.insert(dep.clone())', src/cargo/util/dependency_queue.rs:86
stack backtrace:
1: 0x558d17c3a2a0 - std::sys::backtrace::tracing::imp::write::h9fb600083204ae7f
2: 0x558d17c4383b - std::panicking::default_hook::_$u7b$$u7b$closure$u7d$$u7d$::hca543c34f11229ac
3: 0x558d17c434c3 - std::panicking::default_hook::hc2c969e7453d080c
4: 0x558d17c28048 - std::panicking::rust_panic_with_hook::hfe203e3083c2b544
5: 0x558d17701fef - std::panicking::begin_panic::h4ebf9fe884b2415f
6: 0x558d17802d27 - cargo::ops::cargo_rustc::job_queue::JobQueue::enqueue::h98b20a33b8744ebe
7: 0x558d177f4faf - cargo::ops::cargo_rustc::compile::h93336dd21aba29d0
8: 0x558d1777a513 - cargo::ops::cargo_rustc::compile_targets::h7223dffae01d6b9d
9: 0x558d1776eec9 - cargo::ops::cargo_compile::compile_pkg::h48656ffa9b1541f3
10: 0x558d1776bd09 - cargo::ops::cargo_compile::compile::h1b43b20047c53d10
11: 0x558d1785fb0d - cargo::ops::cargo_test::compile_tests::h028a22d3131e0d65
12: 0x558d1785f379 - cargo::ops::cargo_test::run_tests::h0ad98c54a6f40c9d
13: 0x558d176de256 - cargo::call_main_without_stdin::hd484f08b17c419d1
14: 0x558d1768afe7 - cargo::execute::hab7accd6bf9b5c64
15: 0x558d17683f8b - cargo::call_main_without_stdin::hf099bd36acb849a3
16: 0x558d17680da1 - cargo::main::h2b219a79f378c1ef
17: 0x558d17c430d8 - std::panicking::try::call::hc5e1f5b484ec7f0e
18: 0x558d17c4e0eb - __rust_try
19: 0x558d17c4e08e - __rust_maybe_catch_panic
20: 0x558d17c42b03 - std::rt::lang_start::h61f4934e780b4dfc
21: 0x7f79ea9bf86f - __libc_start_main
22: 0x558d17680728 - <unknown>
```
(version: "cargo 0.11.0-nightly (259324c 2016-05-20)")
Attempt two: see if `features.*` works like `profile.*`:
```
[features.test]
default = ["clientauth"]
```
alas
```
$ cargo test
error: failed to parse manifest at `/home/jbp/rustls/Cargo.toml`
Caused by:
expected a value of type `array`, but found a value of type `table` for the key `features.test`
```
Is this possible?
(found the link in this SO answer: unit testing - Is it possible to enable a rust feature only in test? - Stack Overflow )
The workaround requires you to add the following to the Cargo.toml
of your A
crate:
[dev-dependencies]
A = { path = ".", features = ["for-test"] }
which should allow you to simply run cargo test
in A
where A
has the for-test
feature enabled.
Alternatively you can run cargo test --features for-test
in A
to enable the desired optional feature during testing.
jofas:
--features for-test
Yes, but I don't want to do it through --features for-test, because I think it's weird, and I'd prefer to do it through cargo test.
tools and infrastructure
cargo
If you want to enable the feature of a dependency while testing, you can place the feature in the dev-dependencies table, like this: [dependencies] some-dependency = { path = "some-dependency" } [dev-dependencies] some-dependency = { path =...