Skip to content
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

cfg(test) is not set during doctests #4669

Closed
sunjay opened this issue Oct 27, 2017 · 3 comments
Closed

cfg(test) is not set during doctests #4669

sunjay opened this issue Oct 27, 2017 · 3 comments

Comments

@sunjay
Copy link
Member

sunjay commented Oct 27, 2017

It might be that I'm just not understanding how something works, so please let me know if that is the case!

Since doctests are tests, I expect that #[cfg(test)] would work and cfg!(test) would return true. This works during unit tests, but during doctests, both assumptions are wrong.

The reason I need this is because I have some code (not in doctests) that manages windows (like GUI windows) that I need to not run during any tests. I think this is a cargo problem because the attributes work consistently, I believe it's just cargo that doesn't set the attribute. If there is a workaround I can use, I would really appreciate it. :) (EDIT: My workaround is at the bottom.)

Here's some code that reproduces the problem: (Run with cargo test)

#[test]
fn cfg_test() {
    // This will pass, as it should
    assert!(cfg!(test))
}

/// ```rust
/// # fn main() {
/// // This will fail, even though we *are* in a test
/// assert!(cfg!(test), "cfg!(test) was false");
/// # }
/// ```
pub fn cfg_test2() {}

/// ```rust
/// # fn main() {
/// #[cfg(test)]
/// fn foo() {}
/// #[cfg(not(test))]
/// fn foo() {panic!("cfg(test) was not set")}
/// foo();
/// # }
/// ```
pub fn cfg_test3() {}

This produces the following output:

$ cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running target/debug/deps/foo-3890a4e1c4133892

running 1 test
test cfg_test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests foo

running 2 tests
test src/lib.rs - cfg_test3 (line 15) ... FAILED
test src/lib.rs - cfg_test2 (line 7) ... FAILED

failures:

---- src/lib.rs - cfg_test3 (line 15) stdout ----
	thread 'rustc' panicked at 'test executable failed:

thread 'main' panicked at 'cfg(test) was not set', src/lib.rs:6:10
note: Run with `RUST_BACKTRACE=1` for a backtrace.

', /checkout/src/librustdoc/test.rs:315:16
note: Run with `RUST_BACKTRACE=1` for a backtrace.

---- src/lib.rs - cfg_test2 (line 7) stdout ----
	thread 'rustc' panicked at 'test executable failed:

thread 'main' panicked at 'cfg!(test) was false', src/lib.rs:3:0
note: Run with `RUST_BACKTRACE=1` for a backtrace.

', /checkout/src/librustdoc/test.rs:315:16


failures:
    src/lib.rs - cfg_test2 (line 7)
    src/lib.rs - cfg_test3 (line 15)

test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out

error: test failed, to rerun pass '--doc'

In both cases, since cfg(test) is not set, the test fails.


This is a temporary workaround you can use if you are in need of this functionality:

In Cargo.toml, add the following:

[features]
# The reason we do this is because doctests don't get cfg(test)
# See: https://github.com/rust-lang/cargo/issues/4669
test = []

Then run with cargo test --features "test".

This adds a feature called "test" to tests when they run. This works for both unit tests and doctests.

Then, to test for it, use the following:

#[cfg(any(feature = "test", test))]

You could also just go with the following, since the feature is set for all kinds of tests.

#[cfg(feature = "test")]
@alexcrichton
Copy link
Member

Thanks for the report! I think that this may be an issue for rust-lang/rust, though, where rustdoc is defined?

@sunjay
Copy link
Member Author

sunjay commented Oct 28, 2017

Yes that may be a good idea! I wasn't sure and so I assumed that cargo passes in the config flags. Thanks!

@sunjay
Copy link
Member Author

sunjay commented Oct 28, 2017

This issue was moved to rust-lang/rust#45599

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants