Skip to content

Commit 84819d5

Browse files
0630039863abc-uiSocialpranker
authored andcommitted
split: quote a zero SIZE in the invalid-number diagnostic
Every other rejected SIZE is quoted, because parse_size_u64_max quotes the value it could not parse. A SIZE that parses but is zero took a different path and printed the value bare: $ split --lines 0 file split: invalid number of lines: 0 # was split: invalid number of lines: '0' # GNU, and now Same for --bytes and --line-bytes, and for the obsolete 'split -0' spelling.
1 parent 9ff4114 commit 84819d5

2 files changed

Lines changed: 27 additions & 32 deletions

File tree

‎src/uu/split/src/strategy.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ impl Strategy {
266266
if n > 0 {
267267
Ok(strategy(n))
268268
} else {
269-
Err(error(ParseSizeError::ParseFailure(s.to_owned()), origin()))
269+
Err(error(
270+
ParseSizeError::ParseFailure(s.quote().to_string()),
271+
origin(),
272+
))
270273
}
271274
}
272275
// Check that the user is not specifying more than one strategy.
@@ -288,7 +291,7 @@ impl Strategy {
288291
Ok(Self::Lines(v))
289292
} else {
290293
Err(StrategyError::Lines(
291-
ParseSizeError::ParseFailure(v.to_string()),
294+
ParseSizeError::ParseFailure(v.to_string().quote().to_string()),
292295
None,
293296
))
294297
}

‎tests/by-util/test_split.rs‎

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,12 @@ fn test_split_lines_number() {
428428
.ucmd()
429429
.args(&["--lines", "0", "file"])
430430
.fails_with_code(1)
431-
.stderr_only("split: invalid number of lines: 0\n");
431+
.stderr_only("split: invalid number of lines: '0'\n");
432432
scene
433433
.ucmd()
434434
.args(&["-0", "file"])
435435
.fails_with_code(1)
436-
.stderr_only("split: invalid number of lines: 0\n");
436+
.stderr_only("split: invalid number of lines: '0'\n");
437437
scene
438438
.ucmd()
439439
.args(&["--lines", "2fb", "file"])
@@ -1704,37 +1704,29 @@ fn test_round_robin_limited_file_descriptors() {
17041704
.succeeds();
17051705
}
17061706

1707+
/// A zero SIZE or NUMBER is rejected and quoted, as GNU does, in both option forms.
17071708
#[test]
17081709
fn test_split_invalid_input() {
1709-
// Test if stdout/stderr for '--lines' option is correct
17101710
let scene = TestScenario::new(util_name!());
1711-
let at = &scene.fixtures;
1712-
at.touch("file");
1713-
1714-
scene
1715-
.ucmd()
1716-
.args(&["--lines", "0", "file"])
1717-
.fails()
1718-
.no_stdout()
1719-
.stderr_contains("split: invalid number of lines: 0");
1720-
scene
1721-
.ucmd()
1722-
.args(&["-C", "0", "file"])
1723-
.fails()
1724-
.no_stdout()
1725-
.stderr_contains("split: invalid number of bytes: 0");
1726-
scene
1727-
.ucmd()
1728-
.args(&["-b", "0", "file"])
1729-
.fails()
1730-
.no_stdout()
1731-
.stderr_contains("split: invalid number of bytes: 0");
1732-
scene
1733-
.ucmd()
1734-
.args(&["-n", "0", "file"])
1735-
.fails()
1736-
.no_stdout()
1737-
.stderr_contains("split: invalid number of chunks: '0'");
1711+
scene.fixtures.touch("file");
1712+
1713+
for (option, message) in [
1714+
("-l", "invalid number of lines"),
1715+
("--lines", "invalid number of lines"),
1716+
("-b", "invalid number of bytes"),
1717+
("--bytes", "invalid number of bytes"),
1718+
("-C", "invalid number of bytes"),
1719+
("--line-bytes", "invalid number of bytes"),
1720+
("-n", "invalid number of chunks"),
1721+
("--number", "invalid number of chunks"),
1722+
] {
1723+
scene
1724+
.ucmd()
1725+
.args(&[option, "0", "file"])
1726+
.fails_with_code(1)
1727+
.no_stdout()
1728+
.stderr_contains(format!("split: {message}: '0'\n"));
1729+
}
17381730
}
17391731

17401732
/// Test if there are invalid (non UTF-8) in the arguments - unix

0 commit comments

Comments
 (0)