8000 `sleep`: Replace uucore::parse_time::from_str with fundu by Joining7943 · Pull Request #4448 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

sleep: Replace uucore::parse_time::from_str with fundu #4448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
1 change: 1 addition & 0 deletions .vscode/cspell.dictionaries/workspace.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exacl
filetime
formatteriteminfo
fsext
fundu
getopts
getrandom
globset
Expand Down
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ filetime = "0.2"
fnv = "1.0.7"
fs_extra = "1.1.0"
fts-sys = "0.2"
fundu = "0.3.0"
fundu = "0.4.2"
gcd = "2.2"
glob = "0.3.0"
half = "2.1"
Expand Down
1 change: 1 addition & 0 deletions src/uu/sleep/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ path = "src/sleep.rs"

[dependencies]
clap = { workspace=true }
fundu = { workspace=true }
uucore = { workspace=true }

[[bin]]
Expand Down
29 changes: 25 additions & 4 deletions src/uu/sleep/src/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use uucore::{
};

use clap::{crate_version, Arg, ArgAction, Command};
use fundu::{self, DurationParser, ParseError};

static ABOUT: &str = help_about!("sleep.md");
const USAGE: &str = help_usage!("sleep.md");
Expand Down Expand Up @@ -61,14 +62,34 @@ pub fn uu_app() -> Command {

fn sleep(args: &[&str]) -> UResult<()> {
let mut arg_error = false;

use fundu::TimeUnit::*;
let parser = DurationParser::with_time_units(&[Second, Minute, Hour, Day]);

let sleep_dur = args
.iter()
.filter_map(|input| {
uucore::parse_time::from_str(input.trim()).ok().or_else(|| {
.filter_map(|input| match parser.parse(input.trim()) {
Ok(duration) => Some(duration),
Err(error) => {
arg_error = true;
show_error!("invalid time interval '{input}'");

let reason = match error {
ParseError::Empty if input.is_empty() => "Input was empty".to_string(),
ParseError::Empty => "Found only whitespace in input".to_string(),
ParseError::Syntax(pos, description)
| ParseError::TimeUnit(pos, description) => {
format!("{description} at position {}", pos.saturating_add(1))
}
ParseError::NegativeExponentOverflow | ParseError::PositiveExponentOverflow => {
"Exponent was out of bounds".to_string()
}
ParseError::NegativeNumber => "Number was negative".to_string(),
error => error.to_string(),
};
show_error!("invalid time interval '{input}': {reason}");

None
})
}
})
.fold(Duration::ZERO, |acc, n| acc.saturating_add(n));

Expand Down
16 changes: 9 additions & 7 deletions tests/by-util/test_sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ fn test_invalid_time_interval() {
new_ucmd!()
.arg("xyz")
.fails()
.usage_error("invalid time interval 'xyz'");
.usage_error("invalid time interval 'xyz': Invalid character: 'x' at position 1");
new_ucmd!()
.args(&["--", "-1"])
.fails()
.usage_error("invalid time interval '-1'");
.usage_error("invalid time interval '-1': Number was negative");
}

#[test]
Expand Down Expand Up @@ -204,14 +204,16 @@ fn test_sleep_when_input_has_only_whitespace_then_error(#[case] input: &str) {
.arg(input)
.timeout(Duration::from_secs(10))
.fails()
.usage_error(format!("invalid time interval '{input}'"));
.usage_error(format!(
"invalid time interval '{input}': Found only whitespace in input"
));
}

#[test]
fn test_sleep_when_multiple_input_some_with_error_then_shows_all_errors() {
let expected = "invalid time interval 'abc'\n\
sleep: invalid time interval '1years'\n\
sleep: invalid time interval ' '";
let expected = "invalid time interval 'abc': Invalid character: 'a' at position 1\n\
sleep: invalid time interval '1years': Invalid time unit: 'years' at position 2\n\
sleep: invalid time interval ' ': Found only whitespace in input";

// Even if one of the arguments is valid, but the rest isn't, we should still fail and exit early.
// So, the timeout of 10 seconds ensures we haven't executed `thread::sleep` with the only valid
Expand All @@ -228,5 +230,5 @@ fn test_negative_interval() {
new_ucmd!()
.args(&["--", "-1"])
.fails()
.usage_error("invalid time interval '-1'");
.usage_error("invalid time interval '-1': Number was negative");
}
2 changes: 1 addition & 1 deletion tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4459,7 +4459,7 @@ fn test_follow_when_files_are_pointing_to_same_relative_file_and_file_stays_same
}

#[rstest]
#[case::exponent_exceed_float_max("1.0e2048")]
#[case::exponent_exceed_float_max("1.0e100000")]
#[case::underscore_delimiter("1_000")]
#[case::only_point(".")]
#[case::space_in_primes("' '")]
Expand Down
0