Skip to content

Commit 225f90d

Browse files
committed
cmp: fix '-' extra operand error, clone only when from is None, and add integration tests
1 parent 8f700f9 commit 225f90d

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎src/cmp.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
144144
let mut last_arg = params.executable.clone();
145145

146146
while let Some(param) = opts.next() {
147-
last_arg = param.clone();
147+
if from.is_none() {
148+
last_arg = param.clone();
149+
}
148150

149151
if param == "--" {
150152
break;
@@ -157,7 +159,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
157159
} else {
158160
return Err(usage_error(
159161
&executable_str,
160-
"invalid --ignore-initial value '-'",
162+
"extra operand '-'",
161163
));
162164
}
163165
continue;

‎tests/by-util/test_cmp.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,43 @@ fn cmp_difference_term_follows_messages_locale(
336336
.fails_with_code(1)
337337
.stdout_is(format!("a b differ: {term} 1, line 1\n"));
338338
}
339+
340+
#[rstest]
341+
#[case::missing_operand(
342+
&[],
343+
"cmp: missing operand after 'cmp'\ncmp: Try 'cmp --help' for more information.\n"
344+
)]
345+
#[case::missing_operand_after_flag(
346+
&["-s"],
347+
"cmp: missing operand after '-s'\ncmp: Try 'cmp --help' for more information.\n"
348+
)]
349+
#[case::unrecognized_option(
350+
&["--bogus", "a", "b"],
351+
"cmp: unrecognized option '--bogus'\ncmp: Try 'cmp --help' for more information.\n"
352+
)]
353+
#[case::extra_operand(
354+
&["a", "b", "3", "4", "extra"],
355+
"cmp: extra operand 'extra'\ncmp: Try 'cmp --help' for more information.\n"
356+
)]
357+
#[case::extra_operand_after_dashdash(
358+
&["--", "a", "b", "3", "4", "extra"],
359+
"cmp: extra operand 'extra'\ncmp: Try 'cmp --help' for more information.\n"
360+
)]
361+
#[case::n_requires_argument(
362+
&["a", "b", "-n"],
363+
"cmp: option requires an argument -- 'n'\ncmp: Try 'cmp --help' for more information.\n"
364+
)]
365+
#[case::i_requires_argument(
366+
&["a", "b", "-i"],
367+
"cmp: option requires an argument -- 'i'\ncmp: Try 'cmp --help' for more information.\n"
368+
)]
369+
#[case::extra_dash_operand(
370+
&["a", "b", "1", "2", "-"],
371+
"cmp: extra operand '-'\ncmp: Try 'cmp --help' for more information.\n"
372+
)]
373+
fn cmp_usage_errors_match_gnu(#[case] args: &[&str], #[case] expected_stderr: &str) {
374+
new_ucmd!()
375+
.args(args)
376+
.fails_with_code(2)
377+
.stderr_is(expected_stderr);
378+
}

0 commit comments

Comments
 (0)