-
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
Add unit test checking to cargo check
#4592
Conversation
r? @matklad (rust_highfive has picked a reviewer for you, use r? to override) |
|
I've discovered some problems with this PR, I will be fixing them soon. |
☔ The latest upstream changes (presumably #4616) made this pull request unmergeable. Please resolve the merge conflicts. |
- Add `--profile test` flag to `cargo check` that will enable checking of unit tests. - `--tests` will implicitly enable checking of unit tests within the lib. - Don't implicitly compile binaries when using just `--test` filters. - Fix erroneously linking tests when run with `--test`. Fixes rust-lang#3431, rust-lang#4003, rust-lang#4059, rust-lang#4330.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome thanks so much and sorry for the delay!
src/bin/check.rs
Outdated
filter: ops::CompileFilter::new(options.flag_lib, | ||
&options.flag_bin, options.flag_bins, | ||
filter: ops::CompileFilter::new(options.flag_lib || options.flag_tests, | ||
&options.flag_bin, options.flag_bins || options.flag_tests, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment to why these two ||
conditions were added?
src/bin/check.rs
Outdated
@@ -106,6 +111,17 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult { | |||
&options.flag_exclude, | |||
&options.flag_package)?; | |||
|
|||
let test = options.flag_tests || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic may cause subtle oddities, although that's maybe ok for now? but for example:
cargo check --tests --bin foo
I think that'll check the binary foo
in "test mode" rather than "do you compile mode", right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. The more I think about it, it is probably wrong. check --tests
should not check bins with the test profile. I'll fix it.
src/cargo/ops/cargo_rustc/context.rs
Outdated
@@ -862,7 +862,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> { | |||
ret.extend(self.maybe_lib(unit)); | |||
|
|||
// Integration tests/benchmarks require binaries to be built | |||
if unit.profile.test && | |||
if unit.profile.test && !unit.profile.check && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you expand the above comment to explain this negation?
OK, I have updated it with a different approach. After thinking about it more, I think people expect I also updated Also, I have so far been ignoring doctests. Since I believe that will rely on rustdoc, it might be outside of the domain of |
Gah sorry for the delay! In any case that all sounds great to me, thanks so much! @bors: r+ |
📌 Commit efc0dd1 has been approved by |
Add unit test checking to `cargo check` This is an extension of PR #4039, fixing #3431, #4003, #4059, #4330. The fixes for #4059 can potentially be separated into a separate PR, though there may be some overlap. The general gist of the changes: - Add `--profile test` flag to `cargo check` that will enable checking of unit tests. - `--tests` will implicitly enable checking of unit tests within the lib (checks the same targets as `cargo test`). This affects the `check`, `test`, and `build` commands. - `--benches` behaves similarly by using the same targets as `cargo bench`. - Fix erroneously linking tests when run with `--test`. There is one thing I did not do because I wanted more feedback on what others think the expected behavior should be. What should the behavior of `--all-targets` be? This patch does not (yet) make any changes to its behavior. My initial thinking is that it should *add* a target of `--lib --bins --profile test`, but that essentially means the lib and bin targets will be checked twice (and thus any errors/warnings outside of `#[cfg(test)]` will appear twice, which may be confusing, and generally take twice as long to run). I can add that, but I think it would require adding a new `All` variant to `CompileFilter` so that the code in `generate_targets` can detect this scenario. I wanted feedback before making a more extensive change like that. The downside of not adding it is that `--all-targets` will ignore unit tests (if you don't specify `--profile test`). Summary of the profiles used with this patch: Command | Lib | Bin foo | Test t1 | Example e1 | Bench b1 | ------- | --- | ------- | ------- | ---------- | -------- | `check` | check | check | - | - | - | `check --profile test` | check_test† | check_test† | - | - | - | `check --lib` | check | - | - | - | - | `check --lib --profile test` | check_test† | - | - | - | - | `check --bin foo` | check | check | - | - | - | `check -–bin foo –profile test` | check_test† | check_test† | - | - | - | `check --bins` | check | check | - | - | - | `check --test t1` | check | check | check_test | - | - | `check --tests` | check, check_test† | check, check_test† | check_test | check†, check_test† | - | `check --all-targets` | check, check_test† | check, check_test† | check_test | check, check_test† | check_test | † = different behavior from today
☀️ Test successful - status-appveyor, status-travis |
Thanks @alexcrichton, I know you are super busy (I'm still not convinced you are just one human). I had some questions:
I assume this will get picked up in nightly the next time Cargo is updated in the main rust repo for people to test. I feel like this is a subtle, but significant change in behavior. Hopefully most people will be happy with the changes. |
Certainly! There's a PR upstream for the 1.22 release (which this won't be included in), but you can start a new section for the 1.23 release which would be awesome!
If you're willing, that'd be awesome! If not, feel free to just leave a comment saying it should be closed now.
Indeed! If you'd like to accelerate that, feel free to send a PR yourself to rust-lang/rust :) |
It looks like @Aaronepower has been writing the release notes. I'll just leave this here for when he gets started on 1.23, and let him format/wordsmith as desired.
|
@ehuss how does |
Sorry, no, this was a case of asking before trying. Seems to work as expected! It just wasn't documented anywhere that these work together, though I guess there isn't a good reason why they shouldn't. I suspect it would have helped if the docs somewhere said that selecting target crates and selecting profiles are orthogonal actions, but meh. |
I've seen that target and profile selection is a common source of confusion. Along with improving documentation, I wonder if it might be helpful to display each target/profile being built when -v is used (trying to understand that from the rustc command-line isn't exactly easy). |
This is an extension of PR #4039, fixing #3431, #4003, #4059, #4330. The fixes for #4059 can potentially be separated into a separate PR, though there may be some overlap.
The general gist of the changes:
--profile test
flag tocargo check
that will enable checking of unit tests.--tests
will implicitly enable checking of unit tests within the lib (checks the same targets ascargo test
). This affects thecheck
,test
, andbuild
commands.--benches
behaves similarly by using the same targets ascargo bench
.--test
.There is one thing I did not do because I wanted more feedback on what others think the expected behavior should be. What should the behavior of
--all-targets
be? This patch does not (yet) make any changes to its behavior. My initial thinking is that it should add a target of--lib --bins --profile test
, but that essentially means the lib and bin targets will be checked twice (and thus any errors/warnings outside of#[cfg(test)]
will appear twice, which may be confusing, and generally take twice as long to run). I can add that, but I think it would require adding a newAll
variant toCompileFilter
so that the code ingenerate_targets
can detect this scenario. I wanted feedback before making a more extensive change like that. The downside of not adding it is that--all-targets
will ignore unit tests (if you don't specify--profile test
).Summary of the profiles used with this patch:
check
check --profile test
check --lib
check --lib --profile test
check --bin foo
check -–bin foo –profile test
check --bins
check --test t1
check --tests
check --all-targets
† = different behavior from today