2019-11-17 19:28:43 +00:00
|
|
|
use anyhow::Result;
|
2019-12-17 05:00:15 +00:00
|
|
|
use assert_cmd::Command;
|
|
|
|
use std::io::prelude::*;
|
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+", ""])
|
2019-12-17 05:00:15 +00:00
|
|
|
.write_stdin("abc123def")
|
2019-11-17 19:28:43 +00:00
|
|
|
.assert()
|
|
|
|
.success()
|
|
|
|
.stdout("def");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|