1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// 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.
// spell-checker:ignore dont
use crate::common::util::TestScenario;
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails_with_code(125);
}
// FIXME: this depends on the system having true and false in PATH
// the best solution is probably to generate some test binaries that we can call for any
// utility that requires executing another program (kill, for instance)
#[test]
fn test_subcommand_return_code() {
new_ucmd!().arg("1").arg("true").succeeds();
new_ucmd!().arg("1").arg("false").fails_with_code(1);
}
#[test]
fn test_invalid_time_interval() {
new_ucmd!()
.args(&["xyz", "sleep", "0"])
.fails_with_code(125)
.usage_error("invalid time interval 'xyz'");
}
#[test]
fn test_invalid_kill_after() {
new_ucmd!()
.args(&["-k", "xyz", "1", "sleep", "0"])
.fails_with_code(125)
.usage_error("invalid time interval 'xyz'");
}
#[test]
fn test_command_with_args() {
new_ucmd!()
.args(&["1700", "echo", "-n", "abcd"])
.succeeds()
.stdout_only("abcd");
}
#[test]
fn test_verbose() {
for verbose_flag in ["-v", "--verbose"] {
new_ucmd!()
.args(&[verbose_flag, ".1", "sleep", "10"])
.fails()
.stderr_only("timeout: sending signal TERM to command 'sleep'\n");
new_ucmd!()
.args(&[verbose_flag, "-s0", "-k.1", ".1", "sleep", "10"])
.fails()
.stderr_only("timeout: sending signal EXIT to command 'sleep'\ntimeout: sending signal KILL to command 'sleep'\n");
}
}
#[test]
fn test_zero_timeout() {
new_ucmd!()
.args(&["-v", "0", "sleep", ".1"])
.succeeds()
.no_output();
new_ucmd!()
.args(&["-v", "0", "-s0", "-k0", "sleep", ".1"])
.succeeds()
.no_output();
}
#[test]
fn test_command_empty_args() {
new_ucmd!()
.args(&["", ""])
.fails()
.stderr_contains("timeout: empty string");
}
#[test]
fn test_foreground() {
for arg in ["-f", "--foreground"] {
new_ucmd!()
.args(&[arg, ".1", "sleep", "10"])
.fails_with_code(124)
.no_output();
}
}
#[test]
fn test_preserve_status() {
for arg in ["-p", "--preserve-status"] {
new_ucmd!()
.args(&[arg, ".1", "sleep", "10"])
// 128 + SIGTERM = 128 + 15
.fails_with_code(128 + 15)
.no_output();
}
}
#[test]
fn test_preserve_status_even_when_send_signal() {
// When sending CONT signal, process doesn't get killed or stopped.
// So, expected result is success and code 0.
for cont_spelling in ["CONT", "cOnT", "SIGcont"] {
new_ucmd!()
.args(&["-s", cont_spelling, "--preserve-status", ".1", "sleep", "2"])
.succeeds()
.no_output();
}
}
#[test]
fn test_dont_overflow() {
new_ucmd!()
.args(&["9223372036854775808d", "sleep", "0"])
.succeeds()
.no_output();
new_ucmd!()
.args(&["-k", "9223372036854775808d", "10", "sleep", "0"])
.succeeds()
.no_output();
}
#[test]
fn test_negative_interval() {
new_ucmd!()
.args(&["--", "-1", "sleep", "0"])
.fails()
.usage_error("invalid time interval '-1'");
}
#[test]
fn test_invalid_signal() {
new_ucmd!()
.args(&["-s", "invalid", "1", "sleep", "0"])
.fails()
.usage_error("'invalid': invalid signal");
}
#[test]
fn test_invalid_multi_byte_characters() {
new_ucmd!()
.args(&["10€", "sleep", "0"])
.fails()
.usage_error("invalid time interval '10€'");
}
/// Test that the long form of the `--kill-after` argument is recognized.
#[test]
fn test_kill_after_long() {
new_ucmd!()
.args(&["--kill-after=1", "1", "sleep", "0"])
.succeeds()
.no_output();
}
#[test]
fn test_kill_subprocess() {
new_ucmd!()
.args(&[
// Make sure the CI can spawn the subprocess.
"10",
"sh",
"-c",
"trap 'echo inside_trap' TERM; sleep 30",
])
.fails_with_code(124)
.stdout_contains("inside_trap")
.stderr_contains("Terminated");
}
|