Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/cmds/dotnet/dotnet_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,7 @@ fn format_restore_output(
#[cfg(test)]
mod tests {
use super::*;
use crate::cmds::dotnet::test_support;
use crate::dotnet_format_report;
use std::fs;
use std::time::Duration;
Expand Down Expand Up @@ -2935,16 +2936,16 @@ mod tests {
fn test_merge_test_summary_from_trx_ignores_stale_fallback_file() {
let temp_dir = tempfile::tempdir().expect("create temp dir");
let fallback = temp_dir.path().join("fallback.trx");
fs::write(&fallback, trx_with_counts(2, 1, 1)).expect("write fallback trx");
std::thread::sleep(std::time::Duration::from_millis(5));
let command_started_at = SystemTime::now();
let base = SystemTime::now();
let stale = base - std::time::Duration::from_secs(86_400);
test_support::write_trx_with_mtime(&fallback, &trx_with_counts(2, 1, 1), stale);
let missing_primary = temp_dir.path().join("missing.trx");

let filled = merge_test_summary_from_trx(
binlog::TestSummary::default(),
Some(&missing_primary),
Some(fallback.clone()),
command_started_at,
base,
);

assert_eq!(filled.total, 0);
Expand Down
26 changes: 14 additions & 12 deletions src/cmds/dotnet/dotnet_trx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ fn parse_trx_content(content: &str) -> Option<TestSummary> {
#[cfg(test)]
mod tests {
use super::*;
use crate::cmds::dotnet::test_support;
use std::time::Duration;

#[test]
Expand Down Expand Up @@ -493,11 +494,14 @@ mod tests {
let testresults_dir = temp_dir.path().join("TestResults");
std::fs::create_dir_all(&testresults_dir).expect("create TestResults");

let base = SystemTime::now();
let old = base - Duration::from_secs(86_400);
let new = base + Duration::from_secs(3_600);

let old_trx = testresults_dir.join("old.trx");
let new_trx = testresults_dir.join("new.trx");
std::fs::write(&old_trx, "old").expect("write old");
std::thread::sleep(Duration::from_millis(5));
std::fs::write(&new_trx, "new").expect("write new");
test_support::write_trx_with_mtime(&old_trx, "old", old);
test_support::write_trx_with_mtime(&new_trx, "new", new);

let found = find_recent_trx_in_dir(&testresults_dir).expect("should find newest trx");
assert_eq!(found, new_trx);
Expand Down Expand Up @@ -554,21 +558,19 @@ mod tests {
let trx_dir = temp_dir.path().join("TestResults");
std::fs::create_dir_all(&trx_dir).expect("create TestResults");

let base = SystemTime::now();
let old = base - Duration::from_secs(86_400);
let new = base + Duration::from_secs(3_600);

let trx_old = r#"<?xml version="1.0" encoding="utf-8"?>
<TestRun><ResultSummary><Counters total="2" executed="2" passed="2" failed="0" /></ResultSummary></TestRun>"#;
std::fs::write(trx_dir.join("old.trx"), trx_old).expect("write old trx");

std::thread::sleep(Duration::from_millis(10));

let since = SystemTime::now()
.checked_sub(Duration::from_millis(10))
.expect("threshold overflow");
test_support::write_trx_with_mtime(&trx_dir.join("old.trx"), trx_old, old);

let trx_new = r#"<?xml version="1.0" encoding="utf-8"?>
<TestRun><ResultSummary><Counters total="3" executed="3" passed="2" failed="1" /></ResultSummary></TestRun>"#;
std::fs::write(trx_dir.join("new.trx"), trx_new).expect("write new trx");
test_support::write_trx_with_mtime(&trx_dir.join("new.trx"), trx_new, new);

let summary = parse_trx_files_in_dir_since(&trx_dir, Some(since)).expect("merged summary");
let summary = parse_trx_files_in_dir_since(&trx_dir, Some(base)).expect("merged summary");
assert_eq!(summary.total, 3);
assert_eq!(summary.failed, 1);
}
Expand Down
18 changes: 18 additions & 0 deletions src/cmds/dotnet/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
automod::dir!(pub "src/cmds/dotnet");

#[cfg(test)]
pub(crate) mod test_support {
use std::path::Path;
use std::time::SystemTime;

/// Writes a fixture file and pins its mtime to an explicit timestamp so
/// tests never rely on wall-clock deltas or `thread::sleep` separation.
pub(crate) fn write_trx_with_mtime(path: &Path, contents: &str, modified: SystemTime) {
std::fs::write(path, contents).expect("write fixture");
std::fs::File::options()
.write(true)
.open(path)
.expect("open fixture for write")
.set_modified(modified)
.expect("set fixture mtime");
}
}