Skip to content

Commit 670a926

Browse files
authored
sort: point the caret at the failing part of the -S size
1 parent 5aea503 commit 670a926

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

docs/src/extensions-errors.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ the difference:
276276
| `mknod` | the failing part of the mode given to `-m`/`--mode` | [`mknod -m u+q mydev c 1 3`](https://uutils.org/playground/?cmd=mknod+-m+u%2Bq+mydev+c+1+3) |
277277
| `install`| the failing part of the mode given to `-m`/`--mode` | [`install -m u+q fruits.txt dest`](https://uutils.org/playground/?cmd=install+-m+u%2Bq+fruits.txt+dest) |
278278
| `tr` | the part of a set that is at fault (bad class, backwards range, bad repeat count, …) | [`tr 'qw[y-b]' x`](https://uutils.org/playground/?cmd=tr+%27qw%5By-b%5D%27+x) |
279-
| `sort` | the failing part of a `-k`/`--key` or field specification | [`sort -k2.3x fruits.txt`](https://uutils.org/playground/?cmd=sort+-k2.3x+fruits.txt) |
279+
| `sort` | the failing part of a `-k`/`--key` or field specification, or of the SIZE given to `-S` | [`sort -k2.3x fruits.txt`](https://uutils.org/playground/?cmd=sort+-k2.3x+fruits.txt) |
280280
| `numfmt` | the failing part of a `--field` or `--format` specification | [`numfmt --format=%q 1000`](https://uutils.org/playground/?cmd=numfmt+--format%3D%25q+1000) |
281281
| `printf` | the failing conversion or escape in the format string | [`printf %5.2c q`](https://uutils.org/playground/?cmd=printf+%255.2c+q) |
282282
| `env` | the failing part of a `-S`/`--split-string` string | [`env -S 'echo ${1FOO}'`](https://uutils.org/playground/?cmd=env+-S+%27echo+%24%7B1FOO%7D%27) |
@@ -335,7 +335,7 @@ repeated per utility. Three parsers work this way:
335335
`numfmt --field`. `Range::from_list` reports which item of the list failed
336336
and where it sat.
337337
- **Sizes** (`uucore::parser::parse_size`), for `head`, `tail`, `truncate`,
338-
`split`, `shred` and `stdbuf` today, and available to the other callers of the parser.
338+
`split`, `shred`, `stdbuf` and `sort` today, and available to the other callers of the parser.
339339
`ParseSizeError::span` works out from the operand which of its two parts —
340340
the number or the unit — was rejected, so the error type keeps the shape its
341341
callers build by hand.

src/uu/sort/src/sort.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,8 +2228,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
22282228
}
22292229

22302230
if let Some(size_str) = matches.get_one::<String>(options::BUF_SIZE) {
2231-
settings.buffer_size = GlobalSettings::parse_byte_count(size_str).map_err(|e| {
2232-
USimpleError::new(2, format_error_message(&e, size_str, options::BUF_SIZE))
2231+
settings.buffer_size = GlobalSettings::parse_byte_count(size_str).map_err(|error| {
2232+
let message = format_error_message(&error, size_str, options::BUF_SIZE);
2233+
error.size_value_error(
2234+
key_args.as_deref(),
2235+
size_str,
2236+
0,
2237+
'S',
2238+
options::BUF_SIZE,
2239+
&message,
2240+
USimpleError::new(2, message.clone()),
2241+
)
22332242
})?;
22342243
settings.buffer_size_is_explicit = true;
22352244
} else {

tests/by-util/test_sort.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3361,6 +3361,30 @@ sort: invalid number at field start: invalid count at start of 'sort'
33613361
);
33623362
}
33633363

3364+
#[cfg(unix)]
3365+
#[test]
3366+
fn test_snippet_points_at_the_unknown_unit_of_a_buffer_size() {
3367+
let result = new_ucmd!()
3368+
.terminal_sim_stderr()
3369+
.args(&["-S", "8zz", "/dev/null"])
3370+
.fails_with_code(2);
3371+
3372+
// The number parsed; only the unit did not.
3373+
assert_eq!(
3374+
result.stderr_as_displayed(),
3375+
"\
3376+
sort: invalid suffix in --buffer-size argument '8zz'
3377+
╭─[ sort:1:10 ]
3378+
3379+
1 │ sort -S 8zz /dev/null
3380+
│ ─┬
3381+
│ ╰── not a known unit
3382+
3383+
│ Help: a size is a number and an optional unit: K, M, G and so on for 1024, KB, MB, GB for 1000
3384+
───╯"
3385+
);
3386+
}
3387+
33643388
#[test]
33653389
fn test_plain_message_when_stderr_is_not_a_terminal() {
33663390
// The test harness pipes stderr, so the report must not appear.
@@ -3370,6 +3394,10 @@ sort: invalid number at field start: invalid count at start of 'sort'
33703394
.stderr_only(
33713395
"sort: stray character in field spec: invalid field specification '2.3q'\n",
33723396
);
3397+
new_ucmd!()
3398+
.args(&["-S", "8zz", "/dev/null"])
3399+
.fails_with_code(2)
3400+
.stderr_only("sort: invalid suffix in --buffer-size argument '8zz'\n");
33733401
}
33743402
}
33753403

0 commit comments

Comments
 (0)