mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
and Add some sleep test cases #1735
This commit is contained in:
parent
fd5ec099d0
commit
2158b2c5b4
3 changed files with 89 additions and 42 deletions
|
@ -15,7 +15,7 @@ edition = "2018"
|
||||||
path = "src/sleep.rs"
|
path = "src/sleep.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "0.2.18"
|
clap = "2.33"
|
||||||
uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["parse_time"] }
|
uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["parse_time"] }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
||||||
|
|
|
@ -11,55 +11,56 @@ extern crate uucore;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
static NAME: &str = "sleep";
|
use clap::{App, Arg};
|
||||||
|
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
static ABOUT: &str = "Pause for NUMBER seconds.";
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
static LONG_HELP: &str = "Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default),
|
||||||
let args = args.collect_str();
|
|
||||||
|
|
||||||
let mut opts = getopts::Options::new();
|
|
||||||
opts.optflag("h", "help", "display this help and exit");
|
|
||||||
opts.optflag("V", "version", "output version information and exit");
|
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
|
||||||
Ok(m) => m,
|
|
||||||
Err(f) => {
|
|
||||||
show_error!("{}", f);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if matches.opt_present("help") {
|
|
||||||
let msg = format!(
|
|
||||||
"{0} {1}
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
{0} NUMBER[SUFFIX]
|
|
||||||
or
|
|
||||||
{0} OPTION
|
|
||||||
|
|
||||||
Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default),
|
|
||||||
'm' for minutes, 'h' for hours or 'd' for days. Unlike most implementations
|
'm' for minutes, 'h' for hours or 'd' for days. Unlike most implementations
|
||||||
that require NUMBER be an integer, here NUMBER may be an arbitrary floating
|
that require NUMBER be an integer, here NUMBER may be an arbitrary floating
|
||||||
point number. Given two or more arguments, pause for the amount of time
|
point number. Given two or more arguments, pause for the amount of time
|
||||||
specified by the sum of their values.",
|
specified by the sum of their values.";
|
||||||
NAME, VERSION
|
|
||||||
);
|
mod options {
|
||||||
print!("{}", opts.usage(&msg));
|
pub const NUMBER: &str = "NUMBER";
|
||||||
} else if matches.opt_present("version") {
|
}
|
||||||
println!("{} {}", NAME, VERSION);
|
|
||||||
} else if matches.free.is_empty() {
|
fn get_usage() -> String {
|
||||||
show_error!("missing an argument");
|
format!(
|
||||||
show_error!("for help, try '{0} --help'", NAME);
|
"{0} {1}[SUFFIX]... \n {0} OPTION",
|
||||||
return 1;
|
executable!(),
|
||||||
} else {
|
options::NUMBER
|
||||||
sleep(matches.free);
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
|
let usage = get_usage();
|
||||||
|
|
||||||
|
let matches = App::new(executable!())
|
||||||
|
.version(VERSION)
|
||||||
|
.about(ABOUT)
|
||||||
|
.usage(&usage[..])
|
||||||
|
.after_help(LONG_HELP)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(options::NUMBER)
|
||||||
|
.long(options::NUMBER)
|
||||||
|
.help("pause for NUMBER seconds")
|
||||||
|
.value_name(options::NUMBER)
|
||||||
|
.index(1)
|
||||||
|
.multiple(true)
|
||||||
|
.required(true),
|
||||||
|
)
|
||||||
|
.get_matches_from(args);
|
||||||
|
|
||||||
|
if let Some(values) = matches.values_of(options::NUMBER) {
|
||||||
|
let numbers = values.collect();
|
||||||
|
sleep(numbers);
|
||||||
}
|
}
|
||||||
|
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sleep(args: Vec<String>) {
|
fn sleep(args: Vec<&str>) {
|
||||||
let sleep_dur =
|
let sleep_dur =
|
||||||
args.iter().fold(
|
args.iter().fold(
|
||||||
Duration::new(0, 0),
|
Duration::new(0, 0),
|
||||||
|
|
|
@ -1 +1,47 @@
|
||||||
// ToDO: add tests
|
use crate::common::util::*;
|
||||||
|
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sleep_no_suffix() {
|
||||||
|
let millis_100 = Duration::from_millis(100);
|
||||||
|
let before_test = Instant::now();
|
||||||
|
|
||||||
|
new_ucmd!().args(&["0.1"]).succeeds().stdout_only("");
|
||||||
|
|
||||||
|
let duration = before_test.elapsed();
|
||||||
|
assert!(duration >= millis_100);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sleep_s_suffix() {
|
||||||
|
let millis_100 = Duration::from_millis(100);
|
||||||
|
let before_test = Instant::now();
|
||||||
|
|
||||||
|
new_ucmd!().args(&["0.1s"]).succeeds().stdout_only("");
|
||||||
|
|
||||||
|
let duration = before_test.elapsed();
|
||||||
|
assert!(duration >= millis_100);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sleep_m_suffix() {
|
||||||
|
let millis_600 = Duration::from_millis(600);
|
||||||
|
let before_test = Instant::now();
|
||||||
|
|
||||||
|
new_ucmd!().args(&["0.01m"]).succeeds().stdout_only("");
|
||||||
|
|
||||||
|
let duration = before_test.elapsed();
|
||||||
|
assert!(duration >= millis_600);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sleep_h_suffix() {
|
||||||
|
let millis_360 = Duration::from_millis(360);
|
||||||
|
let before_test = Instant::now();
|
||||||
|
|
||||||
|
new_ucmd!().args(&["0.0001h"]).succeeds().stdout_only("");
|
||||||
|
|
||||||
|
let duration = before_test.elapsed();
|
||||||
|
assert!(duration >= millis_360);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue