Skip to content

Commit

Permalink
#191 parameterize tests to include no-swap flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tennox committed May 17, 2023
1 parent 0bdf62d commit bfd3d53
Showing 1 changed file with 49 additions and 29 deletions.
78 changes: 49 additions & 29 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod cli {
use anyhow::Result;
use assert_cmd::Command;
use rstest::rstest;
use std::io::prelude::*;

fn sd() -> Command {
Expand All @@ -25,36 +26,46 @@ mod cli {
Ok(())
}

#[test]
fn in_place() -> Result<()> {
#[rstest]
fn in_place(#[values(false, true)] no_swap: bool) -> Result<()> {
let mut file = tempfile::NamedTempFile::new()?;
file.write_all(b"abc123def")?;
let path = file.into_temp_path();

sd().args(["abc\\d+", "", path.to_str().unwrap()])
.assert()
.success();
let mut cmd = sd();
cmd.args(["abc\\d+", "", path.to_str().unwrap()]);
if no_swap {
cmd.arg("--no-swap");
}
cmd.assert().success();
assert_file(&path, "def");

Ok(())
}

#[test]
fn in_place_with_empty_result_file() -> Result<()> {
#[rstest]
fn in_place_with_empty_result_file(
#[values(false, true)] no_swap: bool,
) -> Result<()> {
let mut file = tempfile::NamedTempFile::new()?;
file.write_all(b"a7c")?;
let path = file.into_temp_path();

sd().args(["a\\dc", "", path.to_str().unwrap()])
.assert()
.success();
let mut cmd = sd();
cmd.args(["a\\dc", "", path.to_str().unwrap()]);
if no_swap {
cmd.arg("--no-swap");
}
cmd.assert().success();
assert_file(&path, "");

Ok(())
}

#[test]
fn in_place_following_symlink() -> Result<()> {
#[rstest]
fn in_place_following_symlink(
#[values(false, true)] no_swap: bool,
) -> Result<()> {
let dir = tempfile::tempdir()?;
let path = dir.path();
let file = path.join("file");
Expand All @@ -63,39 +74,48 @@ mod cli {
create_soft_link(&file, &link)?;
std::fs::write(&file, "abc123def")?;

sd().args(["abc\\d+", "", link.to_str().unwrap()])
.assert()
.success();
let mut cmd = sd();
cmd.args(["abc\\d+", "", link.to_str().unwrap()]);
if no_swap {
cmd.arg("--no-swap");
}
cmd.assert().success();

assert_file(&file, "def");
assert!(std::fs::symlink_metadata(link)?.file_type().is_symlink());

Ok(())
}

#[test]
fn replace_into_stdout() -> Result<()> {
#[rstest]
fn replace_into_stdout(#[values(false, true)] no_swap: bool) -> Result<()> {
let mut file = tempfile::NamedTempFile::new()?;
file.write_all(b"abc123def")?;

sd().args(["-p", "abc\\d+", "", file.path().to_str().unwrap()])
.assert()
.success()
.stdout(format!(
"{}{}def\n",
ansi_term::Color::Green.prefix(),
ansi_term::Color::Green.suffix()
));
let mut cmd = sd();
cmd.args(["-p", "abc\\d+", "", file.path().to_str().unwrap()]);
if no_swap {
cmd.arg("--no-swap");
}
cmd.assert().success().stdout(format!(
"{}{}def\n",
ansi_term::Color::Green.prefix(),
ansi_term::Color::Green.suffix()
));

assert_file(file.path(), "abc123def");

Ok(())
}

#[test]
fn stdin() -> Result<()> {
sd().args(["abc\\d+", ""])
.write_stdin("abc123def")
#[rstest]
fn stdin(#[values(false, true)] no_swap: bool) -> Result<()> {
let mut cmd = sd();
cmd.args(["abc\\d+", ""]);
if no_swap {
cmd.arg("--no-swap");
}
cmd.write_stdin("abc123def")
.assert()
.success()
.stdout("def");
Expand Down

0 comments on commit bfd3d53

Please sign in to comment.