1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

Merge pull request #1521 from sylvestre/nproc

Add tests for nproc + clap migration
This commit is contained in:
Alex Lyon 2020-06-08 19:18:00 -07:00 committed by GitHub
commit 80987250d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 44 deletions

1
Cargo.lock generated
View file

@ -1610,6 +1610,7 @@ dependencies = [
name = "uu_nproc"
version = "0.0.1"
dependencies = [
"clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -18,6 +18,7 @@ path = "src/nproc.rs"
getopts = "0.2.18"
libc = "0.2.42"
num_cpus = "1.10"
clap = "2.33"
uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary", features=["fs"] }
uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" }

View file

@ -9,6 +9,7 @@
extern crate getopts;
extern crate num_cpus;
extern crate clap;
#[cfg(unix)]
extern crate libc;
@ -17,6 +18,7 @@ extern crate libc;
extern crate uucore;
use std::env;
use clap::{App, Arg};
#[cfg(target_os = "linux")]
pub const _SC_NPROCESSORS_CONF: libc::c_int = 83;
@ -27,50 +29,38 @@ pub const _SC_NPROCESSORS_CONF: libc::c_int = 57;
#[cfg(target_os = "netbsd")]
pub const _SC_NPROCESSORS_CONF: libc::c_int = 1001;
static NAME: &str = "nproc";
static OPT_ALL: &str = "all";
static OPT_IGNORE: &str = "ignore";
static VERSION: &str = env!("CARGO_PKG_VERSION");
static ABOUT: &str = "Print the number of cores available to the current process.";
fn get_usage() -> String {
format!("{0} [OPTIONS]...", executable!())
}
pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = getopts::Options::new();
let usage = get_usage();
let matches = App::new(executable!())
.version(VERSION)
.about(ABOUT)
.usage(&usage[..])
.arg(
Arg::with_name(OPT_ALL)
.short("")
.long("all")
.help("print the number of cores available to the system"),
)
.arg(
Arg::with_name(OPT_IGNORE)
.short("")
.long("ignore")
.takes_value(true)
.help("ignore up to N cores"),
)
.get_matches_from(&args);
opts.optflag(
"",
"all",
"print the number of cores available to the system",
);
opts.optopt("", "ignore", "ignore up to N cores", "N");
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(err) => {
show_error!("{}", err);
return 1;
}
};
if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
return 0;
}
if matches.opt_present("help") {
let msg = format!(
"{0} {1}
Usage:
{0} [OPTIONS]...
Print the number of cores available to the current process.",
NAME, VERSION
);
print!("{}", opts.usage(&msg));
return 0;
}
let mut ignore = match matches.opt_str("ignore") {
let mut ignore = match matches.value_of(OPT_IGNORE) {
Some(numstr) => match numstr.parse() {
Ok(num) => num,
Err(e) => {
@ -81,7 +71,8 @@ Print the number of cores available to the current process.",
None => 0,
};
if !matches.opt_present("all") {
if ! matches.is_present(OPT_ALL) {
// OMP_NUM_THREADS doesn't have an impact on --all
ignore += match env::var("OMP_NUM_THREADS") {
Ok(threadstr) => match threadstr.parse() {
Ok(num) => num,
@ -91,7 +82,7 @@ Print the number of cores available to the current process.",
};
}
let mut cores = if matches.opt_present("all") {
let mut cores = if matches.is_present(OPT_ALL) {
num_cpus_all()
} else {
num_cpus::get()
@ -125,7 +116,7 @@ fn num_cpus_all() -> usize {
}
}
// Other platform(e.g., windows), num_cpus::get() directly.
// Other platforms (e.g., windows), num_cpus::get() directly.
#[cfg(not(any(
target_os = "linux",
target_os = "macos",

View file

@ -1 +1,58 @@
// ToDO: add tests
use crate::common::util::*;
#[test]
fn test_nproc() {
let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd.run();
assert!(result.success);
let nproc: u8 = result.stdout.trim().parse().unwrap();
assert!(nproc > 0);
}
#[test]
fn test_nproc_all_omp() {
let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd.arg("--all").run();
assert!(result.success);
let nproc: u8 = result.stdout.trim().parse().unwrap();
assert!(nproc > 0);
let result = TestScenario::new(util_name!())
.ucmd_keepenv()
.env("OMP_NUM_THREADS", "1")
.run();
assert!(result.success);
let nproc_omp: u8 = result.stdout.trim().parse().unwrap();
assert!(nproc-1 == nproc_omp);
let result = TestScenario::new(util_name!())
.ucmd_keepenv()
.env("OMP_NUM_THREADS", "1") // Has no effect
.arg("--all")
.run();
assert!(result.success);
let nproc_omp: u8 = result.stdout.trim().parse().unwrap();
assert!(nproc == nproc_omp);
}
#[test]
fn test_nproc_ignore() {
let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd.run();
assert!(result.success);
let nproc: u8 = result.stdout.trim().parse().unwrap();
if nproc > 1 {
// Ignore all CPU but one
let result = TestScenario::new(util_name!())
.ucmd_keepenv()
.arg("--ignore")
.arg((nproc - 1).to_string())
.run();
assert!(result.success);
let nproc: u8 = result.stdout.trim().parse().unwrap();
assert!(nproc == 1);
}
}