-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
cargo test
does not export CARGO_CFG_TEST for build.rs scripts
#4789
Comments
This is currently intentional behavior because build scripts don't have a "I'm running tests" mode. That would require a test build script most likely and it's an unimplemented feature so far. |
As there hasn't been any activity here in over 6 months I've marked this as stale and if no further activity happens for 7 days I will close it. I'm a bot so this may be in error! If this issue should remain open, could someone (the author, a team member, or any interested party) please comment to that effect? The team would be especially grateful if such a comment included details such as:
Thank you for contributing! (The cargo team is currently evaluating the use of Stale bot, and using #6035 as the tracking issue to gather feedback.) If you're reading this comment from the distant future, fear not if this was closed automatically. If you believe it's still an issue please leave a comment and a team member can reopen this issue. Opening a new issue is also acceptable! |
As I didn't see any updates in 30 days I'm going to close this. Please see the previous comment for more information! |
@alexcrichton why would a separate test build script be needed and not just allow for |
rust-lang/cargo#4789) so always set the env variable changers
They weren't running for two reasons. 1. I was annotating them with `#[cfg(tests)]` instead of `#[cfg(test)]`. 2. Build scripts don't know about test mode: rust-lang/cargo#4789
* Fix generated tests They weren't running for two reasons. 1. I was annotating them with `#[cfg(tests)]` instead of `#[cfg(test)]`. 2. Build scripts don't know about test mode: rust-lang/cargo#4789 * Appease clippy while keeping build working on Rust 1.22
I'm not sure how this has changed over the last few years, but I don't see why build scripts would need a specific "tests mode". My use case is I have a C++ dependency that I build and link in my application, but I fake it for unit tests so there's no point in building and linking it. A workaround for this for anyone interested is using the self depends trick to enable a feature in dev-dependencies: [package]
name = "my-crate"
[features]
unittest = []
[dev-dependencies]
my-crate = { path = ".", features = ["unittest"] } You can now use |
What if my crate is doing code generation and I want to test the behavior of the generated code? It seems a reasonably logical way to do this is to generate and compile the code in the build script, but this is only needed for tests. |
e here is fairly simple – I need to build a shared library to be used for tests, however it is also important to me that this library is not built when people are compiling the library for regular use.
rustc --test --print=cfg
does output thetest
configuration variable, however whencargo test
is run, the build script does not receive this variable in form ofCARGO_CFG_TEST
.Caveat is that rerunning the build script for both test and non-test would potentially re-run expensive build.rs operations by default, however that seems easily remedied by requiring people to print out a
cargo:rerun-if-env-changed=CARGO_CFG_TEST
(it would become the first exception to the list of environment variables set by cargo and tracked by default).The text was updated successfully, but these errors were encountered: