diff --git a/rstest/tests/integration.rs b/rstest/tests/integration.rs index 93a2d7e..7858dcf 100644 --- a/rstest/tests/integration.rs +++ b/rstest/tests/integration.rs @@ -16,6 +16,12 @@ lazy_static! { static ref ROOT_PROJECT: Project = Project::new(ROOT_DIR.as_ref()); } +pub fn base_prj() -> Project { + let prj_name = sanitize_name(testname()); + + ROOT_PROJECT.subproject(&prj_name) +} + pub fn prj() -> Project { let prj_name = sanitize_name(testname()); diff --git a/rstest/tests/resources/rstest/timeout_async.rs b/rstest/tests/resources/rstest/timeout_async.rs new file mode 100644 index 0000000..1d12cf5 --- /dev/null +++ b/rstest/tests/resources/rstest/timeout_async.rs @@ -0,0 +1,17 @@ +use rstest::*; +use std::time::Duration; + +fn ms(ms: u32) -> Duration { + Duration::from_millis(ms.into()) +} + +async fn delayed_sum(a: u32, b: u32,delay: Duration) -> u32 { + async_std::task::sleep(delay).await; + a + b +} + +#[rstest] +#[timeout(ms(80))] +async fn single_pass() { + assert_eq!(4, delayed_sum(2, 2, ms(10)).await); +} \ No newline at end of file diff --git a/rstest/tests/rstest/mod.rs b/rstest/tests/rstest/mod.rs index c3d31f1..7284595 100644 --- a/rstest/tests/rstest/mod.rs +++ b/rstest/tests/rstest/mod.rs @@ -1,14 +1,16 @@ use std::path::Path; -use super::resources; - use mytest::*; use rstest_test::*; use unindent::Unindent; -fn prj(res: impl AsRef) -> Project { +pub fn resources(res: impl AsRef) -> std::path::PathBuf { let path = Path::new("rstest").join(res.as_ref()); - crate::prj().set_code_file(resources(path)) + super::resources(path) +} + +fn prj(res: impl AsRef) -> Project { + crate::prj().set_code_file(resources(res)) } fn run_test(res: impl AsRef) -> (std::process::Output, String) { @@ -933,6 +935,51 @@ fn timeout() { .assert(output); } +mod async_timeout_feature { + use super::*; + + fn build_prj(features: &[&str]) -> Project { + let prj = crate::base_prj(); + let features = match features.is_empty() { + true => String::new(), + false => format!(r#", features=["{}"]"#, features.join(r#"",""#)), + }; + prj.add_dependency( + "rstest", + &format!( + r#"{{path="{}", default-features = false {}}}"#, + prj.exec_dir_str().as_str(), + features + ), + ); + prj.add_dependency("async-std", r#"{version="*", features=["attributes"]}"#); + prj + } + + #[test] + fn should_not_compile_if_feature_disable() { + let prj = build_prj(&[]); + let output = prj + .set_code_file(resources("timeout_async.rs")) + .run_tests() + .unwrap(); + + assert_in!(output.stderr.str(), "error: Enable async-timeout feature"); + } + + #[test] + fn should_work_if_feature_enabled() { + let prj = build_prj(&["async-timeout"]); + + let output = prj + .set_code_file(resources("timeout_async.rs")) + .run_tests() + .unwrap(); + + TestResults::new().ok("single_pass").assert(output); + } +} + mod should_show_correct_errors { use std::process::Output;