From 1c1fb4d8ad85246c28e9c479a396198f5672beb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Sat, 9 Jul 2022 23:22:03 +1200 Subject: [PATCH] Add testing utility --- .gitignore | 11 ++-- Cargo.lock | 46 ++++++++++++++++ Cargo.toml | 1 + examples/test-cw.rs | 94 ++++++++++++++++++++++++++++++++ tests/cli_tests.rs | 48 ++++------------ tests/trycmd/unix.trycmd | 7 --- tests/trycmd/unix/basics.trycmd | 33 +++++++++++ tests/trycmd/unix/restart.trycmd | 22 ++++++++ 8 files changed, 212 insertions(+), 50 deletions(-) create mode 100644 examples/test-cw.rs delete mode 100644 tests/trycmd/unix.trycmd create mode 100644 tests/trycmd/unix/basics.trycmd create mode 100644 tests/trycmd/unix/restart.trycmd diff --git a/.gitignore b/.gitignore index 3da2447..d14f4b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,11 @@ -/target ~* *~ .*.sw* +.tmp* + +/target + *.tar.xz +*.tzst *.zip -/tests/touchdata -/tests/snapshots/*.new - CHECKSUMS* - -/oldsrc diff --git a/Cargo.lock b/Cargo.lock index c334cbc..3e49c94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -319,6 +319,7 @@ dependencies = [ "clap_complete", "clap_complete_fig", "console-subscriber", + "dircpy", "duct", "dunce", "embed-resource", @@ -527,6 +528,20 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "crossbeam" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae5588f6b3c3cb05239e90bd110f257254aecd01e4635400391aeae07497845" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils", +] + [[package]] name = "crossbeam-channel" version = "0.5.5" @@ -562,6 +577,16 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "crossbeam-queue" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f25d8400f4a7a5778f0e4e52384a48cbd9b5c495d110786187fc750075277a2" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.10" @@ -583,6 +608,17 @@ dependencies = [ "syn", ] +[[package]] +name = "dircpy" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4388680a28717a3ff2b6b60824bf62d67076232d9416c34d17a148dade0e6cd3" +dependencies = [ + "jwalk", + "log", + "walkdir", +] + [[package]] name = "dirs" version = "2.0.2" @@ -1214,6 +1250,16 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" +[[package]] +name = "jwalk" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "172752e853a067cbce46427de8470ddf308af7fd8ceaf9b682ef31a5021b6bb9" +dependencies = [ + "crossbeam", + "rayon", +] + [[package]] name = "kqueue" version = "1.0.6" diff --git a/Cargo.toml b/Cargo.toml index 5eaab01..fe9d922 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,7 @@ embed-resource = "1.7.3" [dev-dependencies] clap_complete = "3.2.3" clap_complete_fig = "3.2.4" +dircpy = "0.3.10" duct = "0.13.5" tempfile = "3.3.0" trycmd = "0.13.4" diff --git a/examples/test-cw.rs b/examples/test-cw.rs new file mode 100644 index 0000000..0874463 --- /dev/null +++ b/examples/test-cw.rs @@ -0,0 +1,94 @@ +use std::{ + env, + fs::write, + path::PathBuf, + thread::sleep, + time::{Duration, Instant}, +}; + +use clap::Parser; +use duct::cmd; +use miette::{bail, IntoDiagnostic, Result}; +use tempfile::TempDir; + +#[derive(Debug, Parser)] +struct Args { + #[clap(long)] + vcs: Option, + + #[clap(long)] + bin: bool, + + #[clap(long, multiple_occurrences = true)] + touch: Vec, + + #[clap(long, default_value = "200")] + before: u64, + + #[clap(long, default_value = "200")] + between: u64, + + #[clap(long, default_value = "5000")] + timeout: u64, + + #[clap(raw = true)] + cmd: Vec, +} + +fn main() -> Result<()> { + let mut args = Args::parse(); + + let tmp_dir = TempDir::new_in(".").into_diagnostic()?; + + let mut init_args = vec!["init", "--quiet", "--name", "cw-test", "--offline"]; + if args.bin { + init_args.push("--bin"); + } + if let Some(vcs) = &args.vcs { + init_args.push("--vcs"); + init_args.push(vcs); + } + cmd("cargo", init_args) + .dir(tmp_dir.path()) + .run() + .into_diagnostic()?; + + let before = Duration::from_millis(args.before); + let between = Duration::from_millis(args.between); + let timeout = Duration::from_millis(args.timeout); + + args.cmd.insert(0, "watch".into()); + let cw = cmd( + PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("target/debug/cargo-watch"), + args.cmd, + ) + .dir(tmp_dir.path()) + .start() + .into_diagnostic()?; + + sleep(before); + + for file in args.touch { + write(tmp_dir.path().join(file), "something").into_diagnostic()?; + sleep(between); + } + + let start = Instant::now(); + let mut good = false; + while start.elapsed() < timeout { + if cw.try_wait().into_diagnostic()?.is_some() { + good = true; + break; + } else { + sleep(Duration::from_millis(10)); + } + } + + tmp_dir.close().into_diagnostic()?; + + if !good { + bail!("Timed out"); + } + + Ok(()) +} diff --git a/tests/cli_tests.rs b/tests/cli_tests.rs index 55a7719..4973242 100644 --- a/tests/cli_tests.rs +++ b/tests/cli_tests.rs @@ -1,44 +1,18 @@ -use std::{ - env::{current_dir, set_current_dir}, - path::PathBuf, -}; +use std::{env, path::PathBuf}; -use duct::cmd; -use miette::{IntoDiagnostic, Result}; -use tempfile::TempDir; -use trycmd::{cargo::cargo_bin, TestCases}; - -fn prepare() -> Result<(PathBuf, TempDir)> { - let tests_dir = current_dir() - .into_diagnostic()? - .join("tests") - .join("trycmd"); - - let tmp_dir = TempDir::new().into_diagnostic()?; - set_current_dir(tmp_dir.path()).into_diagnostic()?; - cmd!( - "cargo", - "init", - "--vcs", - "git", - "--bin", - "--name", - "cw-test", - "--offline" - ) - .run() - .into_diagnostic()?; - - Ok((tests_dir, tmp_dir)) -} +use trycmd::{cargo::cargo_bin, schema::Bin, TestCases}; #[test] #[cfg(unix)] -fn unix_tests() -> Result<()> { - let (tests_dir, tmp_dir) = prepare()?; +fn unix_tests() { TestCases::new() .default_bin_path(cargo_bin!("cargo-watch")) - .case(tests_dir.join("unix.trycmd")); - tmp_dir.close().into_diagnostic()?; - Ok(()) + .register_bin( + "test-cw", + Bin::Path( + PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) + .join("target/debug/examples/test-cw"), + ), + ) + .case("tests/trycmd/unix/*.trycmd"); } diff --git a/tests/trycmd/unix.trycmd b/tests/trycmd/unix.trycmd deleted file mode 100644 index 8cd6225..0000000 --- a/tests/trycmd/unix.trycmd +++ /dev/null @@ -1,7 +0,0 @@ -``` -$ cargo-watch --quit-after-n 1 -- echo foo -[[Running `echo foo`]] -foo -[[Command was successful]] - -``` diff --git a/tests/trycmd/unix/basics.trycmd b/tests/trycmd/unix/basics.trycmd new file mode 100644 index 0000000..3f4e1c8 --- /dev/null +++ b/tests/trycmd/unix/basics.trycmd @@ -0,0 +1,33 @@ +``` +$ test-cw -- +> --quit-after-n 1 -- echo foo +[[Running `echo foo`]] +foo +[[Command was successful]] +[[--quit after n--]] + +``` + +``` +$ test-cw --touch File -- +> --quit-after-n 1 --postpone -- echo foo +[[Running `echo foo`]] +foo +[[Command was successful]] +[[--quit after n--]] + +``` + +``` +$ test-cw --touch File --touch Other -- +> --quit-after-n 2 --postpone -- echo foo +[[Running `echo foo`]] +foo +[[Command was successful]] +[[Running `echo foo`]] +foo +[[Command was successful]] +[[--quit after n--]] + +``` + diff --git a/tests/trycmd/unix/restart.trycmd b/tests/trycmd/unix/restart.trycmd new file mode 100644 index 0000000..8f2b513 --- /dev/null +++ b/tests/trycmd/unix/restart.trycmd @@ -0,0 +1,22 @@ +``` +$ test-cw -- +> --quit-after-n 1 -S sh -s 'echo in; sleep 1; echo out' +[[Running `echo in; sleep 1; echo out`]] +in +out +[[Command was successful]] +[[--quit after n--]] + +``` + +``` +$ test-cw --touch File --touch Other --between 500 -- +> --quit-after-n 2 --postpone -S sh -s 'echo in; sleep 1; echo out' +[[Running `echo in; sleep 1; echo out`]] +in +[[Running `echo in; sleep 1; echo out`]] +... +[[--quit after n--]] + +``` +