coreutils/tests/by-util/test_date.rs

223 lines
5.7 KiB
Rust
Raw Normal View History

2020-05-01 23:03:01 +00:00
extern crate regex;
use self::regex::Regex;
use crate::common::util::*;
#[cfg(all(unix, not(target_os = "macos")))]
use rust_users::*;
2020-05-01 23:03:01 +00:00
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
2020-05-01 23:03:01 +00:00
#[test]
fn test_date_email() {
2022-04-02 08:47:37 +00:00
for param in ["--rfc-email", "--rfc-e", "-R"] {
new_ucmd!().arg(param).succeeds();
}
2020-05-01 23:03:01 +00:00
}
#[test]
fn test_date_rfc_3339() {
let scene = TestScenario::new(util_name!());
2021-04-22 20:37:44 +00:00
let rfc_regexp = concat!(
r#"(\d+)-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])\s([01]\d|2[0-3]):"#,
r#"([0-5]\d):([0-5]\d|60)(\.\d+)?(([Zz])|([\+|\-]([01]\d|2[0-3])))"#
);
2020-05-01 23:03:01 +00:00
let re = Regex::new(rfc_regexp).unwrap();
// Check that the output matches the regexp
2022-04-02 08:47:37 +00:00
for param in ["--rfc-3339", "--rfc-3"] {
scene
.ucmd()
.arg(format!("{}=ns", param))
.succeeds()
.stdout_matches(&re);
2021-04-22 20:37:44 +00:00
scene
.ucmd()
.arg(format!("{}=seconds", param))
.succeeds()
.stdout_matches(&re);
}
2020-05-01 23:03:01 +00:00
}
#[test]
fn test_date_rfc_8601() {
2022-04-02 08:47:37 +00:00
for param in ["--iso-8601", "--i"] {
new_ucmd!().arg(format!("{}=ns", param)).succeeds();
}
2020-05-01 23:03:01 +00:00
}
#[test]
fn test_date_rfc_8601_second() {
2022-04-02 08:47:37 +00:00
for param in ["--iso-8601", "--i"] {
new_ucmd!().arg(format!("{}=second", param)).succeeds();
}
2020-05-01 23:03:01 +00:00
}
#[test]
fn test_date_utc() {
2022-04-02 08:47:37 +00:00
for param in ["--universal", "--utc", "--uni", "--u"] {
new_ucmd!().arg(param).succeeds();
}
2020-05-01 23:03:01 +00:00
}
#[test]
fn test_date_format_y() {
let scene = TestScenario::new(util_name!());
let mut re = Regex::new(r"^\d{4}$").unwrap();
2021-04-22 20:37:44 +00:00
scene.ucmd().arg("+%Y").succeeds().stdout_matches(&re);
2020-05-01 23:03:01 +00:00
re = Regex::new(r"^\d{2}$").unwrap();
2021-04-22 20:37:44 +00:00
scene.ucmd().arg("+%y").succeeds().stdout_matches(&re);
2020-05-01 23:03:01 +00:00
}
#[test]
fn test_date_format_m() {
let scene = TestScenario::new(util_name!());
let mut re = Regex::new(r"\S+").unwrap();
2021-04-22 20:37:44 +00:00
scene.ucmd().arg("+%b").succeeds().stdout_matches(&re);
2020-05-01 23:03:01 +00:00
re = Regex::new(r"^\d{2}$").unwrap();
2021-04-22 20:37:44 +00:00
scene.ucmd().arg("+%m").succeeds().stdout_matches(&re);
2020-05-01 23:03:01 +00:00
}
#[test]
fn test_date_format_day() {
let scene = TestScenario::new(util_name!());
let mut re = Regex::new(r"\S+").unwrap();
2021-04-22 20:37:44 +00:00
scene.ucmd().arg("+%a").succeeds().stdout_matches(&re);
2020-05-01 23:03:01 +00:00
re = Regex::new(r"\S+").unwrap();
2021-04-22 20:37:44 +00:00
scene.ucmd().arg("+%A").succeeds().stdout_matches(&re);
2020-05-01 23:03:01 +00:00
re = Regex::new(r"^\d{1}$").unwrap();
2021-04-22 20:37:44 +00:00
scene.ucmd().arg("+%u").succeeds().stdout_matches(&re);
2020-05-01 23:03:01 +00:00
}
#[test]
fn test_date_format_full_day() {
let re = Regex::new(r"\S+ \d{4}-\d{2}-\d{2}").unwrap();
2021-04-22 20:37:44 +00:00
new_ucmd!()
.arg("+'%a %Y-%m-%d'")
.succeeds()
.stdout_matches(&re);
2020-05-01 23:03:01 +00:00
}
2021-05-11 21:03:59 +00:00
#[test]
fn test_date_nano_seconds() {
// %N nanoseconds (000000000..999999999)
let re = Regex::new(r"^\d{1,9}$").unwrap();
new_ucmd!().arg("+%N").succeeds().stdout_matches(&re);
}
#[test]
fn test_date_format_without_plus() {
// [+FORMAT]
new_ucmd!()
.arg("%s")
.fails()
.stderr_contains("date: invalid date '%s'")
2021-05-11 21:03:59 +00:00
.code_is(1);
}
#[test]
fn test_date_format_literal() {
new_ucmd!().arg("+%%s").succeeds().stdout_is("%s\n");
new_ucmd!().arg("+%%N").succeeds().stdout_is("%N\n");
}
#[test]
#[cfg(all(unix, not(target_os = "macos")))]
fn test_date_set_valid() {
if get_effective_uid() == 0 {
2021-04-22 20:37:44 +00:00
new_ucmd!()
2021-03-19 17:01:43 +00:00
.arg("--set")
.arg("2020-03-12 13:30:00+08:00")
2021-04-22 20:37:44 +00:00
.succeeds()
.no_stdout()
.no_stderr();
}
}
#[test]
#[cfg(any(windows, all(unix, not(target_os = "macos"))))]
fn test_date_set_invalid() {
2021-04-22 20:37:44 +00:00
let result = new_ucmd!().arg("--set").arg("123abcd").fails();
result.no_stdout();
assert!(result.stderr_str().starts_with("date: invalid date "));
}
#[test]
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
fn test_date_set_permissions_error() {
if !(get_effective_uid() == 0 || uucore::os::is_wsl_1()) {
2021-04-22 20:37:44 +00:00
let result = new_ucmd!()
.arg("--set")
.arg("2020-03-11 21:45:00+08:00")
.fails();
result.no_stdout();
assert!(result.stderr_str().starts_with("date: cannot set date: "));
}
}
#[test]
#[cfg(target_os = "macos")]
fn test_date_set_mac_unavailable() {
2021-04-22 20:37:44 +00:00
let result = new_ucmd!()
.arg("--set")
.arg("2020-03-11 21:45:00+08:00")
.fails();
result.no_stdout();
2021-03-19 17:01:43 +00:00
assert!(result
2021-04-22 20:37:44 +00:00
.stderr_str()
2021-03-19 17:01:43 +00:00
.starts_with("date: setting the date is not supported by macOS"));
}
#[test]
#[cfg(all(unix, not(target_os = "macos")))]
/// TODO: expected to fail currently; change to succeeds() when required.
fn test_date_set_valid_2() {
if get_effective_uid() == 0 {
2021-04-22 20:37:44 +00:00
let result = new_ucmd!()
.arg("--set")
2021-05-31 03:55:28 +00:00
.arg("Sat 20 Mar 2021 14:53:01 AWST") // spell-checker:disable-line
.fails();
2021-04-22 20:37:44 +00:00
result.no_stdout();
assert!(result.stderr_str().starts_with("date: invalid date "));
}
}
#[test]
#[cfg(all(unix, not(target_os = "macos")))]
/// TODO: expected to fail currently; change to succeeds() when required.
fn test_date_set_valid_3() {
if get_effective_uid() == 0 {
2021-04-22 20:37:44 +00:00
let result = new_ucmd!()
.arg("--set")
.arg("Sat 20 Mar 2021 14:53:01") // Local timezone
.fails();
2021-04-22 20:37:44 +00:00
result.no_stdout();
assert!(result.stderr_str().starts_with("date: invalid date "));
}
}
#[test]
#[cfg(all(unix, not(target_os = "macos")))]
/// TODO: expected to fail currently; change to succeeds() when required.
fn test_date_set_valid_4() {
if get_effective_uid() == 0 {
2021-04-22 20:37:44 +00:00
let result = new_ucmd!()
.arg("--set")
.arg("2020-03-11 21:45:00") // Local timezone
.fails();
2021-04-22 20:37:44 +00:00
result.no_stdout();
assert!(result.stderr_str().starts_with("date: invalid date "));
}
}