1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-15 19:36:16 +00:00
This commit is contained in:
electricboogie 2021-04-17 23:25:02 -05:00
commit a73d108dd8
17 changed files with 241 additions and 200 deletions

View file

@ -90,3 +90,44 @@ duplicate the string you passed to hyperfine but remove the `target/release/core
Example: `hyperfine "target/release/coreutils sort shuffled_numbers_si.txt -h -o output.txt"` becomes Example: `hyperfine "target/release/coreutils sort shuffled_numbers_si.txt -h -o output.txt"` becomes
`hyperfine "target/release/coreutils sort shuffled_numbers_si.txt -h -o output.txt" "sort shuffled_numbers_si.txt -h -o output.txt"` `hyperfine "target/release/coreutils sort shuffled_numbers_si.txt -h -o output.txt" "sort shuffled_numbers_si.txt -h -o output.txt"`
(This assumes GNU sort is installed as `sort`) (This assumes GNU sort is installed as `sort`)
## Memory and CPU usage
The above benchmarks use hyperfine to measure the speed of sorting. There are however other useful metrics to determine overall
resource usage. One way to measure them is the `time` command. This is not to be confused with the `time` that is built in to the bash shell.
You may have to install `time` first, then you have to run it with `/bin/time -v` to give it precedence over the built in `time`.
<details>
<summary>Example output</summary>
Command being timed: "target/release/coreutils sort shuffled_numbers.txt"
User time (seconds): 0.10
System time (seconds): 0.00
Percent of CPU this job got: 365%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.02
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 25360
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 5802
Voluntary context switches: 462
Involuntary context switches: 73
Swaps: 0
File system inputs: 1184
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
</details>
Useful metrics to look at could be:
- User time
- Percent of CPU this job got
- Maximum resident set size

View file

@ -62,14 +62,16 @@ fn test_invalid_file() {
let folder_name = "asdf"; let folder_name = "asdf";
// First check when file doesn't exist // First check when file doesn't exist
ts.ucmd().arg(folder_name) ts.ucmd()
.arg(folder_name)
.fails() .fails()
.no_stdout() .no_stdout()
.stderr_contains("cksum: error: 'asdf' No such file or directory"); .stderr_contains("cksum: error: 'asdf' No such file or directory");
// Then check when the file is of an invalid type // Then check when the file is of an invalid type
at.mkdir(folder_name); at.mkdir(folder_name);
ts.ucmd().arg(folder_name) ts.ucmd()
.arg(folder_name)
.fails() .fails()
.no_stdout() .no_stdout()
.stderr_contains("cksum: error: 'asdf' Is a directory"); .stderr_contains("cksum: error: 'asdf' Is a directory");

View file

@ -4,10 +4,6 @@ use self::regex::Regex;
#[test] #[test]
fn test_normal() { fn test_normal() {
let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd.run();
assert!(result.success);
let re = Regex::new(r"^[0-9a-f]{8}").unwrap(); let re = Regex::new(r"^[0-9a-f]{8}").unwrap();
assert!(re.is_match(&result.stdout_str())); new_ucmd!().succeeds().stdout_matches(&re);
} }

View file

@ -14,9 +14,7 @@ fn test_hostname() {
#[cfg(not(target_vendor = "apple"))] #[cfg(not(target_vendor = "apple"))]
#[test] #[test]
fn test_hostname_ip() { fn test_hostname_ip() {
let result = new_ucmd!().arg("-i").run(); let result = new_ucmd!().arg("-i").succeeds();
println!("{:#?}", result);
assert!(result.success);
assert!(!result.stdout_str().trim().is_empty()); assert!(!result.stdout_str().trim().is_empty());
} }

View file

@ -1,11 +1,32 @@
use crate::common::util::*; use crate::common::util::*;
// Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'.
// If we are running inside the CI and "needle" is in "stderr" skipping this test is
// considered okay. If we are not inside the CI this calls assert!(result.success).
//
// From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
// stderr: "whoami: cannot find name for user ID 1001"
// Maybe: "adduser --uid 1001 username" can put things right?
// stderr = id: error: Could not find uid 1001: No such id: 1001
fn skipping_test_is_okay(result: &CmdResult, needle: &str) -> bool {
if !result.succeeded() {
println!("result.stdout = {}", result.stdout_str());
println!("result.stderr = {}", result.stderr_str());
if is_ci() && result.stderr_str().contains(needle) {
println!("test skipped:");
return true;
} else {
result.success();
}
}
false
}
fn return_whoami_username() -> String { fn return_whoami_username() -> String {
let scene = TestScenario::new("whoami"); let scene = TestScenario::new("whoami");
let result = scene.cmd("whoami").run(); let result = scene.cmd("whoami").run();
if is_ci() && result.stderr.contains("cannot find name for user ID") { if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
// In the CI, some server are failing to return whoami. println!("test skipped:");
// As seems to be a configuration issue, ignoring it
return String::from(""); return String::from("");
} }
@ -14,40 +35,41 @@ fn return_whoami_username() -> String {
#[test] #[test]
fn test_id() { fn test_id() {
let result = new_ucmd!().arg("-u").run(); let scene = TestScenario::new(util_name!());
if result.stderr.contains("cannot find name for user ID") {
// In the CI, some server are failing to return whoami. let result = scene.ucmd().arg("-u").succeeds();
// As seems to be a configuration issue, ignoring it let uid = result.stdout_str().trim();
let result = scene.ucmd().run();
if skipping_test_is_okay(&result, "Could not find uid") {
return; return;
} }
let uid = result.success().stdout_str().trim();
let result = new_ucmd!().run();
if is_ci() && result.stderr.contains("cannot find name for user ID") {
// In the CI, some server are failing to return whoami.
// As seems to be a configuration issue, ignoring it
return;
}
if !result.stderr_str().contains("Could not find uid") {
// Verify that the id found by --user/-u exists in the list // Verify that the id found by --user/-u exists in the list
result.success().stdout_contains(&uid); result.stdout_contains(uid);
}
} }
#[test] #[test]
fn test_id_from_name() { fn test_id_from_name() {
let username = return_whoami_username(); let username = return_whoami_username();
if username == "" { if username.is_empty() {
// Sometimes, the CI is failing here return;
}
let scene = TestScenario::new(util_name!());
let result = scene.ucmd().arg(&username).run();
if skipping_test_is_okay(&result, "Could not find uid") {
return; return;
} }
let result = new_ucmd!().arg(&username).succeeds();
let uid = result.stdout_str().trim(); let uid = result.stdout_str().trim();
new_ucmd!() let result = scene.ucmd().run();
.succeeds() if skipping_test_is_okay(&result, "Could not find uid") {
return;
}
result
// Verify that the id found by --user/-u exists in the list // Verify that the id found by --user/-u exists in the list
.stdout_contains(uid) .stdout_contains(uid)
// Verify that the username found by whoami exists in the list // Verify that the username found by whoami exists in the list
@ -56,48 +78,42 @@ fn test_id_from_name() {
#[test] #[test]
fn test_id_name_from_id() { fn test_id_name_from_id() {
let result = new_ucmd!().arg("-u").succeeds(); let result = new_ucmd!().arg("-nu").run();
let uid = result.stdout_str().trim();
let result = new_ucmd!().arg("-nu").arg(uid).run(); let username_id = result.stdout_str().trim();
if is_ci() && result.stderr.contains("No such user/group") {
// In the CI, some server are failing to return whoami. let username_whoami = return_whoami_username();
// As seems to be a configuration issue, ignoring it if username_whoami.is_empty() {
return; return;
} }
let username_id = result.success().stdout_str().trim();
let scene = TestScenario::new("whoami");
let result = scene.cmd("whoami").succeeds();
let username_whoami = result.stdout_str().trim();
assert_eq!(username_id, username_whoami); assert_eq!(username_id, username_whoami);
} }
#[test] #[test]
fn test_id_group() { fn test_id_group() {
let mut result = new_ucmd!().arg("-g").succeeds(); let scene = TestScenario::new(util_name!());
let mut result = scene.ucmd().arg("-g").succeeds();
let s1 = result.stdout_str().trim(); let s1 = result.stdout_str().trim();
assert!(s1.parse::<f64>().is_ok()); assert!(s1.parse::<f64>().is_ok());
result = new_ucmd!().arg("--group").succeeds(); result = scene.ucmd().arg("--group").succeeds();
let s1 = result.stdout_str().trim(); let s1 = result.stdout_str().trim();
assert!(s1.parse::<f64>().is_ok()); assert!(s1.parse::<f64>().is_ok());
} }
#[test] #[test]
fn test_id_groups() { fn test_id_groups() {
let result = new_ucmd!().arg("-G").succeeds(); let scene = TestScenario::new(util_name!());
assert!(result.success);
let result = scene.ucmd().arg("-G").succeeds();
let groups = result.stdout_str().trim().split_whitespace(); let groups = result.stdout_str().trim().split_whitespace();
for s in groups { for s in groups {
assert!(s.parse::<f64>().is_ok()); assert!(s.parse::<f64>().is_ok());
} }
let result = new_ucmd!().arg("--groups").succeeds(); let result = scene.ucmd().arg("--groups").succeeds();
assert!(result.success);
let groups = result.stdout_str().trim().split_whitespace(); let groups = result.stdout_str().trim().split_whitespace();
for s in groups { for s in groups {
assert!(s.parse::<f64>().is_ok()); assert!(s.parse::<f64>().is_ok());
@ -106,11 +122,13 @@ fn test_id_groups() {
#[test] #[test]
fn test_id_user() { fn test_id_user() {
let mut result = new_ucmd!().arg("-u").succeeds(); let scene = TestScenario::new(util_name!());
let result = scene.ucmd().arg("-u").succeeds();
let s1 = result.stdout_str().trim(); let s1 = result.stdout_str().trim();
assert!(s1.parse::<f64>().is_ok()); assert!(s1.parse::<f64>().is_ok());
result = new_ucmd!().arg("--user").succeeds(); let result = scene.ucmd().arg("--user").succeeds();
let s1 = result.stdout_str().trim(); let s1 = result.stdout_str().trim();
assert!(s1.parse::<f64>().is_ok()); assert!(s1.parse::<f64>().is_ok());
} }
@ -118,28 +136,34 @@ fn test_id_user() {
#[test] #[test]
fn test_id_pretty_print() { fn test_id_pretty_print() {
let username = return_whoami_username(); let username = return_whoami_username();
if username == "" { if username.is_empty() {
// Sometimes, the CI is failing here
return; return;
} }
let result = new_ucmd!().arg("-p").run(); let scene = TestScenario::new(util_name!());
if result.stdout_str().trim() == "" { let result = scene.ucmd().arg("-p").run();
// Sometimes, the CI is failing here with if result.stdout_str().trim().is_empty() {
// old rust versions on Linux // this fails only on: "MinRustV (ubuntu-latest, feat_os_unix)"
// `rustc 1.40.0 (73528e339 2019-12-16)`
// run: /home/runner/work/coreutils/coreutils/target/debug/coreutils id -p
// thread 'test_id::test_id_pretty_print' panicked at 'Command was expected to succeed.
// stdout =
// stderr = ', tests/common/util.rs:157:13
println!("test skipped:");
return; return;
} }
result.success().stdout_contains(username); result.success().stdout_contains(username);
} }
#[test] #[test]
fn test_id_password_style() { fn test_id_password_style() {
let username = return_whoami_username(); let username = return_whoami_username();
if username == "" { if username.is_empty() {
// Sometimes, the CI is failing here
return; return;
} }
let result = new_ucmd!().arg("-P").succeeds(); let result = new_ucmd!().arg("-P").succeeds();
assert!(result.stdout_str().starts_with(&username)); assert!(result.stdout_str().starts_with(&username));
} }

View file

@ -388,8 +388,7 @@ fn test_mktemp_tmpdir_one_arg() {
.arg("--tmpdir") .arg("--tmpdir")
.arg("apt-key-gpghome.XXXXXXXXXX") .arg("apt-key-gpghome.XXXXXXXXXX")
.succeeds(); .succeeds();
result.no_stderr() result.no_stderr().stdout_contains("apt-key-gpghome.");
.stdout_contains("apt-key-gpghome.");
assert!(PathBuf::from(result.stdout_str().trim()).is_file()); assert!(PathBuf::from(result.stdout_str().trim()).is_file());
} }

View file

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

View file

@ -75,5 +75,5 @@ fn expected_result(args: &[&str]) -> String {
.env("LANGUAGE", "C") .env("LANGUAGE", "C")
.args(args) .args(args)
.run() .run()
.stdout .stdout_move_str()
} }

View file

@ -7,10 +7,11 @@ fn test_get_all() {
env::set_var(key, "VALUE"); env::set_var(key, "VALUE");
assert_eq!(env::var(key), Ok("VALUE".to_string())); assert_eq!(env::var(key), Ok("VALUE".to_string()));
let result = TestScenario::new(util_name!()).ucmd_keepenv().run(); TestScenario::new(util_name!())
assert!(result.success); .ucmd_keepenv()
assert!(result.stdout.contains("HOME=")); .succeeds()
assert!(result.stdout.contains("KEY=VALUE")); .stdout_contains("HOME=")
.stdout_contains("KEY=VALUE");
} }
#[test] #[test]
@ -22,9 +23,8 @@ fn test_get_var() {
let result = TestScenario::new(util_name!()) let result = TestScenario::new(util_name!())
.ucmd_keepenv() .ucmd_keepenv()
.arg("KEY") .arg("KEY")
.run(); .succeeds();
assert!(result.success); assert!(!result.stdout_str().is_empty());
assert!(!result.stdout.is_empty()); assert!(result.stdout_str().trim() == "VALUE");
assert!(result.stdout.trim() == "VALUE");
} }

View file

@ -5,7 +5,7 @@ static GIBBERISH: &'static str = "supercalifragilisticexpialidocious";
#[test] #[test]
fn test_canonicalize() { fn test_canonicalize() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
let actual = ucmd.arg("-f").arg(".").run().stdout; let actual = ucmd.arg("-f").arg(".").run().stdout_move_str();
let expect = at.root_dir_resolved() + "\n"; let expect = at.root_dir_resolved() + "\n";
println!("actual: {:?}", actual); println!("actual: {:?}", actual);
println!("expect: {:?}", expect); println!("expect: {:?}", expect);
@ -15,7 +15,7 @@ fn test_canonicalize() {
#[test] #[test]
fn test_canonicalize_existing() { fn test_canonicalize_existing() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
let actual = ucmd.arg("-e").arg(".").run().stdout; let actual = ucmd.arg("-e").arg(".").run().stdout_move_str();
let expect = at.root_dir_resolved() + "\n"; let expect = at.root_dir_resolved() + "\n";
println!("actual: {:?}", actual); println!("actual: {:?}", actual);
println!("expect: {:?}", expect); println!("expect: {:?}", expect);
@ -25,7 +25,7 @@ fn test_canonicalize_existing() {
#[test] #[test]
fn test_canonicalize_missing() { fn test_canonicalize_missing() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
let actual = ucmd.arg("-m").arg(GIBBERISH).run().stdout; let actual = ucmd.arg("-m").arg(GIBBERISH).run().stdout_move_str();
let expect = path_concat!(at.root_dir_resolved(), GIBBERISH) + "\n"; let expect = path_concat!(at.root_dir_resolved(), GIBBERISH) + "\n";
println!("actual: {:?}", actual); println!("actual: {:?}", actual);
println!("expect: {:?}", expect); println!("expect: {:?}", expect);
@ -37,7 +37,7 @@ fn test_long_redirection_to_current_dir() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
// Create a 256-character path to current directory // Create a 256-character path to current directory
let dir = path_concat!(".", ..128); let dir = path_concat!(".", ..128);
let actual = ucmd.arg("-n").arg("-m").arg(dir).run().stdout; let actual = ucmd.arg("-n").arg("-m").arg(dir).run().stdout_move_str();
let expect = at.root_dir_resolved(); let expect = at.root_dir_resolved();
println!("actual: {:?}", actual); println!("actual: {:?}", actual);
println!("expect: {:?}", expect); println!("expect: {:?}", expect);
@ -48,7 +48,12 @@ fn test_long_redirection_to_current_dir() {
fn test_long_redirection_to_root() { fn test_long_redirection_to_root() {
// Create a 255-character path to root // Create a 255-character path to root
let dir = path_concat!("..", ..85); let dir = path_concat!("..", ..85);
let actual = new_ucmd!().arg("-n").arg("-m").arg(dir).run().stdout; let actual = new_ucmd!()
.arg("-n")
.arg("-m")
.arg(dir)
.run()
.stdout_move_str();
let expect = get_root_path(); let expect = get_root_path();
println!("actual: {:?}", actual); println!("actual: {:?}", actual);
println!("expect: {:?}", expect); println!("expect: {:?}", expect);

View file

@ -65,7 +65,7 @@ fn test_random_shuffle_len() {
// check whether output is the same length as the input // check whether output is the same length as the input
const FILE: &'static str = "default_unsorted_ints.expected"; const FILE: &'static str = "default_unsorted_ints.expected";
let (at, _ucmd) = at_and_ucmd!(); let (at, _ucmd) = at_and_ucmd!();
let result = new_ucmd!().arg("-R").arg(FILE).run().stdout; let result = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str();
let expected = at.read(FILE); let expected = at.read(FILE);
assert_ne!(result, expected); assert_ne!(result, expected);
@ -77,9 +77,9 @@ fn test_random_shuffle_contains_all_lines() {
// check whether lines of input are all in output // check whether lines of input are all in output
const FILE: &'static str = "default_unsorted_ints.expected"; const FILE: &'static str = "default_unsorted_ints.expected";
let (at, _ucmd) = at_and_ucmd!(); let (at, _ucmd) = at_and_ucmd!();
let result = new_ucmd!().arg("-R").arg(FILE).run().stdout; let result = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str();
let expected = at.read(FILE); let expected = at.read(FILE);
let result_sorted = new_ucmd!().pipe_in(result.clone()).run().stdout; let result_sorted = new_ucmd!().pipe_in(result.clone()).run().stdout_move_str();
assert_ne!(result, expected); assert_ne!(result, expected);
assert_eq!(result_sorted, expected); assert_eq!(result_sorted, expected);
@ -92,9 +92,9 @@ fn test_random_shuffle_two_runs_not_the_same() {
// as the starting order, or if both random sorts end up having the same order. // as the starting order, or if both random sorts end up having the same order.
const FILE: &'static str = "default_unsorted_ints.expected"; const FILE: &'static str = "default_unsorted_ints.expected";
let (at, _ucmd) = at_and_ucmd!(); let (at, _ucmd) = at_and_ucmd!();
let result = new_ucmd!().arg("-R").arg(FILE).run().stdout; let result = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str();
let expected = at.read(FILE); let expected = at.read(FILE);
let unexpected = new_ucmd!().arg("-R").arg(FILE).run().stdout; let unexpected = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str();
assert_ne!(result, expected); assert_ne!(result, expected);
assert_ne!(result, unexpected); assert_ne!(result, unexpected);
@ -107,9 +107,9 @@ fn test_random_shuffle_contains_two_runs_not_the_same() {
// as the starting order, or if both random sorts end up having the same order. // as the starting order, or if both random sorts end up having the same order.
const FILE: &'static str = "default_unsorted_ints.expected"; const FILE: &'static str = "default_unsorted_ints.expected";
let (at, _ucmd) = at_and_ucmd!(); let (at, _ucmd) = at_and_ucmd!();
let result = new_ucmd!().arg("-R").arg(FILE).run().stdout; let result = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str();
let expected = at.read(FILE); let expected = at.read(FILE);
let unexpected = new_ucmd!().arg("-R").arg(FILE).run().stdout; let unexpected = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str();
assert_ne!(result, expected); assert_ne!(result, expected);
assert_ne!(result, unexpected); assert_ne!(result, unexpected);

View file

@ -5,8 +5,7 @@ use tempfile::tempdir;
#[test] #[test]
fn test_sync_default() { fn test_sync_default() {
let result = new_ucmd!().run(); new_ucmd!().succeeds();
assert!(result.success);
} }
#[test] #[test]
@ -18,8 +17,10 @@ fn test_sync_incorrect_arg() {
fn test_sync_fs() { fn test_sync_fs() {
let temporary_directory = tempdir().unwrap(); let temporary_directory = tempdir().unwrap();
let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap(); let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap();
let result = new_ucmd!().arg("--file-system").arg(&temporary_path).run(); new_ucmd!()
assert!(result.success); .arg("--file-system")
.arg(&temporary_path)
.succeeds();
} }
#[test] #[test]
@ -27,12 +28,14 @@ fn test_sync_data() {
// Todo add a second arg // Todo add a second arg
let temporary_directory = tempdir().unwrap(); let temporary_directory = tempdir().unwrap();
let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap(); let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap();
let result = new_ucmd!().arg("--data").arg(&temporary_path).run(); new_ucmd!().arg("--data").arg(&temporary_path).succeeds();
assert!(result.success);
} }
#[test] #[test]
fn test_sync_no_existing_files() { fn test_sync_no_existing_files() {
let result = new_ucmd!().arg("--data").arg("do-no-exist").fails(); new_ucmd!()
assert!(result.stderr.contains("error: cannot stat")); .arg("--data")
.arg("do-no-exist")
.fails()
.stderr_contains("error: cannot stat");
} }

View file

@ -28,7 +28,7 @@ fn test_version_flag() {
let version_short = new_ucmd!().arg("-V").run(); let version_short = new_ucmd!().arg("-V").run();
let version_long = new_ucmd!().arg("--version").run(); let version_long = new_ucmd!().arg("--version").run();
assert_eq!(version_short.stdout, version_long.stdout); assert_eq!(version_short.stdout(), version_long.stdout());
} }
#[test] #[test]
@ -36,7 +36,7 @@ fn test_help_flag() {
let help_short = new_ucmd!().arg("-h").run(); let help_short = new_ucmd!().arg("-h").run();
let help_long = new_ucmd!().arg("--help").run(); let help_long = new_ucmd!().arg("--help").run();
assert_eq!(help_short.stdout, help_long.stdout); assert_eq!(help_short.stdout(), help_long.stdout());
} }
#[test] #[test]

View file

@ -2,60 +2,41 @@ use crate::common::util::*;
#[test] #[test]
fn test_uname_compatible() { fn test_uname_compatible() {
let (_, mut ucmd) = at_and_ucmd!(); new_ucmd!().arg("-a").succeeds();
let result = ucmd.arg("-a").run();
assert!(result.success);
} }
#[test] #[test]
fn test_uname_name() { fn test_uname_name() {
let (_, mut ucmd) = at_and_ucmd!(); new_ucmd!().arg("-n").succeeds();
let result = ucmd.arg("-n").run();
assert!(result.success);
} }
#[test] #[test]
fn test_uname_processor() { fn test_uname_processor() {
let (_, mut ucmd) = at_and_ucmd!(); let result = new_ucmd!().arg("-p").succeeds();
assert_eq!(result.stdout_str().trim_end(), "unknown");
let result = ucmd.arg("-p").run();
assert!(result.success);
assert_eq!(result.stdout.trim_end(), "unknown");
} }
#[test] #[test]
fn test_uname_hwplatform() { fn test_uname_hwplatform() {
let (_, mut ucmd) = at_and_ucmd!(); let result = new_ucmd!().arg("-i").succeeds();
assert_eq!(result.stdout_str().trim_end(), "unknown");
let result = ucmd.arg("-i").run();
assert!(result.success);
assert_eq!(result.stdout.trim_end(), "unknown");
} }
#[test] #[test]
fn test_uname_machine() { fn test_uname_machine() {
let (_, mut ucmd) = at_and_ucmd!(); new_ucmd!().arg("-m").succeeds();
let result = ucmd.arg("-m").run();
assert!(result.success);
} }
#[test] #[test]
fn test_uname_kernel_version() { fn test_uname_kernel_version() {
let (_, mut ucmd) = at_and_ucmd!(); new_ucmd!().arg("-v").succeeds();
let result = ucmd.arg("-v").run();
assert!(result.success);
} }
#[test] #[test]
fn test_uname_kernel() { fn test_uname_kernel() {
let (_, mut ucmd) = at_and_ucmd!(); let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd.arg("-o").run(); let result = ucmd.arg("-o").succeeds();
assert!(result.success);
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
assert!(result.stdout.to_lowercase().contains("linux")); assert!(result.stdout_str().to_lowercase().contains("linux"));
} }

View file

@ -4,33 +4,23 @@ use crate::common::util::*;
#[test] #[test]
fn test_uptime() { fn test_uptime() {
let result = TestScenario::new(util_name!()).ucmd_keepenv().run(); TestScenario::new(util_name!())
.ucmd_keepenv()
.succeeds()
.stdout_contains("load average:")
.stdout_contains(" up ");
println!("stdout = {}", result.stdout);
println!("stderr = {}", result.stderr);
assert!(result.success);
assert!(result.stdout.contains("load average:"));
assert!(result.stdout.contains(" up "));
// Don't check for users as it doesn't show in some CI // Don't check for users as it doesn't show in some CI
} }
#[test] #[test]
fn test_uptime_since() { fn test_uptime_since() {
let scene = TestScenario::new(util_name!());
let result = scene.ucmd().arg("--since").succeeds();
println!("stdout = {}", result.stdout);
println!("stderr = {}", result.stderr);
assert!(result.success);
let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}").unwrap(); let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}").unwrap();
assert!(re.is_match(&result.stdout.trim()));
new_ucmd!().arg("--since").succeeds().stdout_matches(&re);
} }
#[test] #[test]
fn test_failed() { fn test_failed() {
let (_at, mut ucmd) = at_and_ucmd!(); new_ucmd!().arg("willfail").fails();
ucmd.arg("willfail").fails();
} }

View file

@ -3,14 +3,11 @@ use std::env;
#[test] #[test]
fn test_users_noarg() { fn test_users_noarg() {
let (_, mut ucmd) = at_and_ucmd!(); new_ucmd!().succeeds();
let result = ucmd.run();
assert!(result.success);
} }
#[test] #[test]
fn test_users_check_name() { fn test_users_check_name() {
let result = TestScenario::new(util_name!()).ucmd_keepenv().run(); let result = TestScenario::new(util_name!()).ucmd_keepenv().succeeds();
assert!(result.success);
// Expectation: USER is often set // Expectation: USER is often set
let key = "USER"; let key = "USER";
@ -21,9 +18,9 @@ fn test_users_check_name() {
// Check if "users" contains the name of the user // Check if "users" contains the name of the user
{ {
println!("username found {}", &username); println!("username found {}", &username);
println!("result.stdout {}", &result.stdout); // println!("result.stdout {}", &result.stdout);
if !&result.stdout.is_empty() { if !result.stdout_str().is_empty() {
assert!(result.stdout.contains(&username)) result.stdout_contains(&username);
} }
} }
} }

View file

@ -1,50 +1,63 @@
use crate::common::util::*; use crate::common::util::*;
use std::env;
// Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'.
// If we are running inside the CI and "needle" is in "stderr" skipping this test is
// considered okay. If we are not inside the CI this calls assert!(result.success).
//
// From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
// stderr: "whoami: error: failed to get username"
// Maybe: "adduser --uid 1001 username" can put things right?
fn skipping_test_is_okay(result: &CmdResult, needle: &str) -> bool {
if !result.succeeded() {
println!("result.stdout = {}", result.stdout_str());
println!("result.stderr = {}", result.stderr_str());
if is_ci() && result.stderr_str().contains(needle) {
println!("test skipped:");
return true;
} else {
result.success();
}
}
false
}
#[test] #[test]
fn test_normal() { fn test_normal() {
let (_, mut ucmd) = at_and_ucmd!(); let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd.run(); let result = ucmd.run();
println!("result.stdout = {}", result.stdout);
println!("result.stderr = {}", result.stderr);
println!("env::var(CI).is_ok() = {}", env::var("CI").is_ok());
for (key, value) in env::vars() { // use std::env;
println!("{}: {}", key, value); // println!("env::var(CI).is_ok() = {}", env::var("CI").is_ok());
} // for (key, value) in env::vars() {
if is_ci() && result.stderr.contains("failed to get username") { // println!("{}: {}", key, value);
// In the CI, some server are failing to return whoami. // }
// As seems to be a configuration issue, ignoring it
if skipping_test_is_okay(&result, "failed to get username") {
return; return;
} }
assert!(result.success); result.no_stderr();
assert!(!result.stdout.trim().is_empty()); assert!(!result.stdout_str().trim().is_empty());
} }
#[test] #[test]
#[cfg(not(windows))] #[cfg(not(windows))]
fn test_normal_compare_id() { fn test_normal_compare_id() {
let (_, mut ucmd) = at_and_ucmd!(); let scene = TestScenario::new(util_name!());
let result = ucmd.run(); let result_ucmd = scene.ucmd().run();
if skipping_test_is_okay(&result_ucmd, "failed to get username") {
println!("result.stdout = {}", result.stdout);
println!("result.stderr = {}", result.stderr);
if is_ci() && result.stderr.contains("failed to get username") {
// In the CI, some server are failing to return whoami.
// As seems to be a configuration issue, ignoring it
return; return;
} }
assert!(result.success);
let ts = TestScenario::new("id");
let id = ts.cmd("id").arg("-un").run();
if is_ci() && id.stderr.contains("cannot find name for user ID") { let result_cmd = scene.cmd("id").arg("-un").run();
// In the CI, some server are failing to return whoami. if skipping_test_is_okay(&result_cmd, "cannot find name for user ID") {
// As seems to be a configuration issue, ignoring it
return; return;
} }
assert_eq!(result.stdout.trim(), id.stdout.trim());
assert_eq!(
result_ucmd.stdout_str().trim(),
result_cmd.stdout_str().trim()
);
} }