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

Add #test_with::lock #90

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
508 changes: 256 additions & 252 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[package]
name = "test-with"
version = "0.12.6"
version = "0.13.0"
authors = ["Antonio Yang <[email protected]>"]
edition = "2021"
license = "MIT"
description = "A lib help you run test with condition"
repository = "https://github.com/yanganto/test-with"
keywords = [ "testing", "condition", "toggle", "integration", "ignore" ]
categories = [ "development-tools", "testing" ]
rust-version = "1.77.0" # Due to std::fs::create_new

[lib]
proc-macro = true
Expand Down Expand Up @@ -43,7 +44,7 @@ user = ["uzers"]
executable = ["which"]

[dev-dependencies]
tokio = { version = "1.15.0", features = ["rt", "macros"] }
serial_test = "2.0.0"
tokio = { version = "1.38.0", features = ["rt", "rt-multi-thread", "macros"] }
serial_test = "3.1.1"
libtest-with = { version = "0.7.0-0", features = ["net", "resource", "user", "executable"] }
rstest = "0.21.0"
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,38 @@ mod test_with_mock {

Please check out examples uder the [example/runner](https://github.com/yanganto/test-with/tree/main/examples/runner) project.

## Lock
`#[test_with::lock(LOCK_NAME)]` is way to make sure your test casess can run one by one by using file locks.
The first parameter is the name of the file lock, the second parameter to specific the waiting seconds,
default will be 60 seconds.

```rust
// `LOCK` is file based lock to prevent test1 an test2 run at the same time
#[test_with::lock(LOCK)]
fn test_1() {
assert!(true);
}

// `LOCK` is file based lock to prevent test1 an test2 run at the same time
#[test_with::lock(LOCK)]
fn test_2() {
assert!(true);
}

// `ANOTHER_LOCK` is file based lock to prevent test3 an test4 run at the same time with 3 sec
// waiting time.
#[test_with::lock(ANOTHER_LOCK, 3)]
fn test_3() {
assert!(true);
}

// `ANOTHER_LOCK` is file based lock to prevent test3 an test4 run at the same time with 3 sec
// waiting time.
#[test_with::lock(ANOTHER_LOCK, 3)]
fn test_4() {
assert!(true);
}
```

## Relating issues
* [Solve this in runtime][original-issue]
Expand Down
30 changes: 30 additions & 0 deletions examples/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
fn main() {}

#[cfg(test)]
mod tests {
// `LOCK` is file based lock to prevent test1 an test2 run at the same time
#[test_with::lock(LOCK)]
fn test_1() {
assert!(true);
}

// `LOCK` is file based lock to prevent test1 an test2 run at the same time
#[test_with::lock(LOCK)]
fn test_2() {
assert!(true);
}

// `ANOTHER_LOCK` is file based lock to prevent test3 an test4 run at the same time with 3 sec
// waiting time.
#[test_with::lock(ANOTHER_LOCK, 3)]
fn test_3() {
assert!(true);
}

// `ANOTHER_LOCK` is file based lock to prevent test3 an test4 run at the same time with 3 sec
// waiting time.
#[test_with::lock(ANOTHER_LOCK, 3)]
fn test_4() {
assert!(true);
}
}
14 changes: 12 additions & 2 deletions examples/runner/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 12 additions & 46 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
pkgs = import nixpkgs {
inherit system overlays;
};
rust = pkgs.rust-bin.stable."1.74.1".default;
rust = pkgs.rust-bin.stable."1.77.0".default;
dr = dependency-refresh.defaultPackage.${system};

publishScript = pkgs.writeShellScriptBin "crate-publish" ''
Expand Down
50 changes: 48 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ use std::net::IpAddr;
use std::net::TcpStream;

use proc_macro::TokenStream;
#[cfg(any(feature = "resource", feature = "icmp"))]
use proc_macro_error::abort_call_site;
use proc_macro_error::proc_macro_error;
use syn::{parse_macro_input, ItemFn, ItemMod};
Expand All @@ -68,7 +67,7 @@ use syn::{Item, ItemStruct, ItemType};
#[cfg(feature = "executable")]
use which::which;

use crate::utils::{fn_macro, is_module, mod_macro, sanitize_env_vars_attr};
use crate::utils::{fn_macro, is_module, lock_macro, mod_macro, sanitize_env_vars_attr};

mod utils;

Expand Down Expand Up @@ -2821,3 +2820,50 @@ mod tests {
}
}
}

/// Run test case one by one when the lock is acquired
/// It will automatically implement a file lock for the test case to prevent it run in the same
/// time. Also, you can pass the second parameter to specific the waiting seconds, default will be
/// 60 seconds.
/// ```
/// #[cfg(test)]
/// mod tests {
///
/// // `LOCK` is file based lock to prevent test1 an test2 run at the same time
/// #[test_with::lock(LOCK)]
/// #[test]
/// fn test_1() {
/// assert!(true);
/// }
///
/// // `LOCK` is file based lock to prevent test1 an test2 run at the same time
/// #[test_with::lock(LOCK)]
/// #[test]
/// fn test_2() {
/// assert!(true);
/// }
///
/// // `ANOTHER_LOCK` is file based lock to prevent test3 an test4 run at the same time with 3 sec
/// // waiting time.
/// #[test_with::lock(ANOTHER_LOCK, 3)]
/// fn test_3() {
/// assert!(true);
/// }
///
/// // `ANOTHER_LOCK` is file based lock to prevent test3 an test4 run at the same time with 3 sec
/// // waiting time.
/// #[test_with::lock(ANOTHER_LOCK, 3)]
/// fn test_4() {
/// assert!(true);
/// }
///
/// }
#[proc_macro_attribute]
#[proc_macro_error]
pub fn lock(attr: TokenStream, stream: TokenStream) -> TokenStream {
if is_module(&stream) {
abort_call_site!("#[test_with::lock] only works with fn")
} else {
lock_macro(attr, parse_macro_input!(stream as ItemFn))
}
}
Loading
Loading