File: cli.rs

package info (click to toggle)
rust-sd 0.7.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 180 kB
  • sloc: makefile: 10
file content (66 lines) | stat: -rw-r--r-- 1,453 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use anyhow::Result;
use assert_cmd::Command;
use std::io::prelude::*;

fn sd() -> Command {
    Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Error invoking sd")
}

fn assert_file(path: &std::path::Path, content: &str) {
    assert_eq!(content, std::fs::read_to_string(path).unwrap());
}

#[test]
fn in_place() -> Result<()> {
    let mut file = tempfile::NamedTempFile::new()?;
    file.write(b"abc123def")?;
    let path = file.into_temp_path();

    sd().args(&["abc\\d+", "", path.to_str().unwrap()])
        .assert()
        .success();
    assert_file(&path.to_path_buf(), "def");

    Ok(())
}

#[test]
fn in_place_with_empty_result_file() -> Result<()> {
    let mut file = tempfile::NamedTempFile::new()?;
    file.write(b"a7c")?;
    let path = file.into_temp_path();

    sd().args(&["a\\dc", "", path.to_str().unwrap()])
        .assert()
        .success();
    assert_file(&path.to_path_buf(), "");

    Ok(())
}

#[test]
fn replace_into_stdout() -> Result<()> {
    let mut file = tempfile::NamedTempFile::new()?;
    file.write(b"abc123def")?;

    #[rustfmt::skip]
    sd().args(&["-p", "abc\\d+", "", file.path().to_str().unwrap()])
        .assert()
        .success()
        .stdout("def");

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

    Ok(())
}

#[test]
fn stdin() -> Result<()> {
    sd().args(&["abc\\d+", ""])
        .write_stdin("abc123def")
        .assert()
        .success()
        .stdout("def");

    Ok(())
}