Skip to content

Commit ac1d824

Browse files
committed
Handle the rewrite of "-" to "/dev/stdin" in main to leave the filenames unchanged (fixes #46)
1 parent 34a5cc7 commit ac1d824

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

src/main.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use crate::params::{parse_params, Format};
77
use std::env;
8-
8+
use std::ffi::OsString;
99
use std::fs;
1010
use std::io::{self, Write};
1111
use std::process::{exit, ExitCode};
@@ -38,19 +38,25 @@ fn main() -> ExitCode {
3838
)
3939
}
4040
};
41-
if same_file::is_same_file(&params.from, &params.to).unwrap_or(false) {
41+
if params.from == "-" && params.to == "-"
42+
|| same_file::is_same_file(&params.from, &params.to).unwrap_or(false)
43+
{
4244
maybe_report_identical_files();
4345
return ExitCode::SUCCESS;
4446
}
4547
// read files
46-
let from_content = match fs::read(&params.from) {
48+
fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> {
49+
let stdin = OsString::from("/dev/stdin");
50+
fs::read(if filepath == "-" { &stdin } else { filepath })
51+
}
52+
let from_content = match read_file_contents(&params.from) {
4753
Ok(from_content) => from_content,
4854
Err(e) => {
4955
eprintln!("Failed to read from-file: {e}");
5056
return ExitCode::from(2);
5157
}
5258
};
53-
let to_content = match fs::read(&params.to) {
59+
let to_content = match read_file_contents(&params.to) {
5460
Ok(to_content) => to_content,
5561
Err(e) => {
5662
eprintln!("Failed to read to-file: {e}");

src/params.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
6767
}
6868
if param == "-" {
6969
if from.is_none() {
70-
from = Some(OsString::from("/dev/stdin"));
70+
from = Some(param);
7171
} else if to.is_none() {
72-
to = Some(OsString::from("/dev/stdin"));
72+
to = Some(param);
7373
} else {
7474
return Err(format!("Usage: {} <from> <to>", exe.to_string_lossy()));
7575
}
@@ -461,23 +461,23 @@ mod tests {
461461
assert_eq!(
462462
Ok(Params {
463463
from: os("foo"),
464-
to: os("/dev/stdin"),
464+
to: os("-"),
465465
..Default::default()
466466
}),
467467
parse_params([os("diff"), os("foo"), os("-")].iter().cloned())
468468
);
469469
assert_eq!(
470470
Ok(Params {
471-
from: os("/dev/stdin"),
471+
from: os("-"),
472472
to: os("bar"),
473473
..Default::default()
474474
}),
475475
parse_params([os("diff"), os("-"), os("bar")].iter().cloned())
476476
);
477477
assert_eq!(
478478
Ok(Params {
479-
from: os("/dev/stdin"),
480-
to: os("/dev/stdin"),
479+
from: os("-"),
480+
to: os("-"),
481481
..Default::default()
482482
}),
483483
parse_params([os("diff"), os("-"), os("-")].iter().cloned())

tests/integration.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,30 @@ fn unknown_param() -> Result<(), Box<dyn std::error::Error>> {
2222
}
2323

2424
#[test]
25-
fn cannot_read_from_file() -> Result<(), Box<dyn std::error::Error>> {
25+
fn cannot_read_files() -> Result<(), Box<dyn std::error::Error>> {
26+
let file = NamedTempFile::new()?;
27+
2628
let mut cmd = Command::cargo_bin("diffutils")?;
27-
cmd.arg("foo.txt").arg("bar.txt");
29+
cmd.arg("foo.txt").arg(file.path());
2830
cmd.assert()
2931
.code(predicate::eq(2))
3032
.failure()
3133
.stderr(predicate::str::starts_with("Failed to read from-file"));
32-
Ok(())
33-
}
3434

35-
#[test]
36-
fn cannot_read_to_file() -> Result<(), Box<dyn std::error::Error>> {
37-
let file = NamedTempFile::new()?;
3835
let mut cmd = Command::cargo_bin("diffutils")?;
39-
cmd.arg(file.path()).arg("bar.txt");
36+
cmd.arg(file.path()).arg("foo.txt");
4037
cmd.assert()
4138
.code(predicate::eq(2))
4239
.failure()
4340
.stderr(predicate::str::starts_with("Failed to read to-file"));
41+
42+
let mut cmd = Command::cargo_bin("diffutils")?;
43+
cmd.arg("foo.txt").arg("foo.txt");
44+
cmd.assert()
45+
.code(predicate::eq(2))
46+
.failure()
47+
.stderr(predicate::str::starts_with("Failed to read from-file"));
48+
4449
Ok(())
4550
}
4651

@@ -177,7 +182,7 @@ fn read_from_stdin() -> Result<(), Box<dyn std::error::Error>> {
177182
.code(predicate::eq(1))
178183
.failure()
179184
.stdout(predicate::eq(format!(
180-
"--- {}\t\n+++ /dev/stdin\t\n@@ -1 +1 @@\n-foo\n+bar\n",
185+
"--- {}\t\n+++ -\t\n@@ -1 +1 @@\n-foo\n+bar\n",
181186
file1.path().to_string_lossy()
182187
)));
183188

@@ -190,12 +195,12 @@ fn read_from_stdin() -> Result<(), Box<dyn std::error::Error>> {
190195
.code(predicate::eq(1))
191196
.failure()
192197
.stdout(predicate::eq(format!(
193-
"--- /dev/stdin\t\n+++ {}\t\n@@ -1 +1 @@\n-foo\n+bar\n",
198+
"--- -\t\n+++ {}\t\n@@ -1 +1 @@\n-foo\n+bar\n",
194199
file2.path().to_string_lossy()
195200
)));
196201

197202
let mut cmd = Command::cargo_bin("diffutils")?;
198-
cmd.arg("-u").arg("-").arg("-").write_stdin("foo\n");
203+
cmd.arg("-u").arg("-").arg("-");
199204
cmd.assert()
200205
.code(predicate::eq(0))
201206
.success()

0 commit comments

Comments
 (0)