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

Guide new users to add use super::*; to mod test #10706

Merged
merged 5 commits into from
May 29, 2022

Conversation

merelymyself
Copy link
Contributor

What does this PR try to resolve?

Currently, cargo init --lib produces examples for unit tests. However, new users will find that they are unable to use functions they defined above. This should resolve the confusion.

Fixes #10559

How should we test and review this PR?

This PR does not create new features. Test this PR using the already-existing tests.

Additional information

I didn't think this was a major change, so I did not open a RFC for it. Please let me know if I should have!

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @ehuss (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 27, 2022
@Muscraft
Copy link
Member

Thinking about this some more, I think adding something for users to uncomment only solves a symptom of the issue and not the issue itself. I think the main issue is the error message from rustc (which appears to be generated in this function). It might be a better idea (and a harder change), to update the error message to include use super::*;.

Something like:

error[E0425]: cannot find function `add_numbers` in this scope
 --> src/lib.rs:9:22
   |
9  |         let result = add_numbers(2, 2);
   |                      ^^^^^^^^^^^ not found in this scope
   |
help: consider importing one of these items
   |
LL | use super::*;
   |
LL | use crate::add_numbers;
   |

I mocked this from here

I think one of the maintainers may have more insight than I do on the route to go about this

src/cargo/ops/cargo_new.rs Outdated Show resolved Hide resolved
@merelymyself
Copy link
Contributor Author

@epage - followed your second suggestion, thought it was the clearest. Thank you!

src/cargo/ops/cargo_new.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@epage epage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm fine with this. The new template does not make a material difference in the boiletplate a user has to delete while it better teaches a user how the existing boilerplate would work.

I'm assuming this is small enough not to need an FCP but if anyone from the cargo team disagrees, we can always revert it and go through an FCP

@epage
Copy link
Contributor

epage commented May 29, 2022

@bors r+

@bors
Copy link
Contributor

bors commented May 29, 2022

📌 Commit 754a5bf has been approved by epage

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 29, 2022
@bors
Copy link
Contributor

bors commented May 29, 2022

⌛ Testing commit 754a5bf with merge ca4edab...

@bors
Copy link
Contributor

bors commented May 29, 2022

☀️ Test successful - checks-actions
Approved by: epage
Pushing ca4edab to master...

@bors bors merged commit ca4edab into rust-lang:master May 29, 2022
sunfishcode added a commit to sunfishcode/book that referenced this pull request May 31, 2022
rust-lang/cargo#10706 switched the `cargo init --lib`-generated src/lib.rs
to use a function and `use super::*;` inside the `mod test`. This makes
it easier for new users to write their own functions and add tests, as it
means the tests can refer to the new functions without any extra work, and
without rustc asking them to add explicit `use`s for each new thing they add.

This PR updates the parts of the book that use this src/lib.rs and
similar examples, to match the new output of `cargo init --lib`, and to
additionally help guide users to using `use super::*;` inside their
`mod test`s.

There is one non-example change, which is to update the wording in
src/ch11-01-writing-tests.md to better reflect the new content in the
associated example.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 1, 2022
Update cargo

5 commits in 39ad1039d9e3e1746177bf5d134af4c164f95528..38472bc19f2f76e245eba54a6e97ee6821b3c1db
2022-05-25 00:50:02 +0000 to 2022-05-31 02:03:24 +0000
- Emit warning upon encountering multiple packages with the same name (rust-lang/cargo#10701)
- Guide new users to add use `super::*;` to `mod test` (rust-lang/cargo#10706)
- Document how to debug change detection events (rust-lang/cargo#10708)
- fix(publish): add more check when use `publish -p <SPEC>` (rust-lang/cargo#10677)
- fix key formatting when switching to a dotted `WorkspaceSource` (rust-lang/cargo#10705)
@ehuss ehuss added this to the 1.63.0 milestone Jun 22, 2022
sunfishcode added a commit to sunfishcode/book that referenced this pull request Nov 11, 2022
rust-lang/cargo#10706 switched the `cargo init --lib`-generated src/lib.rs
to use a function and `use super::*;` inside the `mod test`. This makes
it easier for new users to write their own functions and add tests, as it
means the tests can refer to the new functions without any extra work, and
without rustc asking them to add explicit `use`s for each new thing they add.

This PR updates the parts of the book that use this src/lib.rs and
similar examples, to match the new output of `cargo init --lib`, and to
additionally help guide users to using `use super::*;` inside their
`mod test`s.

There is one non-example change, which is to update the wording in
src/ch11-01-writing-tests.md to better reflect the new content in the
associated example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Guide new users to add use super::*; to mod test.
6 participants