2023-08-21 10:49:27 +02:00
|
|
|
// This file is part of the uutils coreutils package.
|
|
|
|
|
//
|
|
|
|
|
// For the full copyright and license information, please view the LICENSE
|
|
|
|
|
// file that was distributed with this source code.
|
2026-04-28 05:08:39 +09:00
|
|
|
// spell-checker:ignore getpriority setpriority
|
2025-03-28 09:51:51 +01:00
|
|
|
use uutests::new_ucmd;
|
2021-03-12 13:38:02 -05:00
|
|
|
|
|
|
|
|
#[test]
|
2022-02-09 13:08:28 -05:00
|
|
|
#[cfg(not(target_os = "android"))]
|
2021-03-12 13:38:02 -05:00
|
|
|
fn test_get_current_niceness() {
|
2026-04-28 05:08:39 +09:00
|
|
|
// Test that the nice command with no arguments returns the default nice value
|
|
|
|
|
let nice = rustix::process::getpriority_process(None).unwrap();
|
|
|
|
|
new_ucmd!().succeeds().stdout_is(format!("{nice}\n"));
|
2021-03-12 13:38:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2022-02-09 13:08:28 -05:00
|
|
|
#[cfg(not(target_os = "android"))]
|
2021-03-12 13:38:02 -05:00
|
|
|
fn test_negative_adjustment() {
|
|
|
|
|
// This assumes the test suite is run as a normal (non-root) user, and as
|
|
|
|
|
// such attempting to set a negative niceness value will be rejected by
|
|
|
|
|
// the OS. If it gets denied, then we know a negative value was parsed
|
|
|
|
|
// correctly.
|
|
|
|
|
|
2025-03-09 16:53:56 +01:00
|
|
|
let res = new_ucmd!().args(&["-n", "-1", "true"]).succeeds();
|
2025-03-24 21:04:32 +01:00
|
|
|
assert!(
|
|
|
|
|
res.stderr_str()
|
|
|
|
|
.starts_with("nice: warning: setpriority: Permission denied")
|
|
|
|
|
); // spell-checker:disable-line
|
2021-03-12 13:38:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_adjustment_with_no_command_should_error() {
|
2021-11-09 17:23:41 -03:00
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-n", "19"])
|
|
|
|
|
.fails()
|
|
|
|
|
.usage_error("A command must be given with an adjustment.");
|
2021-03-12 13:38:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_command_with_no_adjustment() {
|
2025-03-09 16:53:56 +01:00
|
|
|
new_ucmd!().args(&["echo", "a"]).succeeds().stdout_is("a\n");
|
2021-03-12 13:38:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_command_with_no_args() {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-n", "19", "echo"])
|
2025-03-09 16:53:56 +01:00
|
|
|
.succeeds()
|
2021-03-12 13:38:02 -05:00
|
|
|
.stdout_is("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_command_with_args() {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-n", "19", "echo", "a", "b", "c"])
|
2025-03-09 16:53:56 +01:00
|
|
|
.succeeds()
|
2021-03-12 13:38:02 -05:00
|
|
|
.stdout_is("a b c\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_command_where_command_takes_n_flag() {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-n", "19", "echo", "-n", "a"])
|
2025-03-09 16:53:56 +01:00
|
|
|
.succeeds()
|
2021-03-12 13:38:02 -05:00
|
|
|
.stdout_is("a");
|
|
|
|
|
}
|
2022-04-30 22:45:17 +02:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_argument() {
|
2025-03-01 15:29:11 +01:00
|
|
|
new_ucmd!().arg("--invalid").fails_with_code(125);
|
2022-04-30 22:45:17 +02:00
|
|
|
}
|
2023-01-12 15:42:06 +00:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_bare_adjustment() {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-1", "echo", "-n", "a"])
|
2025-03-09 16:53:56 +01:00
|
|
|
.succeeds()
|
2023-01-12 15:42:06 +00:00
|
|
|
.stdout_is("a");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_trailing_empty_adjustment() {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-n", "1", "-n"])
|
|
|
|
|
.fails()
|
|
|
|
|
.stderr_str()
|
|
|
|
|
.starts_with(
|
|
|
|
|
"error: The argument '--adjustment <adjustment>' requires a value but none was supplied",
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-01-19 00:02:28 +09:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_nice_huge() {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&[
|
|
|
|
|
"-n",
|
|
|
|
|
"99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
|
|
|
|
|
"true",
|
|
|
|
|
])
|
|
|
|
|
.succeeds()
|
|
|
|
|
.no_stdout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_nice_huge_negative() {
|
|
|
|
|
new_ucmd!().args(&["-n", "-9999999999", "true"]).succeeds();
|
|
|
|
|
//.stderr_contains("Permission denied"); Depending on platform?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_sign_middle() {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-n", "-2+4", "true"])
|
|
|
|
|
.fails_with_code(125)
|
|
|
|
|
.no_stdout()
|
|
|
|
|
.stderr_contains("invalid");
|
|
|
|
|
}
|
|
|
|
|
//uu: "-2+4" is not a valid number: invalid digit found in string
|
|
|
|
|
//gnu: invalid adjustment `-2+4'
|
|
|
|
|
//Both message is fine
|