extern crate regex;
use crate::common::util::*;
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
#[test]
fn test_invalid_option() {
new_ucmd!().arg("-w").arg("-q").arg("/").fails();
}
#[cfg(unix)]
const NORMAL_FORMAT_STR: &str =
"%a %A %b %B %d %D %f %F %g %G %h %i %m %n %o %s %u %U %x %X %y %Y %z %Z"; #[cfg(any(target_os = "linux", target_os = "android"))]
const DEV_FORMAT_STR: &str =
"%a %A %b %B %d %D %f %F %g %G %h %i %m %n %o %s (%t/%T) %u %U %w %W %x %X %y %Y %z %Z";
#[cfg(target_os = "linux")]
const FS_FORMAT_STR: &str = "%b %c %i %l %n %s %S %t %T";
#[test]
#[cfg(any(target_os = "linux", target_os = "android"))]
fn test_terse_fs_format() {
let args = ["-f", "-t", "/proc"];
let ts = TestScenario::new(util_name!());
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).run().stdout_is(expected_stdout);
}
#[test]
#[cfg(target_os = "linux")]
fn test_fs_format() {
let args = ["-f", "-c", FS_FORMAT_STR, "/dev/shm"];
let ts = TestScenario::new(util_name!());
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).run().stdout_is(expected_stdout);
}
#[cfg(unix)]
#[test]
fn test_terse_normal_format() {
let args = ["-t", "/"];
let ts = TestScenario::new(util_name!());
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str();
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
println!("actual: {:?}", actual);
println!("expect: {:?}", expect);
let v_actual: Vec<&str> = actual.trim().split(' ').collect();
let mut v_expect: Vec<&str> = expect.trim().split(' ').collect();
assert!(!v_expect.is_empty());
if v_actual.len() == v_expect.len() - 1 && v_expect[v_expect.len() - 1].contains(':') {
v_expect.pop();
}
assert!(
expect == "0"
|| expect == "0\n"
|| v_actual
.iter()
.zip(v_expect.iter())
.all(|(a, e)| a == e || *e == "0" || *e == "0\n")
);
}
#[cfg(unix)]
#[test]
fn test_format_created_time() {
let args = ["-c", "%w", "/bin"];
let ts = TestScenario::new(util_name!());
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str();
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
println!("actual: {:?}", actual);
println!("expect: {:?}", expect);
let re = regex::Regex::new(r"\s").unwrap();
let v_actual: Vec<&str> = re.split(&actual).collect();
let v_expect: Vec<&str> = re.split(&expect).collect();
assert!(!v_expect.is_empty());
assert!(
expect == "-"
|| expect == "-\n"
|| v_actual
.iter()
.zip(v_expect.iter())
.all(|(a, e)| a == e || *e == "-" || *e == "-\n")
);
}
#[cfg(unix)]
#[test]
fn test_format_created_seconds() {
let args = ["-c", "%W", "/bin"];
let ts = TestScenario::new(util_name!());
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str();
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
println!("actual: {:?}", actual);
println!("expect: {:?}", expect);
let re = regex::Regex::new(r"\s").unwrap();
let v_actual: Vec<&str> = re.split(&actual).collect();
let v_expect: Vec<&str> = re.split(&expect).collect();
assert!(!v_expect.is_empty());
assert!(
expect == "0"
|| expect == "0\n"
|| v_actual
.iter()
.zip(v_expect.iter())
.all(|(a, e)| a == e || *e == "0" || *e == "0\n")
);
}
#[cfg(unix)]
#[test]
fn test_normal_format() {
let args = ["-c", NORMAL_FORMAT_STR, "/bin"];
let ts = TestScenario::new(util_name!());
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
}
#[cfg(unix)]
#[test]
fn test_symlinks() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let mut tested: bool = false;
for file in [
"/bin/sh",
"/data/data/com.termux/files/usr/bin/sh", "/bin/sudoedit",
"/usr/bin/ex",
"/etc/localtime",
"/etc/aliases",
] {
if at.file_exists(file) && at.is_symlink(file) {
tested = true;
let args = ["-c", NORMAL_FORMAT_STR, file];
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
let args = ["-L", "-c", NORMAL_FORMAT_STR, file];
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
}
}
if !tested {
panic!("No symlink found to test in this environment");
}
}
#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))]
#[test]
fn test_char() {
let args = [
"-c",
#[cfg(any(target_os = "linux", target_os = "android"))]
DEV_FORMAT_STR,
#[cfg(target_os = "linux")]
"/dev/pts/ptmx",
#[cfg(any(target_vendor = "apple"))]
"%a %A %b %B %d %D %f %F %g %G %h %i %m %n %o %s (/%T) %u %U %W %X %y %Y %z %Z",
#[cfg(any(target_os = "android", target_vendor = "apple"))]
"/dev/ptmx",
];
let ts = TestScenario::new(util_name!());
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
}
#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))]
#[test]
fn test_date() {
let args = [
"-c",
#[cfg(any(target_os = "linux", target_os = "android"))]
"%z",
#[cfg(target_os = "linux")]
"/bin/sh",
#[cfg(any(target_vendor = "apple"))]
"%z",
#[cfg(any(target_os = "android", target_vendor = "apple"))]
"/bin/sh",
];
let ts = TestScenario::new(util_name!());
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
let args = [
"-c",
#[cfg(any(target_os = "linux", target_os = "android"))]
"%z",
#[cfg(target_os = "linux")]
"/dev/ptmx",
#[cfg(any(target_vendor = "apple"))]
"%z",
#[cfg(any(target_os = "android", target_vendor = "apple"))]
"/dev/ptmx",
];
let ts = TestScenario::new(util_name!());
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
}
#[cfg(unix)]
#[test]
fn test_multi_files() {
let args = [
"-c",
NORMAL_FORMAT_STR,
"/dev",
"/usr/lib",
#[cfg(target_os = "linux")]
"/etc/fstab",
"/var",
];
let ts = TestScenario::new(util_name!());
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
}
#[cfg(unix)]
#[test]
fn test_printf() {
let args = [
"--printf=123%-# 15q\\r\\\"\\\\\\a\\b\\e\\f\\v%+020.23m\\x12\\167\\132\\112\\n",
"/",
];
let ts = TestScenario::new(util_name!());
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
}
#[test]
#[cfg(unix)]
fn test_pipe_fifo() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkfifo("FIFO");
ucmd.arg("FIFO")
.run()
.no_stderr()
.stdout_contains("fifo")
.stdout_contains("File: FIFO")
.succeeded();
}
#[test]
#[cfg(all(unix, not(any(target_os = "android", target_os = "freebsd"))))]
fn test_stdin_pipe_fifo1() {
new_ucmd!()
.arg("-")
.set_stdin(std::process::Stdio::piped())
.run()
.no_stderr()
.stdout_contains("fifo")
.stdout_contains("File: -")
.succeeded();
new_ucmd!()
.args(&["-L", "-"])
.set_stdin(std::process::Stdio::piped())
.run()
.no_stderr()
.stdout_contains("fifo")
.stdout_contains("File: -")
.succeeded();
}
#[test]
#[cfg(all(unix, not(target_os = "android")))]
fn test_stdin_pipe_fifo2() {
new_ucmd!()
.arg("-")
.set_stdin(std::process::Stdio::null())
.run()
.no_stderr()
.stdout_contains("character special file")
.stdout_contains("File: -")
.succeeded();
}
#[test]
#[cfg(all(
unix,
not(any(target_os = "android", target_os = "macos", target_os = "freebsd"))
))]
fn test_stdin_redirect() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.touch("f");
ts.ucmd()
.arg("-")
.set_stdin(std::fs::File::open(at.plus("f")).unwrap())
.run()
.no_stderr()
.stdout_contains("regular empty file")
.stdout_contains("File: -")
.succeeded();
}