Hello, any idea why adding `#[cfg(test)]` to a tes...
# development
h
Hello, any idea why adding
#[cfg(test)]
to a test support function on
WorkunitStore
would not actually compile when running
./cargo test
? When I have the attribute, I get
no function or associated item named
setup_for_tests
found for struct
WorkunitStore
in the current scope.
But when I remove
#[cfg(test)]
, it works. I'm wondering if I'm missing some gotcha, I'm not finding much. https://github.com/pantsbuild/pants/pull/11436/files#diff-230c46e3890a73dee860295c27128ef3aadc239d2db0f6bcd1ac67db038385efR757
e
You get that error message in what code? I'm guessing this is an integration test issue 😕 ... in which case the reccomendation I made was bogus. See: https://doc.rust-lang.org/book/ch11-03-test-organization.html#integration-tests And please accept my apologies in advance - guessing this is it.
It looks like adding a custom cfg for this is not simple enough to be worth it either.
👍 1
h
You get that error message in what code?
When running any of the tests that call our new helper function. Those tests are colocated in the same directory so I don't think integration tests? Either way, this probably isn't worth the trouble
e
They are most definitely not in the same directory - just checked.
h
Ah, not the same directory as
workunit_store/src
..yeah, that makes sense. Good point. Thanks for figuring that out, will revert
a
Dependencies aren't compiled specially for test runs, so
#[cfg(test)]
only takes affect within a single crate
💡 1
e
😕, yeah. I guess to create a fixture shareable amongst crates but not part of the public API you need to break out a dedicated -fixtures crate that only tests ever depend upon. ... OR: I guess features could be used for this. the workunit_store crate could have a "fixtures" feature and other creates dev-dependencies include that?
If so that trick doesn't seem too bad as a general thing to do.
a
This is why we have a testutil crate or two, FWIW
Sadly features are purely additive, and leak across all configurations, so dev-dep features currently get applied to target-deps too , though I think that's being fixed soon
e
Ah - ok. If that does get fixed it seems like a clean mechanism and also allows fixtures to poke into private details which is a win over the test util crates.