One of many test modules gets treated as doc tests, then inexplicably doesn't compile


#1

Hi, I’m having some weird behavior running my tests. I feel like I’m missing something obvious, but I can’t find what. I have a large project, and it compiles just fine.

I have one file containing some code, and a test module for that code. Here’s the subsection of the file:

#[cfg(test)]
pub mod tests {
    use super::*;

    #[test]
    pub fn fat_ptr_large_test() {
        let mut arr = [0u8; 100];
        for i in 0..100 {
            arr[i] = i as u8;
        }
        let direct: Direct<[u8; 100], [u8; 20]> = Direct::new(arr.clone());
        for i in 0..100 {
            assert_eq!(arr[i], direct[i]);
        }
    }

    #[test]
    pub fn fat_ptr_small_test() {
        let mut arr = [0u8; 20];
        for i in 0..20 {
            arr[i] = i as u8;
        }
        let direct: Direct<[u8; 20], [u8; 100]> = Direct::new(arr.clone());
        for i in 0..20 {
            assert_eq!(arr[i], direct[i]);
        }
    }
}

Now when I try to test it with cargo test -p core -- --test-threads=1 (via IntelliJ IDEA),

  1. All of my other tests run and pass
  2. It appears to try to treat these tests a doc-tests
  3. They fail to compile

Take a look at the end of the output:

test threading::threadpool::tests::dim_helper ... ok
test threading::threadpool::tests::helper_channel ... ok

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

   Doc-tests core
error[E0432]: unresolved import `core::array`
 --> C:\Users\kahlo\Desktop\Rust\rustcraft\core\src\collections\direct.rs:2:11
  |
2 | use core::array::FixedSizeArray;
  |           ^^^^^ Could not find `array` in `core`

error[E0432]: unresolved import `core::marker`
 --> C:\Users\kahlo\Desktop\Rust\rustcraft\core\src\collections\direct.rs:3:11
  |
3 | use core::marker::Unsize;
  |           ^^^^^^ Could not find `marker` in `core`

error[E0432]: unresolved import `core::ops`
 --> C:\Users\kahlo\Desktop\Rust\rustcraft\core\src\collections\direct.rs:4:11
  |
4 | use core::ops::CoerceUnsized;
  |           ^^^ Could not find `ops` in `core`


running 0 tests

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


Process finished with exit code 0

I am running on the nightly channel (I am using nightly-only features) and I literally just updated with rustup.

Any suggestions? Thanks.


#2

I can’t be sure, but it looks like the imports referenced by the error log don’t even occur in your test module.
So perhaps it’s another module altogether that is getting executed as a set of doc tests?

Also keep in mind that something like use super::* will only import items from the parent module, and nowhere else.


#3

The test module is a direct child of the module it’s testing. It use super::*; to import the code it’s testing, and the compiler errors are occurring in the module it’s testing. However, when I compile normally, those errors do not occur.

It may be relevant that, to get this to compile, I need to be on the nightly channel, and using several feature imports such as fixed_size_array, unsize, and coerce_unsized.

Here is the whole file, including both modules, if that makes this more clear: https://pastebin.com/gBEQa5gL


#4

It’s not the test module that errors out, but the module that defines Direct; the crucial hint for this is in the line numbers, which are right on top of the file, and not in the middle somewhere.

But I’m not sure why it errors out there; it could have something to do with not providing some necessary feature flag (the FixedSizeArray trait is unstable, as I’m sure you know) or perhaps you’re using a build farm that tries to build using stable.

What happens when you execute cargo +nightly test -p core -- --test-threads=1? (note the +nightly in the command).


#5

Yep, I get the exact same output when I run the command directly from the shell.