feat: add include_str and include_bytes file input behaviour#297
feat: add include_str and include_bytes file input behaviour#297la10736 merged 1 commit intola10736:masterfrom
include_str and include_bytes file input behaviour#297Conversation
la10736
left a comment
There was a problem hiding this comment.
Great work!
Just some notes:
- I prefer a simple syntax like
#[mode = (path|str|bytes)]: no justmode, no"that lead to think to something like free text, and smaller names - I would not ignore typo error and replace it with default mode, I prefer to give a clear error with the correct span
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub(crate) enum FilesMode { | ||
| Path, | ||
| IncludeStr, | ||
| IncludeBytes, | ||
| } | ||
|
|
||
| impl Default for FilesMode { | ||
| fn default() -> Self { | ||
| Self::Path | ||
| } | ||
| } |
There was a problem hiding this comment.
| #[derive(Debug, Clone, PartialEq, Eq)] | |
| pub(crate) enum FilesMode { | |
| Path, | |
| IncludeStr, | |
| IncludeBytes, | |
| } | |
| impl Default for FilesMode { | |
| fn default() -> Self { | |
| Self::Path | |
| } | |
| } | |
| #[derive(Debug, Clone, PartialEq, Eq)] | |
| pub(crate) enum FilesMode { | |
| #[default] | |
| Path, | |
| IncludeStr, | |
| IncludeBytes, | |
| } |
la10736
left a comment
There was a problem hiding this comment.
Just to add some other notes...
Please don't forget to add a changelog line too.
| LitStrAttr::parse_meta_name_value(&attr).map_err(|_| { | ||
| attr.error( | ||
| "Use #[file_mode = \"...\"] to define the argument of the file input", | ||
| ) | ||
| }) |
There was a problem hiding this comment.
Here you can try to parse directly a FilesMode (should implement a Parse trait) that get an Ident and try to match it whit the expected varaints.
| .push(attr.error(r#"Cannot use #[file_mode = "..."] more than once"#)) | ||
| }); | ||
| FilesMode::from_str(&value.value()) | ||
| .unwrap_or(Default::default()) |
There was a problem hiding this comment.
That's is quite awful... if the user wrote a typo I would notice to him
There was a problem hiding this comment.
Thanks... I really appreciate your attention to test.
Can you please add also some test that check a correct error message when we use a wrong mode: you can add a case to the test module that check error messages for the other cases.
6ee9dd1 to
a3e7fba
Compare
|
Thank you for the feedback. I think I addressed all of your comments in a3e7fba. |
a3e7fba to
3c4701e
Compare
la10736
left a comment
There was a problem hiding this comment.
Great, thanks again. Let me know what you think about my comment on println! on the build script.
rstest/src/lib.rs
Outdated
| /// Note on `println!("cargo::rerun-if-changed=tests/resources");`: | ||
| /// All files that get embedded by [include_str] or [include_bytes] will be checked for changes | ||
| /// when recompiling. So the `println` is not strictly needed when all necessary files are included | ||
| /// with `#[mode = str]` or `#[mode = bytes]`. |
There was a problem hiding this comment.
I think that's useless. println!("cargo::rerun-if-changed=tests/resources"); is not used to force recompile when the file body changes, but when something change in the folder tests/resources and recompilation should occur to add or remove tests. println!("cargo::rerun-if-changed=tests/resources"); still need if you would like that cargo test catch if you add or remove some test cases by add or remove files in this folder.
There was a problem hiding this comment.
Ah right you still need the println! for when files get added or removed even when using include_str or include_bytes. Then the note is not correct. I'll remove it.
Adds the ability to set the file input format using the `#[file_mode = "..."]` attribute. The attribute accepts `path`, `include_str`, and `include_bytes`. `path` is the default behaviour, `include_str` and `include_bytes` pass the file contents instead of the path of the file. Closes la10736#295.
3c4701e to
4cafedb
Compare
|
Does the failing builds have anything to do with this PR? |
| /// fn for_each_path( | ||
| /// #[files("src/**/*.rs")] #[exclude("test")] #[mode = path] path: PathBuf | ||
| /// ) { | ||
| /// assert!(contents.len() >= 0) |
There was a problem hiding this comment.
What is contents here ?
We have path, but no contents in scope.
It looks like a copy/paste error
There was a problem hiding this comment.
it seems like I made a copy and paste error. But I don't understand how this doesn't make the doc tests fail.
There was a problem hiding this comment.
Maybe because the doc test with rstest annotation just render the cases and mark the original function with #[cfg(test)]. The doc test doesn't compile with the test attribute, so if you did some mistakes in the function body they'll be ignored
There was a problem hiding this comment.
Can you open a PR with the fix please?
There was a problem hiding this comment.
Maybe because the doc test with
rstestannotation just render the cases and mark the original function with#[cfg(test)]. The doc test doesn't compile with thetestattribute, so if you did some mistakes in the function body they'll be ignored
This is unfortunate. I commented out these #[cfg(test)] with this patch and found that the doc example in this thread and this other doc example do not compile.
Maybe it is worth to spend some time trying to make it so these functions do get compiled? A quick (but also dirty) method would be to replace the #[cfg(test)] with a #[cfg(any(test, docttest))].
diff --git i/rstest_macros/src/render/mod.rs w/rstest_macros/src/render/mod.rs
index 2a08918..80157e2 100644
--- i/rstest_macros/src/render/mod.rs
+++ w/rstest_macros/src/render/mod.rs
@@ -437,10 +437,10 @@ fn test_group(mut test: ItemFn, rendered_cases: TokenStream) -> TokenStream {
test.attrs = vec![];
quote! {
- #[cfg(test)]
+ // #[cfg(test)]
#test
- #[cfg(test)]
+ // #[cfg(test)]
mod #fname {
use super::*;Can you open a PR with the fix please?
I opened #301.
There was a problem hiding this comment.
Nice catch, I'll try to book some time in the launch block to do it. Thanks
There was a problem hiding this comment.
Sadly I cannot find a way to do it. Also the quick and dirty solution still not works. It's a well know issue https://users.rust-lang.org/t/is-the-doctest-compiler-flag-broken-or-is-my-understanding/86477 rust-lang/rust#67295
Adds the ability to set the file input format using the
#[file_mode = "..."]attribute. The attribute acceptspath,include_str, andinclude_bytes.pathis the default behaviour,include_strandinclude_bytespass the file contents instead of the path of the file.Closes #295.
In the original issue I suggested using a
#[embed]attribute but instead I decided to use the namesinclude_strandinclude_bytesso that it is easier to find.I'm not sure if
file_modeis a good name for this.