-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Improve doc cfg(test) and tests directory #38823
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @steveklabnik (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
I don't like very much the way you worded it. :-/ |
@GuillaumeGomez right. What do you think:
|
I do not like this addition. The section already mentions:
Maybe we should just stress a bit more that this means that the main crate is not compiled in test mode? |
Something like:
cc @badboy |
Hum, I'm shared on this. :-/ cc @rust-lang/docs |
I'm not a fan of the proposed wording for this, but I can't think of anything better to suggest right now. I'll think about this... |
Here's how I'd word this:
WDTY @rust-lang/docs ? |
@steveklabnik: I don't like your wording either. Too confusing. |
Could you elaborate on what specifically is confusing? |
Sure!
I think a better wording would be:
|
I like that too. @Freyskeyd what do you think? |
That's nice, what do you think of:
|
@Freyskeyd The underlying issue here is not that a certain directory exists, but that when running integration tests, the library crate being tested will not be compiled with Regarding @steveklabnik's suggestion:
I like this part.
I like the direction of this: indicating the library is being built in the same manner as a consumer might build it, but I'm not much a fan of the wording. I've read it through a dozen times and still don't really get it. I might be too sleepy right now though 😴 . This is clearer to me, albeit maybe a bit wordy:
|
@frewsxcv: your sentence has the same issue:
This is how it works, but it'd be great to include why, like I did when I proposed a modified version of what @steveklabnik suggested. |
@GuillaumeGomez what do you think:
|
@Freyskeyd: Great! 👍 |
be64366
to
eeae310
Compare
@@ -499,6 +499,10 @@ be imported in every test with `mod common;` | |||
That's all there is to the `tests` directory. The `tests` module isn't needed | |||
here, since the whole thing is focused on tests. | |||
|
|||
Note, when building integration tests, cargo will not pass the test flag to the | |||
compiler. It means that all parts in cfg(test) won't be included in the build |
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.
Nit:
`cfg(test)`
@@ -499,6 +499,10 @@ be imported in every test with `mod common;` | |||
That's all there is to the `tests` directory. The `tests` module isn't needed | |||
here, since the whole thing is focused on tests. | |||
|
|||
Note, when building integration tests, cargo will not pass the test flag to the |
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.
Nit:
cargo will not pass the `test` attribute
https://doc.rust-lang.org/book/testing.html#the-test-attribute
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.
@frewsxcv you want me to add a link to this page or add single quote around test
?
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.
Adding the grave accents and calling it an "attribute" instead of a "flag", since TRPL calls it an "attribute".
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.
The reference also calls them "attributes":
https://doc.rust-lang.org/reference.html#function-only-attributes
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.
yes, but in conditional-compilation it's a flag:
test - Enabled when compiling the test harness (using the --test flag).
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.
That's referring to --test
, which is a command line argument, also called a "flag"
https://en.wikipedia.org/wiki/Command-line_interface#Command-line_option
Signed-off-by: Freyskeyd <[email protected]>
eeae310
to
b996a7e
Compare
@steveklabnik What do you think ? :) |
Looks fine to me, let's @bors: r+ rollup |
📌 Commit b996a7e has been approved by |
…Test, r=steveklabnik Improve doc cfg(test) and tests directory Hi, I was facing a problem with my code organisation. I was using a tests directory and i defined some `#[cfg(test)]` in my `src/`. But i was not able to use it in my `tests` folder. ```bash . ├── Cargo.lock ├── Cargo.toml ├── src │ ├── lib.rs │ └── test.rs └── tests └── x.rs ``` > src/lib.rs ```rust pub mod test; fn tesst() { assert!(test::t()); } ``` > src/test.rs ```rust pub fn t() -> bool { true } ``` > test/x.rs ```rust extern crate testt; use testt::test; fn tesst() { assert!(test::t()); } ``` I was unable to compile using `cargo test`: ```bash error[E0432]: unresolved import `testt::test` --> tests/x.rs:3:5 | 3 | use testt::test; | ^^^^^^^^^^^ no `test` in `testt` ``` If i remove the `tests` directory everything works fine. To use an utils module in your `tests` directory, you need to create a module in the directory (like `tests/utils.rs`). My `tests/x.rs` look like this now: ```rust extern crate testt; mod utils; fn tesst() { assert!(utils::t()); } ``` And my tree: ```bash . ├── Cargo.lock ├── Cargo.toml ├── src │ └── lib.rs └── tests ├── utils.rs └── x.rs ``` I think that thing must be documented in the book. Ping: - @badboy : Because he's the one who showed me the path - @shahn : Because he helped me too to find the solution Signed-off-by: Freyskeyd <[email protected]>
Hi,
I was facing a problem with my code organisation. I was using a tests directory and i defined some
#[cfg(test)]
in mysrc/
. But i was not able to use it in mytests
folder.. ├── Cargo.lock ├── Cargo.toml ├── src │ ├── lib.rs │ └── test.rs └── tests └── x.rs
I was unable to compile using
cargo test
:If i remove the
tests
directory everything works fine. To use an utils module in yourtests
directory, you need to create a module in the directory (liketests/utils.rs
). Mytests/x.rs
look like this now:And my tree:
. ├── Cargo.lock ├── Cargo.toml ├── src │ └── lib.rs └── tests ├── utils.rs └── x.rs
I think that thing must be documented in the book.
Ping:
Signed-off-by: Freyskeyd [email protected]