Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {
return Ok(Cmp::Different);
}

let smaller = cmp::min(a_size, b_size) as BytesLimitU64;
// The offsets count from the end of the skipped bytes.
let a_left = a_size.saturating_sub(params.skip_a.unwrap_or(0));
let b_left = b_size.saturating_sub(params.skip_b.unwrap_or(0));
let smaller = cmp::min(a_left, b_left) as BytesLimitU64;
offset_width = cmp::min(smaller, offset_width);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/by-util/test_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ fn cmp_verbose_zero_length_metadata() -> Result<(), Box<dyn std::error::Error>>
Ok(())
}

// The offset column is as wide as the bytes left after the skips, as in GNU
// cmp: 9 bytes are compared here, so the offsets take one column.
#[test]
fn cmp_verbose_offset_width_after_skip() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
let a_path = tmp_dir.path().join("a");
let b_path = tmp_dir.path().join("b");
File::create(&a_path)?.write_all(b"0123456789abcdefghijklmnopqrstuvwxyz")?;
File::create(&b_path)?.write_all(b"0123456789abcdefghijklmnopqrstuvwxyz")?;

let mut cmd = cargo_bin_cmd!("diffutils");
cmd.env("LC_ALL", "C");
cmd.arg("cmp")
.arg("-l")
.arg("--ignore-initial=27:26")
.arg(&a_path)
.arg(&b_path);
cmd.assert()
.code(predicate::eq(1))
.stdout(predicate::str::starts_with("1 162 161\n2 163 162\n"))
.stderr(predicate::str::ends_with(" after byte 9\n"));
Ok(())
}

#[test]
fn cmp_incompatible_params() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = cargo_bin_cmd!("diffutils");
Expand Down
Loading