Skip to content

Commit 34a5cc7

Browse files
authored
Merge pull request #45 from oSoMoN/stdin-tests
Unit and integration tests for reading data from standard input
2 parents 25e4a17 + 6a152cd commit 34a5cc7

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

src/params.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,40 @@ mod tests {
457457
);
458458
}
459459
#[test]
460+
fn default_to_stdin() {
461+
assert_eq!(
462+
Ok(Params {
463+
from: os("foo"),
464+
to: os("/dev/stdin"),
465+
..Default::default()
466+
}),
467+
parse_params([os("diff"), os("foo"), os("-")].iter().cloned())
468+
);
469+
assert_eq!(
470+
Ok(Params {
471+
from: os("/dev/stdin"),
472+
to: os("bar"),
473+
..Default::default()
474+
}),
475+
parse_params([os("diff"), os("-"), os("bar")].iter().cloned())
476+
);
477+
assert_eq!(
478+
Ok(Params {
479+
from: os("/dev/stdin"),
480+
to: os("/dev/stdin"),
481+
..Default::default()
482+
}),
483+
parse_params([os("diff"), os("-"), os("-")].iter().cloned())
484+
);
485+
assert!(parse_params([os("diff"), os("foo"), os("bar"), os("-")].iter().cloned()).is_err());
486+
assert!(parse_params([os("diff"), os("-"), os("-"), os("-")].iter().cloned()).is_err());
487+
}
488+
#[test]
489+
fn missing_arguments() {
490+
assert!(parse_params([os("diff")].iter().cloned()).is_err());
491+
assert!(parse_params([os("diff"), os("foo")].iter().cloned()).is_err());
492+
}
493+
#[test]
460494
fn unknown_argument() {
461495
assert!(
462496
parse_params([os("diff"), os("-g"), os("foo"), os("bar")].iter().cloned()).is_err()

tests/integration.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
// For the full copyright and license information, please view the LICENSE-*
44
// files that was distributed with this source code.
55

6-
use assert_cmd::prelude::*;
6+
use assert_cmd::cmd::Command;
77
use predicates::prelude::*;
88
use std::io::Write;
9-
use std::process::Command;
109
use tempfile::NamedTempFile;
1110

1211
// Integration tests for the diffutils command
@@ -161,3 +160,46 @@ fn missing_newline() -> Result<(), Box<dyn std::error::Error>> {
161160
.stderr(predicate::str::starts_with("No newline at end of file"));
162161
Ok(())
163162
}
163+
164+
#[test]
165+
fn read_from_stdin() -> Result<(), Box<dyn std::error::Error>> {
166+
let mut file1 = NamedTempFile::new()?;
167+
file1.write_all("foo\n".as_bytes())?;
168+
let mut file2 = NamedTempFile::new()?;
169+
file2.write_all("bar\n".as_bytes())?;
170+
171+
let mut cmd = Command::cargo_bin("diffutils")?;
172+
cmd.arg("-u")
173+
.arg(file1.path())
174+
.arg("-")
175+
.write_stdin("bar\n");
176+
cmd.assert()
177+
.code(predicate::eq(1))
178+
.failure()
179+
.stdout(predicate::eq(format!(
180+
"--- {}\t\n+++ /dev/stdin\t\n@@ -1 +1 @@\n-foo\n+bar\n",
181+
file1.path().to_string_lossy()
182+
)));
183+
184+
let mut cmd = Command::cargo_bin("diffutils")?;
185+
cmd.arg("-u")
186+
.arg("-")
187+
.arg(file2.path())
188+
.write_stdin("foo\n");
189+
cmd.assert()
190+
.code(predicate::eq(1))
191+
.failure()
192+
.stdout(predicate::eq(format!(
193+
"--- /dev/stdin\t\n+++ {}\t\n@@ -1 +1 @@\n-foo\n+bar\n",
194+
file2.path().to_string_lossy()
195+
)));
196+
197+
let mut cmd = Command::cargo_bin("diffutils")?;
198+
cmd.arg("-u").arg("-").arg("-").write_stdin("foo\n");
199+
cmd.assert()
200+
.code(predicate::eq(0))
201+
.success()
202+
.stdout(predicate::str::is_empty());
203+
204+
Ok(())
205+
}

0 commit comments

Comments
 (0)