-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
make code compiled with cfg(test) visible across crates #8379
Comments
I have a util crate that exports some mocks. Real code imports a real routine from the crate and test code imports a mock routine from the crate. However this does not work! Is there a way to implement this or do I have to stuff everything with a mock in the same crate? As work around I declare the mock in the crate that needs the mock, but then I have to duplicate this mock in every crate :( |
Did you ever come up with or find a more ergonomic solution to this? After running into this, I've been very surprised at this being a largely unresolved issue. It seems like it would be more of a common practice to, for example, provide a So far, I've seen suggestions for mock crates, or a "testing" feature-flag with #[cfg(not(testing))] -> panicking. These seem like hacks. |
While this would be nice to have, you can replicate this in a less nice way using crate features. On your utils crate, add a feature, e.g. [dev-dependencies]
my-crate = {path = "../my-crate", features = ["test-utils"]} |
Also facing this problem, thx for the "feature" workaround. This is how I used the workaround if anybody needs: 0LNetworkCommunity/libra-legacy-v6@6106c07, look for "ol-smoke-test". |
Seems like rust-lang/cargo#8379 is the issue here.
* Catch violations in times(...) expectations Adds a new quit() method to universe that causes all spawned actors to quit. It also collects actors exit codes that can be used to verify that all actors quit gracefully, which in turn allows to fail the test if mock objects expectations are not met. Relates to #2754 * Make clippy and rustfmt happy * Fix test_indexer_trigger_on_memory_limit * Update quickwit/quickwit-actors/src/actor_handle.rs Improve panic handling Co-authored-by: Paul Masurel <[email protected]> * Add universe.assert_quit() method Also adds a check to the universe.drop() to make sure that universe.assert_quit() was indeed called at the end of the test. * Stop test_ingest_api_source tests from hanging. * Fix compilation errors when testing a single crate Seems like rust-lang/cargo#8379 is the issue here. * Update assert_quits after merge * Avoid using map with side-effects. Co-authored-by: Paul Masurel <[email protected]> * Switch ActorJoinHandle to Shared * Explain why we join ingest source first * There should be only one universe. * Fix format --------- Co-authored-by: Paul Masurel <[email protected]>
I'm seriously thinking about coping with the limitation described in the issue... @oeed this work around looks nice but i think it's affected by cargo's feature unification, right? |
How to enable the feature for a integration test? |
I found a way to work around rust-lang/cargo#8379
Updated implementation of #7369 which was left out in the cold. This was motivated again following changes in #9691 and #9689 where we could not test the changes without actually deprecating or removing rules. --- Follow-up to discussion in #7210 Moves integration tests from using rules that are transitively in nursery / preview groups to dedicated test rules that only exist during development. These rules always raise violations (they do not require specific file behavior). The rules are not available in production or in the documentation. Uses features instead of `cfg(test)` for cross-crate support per rust-lang/cargo#8379
FYI, I implemented this with the quoted concern being addressed. If interested, please look for It seems Note that I recently encountered a minor problem: #10958, see extended explanation as well: #10958 (comment) In near (?) future, I'm going to publish |
I find myself creating test helpers/mocks in my test modules that are marked with
#[cfg(test)]
. They are exported aspub
and can conveniently be used throughout the crate.These helpers are not visible from other crates nor from integration tests in particular.
Making code compiled with
cfg(test)
be visible outside of the scope of a crate would allow for better code reuse. Please consider adapting that behavior.Related: rust-lang/rust#45599
The text was updated successfully, but these errors were encountered: