Add testing utility

This commit is contained in:
Félix Saparelli 2022-07-09 23:22:03 +12:00
parent c03096b2c2
commit 1c1fb4d8ad
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
8 changed files with 212 additions and 50 deletions

11
.gitignore vendored
View File

@ -1,12 +1,11 @@
/target
~*
*~
.*.sw*
.tmp*
/target
*.tar.xz
*.tzst
*.zip
/tests/touchdata
/tests/snapshots/*.new
CHECKSUMS*
/oldsrc

46
Cargo.lock generated
View File

@ -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"

View File

@ -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"

94
examples/test-cw.rs Normal file
View File

@ -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<String>,
#[clap(long)]
bin: bool,
#[clap(long, multiple_occurrences = true)]
touch: Vec<PathBuf>,
#[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<String>,
}
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(())
}

View File

@ -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");
}

View File

@ -1,7 +0,0 @@
```
$ cargo-watch --quit-after-n 1 -- echo foo
[[Running `echo foo`]]
foo
[[Command was successful]]
```

View File

@ -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--]]
```

View File

@ -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--]]
```