Skip to content

Commit

Permalink
Values_from_files: Implements #163 (#199)
Browse files Browse the repository at this point in the history
* Moved extract files in RsTestData

* Base implementation without test

* MVP completed and test example in playground

* Make values creation more testable

* Revert "Implemented arguments's files parsing"

This reverts commit 6a197fd.

* Update rstest requirement from 0.16.0 to 0.17.0 (#192)

Updates the requirements on [rstest](https://github.com/la10736/rstest) to permit the latest version.
- [Release notes](https://github.com/la10736/rstest/releases)
- [Changelog](https://github.com/la10736/rstest/blob/master/CHANGELOG.md)
- [Commits](0.16.0...0.17.0)

---
updated-dependencies:
- dependency-name: rstest
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Fix #189

* Call visit_item_fn_mut in a custom
visit_item_fn_mut can cause wired behavior
because it will also visit inner functions.
A clean solution to #189 is to take the responsibility
of the two extraction separated.
Also we removed the inner visit_item_fn_mut for
all others extractions because are useless and can cause
also wired bugs

* Update changelog

* Prepare new version

* Make values creation more testable

* Missed mod files in rebase

* Fix imports

* Refactor for testing

* Coverage all errors

* Introduce exclude parsing

* Implemented filter

* fix README.md (#194)

(cherry picked from commit 4378e0e)

* Fixed tests for last rust version

* Use relative path to make
regex cross platform

* Introduce logic to accept
or reject dot files

* Implement sort and multiple
glob (missed parsing)

* Implemented support for more exclude

* Implement parsing new syntax
(include_dot_files and multiple files and exclude attributes).
Tests about new syntax and error.
Missed:
- error should point attributes and not arguments
- Docs
- Refactoring (clean code)
- Test E2E
- Change log

* Now error point to attributes

* Refactor

* Refactor

* Add licenses (#196)

Symlink license files from the parent directory so that crates are
created with them.

Needed for distribution especially for the MIT license.

Signed-off-by: Michel Alexandre Salim <salimma@fedoraproject.org>

* Fixed docs: example in wrong section

* Docs

* Changelog

* E2E test

* Move tests on sanitize_ident near to implementation

---------

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Michel Alexandre Salim <salimma@fedoraproject.org>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: kenkoooo <kenkou.n@gmail.com>
Co-authored-by: Michel Alexandre Salim <michel@michel-slm.name>
  • Loading branch information
4 people committed Jun 7, 2023
1 parent 7b5e422 commit 0399a31
Show file tree
Hide file tree
Showing 21 changed files with 1,011 additions and 118 deletions.
122 changes: 122 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
// Usare IntelliSense per informazioni sui possibili attributi.
// Al passaggio del mouse vengono visualizzate le descrizioni degli attributi esistenti.
// Per altre informazioni, visitare: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug integration test 'acceptance'",
"cargo": {
"args": [
"test",
"--no-run",
"--test=acceptance",
"--package=rstest_reuse"
],
"filter": {
"name": "acceptance",
"kind": "test"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'rstest_test'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=rstest_test"
],
"filter": {
"name": "rstest_test",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug integration test 'framework'",
"cargo": {
"args": [
"test",
"--no-run",
"--test=framework",
"--package=rstest_test"
],
"filter": {
"name": "framework",
"kind": "test"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug integration test 'macros'",
"cargo": {
"args": [
"test",
"--no-run",
"--test=macros",
"--package=rstest_test"
],
"filter": {
"name": "macros",
"kind": "test"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'rstest'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=rstest"
],
"filter": {
"name": "rstest",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug integration test 'integration'",
"cargo": {
"args": [
"test",
"--no-run",
"--test=integration",
"--package=rstest"
],
"filter": {
"name": "integration",
"kind": "test"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
### Add

- Add support for `RSTEST_TIMEOUT` environment variable to define a max timeout
for each function (see [#190](https://github.com/la10736/rstest/issues/187) for details).
for each function (see [#190](https://github.com/la10736/rstest/issues/190) for details).
Thanks to @aviramha for idea and PR
- `#[files("glob path")]` attribute to generate tests based on files that
satisfy the given glob path (see [#163](https://github.com/la10736/rstest/issues/163) for details).

### Changed

Expand Down
4 changes: 4 additions & 0 deletions playground/files/myname.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
myname.txt


something else
1 change: 1 addition & 0 deletions playground/files/other
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no test
1 change: 1 addition & 0 deletions playground/files/should_fail.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
something else
46 changes: 11 additions & 35 deletions playground/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
use rstest::*;

#[fixture]
#[awt]
async fn two_args_mix_fixture(
#[future]
#[default(async { 4 })]
four: u32,
#[default(2)] two: u32,
) -> u32 {
four * 10 + two
}

// #[rstest]
// #[awt]
// async fn use_two_args_mix_fixture(#[future] two_args_mix_fixture: u32) {
// assert_eq!(42, two_args_mix_fixture);
// }

// #[rstest]
// #[awt]
// async fn use_two_args_mix_fixture_inject_first(
// #[future]
// #[with(async { 5 })]
// two_args_mix_fixture: u32,
// ) {
// assert_eq!(52, two_args_mix_fixture);
// }
use std::fs::File;
use std::path::PathBuf;
use std::io::Read;

#[rstest]
#[awt]
async fn use_two_args_mix_fixture_inject_both(
#[future]
#[with(async { 3 }, 1)]
two_args_mix_fixture: u32,
) {
assert_eq!(31, two_args_mix_fixture);
}
fn start_with_name(#[files("files/*.txt")] path: PathBuf) {
let name = path.file_name().unwrap();
let mut f = File::open(&path).unwrap();
let mut contents = String::new();
f.read_to_string(&mut contents).unwrap();

assert!(contents.starts_with(name.to_str().unwrap()))
}
33 changes: 33 additions & 0 deletions rstest/tests/resources/rstest/files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use rstest::*;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;

#[rstest]
fn start_with_name(
#[files("files/**/*.txt")]
#[exclude("exclude")]
path: PathBuf,
) {
let name = path.file_name().unwrap();
let mut f = File::open(&path).unwrap();
let mut contents = String::new();
f.read_to_string(&mut contents).unwrap();

assert!(contents.starts_with(name.to_str().unwrap()))
}

#[rstest]
fn start_with_name_with_include(
#[files("files/**/*.txt")]
#[exclude("exclude")]
#[include_dot_files]
path: PathBuf,
) {
let name = path.file_name().unwrap();
let mut f = File::open(&path).unwrap();
let mut contents = String::new();
f.read_to_string(&mut contents).unwrap();

assert!(contents.starts_with(name.to_str().unwrap()))
}
50 changes: 49 additions & 1 deletion rstest/tests/rstest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::{fs::File, io::Write, path::Path};

use mytest::*;
use rstest_test::*;
Expand All @@ -21,6 +21,54 @@ fn run_test(res: impl AsRef<Path>) -> (std::process::Output, String) {
)
}

#[test]
fn files() {
let prj = prj("files.rs");
let files_path = prj.path().join("files");
let sub_folder = files_path.join("sub");
std::fs::create_dir(&files_path).unwrap();
std::fs::create_dir(&sub_folder).unwrap();

for n in 0..4 {
let name = format!("element_{}.txt", n);
let path = files_path.join(&name);
let mut out = File::create(path).unwrap();
out.write_all(name.as_bytes()).unwrap();
out.write_all(b"--\n").unwrap();
out.write_all(b"something else\n").unwrap();
}
let dot = files_path.join(".ignore_me.txt");
File::create(dot)
.unwrap()
.write_all(b".ignore_me.txt--\n")
.unwrap();
let exclude = files_path.join("exclude.txt");
File::create(exclude)
.unwrap()
.write_all(b"excluded\n")
.unwrap();
let sub = sub_folder.join("sub_dir_file.txt");
File::create(sub)
.unwrap()
.write_all(b"sub_dir_file.txt--\nmore")
.unwrap();
let output = prj.run_tests().unwrap();

TestResults::new()
.ok("start_with_name::path_1_files_element_0_txt")
.ok("start_with_name::path_2_files_element_1_txt")
.ok("start_with_name::path_3_files_element_2_txt")
.ok("start_with_name::path_4_files_element_3_txt")
.ok("start_with_name::path_5_files_sub_sub_dir_file_txt")
.ok("start_with_name_with_include::path_1_files__ignore_me_txt")
.ok("start_with_name_with_include::path_2_files_element_0_txt")
.ok("start_with_name_with_include::path_3_files_element_1_txt")
.ok("start_with_name_with_include::path_4_files_element_2_txt")
.ok("start_with_name_with_include::path_5_files_element_3_txt")
.ok("start_with_name_with_include::path_6_files_sub_sub_dir_file_txt")
.assert(output);
}

#[test]
fn mutable_input() {
let (output, _) = run_test("mut.rs");
Expand Down
4 changes: 4 additions & 0 deletions rstest_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ default = ["async-timeout"]

[dependencies]
cfg-if = "1.0.0"
glob = "0.3.1"
proc-macro2 = "1.0.39"
quote = "1.0.19"
regex = "1.7.3"
relative-path = "1.8.0"
syn = {version = "2.0.2", features = ["full", "parsing", "extra-traits", "visit", "visit-mut"]}
unicode-ident = "1.0.5"

[dev-dependencies]
actix-rt = "2.7.0"
async-std = {version = "1.12.0", features = ["attributes"]}
maplit = "1.0.2"
pretty_assertions = "1.2.1"
rstest = {version = "0.17.0", default-features = false}
rstest_reuse = {version = "0.5.0", path = "../rstest_reuse"}
Expand Down
43 changes: 32 additions & 11 deletions rstest_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,21 +647,42 @@ pub fn fixture(
/// Also value list implements the magic conversion feature: every time the value type
/// implements `FromStr` trait you can use a literal string to define it.
///
/// ## Use Parametrize definition in more tests
///
/// If you need to use a test list for more than one test you can use
/// [`rstest_reuse`](https://crates.io/crates/rstest_reuse) crate.
/// With this helper crate you can define a template and use it everywhere.
///
/// ```
/// # use rstest::rstest;
/// # use std::net::SocketAddr;
/// #[rstest]
/// fn given_port(#[values("1.2.3.4:8000", "4.3.2.1:8000", "127.0.0.1:8000")] addr: SocketAddr) {
/// assert_eq(8000, addr.port())
/// assert_eq!(8000, addr.port())
/// }
/// ```
///
/// ## Files path as input arguments
///
/// If you need to create a test for each file in a given location you can use
/// `#[files("glob path syntax")]` attribute to generate a test for each file that
/// satisfy the given glob path.
///
/// ```
/// # use rstest::rstest;
/// # use std::path::{Path, PathBuf};
/// # fn check_file(path: &Path) -> bool { true };
/// #[rstest]
/// fn for_each_file(#[files("resources/valid_cases/*")] path: PathBuf) {
/// assert!(check_file(&path))
/// }
/// ```
/// The default behavior is to ignore the files that starts with `"."` but you can
/// modify this by use `#[include_dot_files]` attribute. The `files` attribute can be
/// used more than once on the same variable and you can also create some custom
/// exclusion rules with the `#[exclude("regex")]` attributes that filter out all
/// paths that verify the regular expression.
///
/// ## Use Parametrize definition in more tests
///
/// If you need to use a test list for more than one test you can use
/// [`rstest_reuse`](https://crates.io/crates/rstest_reuse) crate.
/// With this helper crate you can define a template and use it everywhere.
///
/// ```rust,ignore
/// use rstest::rstest;
/// use rstest_reuse::{self, *};
Expand All @@ -674,7 +695,7 @@ pub fn fixture(
///
/// #[apply(two_simple_cases)]
/// fn it_works(#[case] a: u32,#[case] b: u32) {
/// assert!(a == b);
/// assert_eq!(a, b);
/// }
/// ```
///
Expand Down Expand Up @@ -753,9 +774,9 @@ pub fn fixture(
/// assert_eq!(expected, base.await / div);
/// }
/// ```
///
///
/// ### Default timeout
///
///
/// You can set a default timeout for test using the `RSTEST_TIMEOUT` environment variable.
/// The value is in seconds and is evaluated on test compile time.///
///
Expand All @@ -764,7 +785,7 @@ pub fn fixture(
/// You can define an execution timeout for your tests with `#[timeout(<duration>)]` attribute. Timeouts
/// works both for sync and async tests and is runtime agnostic. `#[timeout(<duration>)]` take an
/// expression that should return a `std::time::Duration`. Follow a simple async example:
///
///
/// ```rust
/// use rstest::*;
/// use std::time::Duration;
Expand Down
Loading

0 comments on commit 0399a31

Please sign in to comment.