Add glob support to utouch (issue #13623) (#14674)

# Description
These changes resolve #13623 where globs are not handled by `utouch`. 

# User-Facing Changes
- Glob patterns passed to `utouch` will be resolved to all individual
files that match the pattern. For example, running `utouch *.txt` in a
directory that already has `file1.txt` and `file2.txt` is the same thing
as running `utouch file1.txt file2.txt`. All flags such as `-a`, `-m`
and `-c` will be respected.
- If a glob pattern is provided to `utouch` and doesn't match any files,
a file will be created with the literal name of the glob pattern. This
only applies to Linux/MacOS because Windows forbids creating file names
with restricted characters (see [naming a file
docs](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file))

---------

Co-authored-by: Henry Jetmundsen <hjetmundsen@atlassian.com>
This commit is contained in:
Henry Jetmundsen
2025-01-01 17:38:15 -08:00
committed by GitHub
parent 62bd6fe08b
commit c46ca36bcd
2 changed files with 104 additions and 2 deletions

View File

@ -83,6 +83,34 @@ fn creates_two_files() {
})
}
// Windows forbids file names with reserved characters
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file
#[test]
#[cfg(not(windows))]
fn creates_a_file_when_glob_is_quoted() {
Playground::setup("create_test_glob", |dirs, _sandbox| {
nu!(
cwd: dirs.test(),
"utouch '*.txt'"
);
let path = dirs.test().join("*.txt");
assert!(path.exists());
})
}
#[test]
fn fails_when_glob_has_no_matches() {
Playground::setup("create_test_glob_no_matches", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(),
"utouch *.txt"
);
assert!(actual.err.contains("No matches found for glob *.txt"));
})
}
#[test]
fn change_modified_time_of_file_to_today() {
Playground::setup("change_time_test_9", |dirs, sandbox| {
@ -168,6 +196,31 @@ fn change_modified_and_access_time_of_file_to_today() {
})
}
#[test]
fn change_modified_and_access_time_of_files_matching_glob_to_today() {
Playground::setup("change_mtime_atime_test_glob", |dirs, sandbox| {
sandbox.with_files(&[Stub::EmptyFile("file.txt")]);
let path = dirs.test().join("file.txt");
filetime::set_file_times(&path, TIME_ONE, TIME_ONE).unwrap();
nu!(
cwd: dirs.test(),
"utouch *.txt"
);
let metadata = path.metadata().unwrap();
// Check only the date since the time may not match exactly
let today = Local::now().date_naive();
let mtime_day = DateTime::<Local>::from(metadata.modified().unwrap()).date_naive();
let atime_day = DateTime::<Local>::from(metadata.accessed().unwrap()).date_naive();
assert_eq!(today, mtime_day);
assert_eq!(today, atime_day);
})
}
#[test]
fn not_create_file_if_it_not_exists() {
Playground::setup("change_time_test_28", |dirs, _sandbox| {