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
156 changes: 108 additions & 48 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
let mut format = None;
let mut context = None;
let mut show_c_function = false;
let tabsize_re = Regex::new(r"^--tabsize=(?<num>\d+)$").unwrap();
let width_re = Regex::new(r"^--width=(?P<long>\d+)$").unwrap();
while let Some((position, param)) = opts.next() {
// -L LABEL, -LLABEL, --label LABEL or --label=LABEL: the first names
// the from-file in headers and messages, the second the to-file.
Expand Down Expand Up @@ -242,7 +240,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
push_function_line_pattern(&mut params, C_FUNCTION_PATTERN)?;
continue;
}
if let Some(argument) = option_argument(&param, "-F", "--show-function-line") {
if let Some(argument) = option_argument(&param, Some("-F"), "--show-function-line") {
let pattern = argument_value(argument, &param, &mut opts)?;
push_function_line_pattern(&mut params, &pattern.to_string_lossy())?;
continue;
Expand All @@ -259,7 +257,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
params.ignore_file_name_case = false;
continue;
}
if let Some(argument) = option_argument(&param, "-S", "--starting-file") {
if let Some(argument) = option_argument(&param, Some("-S"), "--starting-file") {
params.starting_file = Some(argument_value(argument, &param, &mut opts)?);
continue;
}
Expand Down Expand Up @@ -296,37 +294,21 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
format = Some(Format::SideBySide);
continue;
}
if width_re.is_match(param.to_string_lossy().as_ref()) {
let param = param.into_string().unwrap();
let width_str: &str = width_re
.captures(param.as_str())
.unwrap()
.name("long")
.unwrap()
.as_str();
if let Some(argument) = option_argument(&param, Some("-W"), "--width") {
let width = argument_value(argument, &param, &mut opts)?;
let width_str = width.to_string_lossy();

params.width = match width_str.parse::<usize>() {
Ok(num) => {
if num == 0 {
return Err("invalid width «0»".to_string());
}

num
}
Err(_) => return Err(format!("invalid width «{width_str}»")),
Ok(num) if num != 0 => num,
_ => return Err(format!("invalid width «{width_str}»")),
};

continue;
}
if tabsize_re.is_match(param.to_string_lossy().as_ref()) {
// Because param matches the regular expression,
// it is safe to assume it is valid UTF-8.
let param = param.into_string().unwrap();
let tabsize_str = tabsize_re
.captures(param.as_str())
.unwrap()
.name("num")
.unwrap()
.as_str();
if let Some(argument) = option_argument(&param, None, "--tabsize") {
let tabsize = argument_value(argument, &param, &mut opts)?;
let tabsize_str = tabsize.to_string_lossy();

params.tabsize = match tabsize_str.parse::<usize>() {
Ok(num) if num != 0 && num <= MAX_TABSIZE => num,
_ => return Err(format!("invalid tabsize «{tabsize_str}»")),
Expand Down Expand Up @@ -480,15 +462,23 @@ enum OptionArgument<'a> {

/// Returns how the argument of the option named `short` (like `-F`) or
/// `long` is given, if `param` is that option.
fn option_argument<'a>(param: &'a OsStr, short: &str, long: &str) -> Option<OptionArgument<'a>> {
if param == short || param == long {
fn option_argument<'a>(
param: &'a OsStr,
short: Option<&str>,
long: &str,
) -> Option<OptionArgument<'a>> {
if param == long || short.is_some_and(|short| param == short) {
return Some(OptionArgument::Separate);
}

let param = param.to_str()?;

param
.strip_prefix(long)
.and_then(|rest| rest.strip_prefix('='))
.or_else(|| param.strip_prefix(short).filter(|value| !value.is_empty()))
.or_else(|| {
short.and_then(|short| param.strip_prefix(short).filter(|value| !value.is_empty()))
})
.map(OptionArgument::Attached)
}

Expand Down Expand Up @@ -1010,6 +1000,37 @@ mod tests {
}
#[test]
fn tabsize() {
// Separate argument.
assert_eq!(
Ok(Params {
executable: os("diff"),
from: os("foo"),
to: os("bar"),
tabsize: 42,
..Default::default()
}),
parse_params(
[os("diff"), os("--tabsize"), os("42"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
// Missing argument.
assert!(parse_params([os("diff"), os("--tabsize")].iter().cloned().peekable()).is_err());
assert!(parse_params(
[
os("diff"),
os("--tabsize"),
os("invalid"),
os("foo"),
os("bar")
]
.iter()
.cloned()
.peekable()
)
.is_err());
assert_eq!(
Ok(Params {
executable: os("diff"),
Expand Down Expand Up @@ -1140,21 +1161,60 @@ mod tests {
}
#[test]
fn width() {
assert_eq!(
Ok(Params {
executable: os("diff"),
from: os("foo"),
to: os("bar"),
width: 100,
..Default::default()
}),
parse_params(
[os("diff"), os("--width=100"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
let parse = |args: &[&str]| {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add the test from the issue in tests/by-util/test_diff.rs, so the real binary is checked with -y -W 20

let args = std::iter::once("diff")
.chain(args.iter().copied())
.chain(["foo", "bar"]);

parse_params(args.map(os).peekable()).map(|params| params.width)
};

// All supported forms of the width option.
for args in [
&["-W", "100"][..],
&["-W100"][..],
&["--width=100"][..],
&["--width", "100"][..],
] {
assert_eq!(Ok(100), parse(args), "{args:?}");
}

// Zero is not a valid width.
for args in [
&["-W", "0"][..],
&["-W0"][..],
&["--width=0"][..],
&["--width", "0"][..],
] {
assert_eq!(
Err("invalid width «0»".to_string()),
parse(args),
"{args:?}"
);
}

// Non-numeric values are rejected.
for args in [
&["-W", "abc"][..],
&["-Wabc"][..],
&["--width=abc"][..],
&["--width", "abc"][..],
] {
assert_eq!(
Err("invalid width «abc»".to_string()),
parse(args),
"{args:?}"
);
}

// Missing option arguments are rejected.
for option in ["-W", "--width"] {
assert_eq!(
Err(format!("option '{option}' requires an argument")),
parse_params([os("diff"), os(option)].into_iter().peekable())
.map(|params| params.width)
);
}
}
#[test]
fn labels() {
Expand Down
27 changes: 27 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,33 @@ fn sdiff_gutter_marker_column(#[case] options: &[&str], #[case] expected: &str)
.stdout_is(expected);
}

// Regression for #396: -W must accept a separate argument.
#[test]
fn sdiff_width_separate_argument() {
let (at, ts) = at_and_ts!();
at.write("file1", "aa\n");
at.write("file2", "bb\n");

ts.ucmd()
.args(&["-y", "-W", "20", "file1", "file2"])
.run()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please use .fails_with_code(1) instead of .run()? so we also check the exit code

.stdout_is("aa <\n >\tbb\n");
}

// Regression for #396: --tabsize must accept a separate argument.
#[test]
fn tabsize_separate_argument() {
let (at, ts) = at_and_ts!();
at.write("file1", "same\n");
at.write("file2", "same\n");

ts.ucmd()
.args(&["--tabsize", "4", "file1", "file2"])
.succeeds()
.no_stdout()
.no_stderr();
}

#[rstest]
#[case::delete_and_append(
"north\neast\nsouth\nwest\n",
Expand Down
Loading