Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome {
);
}
};
// Side by side and ifdef output print the lines of identical files, so
// they still need the contents.
let prints_identical_lines = matches!(params.format, Format::SideBySide | Format::Ifdef);
// standard input given twice is compared with itself
let stdin_twice = params.from == "-" && params.to == "-";
// if from and to are the same file, no need to perform any comparison
if params.from == "-" && params.to == "-"
|| same_file::is_same_file(&params.from, &params.to).unwrap_or(false)
if !prints_identical_lines
&& (stdin_twice || same_file::is_same_file(&params.from, &params.to).unwrap_or(false))
{
maybe_report_identical_files();
return Outcome::Identical;
Expand Down Expand Up @@ -114,27 +119,33 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome {
vec![]
}
};
let to_content = match read_file_contents(&params.to) {
Ok(to_content) => to_content,
Err(e)
if e.kind() == io::ErrorKind::NotFound && params.missing_file == MissingFile::Empty =>
{
to_missing = true;
vec![]
}
Err(e) => {
report_failure_to_read_input_file(&params.executable, &params.to, &e);
io_error = true;
vec![]
let to_content = if stdin_twice {
// standard input can only be read once
from_content.clone()
} else {
match read_file_contents(&params.to) {
Ok(to_content) => to_content,
Err(e)
if e.kind() == io::ErrorKind::NotFound
&& params.missing_file == MissingFile::Empty =>
{
to_missing = true;
vec![]
}
Err(e) => {
report_failure_to_read_input_file(&params.executable, &params.to, &e);
io_error = true;
vec![]
}
}
};
if io_error {
return Outcome::Trouble;
}

// identical contents need no diff, unless side by side output shows their
// lines or an ed script warns about a missing newline
if from_content == to_content && !matches!(params.format, Format::SideBySide | Format::Ed) {
// identical contents need no diff, unless side by side or ifdef output
// shows their lines or an ed script warns about a missing newline
if from_content == to_content && !prints_identical_lines && params.format != Format::Ed {
maybe_report_identical_files();
return Outcome::Identical;
}
Expand Down
67 changes: 67 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,73 @@ fn no_differences_side_by_side() {
.stdout_is("foo\t\t foo\nbar\t\t bar\n");
}

// Ifdef output of two identical files is the file itself.
#[test]
fn no_differences_ifdef() {
let (at, ts) = at_and_ts!();
at.write("file1", "foo\nbar\n");
at.write("file2", "foo\nbar\n");
ts.ucmd()
.args(&["-D", "X", "file1", "file2"])
.succeeds()
.stdout_is("foo\nbar\n");
ts.ucmd()
.args(&["-D", "X", "-s", "file1", "file2"])
.succeeds()
.stdout_is("foo\nbar\nFiles file1 and file2 are identical\n");
}

// Comparing a file with itself still prints its lines in side by side and
// ifdef output.
#[test]
fn same_file_prints_lines() {
let (at, ts) = at_and_ts!();
at.write("file1", "foo\nbar\n");
ts.ucmd()
.args(&["-D", "X", "file1", "file1"])
.succeeds()
.stdout_is("foo\nbar\n");
ts.ucmd()
.args(&["-y", "--width=40", "file1", "file1"])
.succeeds()
.stdout_is("foo\t\t foo\nbar\t\t bar\n");
ts.ucmd()
.args(&["-u", "file1", "file1"])
.succeeds()
.no_stdout();
}

// Standard input given as both files is read once and compared with itself,
// so side by side and ifdef output print its lines, as GNU diff does.
#[test]
fn stdin_twice_ifdef_prints_lines() {
new_ucmd!()
.args(&["-D", "X", "-", "-"])
.pipe_in("foo\nbar\n")
.succeeds()
.stdout_is("foo\nbar\n");
}

#[test]
fn stdin_twice_side_by_side_prints_lines() {
new_ucmd!()
.args(&["-y", "--width=40", "-", "-"])
.pipe_in("foo\nbar\n")
.succeeds()
.stdout_is("foo\t\t foo\nbar\t\t bar\n");
}

#[test]
fn stdin_twice_normal_prints_nothing() {
new_ucmd!()
.args(&["-", "-"])
.pipe_in("foo\nbar\n")
// without lines to print, standard input is not read
.ignore_stdin_write_error()
.succeeds()
.no_stdout();
}

#[rstest]
fn differences(#[values(&[][..], &["-u"], &["-c"], &["-e"])] format: &[&str]) {
let (at, ts) = at_and_ts!();
Expand Down
Loading