Skip to content

Commit

Permalink
Add runtime_env(#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto authored Aug 5, 2023
1 parent d5b0c7e commit e23643a
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
.direnv/
# The library shouldn't decide about the exact versions of
# its dependencies, but let the downstream crate decide.
Cargo.lock
Cargo.lock
examples/runner/target
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "test-with"
version = "0.9.7"
version = "0.10.0"
authors = ["Antonio Yang <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down Expand Up @@ -33,6 +33,7 @@ users = { version = "0.11", optional = true }
default = ["net", "resource", "user", "executable"]
ign-msg = []

runtime = []
net = ["http", "icmp"]
http = ["reqwest"]
icmp = ["ping"]
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The features you can use are `net`(`http`, `icmp`), `resource`, `user`, `executa
Currently, the condition is checked on build-time not runtime and not perfect and good for most develop scenario,
because of this [issue][original-issue] of rust-lang.
Here is the [slides][coscup-slides] of a talk in COSCUP and help you know more about it.
If you really want to check the condition in runtime, please check [runtime section](https://github.com/yanganto/test-with#runtime).
The `runtime` feature and runtime macros (`test_with::runner!`, `#[test_with::module]`, `#[test_with::runtime_env()]`) can help you run the test and check the conditions in runtime.

If you forget to add `#[test]` flag on the test case, `#[test_with]` macro will add it for you.

Expand Down Expand Up @@ -266,6 +268,34 @@ Require `executable` feature, if default features are disabled.
}
```

## Runtime
We can let an example to do thing that cargo test runner do, and ignore testcase in runtime.
The testcase of in the example will not in `#[cfg(test)]` or `#[test]` anymore, and use `#[test_with::runtime_*]`,
the test runner will treat it as the test in Rust and also provide the same summary as `cargo test`.

The `runtime` feature should be enabled and also include the `libtest-with` in `Cargo.toml`
```toml
test-with = { version = "0.10", features = ["runtime"] }
libtest-with = "0.6.1-0"
```

Create an example with the following runtime macros (`test_with::runner!`, `#[test_with::module]`, `#[test_with::runtime_env()]`).
```rust

test_with::runner!(module_name);

#[test_with::module]
mod module_name {
#[test_with::runtime_env(PWD)]
fn test_works() {
}
}

```

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


## Relating issues
* [Solve this in runtime][original-issue]

Expand Down
8 changes: 8 additions & 0 deletions examples/runner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "runner"
version = "0.1.0"
edition = "2021"

[dependencies]
test-with = { path = "../../", features = ["runtime"] }
libtest-with = "0.6.1-0"
39 changes: 39 additions & 0 deletions examples/runner/examples/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
test_with::runner!(env);

#[test_with::module]
mod env {
#[test_with::runtime_env(PWD)]
fn test_works() {
assert!(true);
}

// Will rase error when using `#[test]`
//
// #[test_with::runtime_env(PWD)]
// #[test]
// fn test_works() {
// assert!(true);
// }

#[test_with::runtime_env(NOTHING)]
fn test_ignored() {
panic!("should be ignored")
}

// Will rase error when using non-runtime macro
//
// #[test_with::env(PWD, SAYING)]
// fn test_works_too() {
// assert!(true);
// }

#[test_with::runtime_env(PWD, SAYING)]
fn test_works_too() {
assert!(true);
}

#[test_with::runtime_env(PWD, NOT_SAYING)]
fn test_ignored_too() {
panic!("should be ignored")
}
}
3 changes: 3 additions & 0 deletions examples/runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Please `cargo run --example test`");
}
66 changes: 51 additions & 15 deletions flake.lock

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

6 changes: 5 additions & 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.64.0".default;
rust = pkgs.rust-bin.stable."1.71.1".default;
dr = dependency-refresh.defaultPackage.${system};

publishScript = pkgs.writeShellScriptBin "crate-publish" ''
Expand All @@ -40,6 +40,10 @@
cargo run --no-default-features --features=executable --example=executable
cargo install cargo-hack
cargo hack test --examples
# runtime ignore example
cd examples/runner
cargo run --example test
'';
in
with pkgs;
Expand Down
Loading

0 comments on commit e23643a

Please sign in to comment.