2022-11-26 19:19:02 +01:00
|
|
|
use chrono::{DateTime, Local};
|
2022-04-07 13:44:05 +02:00
|
|
|
use nu_test_support::fs::Stub;
|
2020-02-18 21:54:32 +01:00
|
|
|
use nu_test_support::nu;
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
|
|
|
|
#[test]
|
2020-03-01 18:19:09 +01:00
|
|
|
fn creates_a_file_when_it_doesnt_exist() {
|
|
|
|
Playground::setup("create_test_1", |dirs, _sandbox| {
|
2020-02-18 21:54:32 +01:00
|
|
|
nu!(
|
2020-03-01 18:19:09 +01:00
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch i_will_be_created.txt"
|
2020-02-18 21:54:32 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("i_will_be_created.txt");
|
2020-08-22 02:08:30 +02:00
|
|
|
assert!(path.exists());
|
|
|
|
})
|
|
|
|
}
|
2020-02-18 21:54:32 +01:00
|
|
|
|
2020-08-22 02:08:30 +02:00
|
|
|
#[test]
|
|
|
|
fn creates_two_files() {
|
|
|
|
Playground::setup("create_test_2", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch a b"
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("a");
|
2020-02-18 21:54:32 +01:00
|
|
|
assert!(path.exists());
|
2020-08-22 02:08:30 +02:00
|
|
|
|
|
|
|
let path2 = dirs.test().join("b");
|
|
|
|
assert!(path2.exists());
|
2020-02-18 21:54:32 +01:00
|
|
|
})
|
|
|
|
}
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_modified_time_of_file_to_today() {
|
|
|
|
Playground::setup("change_time_test_9", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![Stub::EmptyFile("file.txt")]);
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -m file.txt"
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file.txt");
|
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
2022-11-26 19:19:02 +01:00
|
|
|
let date = Local::now().date_naive();
|
|
|
|
let actual_date_time: DateTime<Local> =
|
|
|
|
DateTime::from(path.metadata().unwrap().modified().unwrap());
|
|
|
|
let actual_date = actual_date_time.date_naive();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
assert_eq!(date, actual_date);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_access_time_of_file_to_today() {
|
|
|
|
Playground::setup("change_time_test_18", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![Stub::EmptyFile("file.txt")]);
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -a file.txt"
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file.txt");
|
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
2022-11-26 19:19:02 +01:00
|
|
|
let date = Local::now().date_naive();
|
|
|
|
let actual_date_time: DateTime<Local> =
|
|
|
|
DateTime::from(path.metadata().unwrap().accessed().unwrap());
|
|
|
|
let actual_date = actual_date_time.date_naive();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
assert_eq!(date, actual_date);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_modified_and_access_time_of_file_to_today() {
|
|
|
|
Playground::setup("change_time_test_27", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![Stub::EmptyFile("file.txt")]);
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -a -m file.txt"
|
|
|
|
);
|
|
|
|
|
|
|
|
let metadata = dirs.test().join("file.txt").metadata().unwrap();
|
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
2022-11-26 19:19:02 +01:00
|
|
|
let date = Local::now().date_naive();
|
|
|
|
let adate_time: DateTime<Local> = DateTime::from(metadata.accessed().unwrap());
|
|
|
|
let adate = adate_time.date_naive();
|
|
|
|
let mdate_time: DateTime<Local> = DateTime::from(metadata.modified().unwrap());
|
|
|
|
let mdate = mdate_time.date_naive();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
assert_eq!(date, adate);
|
|
|
|
assert_eq!(date, mdate);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_create_file_if_it_not_exists() {
|
|
|
|
Playground::setup("change_time_test_28", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
2022-09-28 02:48:34 +02:00
|
|
|
r#"touch -c file.txt"#
|
2022-04-07 13:44:05 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file.txt");
|
|
|
|
|
|
|
|
assert!(!path.exists());
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
r#"touch -c file.txt"#
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file.txt");
|
|
|
|
|
|
|
|
assert!(!path.exists());
|
|
|
|
})
|
|
|
|
}
|
2023-04-05 19:22:56 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn creates_file_three_dots() {
|
|
|
|
Playground::setup("create_test_1", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch file..."
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file...");
|
|
|
|
assert!(path.exists());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn creates_file_four_dots() {
|
|
|
|
Playground::setup("create_test_1", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch file...."
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file....");
|
|
|
|
assert!(path.exists());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn creates_file_four_dots_quotation_marks() {
|
|
|
|
Playground::setup("create_test_1", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch 'file....'"
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file....");
|
|
|
|
assert!(path.exists());
|
|
|
|
})
|
|
|
|
}
|