-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathcli.rs
More file actions
56 lines (45 loc) · 1.56 KB
/
cli.rs
File metadata and controls
56 lines (45 loc) · 1.56 KB
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
use assert_cmd::cargo::*; // Import cargo_bin_cmd! macro and methods
use predicates::prelude::*; // Used for writing assertions
#[test]
fn file_doesnt_exist() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = cargo_bin_cmd!("grrs");
cmd.arg("foobar").arg("test/file/doesnt/exist");
cmd.assert()
.failure()
.stderr(predicate::str::contains("could not read file"));
Ok(())
}
use assert_fs::prelude::*;
#[test]
fn find_content_in_file() -> Result<(), Box<dyn std::error::Error>> {
let file = assert_fs::NamedTempFile::new("sample.txt")?;
file.write_str("A test\nActual content\nMore content\nAnother test")?;
let mut cmd = cargo_bin_cmd!("grrs");
cmd.arg("test").arg(file.path());
cmd.assert()
.success()
.stdout(predicate::str::contains("A test\nAnother test"));
Ok(())
}
#[test]
fn find_content_with_cwd_in_tmp_dir() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = assert_fs::TempDir::new()?;
let child_dir = tmp_dir.child("child_dir");
let file = child_dir.child("sample.txt");
file.write_str("A test\nActual content\nMore content\nAnother test")?;
cargo_bin_cmd!("grrs")
.current_dir(&tmp_dir)
.arg("test")
.arg("sample.txt")
.assert()
.failure()
.stderr(predicate::str::contains("could not read file"));
cargo_bin_cmd!("grrs")
.current_dir(&child_dir)
.arg("test")
.arg("sample.txt")
.assert()
.success()
.stdout(predicate::str::contains("A test\nAnother test"));
Ok(())
}