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
84 changes: 66 additions & 18 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
let skip = parse_size(&skip_desc, "ignore-initial", &skip_desc)?;
(skip, skip)
};
params.skip_a = Some(skip_a);
params.skip_b = Some(skip_b);
params.skip_a = params.skip_a.max(Some(skip_a));
params.skip_b = params.skip_b.max(Some(skip_b));
continue;
}
if param == "-s" || param == "--quiet" || param == "--silent" {
Expand Down Expand Up @@ -253,24 +253,23 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
OsString::from("-")
};

// GNU cmp ignores positional skip arguments if -i is provided.
if params.skip_a.is_none() {
if skip_pos1.is_some() {
params.skip_a = skip_pos1;
} else if let Some(param) = opts.next() {
if skip_pos1.is_none() {
if let Some(param) = opts.next() {
let param_str = param.to_string_lossy().to_string();
params.skip_a = Some(parse_size(&param_str, "ignore-initial", &param_str)?);
skip_pos1 = Some(parse_size(&param_str, "ignore-initial", &param_str)?);
}
};
if params.skip_b.is_none() {
if skip_pos2.is_some() {
params.skip_b = skip_pos2;
} else if let Some(param) = opts.next() {
}
if skip_pos2.is_none() {
if let Some(param) = opts.next() {
let param_str = param.to_string_lossy().to_string();
params.skip_b = Some(parse_size(&param_str, "ignore-initial", &param_str)?);
skip_pos2 = Some(parse_size(&param_str, "ignore-initial", &param_str)?);
}
}

// Like GNU cmp, use the larger of the -i value and the positional skip.
params.skip_a = params.skip_a.max(skip_pos1);
params.skip_b = params.skip_b.max(skip_pos2);

Ok(params)
}

Expand Down Expand Up @@ -1140,21 +1139,21 @@ mod tests {
}
}

// Ignores positional arguments when -i is provided.
// Uses the larger of the -i value and the positional skip, like GNU.
assert_eq!(
Ok(Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
skip_a: Some(1),
skip_b: Some(2),
skip_a: Some(5),
skip_b: Some(4),
..Default::default()
}),
parse_params(
[
os("cmp"),
os("-i"),
os("1:2"),
os("5:2"),
os("foo"),
os("bar"),
os("3"),
Expand All @@ -1165,6 +1164,55 @@ mod tests {
.peekable()
)
);
assert_eq!(
Ok(Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
skip_a: Some(6),
skip_b: Some(1),
..Default::default()
}),
parse_params(
[
os("cmp"),
os("-i"),
os("1"),
os("--"),
os("foo"),
os("bar"),
os("6")
]
.iter()
.cloned()
.peekable()
)
);

// Also the larger value when -i is given more than once.
assert_eq!(
Ok(Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
skip_a: Some(2),
skip_b: Some(1),
..Default::default()
}),
parse_params(
[
os("cmp"),
os("-i"),
os("1"),
os("--ignore-initial=2:0"),
os("foo"),
os("bar")
]
.iter()
.cloned()
.peekable()
)
);

// Failure cases
assert_eq!(
Expand Down
58 changes: 57 additions & 1 deletion tests/by-util/test_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ fn cmp_skip_args_parsing() -> Result<(), Box<dyn std::error::Error>> {
.stderr(predicate::str::is_empty())
.stdout(predicate::str::is_empty());

// Positional skips should be ignored
// A smaller positional skip does not override -i
let mut cmd = cargo_bin_cmd!("diffutils");
cmd.env("LC_ALL", "C");
cmd.arg("cmp");
Expand Down Expand Up @@ -407,6 +407,62 @@ fn cmp_skip_args_parsing() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[test]
fn cmp_skip_uses_larger_of_option_and_positional() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;

let a_path = tmp_dir.path().join("a");
let mut a = File::create(&a_path).unwrap();
a.write_all(b"12345xyz\n").unwrap();

let b_path = tmp_dir.path().join("b");
let mut b = File::create(&b_path).unwrap();
b.write_all(b"abcdexyz\n").unwrap();

// Like GNU cmp, the larger of -i and the positional skip wins.
let mut cmd = cargo_bin_cmd!("diffutils");
cmd.env("LC_ALL", "C");
cmd.arg("cmp");
cmd.arg("-i");
cmd.arg("1");
cmd.arg(&a_path).arg(&b_path);
cmd.arg("5").arg("5");
cmd.assert()
.code(predicate::eq(0))
.success()
.stderr(predicate::str::is_empty())
.stdout(predicate::str::is_empty());

// The same applies when -i is given more than once.
let mut cmd = cargo_bin_cmd!("diffutils");
cmd.env("LC_ALL", "C");
cmd.arg("cmp");
cmd.arg("-i");
cmd.arg("5");
cmd.arg("--ignore-initial=1");
cmd.arg(&a_path).arg(&b_path);
cmd.assert()
.code(predicate::eq(0))
.success()
.stderr(predicate::str::is_empty())
.stdout(predicate::str::is_empty());

let mut cmd = cargo_bin_cmd!("diffutils");
cmd.env("LC_ALL", "C");
cmd.arg("cmp");
cmd.arg("-i");
cmd.arg("1");
cmd.arg(&a_path).arg(&b_path);
cmd.arg("2").arg("2");
cmd.assert()
.code(predicate::eq(1))
.failure()
.stderr(predicate::str::is_empty())
.stdout(predicate::str::ends_with(" differ: char 1, line 1\n"));

Ok(())
}

#[test]
fn cmp_skip_suffix_parsing() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
Expand Down
Loading