diff --git a/Mono.TextTemplating.Build.Tests/MSBuildExecutionTests.cs b/Mono.TextTemplating.Build.Tests/MSBuildExecutionTests.cs
index 1b16527..56c2d23 100644
--- a/Mono.TextTemplating.Build.Tests/MSBuildExecutionTests.cs
+++ b/Mono.TextTemplating.Build.Tests/MSBuildExecutionTests.cs
@@ -1,10 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-using System;
-using System.IO;
using System.Linq;
-
using Xunit;
namespace Mono.TextTemplating.Tests
@@ -154,15 +151,15 @@ void AssertNoopBuild ()
AssertNoopBuild ();
// check touching a template causes rebuild of that file only
- File.SetLastWriteTime (fooTemplate, DateTime.Now);
+ WriteTimeTracker.SetWriteTimeNewerThan (barWriteTime, barTemplate);
ExecuteAndValidate ();
- fooWriteTime.AssertChanged ();
- barWriteTime.AssertSame ();
+ fooWriteTime.AssertSame ();
+ barWriteTime.AssertChanged ();
AssertNoopBuild ();
// check touching the include causes rebuild of the file that uses it
- File.SetLastWriteTime (includeFile, DateTime.Now);
+ WriteTimeTracker.SetWriteTimeNewerThan (fooWriteTime, includeFile);
ExecuteAndValidate ();
fooWriteTime.AssertChanged ();
barWriteTime.AssertSame ();
@@ -170,7 +167,6 @@ void AssertNoopBuild ()
AssertNoopBuild ();
// check changing a parameter causes rebuild of both files
- File.SetLastWriteTime (includeFile, DateTime.Now);
project.Project.GetItems ("T4Argument").Single (i => i.UnevaluatedInclude == "Year").SetMetadataValue ("Value", "2021");
ExecuteAndValidate ();
fooGenerated.AssertTextStartsWith ("Helper says Hello 2021!");
diff --git a/Mono.TextTemplating.Build.Tests/Mono.TextTemplating.Build.Tests.csproj b/Mono.TextTemplating.Build.Tests/Mono.TextTemplating.Build.Tests.csproj
index 89ac0d1..e3dd14d 100644
--- a/Mono.TextTemplating.Build.Tests/Mono.TextTemplating.Build.Tests.csproj
+++ b/Mono.TextTemplating.Build.Tests/Mono.TextTemplating.Build.Tests.csproj
@@ -34,6 +34,7 @@
+
diff --git a/Mono.TextTemplating.Tests/TestDataPath.cs b/Mono.TextTemplating.Tests/TestDataPath.cs
index cb0e9d8..971e33c 100644
--- a/Mono.TextTemplating.Tests/TestDataPath.cs
+++ b/Mono.TextTemplating.Tests/TestDataPath.cs
@@ -147,15 +147,6 @@ public DateTime AssertWriteTimeNewerThan (DateTime previousWriteTime)
}
}
-sealed class WriteTimeTracker
-{
- readonly TestDataPath file;
- DateTime lastWriteTime;
- public WriteTimeTracker (TestDataPath file) => lastWriteTime = (this.file = file).GetLastWriteTime ();
- public void AssertChanged () => lastWriteTime = file.AssertWriteTimeNewerThan (lastWriteTime);
- public void AssertSame () => file.AssertWriteTimeEquals (lastWriteTime);
-}
-
static class StringNormalizationExtensions
{
public static string NormalizeNewlines (this string s, string newLine = "\n") => s.Replace ("\r\n", "\n").Replace ("\n", newLine);
diff --git a/Mono.TextTemplating.Tests/WriteTimeTracker.cs b/Mono.TextTemplating.Tests/WriteTimeTracker.cs
new file mode 100644
index 0000000..ae93ff4
--- /dev/null
+++ b/Mono.TextTemplating.Tests/WriteTimeTracker.cs
@@ -0,0 +1,65 @@
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Threading;
+
+namespace Mono.TextTemplating.Tests;
+
+sealed class WriteTimeTracker
+{
+ readonly TestDataPath file;
+ DateTime lastWriteTime;
+ public WriteTimeTracker (TestDataPath file) => lastWriteTime = (this.file = file).GetLastWriteTime ();
+ public void AssertChanged () => lastWriteTime = file.AssertWriteTimeNewerThan (lastWriteTime);
+ public void AssertSame () => file.AssertWriteTimeEquals (lastWriteTime);
+
+ public DateTime WaitUntilLaterNow () => LaterNowThan (lastWriteTime);
+
+ static DateTime GetNewestWriteTime (IEnumerable trackers)
+ {
+ DateTime newest = DateTime.MinValue;
+ foreach (var tracker in trackers) {
+ if (newest < tracker.lastWriteTime) {
+ newest = tracker.lastWriteTime;
+ }
+ }
+ return newest;
+ }
+
+ public static void SetWriteTimeNewerThan (IEnumerable trackers, string filePath)
+ => SetWriteTimeNewerThan (GetNewestWriteTime (trackers), filePath);
+
+ public static void SetWriteTimeNewerThan (WriteTimeTracker tracker, string filePath)
+ => SetWriteTimeNewerThan (tracker.lastWriteTime, filePath);
+
+ public static void SetWriteTimeNewerThan (IEnumerable trackers, params string[] filePaths)
+ => SetWriteTimeNewerThan (GetNewestWriteTime (trackers), filePaths);
+
+ public static void SetWriteTimeNewerThan (WriteTimeTracker tracker, params string[] filePaths)
+ => SetWriteTimeNewerThan (tracker.lastWriteTime, filePaths);
+
+ ///
+ /// Waits until `DateTime.Now` is newer than `time`, then return this newer value.
+ ///
+ static DateTime LaterNowThan (DateTime time)
+ {
+ DateTime now;
+ while ((now = DateTime.Now) <= time) {
+ Thread.Sleep (10);
+ }
+ return now;
+ }
+
+ static void SetWriteTimeNewerThan (DateTime time, string filePath) => File.SetLastWriteTime (filePath, LaterNowThan (time));
+
+ static void SetWriteTimeNewerThan (DateTime time, params string[] filePaths)
+ {
+ var now = LaterNowThan (time);
+ foreach (var file in filePaths) {
+ File.SetLastWriteTime (file, now);
+ }
+ }
+}