Reset tests

This commit is contained in:
Félix Saparelli 2022-07-08 22:19:02 +12:00
parent c876f7f39c
commit c268fcfc37
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
10 changed files with 15 additions and 317 deletions

15
tests/cli_tests.rs Normal file
View File

@ -0,0 +1,15 @@
use std::env::set_current_dir;
use duct::cmd;
use tempfile::TempDir;
#[test]
fn cli_tests() -> Box<dyn std::error::Error + Send> {
let tmp_dir = TempDir::new()?;
set_current_dir(tmp_dir.path())?;
cmd!("cargo", "init", "--vcs", "git", "--bin").run()?;
trycmd::TestCases::new().case("tests/cmd/*.trycmd");
tmp_dir.close()?;
}

View File

@ -1,207 +0,0 @@
use assert_cmd::prelude::*;
use std::{
fs::OpenOptions,
io::{self, Write},
path::PathBuf,
process::{Command, Stdio},
thread::sleep,
time::{Duration, Instant},
};
use wait_timeout::ChildExt;
fn touch(n: u8) -> io::Result<()> {
let path: PathBuf = format!("./tests/touchdata/{}.txt", n).into();
let mut file = OpenOptions::new().create(true).write(true).open(path)?;
writeln!(&mut file, "{:?}", Instant::now())?;
Ok(())
}
fn std_to_string<T: io::Read>(handle: &mut Option<T>) -> String {
if let Some(ref mut handle) = handle {
let mut buf = String::with_capacity(1024);
handle.read_to_string(&mut buf).unwrap();
buf
} else {
unreachable!()
}
}
// fsevents has trouble
#[cfg(not(target_os = "macos"))]
#[ignore]
#[test]
fn without_poll() {
let mut main = Command::cargo_bin("cargo-watch")
.unwrap()
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.args(&[
"watch", // TODO: can that be omitted?
"--testing-only--once",
"--no-gitignore",
"-w",
"./tests/touchdata/",
"-s",
"echo it runs",
])
.spawn()
.unwrap();
sleep(Duration::from_secs(2));
touch(0).unwrap();
if main
.wait_timeout(Duration::from_secs(30))
.unwrap()
.is_none()
{
main.kill().unwrap();
}
main.wait_with_output().unwrap().assert().success();
}
#[test]
#[ignore]
fn with_poll() {
let mut main = Command::cargo_bin("cargo-watch")
.unwrap()
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.args(&[
"watch",
"--testing-only--once",
"--no-gitignore",
"--poll",
"-w",
"./tests/touchdata/",
"-s",
"echo it runs",
])
.spawn()
.unwrap();
sleep(Duration::from_secs(2));
touch(1).unwrap();
if main
.wait_timeout(Duration::from_secs(30))
.unwrap()
.is_none()
{
main.kill().unwrap();
}
main.wait_with_output().unwrap().assert().success();
}
#[test]
#[ignore]
#[cfg(not(windows))] // annoyingly, theres some kind of encoding or extra bytes getting added here, needs debugging
fn with_announce() {
let mut main = Command::cargo_bin("cargo-watch")
.unwrap()
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.args(&[
"watch",
"--testing-only--once",
"--no-gitignore",
"--poll",
"-w",
"./tests/touchdata/",
"-s",
"echo with announce",
])
.spawn()
.unwrap();
sleep(Duration::from_secs(2));
touch(2).unwrap();
if main
.wait_timeout(Duration::from_secs(30))
.unwrap()
.is_none()
{
main.kill().unwrap();
}
insta::assert_snapshot!("with_announce.stderr", std_to_string(&mut main.stderr));
insta::assert_snapshot!("with_announce.stdout", std_to_string(&mut main.stdout));
}
#[test]
#[ignore]
fn without_announce() {
let mut main = Command::cargo_bin("cargo-watch")
.unwrap()
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.args(&[
"watch",
"--testing-only--once",
"--no-gitignore",
"--quiet",
"--poll",
"-w",
"./tests/touchdata/",
"-s",
"echo without announce",
])
.spawn()
.unwrap();
sleep(Duration::from_secs(2));
touch(3).unwrap();
if main
.wait_timeout(Duration::from_secs(30))
.unwrap()
.is_none()
{
main.kill().unwrap();
}
insta::assert_snapshot!("without_announce.stderr", std_to_string(&mut main.stderr));
insta::assert_snapshot!("without_announce.stdout", std_to_string(&mut main.stdout));
}
#[cfg(unix)]
#[ignore]
#[test]
fn with_error() {
let mut main = Command::cargo_bin("cargo-watch")
.unwrap()
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.args(&[
"watch",
"--testing-only--once",
"--no-gitignore",
"--poll",
"-w",
"./tests/touchdata/",
"-s",
"echo with error",
"-s",
"false",
])
.spawn()
.unwrap();
sleep(Duration::from_secs(2));
touch(4).unwrap();
if main
.wait_timeout(Duration::from_secs(30))
.unwrap()
.is_none()
{
main.kill().unwrap();
}
insta::assert_snapshot!("with_error.stderr", std_to_string(&mut main.stderr));
insta::assert_snapshot!("with_error.stdout", std_to_string(&mut main.stdout));
}

View File

@ -1,69 +0,0 @@
use assert_cmd::prelude::*;
use predicates::str::is_match;
use std::{
process::{Command, Stdio},
time::Duration,
};
use wait_timeout::ChildExt;
#[test]
fn with_cargo() {
let mut main = Command::new("cargo")
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.args(&["watch", "--version"])
.spawn()
.unwrap();
if main.wait_timeout(Duration::from_secs(1)).unwrap().is_none() {
main.kill().unwrap();
}
main.wait_with_output()
.unwrap()
.assert()
.success()
.stdout(is_match(r"cargo-watch \d+\.\d+\.\d+\n").unwrap());
}
#[test]
fn without_cargo() {
let mut main = Command::cargo_bin("cargo-watch")
.unwrap()
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.args(&["watch", "--version"])
.spawn()
.unwrap();
if main.wait_timeout(Duration::from_secs(1)).unwrap().is_none() {
main.kill().unwrap();
}
main.wait_with_output()
.unwrap()
.assert()
.success()
.stdout(is_match(r"cargo-watch \d+\.\d+\.\d+\n").unwrap());
}
#[test]
fn without_watch() {
let mut main = Command::cargo_bin("cargo-watch")
.unwrap()
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.args(&["--version"])
.spawn()
.unwrap();
if main.wait_timeout(Duration::from_secs(1)).unwrap().is_none() {
main.kill().unwrap();
}
main.wait_with_output()
.unwrap()
.assert()
.success()
.stdout(is_match(r"cargo-watch \d+\.\d+\.\d+\n").unwrap());
}

View File

@ -1,7 +0,0 @@
---
source: tests/echo.rs
expression: std_to_string(&mut main.stderr)
---
WARN - Polling for changes every 500ms

View File

@ -1,8 +0,0 @@
---
source: tests/echo.rs
expression: std_to_string(&mut main.stdout)
---
[Running 'echo with announce']
with announce
[Finished running. Exit status: 0]

View File

@ -1,7 +0,0 @@
---
source: tests/echo.rs
expression: std_to_string(&mut main.stderr)
---
WARN - Polling for changes every 500ms

View File

@ -1,8 +0,0 @@
---
source: tests/echo.rs
expression: std_to_string(&mut main.stdout)
---
[Running 'echo with error && false']
with error
[Finished running. Exit status: 1]

View File

@ -1,5 +0,0 @@
---
source: tests/echo.rs
expression: std_to_string(&mut main.stderr)
---

View File

@ -1,6 +0,0 @@
---
source: tests/echo.rs
expression: std_to_string(&mut main.stdout)
---
without announce