Skip to content

Commit 86bd05c

Browse files
Merge branch 'context-diff-modification-time' of github.com:TanmayPatil105/diffutils into context-diff-modification-time
2 parents 00e18a6 + f6eb083 commit 86bd05c

File tree

5 files changed

+70
-25
lines changed

5 files changed

+70
-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;
@@ -39,19 +39,29 @@ fn main() -> ExitCode {
3939
);
4040
}
4141
};
42-
if same_file::is_same_file(&params.from, &params.to).unwrap_or(false) {
42+
if params.from == "-" && params.to == "-"
43+
|| same_file::is_same_file(&params.from, &params.to).unwrap_or(false)
44+
{
4345
maybe_report_identical_files();
4446
return ExitCode::SUCCESS;
4547
}
4648
// read files
47-
let from_content = match fs::read(&params.from) {
49+
fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> {
50+
if filepath == "-" {
51+
let mut content = Vec::new();
52+
io::stdin().read_to_end(&mut content).and(Ok(content))
53+
} else {
54+
fs::read(filepath)
55+
}
56+
}
57+
let from_content = match read_file_contents(&params.from) {
4858
Ok(from_content) => from_content,
4959
Err(e) => {
5060
eprintln!("Failed to read from-file: {e}");
5161
return ExitCode::from(2);
5262
}
5363
};
54-
let to_content = match fs::read(&params.to) {
64+
let to_content = match read_file_contents(&params.to) {
5565
Ok(to_content) => to_content,
5666
Err(e) => {
5767
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: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,30 @@ fn unknown_param() -> Result<(), Box<dyn std::error::Error>> {
2323
}
2424

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

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

@@ -180,7 +185,7 @@ fn read_from_stdin() -> Result<(), Box<dyn std::error::Error>> {
180185
assert_diff_eq!(
181186
output,
182187
format!(
183-
"--- {}\t\n+++ /dev/stdin\t\n@@ -1 +1 @@\n-foo\n+bar\n",
188+
"--- {}\t\n+++ -\t\n@@ -1 +1 @@\n-foo\n+bar\n",
184189
file1.path().to_string_lossy()
185190
)
186191
);
@@ -196,17 +201,36 @@ fn read_from_stdin() -> Result<(), Box<dyn std::error::Error>> {
196201
assert_diff_eq!(
197202
output,
198203
format!(
199-
"--- /dev/stdin\t\n+++ {}\t\n@@ -1 +1 @@\n-foo\n+bar\n",
204+
"--- -\t\n+++ {}\t\n@@ -1 +1 @@\n-foo\n+bar\n",
200205
file2.path().to_string_lossy()
201206
)
202207
);
203208

204209
let mut cmd = Command::cargo_bin("diffutils")?;
205-
cmd.arg("-u").arg("-").arg("-").write_stdin("foo\n");
210+
cmd.arg("-u").arg("-").arg("-");
206211
cmd.assert()
207212
.code(predicate::eq(0))
208213
.success()
209214
.stdout(predicate::str::is_empty());
210215

216+
#[cfg(unix)]
217+
{
218+
let mut cmd = Command::cargo_bin("diffutils")?;
219+
cmd.arg("-u")
220+
.arg(file1.path())
221+
.arg("/dev/stdin")
222+
.write_stdin("bar\n");
223+
cmd.assert().code(predicate::eq(1)).failure();
224+
225+
let output = cmd.output().unwrap().stdout;
226+
assert_diff_eq!(
227+
output,
228+
format!(
229+
"--- {}\t\n+++ /dev/stdin\t\n@@ -1 +1 @@\n-foo\n+bar\n",
230+
file1.path().to_string_lossy()
231+
)
232+
);
233+
}
234+
211235
Ok(())
212236
}

0 commit comments

Comments
 (0)