sd/tests/cli.rs

54 lines
1.1 KiB
Rust
Raw Normal View History

2019-11-17 19:28:43 +00:00
use anyhow::Result;
use assert_cmd::prelude::*;
2019-11-19 04:12:03 +00:00
use std::{io::prelude::*, process::Command};
2019-11-17 19:28:43 +00:00
fn sd() -> Command {
Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Error invoking sd")
}
2019-11-19 04:12:03 +00:00
fn assert_file(path: &std::path::Path, content: &str) {
assert_eq!(content, std::fs::read_to_string(path).unwrap());
}
2019-11-17 19:28:43 +00:00
#[test]
fn in_place() -> Result<()> {
2019-11-19 04:12:03 +00:00
let mut file = tempfile::NamedTempFile::new()?;
file.write(b"abc123def")?;
2019-11-17 19:28:43 +00:00
2019-11-19 04:12:03 +00:00
sd().args(&["abc\\d+", "", file.path().to_str().unwrap()])
.assert()
.success();
assert_file(file.path(), "def");
2019-11-17 19:28:43 +00:00
Ok(())
}
#[test]
fn replace_into_stdout() -> Result<()> {
2019-11-19 04:12:03 +00:00
let mut file = tempfile::NamedTempFile::new()?;
file.write(b"abc123def")?;
2019-11-17 19:28:43 +00:00
#[rustfmt::skip]
sd()
.args(&["-p", "abc\\d+", "", file.path().to_str().unwrap()])
.assert()
.success()
.stdout("def");
2019-11-19 04:12:03 +00:00
assert_file(file.path(), "abc123def");
2019-11-17 19:28:43 +00:00
Ok(())
}
#[test]
fn stdin() -> Result<()> {
sd().args(&["abc\\d+", ""])
.with_stdin()
.buffer("abc123def")
.assert()
.success()
.stdout("def");
Ok(())
}