-
Notifications
You must be signed in to change notification settings - Fork 2.6k
target.cfg doesn't support features #14306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thank you for your detailed reproduction!
This seems to be a duplicate of #8170. Close in favor of that. Let us know if there is any reason to keep it separately. |
@weihanglo Ah, I missed that part of the docs. Sorry about that. However, the docs say "values set by build scripts", but adding the following to if cfg!(feature = "_test_as_root") {
println!("cargo::rustc-cfg=_test_as_root")
} #8170 mentions that this is because the config is "once per cargo run". In that case, should the line about build scripts be removed from the docs? How else could you use a build script to modify the In fact, even something like
Doesn't work, which it seems like it should given the description in the docs. |
For For |
Looks like a mistake to me. |
This was a mistake, see rust-lang#14306
This was a mistake, see rust-lang#14306
This was a mistake, see rust-lang#14306
This was a mistake, see rust-lang#14306
Problem
I am working on a library which requires some tests to be run as root. However, only some of the tests actually need root and so I designed the test suite such that the tests that require root are only run when you actually have root. It seems the most rust-like way of doing this (given how restrictive libtest is) is to create a dummy feature
_test_as_root
and then do:(The alternative is to make the test always pass if it is running as root, but this is less than ideal
Given that
sudo cargo
doesn't work in general, and is quite painful to get working, it would be ideal if I could just do this in CI:Using the suggestion from #5999 (comment), it seems like setting a
runner
withtarget.'cfg(feature = "_test_as_root")
would work. However:If you use
target.x86_64-unknown-linux-gnu
it works. It seem that thecfg(feature=...)
is never matched? Even if you changerunner
tofalse
or some non-existent command the tests still run as if the setting wasn't there.The docs states that the format of
target.'cfg(...)'
matches the conditional compilation spec (which includes reference tocfg(feature = ...)
so it seems like this should work.Steps
Run this script:
Then note that:
cargo test
succeeds andtest_only_as_root
is ignored.cargo test --features _test_as_root
fails because the.cargo/config.toml
had no effect.Possible Solution(s)
Parse
[target.'cfg(feature = ...)']
configurations? I don't know if there is some technical restriction that stopsfeature
from being understood bycargo
when parsing.cargo/config.toml
.Notes
In my particular case, using #5999 (comment) this issue can be worked around by using environment variables like so:
But this is fairly ugly and unlike
[target.'cfg(feature = "_test_as_root")']
the feature and test runner are now decoupled and so the test will no longer run as root on different targets.Version
The text was updated successfully, but these errors were encountered: