Skip to content

Commit 7f2fb04

Browse files
committed
update tests
1 parent 6eb3e3c commit 7f2fb04

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

tests/by-util/test_sleep.rs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
// file that was distributed with this source code.
55
use rstest::rstest;
66

7-
// spell-checker:ignore dont SIGBUS SIGSEGV sigsegv sigbus
7+
use uucore::display::Quotable;
8+
// spell-checker:ignore dont SIGBUS SIGSEGV sigsegv sigbus infd
89
use uutests::new_ucmd;
910
use uutests::util::TestScenario;
1011
use uutests::util_name;
@@ -19,11 +20,11 @@ fn test_invalid_time_interval() {
1920
new_ucmd!()
2021
.arg("xyz")
2122
.fails()
22-
.usage_error("invalid time interval 'xyz': Invalid input: xyz");
23+
.usage_error("invalid time interval 'xyz'");
2324
new_ucmd!()
2425
.args(&["--", "-1"])
2526
.fails()
26-
.usage_error("invalid time interval '-1': Number was negative");
27+
.usage_error("invalid time interval '-1'");
2728
}
2829

2930
#[test]
@@ -228,16 +229,25 @@ fn test_sleep_when_multiple_inputs_exceed_max_duration_then_no_error() {
228229
#[rstest]
229230
#[case::whitespace_prefix(" 0.1s")]
230231
#[case::multiple_whitespace_prefix(" 0.1s")]
231-
#[case::whitespace_suffix("0.1s ")]
232-
#[case::mixed_newlines_spaces_tabs("\n\t0.1s \n ")]
233-
fn test_sleep_when_input_has_whitespace_then_no_error(#[case] input: &str) {
232+
fn test_sleep_when_input_has_leading_whitespace_then_no_error(#[case] input: &str) {
234233
new_ucmd!()
235234
.arg(input)
236235
.timeout(Duration::from_secs(10))
237236
.succeeds()
238237
.no_output();
239238
}
240239

240+
#[rstest]
241+
#[case::whitespace_suffix("0.1s ")]
242+
#[case::mixed_newlines_spaces_tabs("\n\t0.1s \n ")]
243+
fn test_sleep_when_input_has_trailing_whitespace_then_error(#[case] input: &str) {
244+
new_ucmd!()
245+
.arg(input)
246+
.timeout(Duration::from_secs(10))
247+
.fails()
248+
.usage_error(format!("invalid time interval {}", input.quote()));
249+
}
250+
241251
#[rstest]
242252
#[case::only_space(" ")]
243253
#[case::only_tab("\t")]
@@ -247,16 +257,14 @@ fn test_sleep_when_input_has_only_whitespace_then_error(#[case] input: &str) {
247257
.arg(input)
248258
.timeout(Duration::from_secs(10))
249259
.fails()
250-
.usage_error(format!(
251-
"invalid time interval '{input}': Found only whitespace in input"
252-
));
260+
.usage_error(format!("invalid time interval {}", input.quote()));
253261
}
254262

255263
#[test]
256264
fn test_sleep_when_multiple_input_some_with_error_then_shows_all_errors() {
257-
let expected = "invalid time interval 'abc': Invalid input: abc\n\
258-
sleep: invalid time interval '1years': Invalid time unit: 'years' at position 2\n\
259-
sleep: invalid time interval ' ': Found only whitespace in input";
265+
let expected = "invalid time interval 'abc'\n\
266+
sleep: invalid time interval '1years'\n\
267+
sleep: invalid time interval ' '";
260268

261269
// Even if one of the arguments is valid, but the rest isn't, we should still fail and exit early.
262270
// So, the timeout of 10 seconds ensures we haven't executed `thread::sleep` with the only valid
@@ -273,7 +281,35 @@ fn test_negative_interval() {
273281
new_ucmd!()
274282
.args(&["--", "-1"])
275283
.fails()
276-
.usage_error("invalid time interval '-1': Number was negative");
284+
.usage_error("invalid time interval '-1'");
285+
}
286+
287+
#[rstest]
288+
#[case::int("0x0")]
289+
#[case::negative_zero("-0x0")]
290+
#[case::int_suffix("0x0s")]
291+
#[case::int_suffix("0x0h")]
292+
#[case::frac("0x0.1")]
293+
#[case::frac_suffix("0x0.1s")]
294+
#[case::frac_suffix("0x0.001h")]
295+
#[case::scientific("0x1.0p-3")]
296+
#[case::scientific_suffix("0x1.0p-4s")]
297+
fn test_valid_hex_duration(#[case] input: &str) {
298+
new_ucmd!().args(&["--", input]).succeeds().no_output();
299+
}
300+
301+
#[rstest]
302+
#[case::negative("-0x1")]
303+
#[case::negative_suffix("-0x1s")]
304+
#[case::negative_frac_suffix("-0x0.1s")]
305+
#[case::wrong_capitalization("infD")]
306+
#[case::wrong_capitalization("INFD")]
307+
#[case::wrong_capitalization("iNfD")]
308+
fn test_invalid_hex_duration(#[case] input: &str) {
309+
new_ucmd!()
310+
.args(&["--", input])
311+
.fails()
312+
.usage_error(format!("invalid time interval {}", input.quote()));
277313
}
278314

279315
#[cfg(unix)]

tests/by-util/test_timeout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn test_command_empty_args() {
7777
new_ucmd!()
7878
.args(&["", ""])
7979
.fails()
80-
.stderr_contains("timeout: empty string");
80+
.stderr_contains("timeout: invalid time interval ''");
8181
}
8282

8383
#[test]

0 commit comments

Comments
 (0)