Skip to content

Commit f6eb083

Browse files
Merge branch 'main' into context-diff-modification-time
2 parents 72da7fc + be66ff3 commit f6eb083

File tree

5 files changed

+67
-25
lines changed

5 files changed

+67
-25
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ unicode-width = "0.1.11"
2525
pretty_assertions = "1"
2626
assert_cmd = "2.0.14"
2727
predicates = "3.1.0"
28-
tempfile = "3.10.0"
28+
tempfile = "3.10.1"
2929

3030
# The profile that 'cargo dist' will build with
3131
[profile.dist]

src/main.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
use crate::params::{parse_params, Format};
77
use std::env;
8-
8+
use std::ffi::OsString;
99
use std::fs;
10-
use std::io::{self, Write};
10+
use std::io::{self, Read, Write};
1111
use std::process::{exit, ExitCode};
1212

1313
mod context_diff;
@@ -38,19 +38,29 @@ 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+
if filepath == "-" {
50+
let mut content = Vec::new();
51+
io::stdin().read_to_end(&mut content).and(Ok(content))
52+
} else {
53+
fs::read(filepath)
54+
}
55+
}
56+
let from_content = match read_file_contents(&params.from) {
4757
Ok(from_content) => from_content,
4858
Err(e) => {
4959
eprintln!("Failed to read from-file: {e}");
5060
return ExitCode::from(2);
5161
}
5262
};
53-
let to_content = match fs::read(&params.to) {
63+
let to_content = match read_file_contents(&params.to) {
5464
Ok(to_content) => to_content,
5565
Err(e) => {
5666
eprintln!("Failed to read to-file: {e}");

src/params.rs

Lines changed: 17 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())
@@ -502,4 +502,15 @@ mod tests {
502502
fn empty() {
503503
assert!(parse_params([].iter().cloned()).is_err());
504504
}
505+
#[test]
506+
fn conflicting_output_styles() {
507+
for (arg1, arg2) in [("-u", "-c"), ("-u", "-e"), ("-c", "-u"), ("-c", "-U42")] {
508+
assert!(parse_params(
509+
[os("diff"), os(arg1), os(arg2), os("foo"), os("bar")]
510+
.iter()
511+
.cloned()
512+
)
513+
.is_err());
514+
}
515+
}
505516
}

tests/integration.rs

Lines changed: 32 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,16 +195,32 @@ 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()
202207
.stdout(predicate::str::is_empty());
203208

209+
#[cfg(unix)]
210+
{
211+
let mut cmd = Command::cargo_bin("diffutils")?;
212+
cmd.arg("-u")
213+
.arg(file1.path())
214+
.arg("/dev/stdin")
215+
.write_stdin("bar\n");
216+
cmd.assert()
217+
.code(predicate::eq(1))
218+
.failure()
219+
.stdout(predicate::eq(format!(
220+
"--- {}\t\n+++ /dev/stdin\t\n@@ -1 +1 @@\n-foo\n+bar\n",
221+
file1.path().to_string_lossy()
222+
)));
223+
}
224+
204225
Ok(())
205226
}

0 commit comments

Comments
 (0)