Skip to content

Commit 11dcc66

Browse files
committed
Restore inline mtime; stabilize list_files test
1 parent 45089b1 commit 11dcc66

1 file changed

Lines changed: 52 additions & 22 deletions

File tree

src/root/ifconfig.rs

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::{
55
io::{Read, Write},
66
net::IpAddr,
77
process::Command,
8-
time::SystemTime,
98
};
109

1110
use anyhow::{Result, anyhow};
@@ -429,13 +428,17 @@ fn list_files(
429428
for path in paths.flatten() {
430429
let filepath = path.path();
431430
let metadata = fs::metadata(filepath)?;
432-
let modified = format_modified_time(metadata.modified()?);
431+
let modified: DateTime<Local> = metadata.modified()?.into();
433432

434433
if let Some(filename) = path.path().file_name()
435434
&& let Some(filename) = filename.to_str()
436435
{
437436
if metadata.is_file() {
438-
files.push((metadata.len(), modified, filename.to_string()));
437+
files.push((
438+
metadata.len(),
439+
format!("{}", modified.format("%Y/%m/%d %T")),
440+
filename.to_string(),
441+
));
439442
} else if subdir && metadata.is_dir() {
440443
files.push((0, String::new(), filename.to_string()));
441444
/*
@@ -458,13 +461,6 @@ fn list_files(
458461
Ok(files)
459462
}
460463

461-
// Formats a file modification time as local time in the pattern
462-
// "YYYY/MM/DD HH:MM:SS".
463-
fn format_modified_time(modified: SystemTime) -> String {
464-
let modified: DateTime<Local> = modified.into();
465-
modified.format("%Y/%m/%d %T").to_string()
466-
}
467-
468464
fn run_command(cmd: &str, args: &[&str]) -> Result<bool> {
469465
let status = Command::new(cmd)
470466
.env("PATH", "/usr/sbin:/usr/bin:/sbin:/bin")
@@ -688,23 +684,57 @@ mod tests {
688684
fs::remove_dir_all(&dir).expect("temp test directory should be removable");
689685
}
690686

687+
const LIST_FILES_MTIME_CHILD_ENV: &str = "ROXY_LIST_FILES_MTIME_CHILD";
688+
691689
#[test]
692690
fn list_files_formats_modified_time_in_local_timezone() {
693-
// SAFETY: TZ is mutated process-wide. We force UTC so that the
694-
// local-time formatting in `list_files` is deterministic across CI
695-
// and developer machines, regardless of the host's actual zone.
696-
unsafe {
697-
std::env::set_var("TZ", "UTC");
691+
if std::env::var_os(LIST_FILES_MTIME_CHILD_ENV).is_some() {
692+
let dir = make_temp_test_dir("list-mtime");
693+
let file_path = dir.join("fixed.yaml");
694+
write_text_file(&file_path, "fixed");
695+
696+
let ts = UNIX_EPOCH + Duration::from_secs(1_700_000_000);
697+
let file = OpenOptions::new()
698+
.write(true)
699+
.open(&file_path)
700+
.expect("test file should be openable for timestamp update");
701+
file.set_modified(ts)
702+
.expect("test file modified time should be settable");
703+
704+
let listed = list_files(
705+
dir.to_str().expect("temp path should be valid utf-8"),
706+
None,
707+
false,
708+
)
709+
.expect("listing files should succeed");
710+
assert_eq!(
711+
listed,
712+
vec![(
713+
5,
714+
"2023/11/14 22:13:20".to_string(),
715+
"fixed.yaml".to_string()
716+
)]
717+
);
718+
719+
fs::remove_dir_all(&dir).expect("temp test directory should be removable");
720+
return;
698721
}
699722

700-
// 1_700_000_000 seconds after the Unix epoch is
701-
// 2023-11-14 22:13:20 UTC. With TZ=UTC, local time equals UTC.
702-
let ts = UNIX_EPOCH + Duration::from_secs(1_700_000_000);
703-
assert_eq!(format_modified_time(ts), "2023/11/14 22:13:20");
723+
let output = std::process::Command::new(
724+
std::env::current_exe().expect("current test executable should be available"),
725+
)
726+
.arg("list_files_formats_modified_time_in_local_timezone")
727+
.env(LIST_FILES_MTIME_CHILD_ENV, "1")
728+
.env("TZ", "UTC")
729+
.output()
730+
.expect("child test process should run");
704731

705-
// Verify the format width is fixed: 4+1+2+1+2+1+2+1+2+1+2 = 19 chars.
706-
assert_eq!(format_modified_time(UNIX_EPOCH).len(), 19);
707-
assert_eq!(format_modified_time(UNIX_EPOCH), "1970/01/01 00:00:00");
732+
assert!(
733+
output.status.success(),
734+
"child test process should pass\nstdout:\n{}\nstderr:\n{}",
735+
String::from_utf8_lossy(&output.stdout),
736+
String::from_utf8_lossy(&output.stderr)
737+
);
708738
}
709739

710740
#[test]

0 commit comments

Comments
 (0)