Skip to content

Commit 450166a

Browse files
committed
Test improvements
1 parent 73d5633 commit 450166a

File tree

6 files changed

+20
-17
lines changed

6 files changed

+20
-17
lines changed

src/BuiltInTools/dotnet-watch/HotReload/CompilationHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ public void DiscardProjectBaselines(ImmutableDictionary<ProjectId, string> proje
8888
public void UpdateProjectBaselines(ImmutableDictionary<ProjectId, string> projectsToBeRebuilt, CancellationToken cancellationToken)
8989
{
9090
_hotReloadService.UpdateBaselines(Workspace.CurrentSolution, projectsToBeRebuilt.Keys.ToImmutableArray());
91-
_reporter.Report(MessageDescriptor.ProjectBaselinesUpdated);
9291
}
9392

9493
public async ValueTask StartSessionAsync(CancellationToken cancellationToken)

src/BuiltInTools/dotnet-watch/HotReloadDotNetWatcher.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ void FileChangedCallback(ChangedPath change)
369369

370370
// Update project baselines to reflect changes to the restarted projects.
371371
compilationHandler.UpdateProjectBaselines(projectsToRebuild, iterationCancellationToken);
372+
373+
Context.Reporter.Report(MessageDescriptor.ProjectsRebuilt, projectsToRebuild.Count);
372374
}
373375

374376
if (projectsToRestart is not [])
@@ -392,6 +394,8 @@ await Task.WhenAll(
392394
}
393395
}))
394396
.WaitAsync(shutdownCancellationToken);
397+
398+
Context.Reporter.Report(MessageDescriptor.ProjectsRestarted, projectsToRestart.Length);
395399
}
396400

397401
async Task<ImmutableList<ChangedFile>> CaptureChangedFilesSnapshot(ImmutableDictionary<ProjectId, string>? rebuiltProjects)

src/BuiltInTools/dotnet-watch/Internal/IReporter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public MessageDescriptor ToErrorWhen(bool condition)
6363
// predefined messages used for testing:
6464
public static readonly MessageDescriptor HotReloadSessionStarting = new(Format: null, Emoji: null, MessageSeverity.None, s_id++);
6565
public static readonly MessageDescriptor HotReloadSessionStarted = new("Hot reload session started.", HotReloadEmoji, MessageSeverity.Verbose, s_id++);
66-
public static readonly MessageDescriptor ProjectBaselinesUpdated = new("Project baselines updated.", HotReloadEmoji, MessageSeverity.Verbose, s_id++);
66+
public static readonly MessageDescriptor ProjectsRebuilt = new("Projects rebuilt ({0})", HotReloadEmoji, MessageSeverity.Verbose, s_id++);
67+
public static readonly MessageDescriptor ProjectsRestarted = new("Projects restarted ({0})", HotReloadEmoji, MessageSeverity.Verbose, s_id++);
6768
public static readonly MessageDescriptor FixBuildError = new("Fix the error to continue or press Ctrl+C to exit.", WatchEmoji, MessageSeverity.Warning, s_id++);
6869
public static readonly MessageDescriptor WaitingForChanges = new("Waiting for changes", WatchEmoji, MessageSeverity.Verbose, s_id++);
6970
public static readonly MessageDescriptor LaunchedProcess = new("Launched '{0}' with arguments '{1}': process id {2}", LaunchEmoji, MessageSeverity.Verbose, s_id++);

test/dotnet-watch.Tests/HotReload/RuntimeProcessLauncherTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public async Task UpdateAndRudeEdit(TriggerEvent trigger)
188188

189189
var changeHandled = w.Reporter.RegisterSemaphore(MessageDescriptor.HotReloadChangeHandled);
190190
var sessionStarted = w.Reporter.RegisterSemaphore(MessageDescriptor.HotReloadSessionStarted);
191-
var projectBaselinesUpdated = w.Reporter.RegisterSemaphore(MessageDescriptor.ProjectBaselinesUpdated);
191+
var projectBaselinesUpdated = w.Reporter.RegisterSemaphore(MessageDescriptor.ProjectsRebuilt);
192192
await launchCompletionA.Task;
193193
await launchCompletionB.Task;
194194

test/dotnet-watch.Tests/Utilities/AssertEx.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public static void EqualFileList(IEnumerable<string> expectedFiles, IEnumerable<
233233
}
234234
}
235235

236-
public static void Contains(string expected, IEnumerable<string> items)
236+
public static void ContainsSubstring(string expected, IEnumerable<string> items)
237237
{
238238
if (items.Any(item => item.Contains(expected)))
239239
{
@@ -253,8 +253,5 @@ public static void Contains(string expected, IEnumerable<string> items)
253253

254254
Fail(message.ToString());
255255
}
256-
257-
public static void DoesNotContain(string expected, IEnumerable<string> items)
258-
=> Assert.DoesNotContain(expected, items);
259256
}
260257
}

test/dotnet-watch.Tests/Watch/Utilities/WatchableApp.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
#nullable disable
55

6+
using System.Runtime.CompilerServices;
7+
68
namespace Microsoft.DotNet.Watch.UnitTests
79
{
8-
internal sealed class WatchableApp(ITestOutputHelper logger) : IDisposable
10+
internal sealed class WatchableApp(DebugTestOutputLogger logger) : IDisposable
911
{
1012
// Test apps should output this message as soon as they start running:
1113
private const string StartedMessage = "Started";
@@ -18,7 +20,7 @@ internal sealed class WatchableApp(ITestOutputHelper logger) : IDisposable
1820

1921
public TestFlags TestFlags { get; private set; }
2022

21-
public ITestOutputHelper Logger => logger;
23+
public DebugTestOutputLogger Logger => logger;
2224

2325
public AwaitableProcess Process { get; private set; }
2426

@@ -32,32 +34,32 @@ public static string GetLinePrefix(MessageDescriptor descriptor, string projectD
3234
=> $"dotnet watch {descriptor.Emoji}{(projectDisplay != null ? $" [{projectDisplay}]" : "")} {descriptor.Format}";
3335

3436
public void AssertOutputContains(string message)
35-
=> AssertEx.Contains(message, Process.Output);
37+
=> AssertEx.ContainsSubstring(message, Process.Output);
3638

3739
public void AssertOutputDoesNotContain(string message)
38-
=> AssertEx.DoesNotContain(message, Process.Output);
40+
=> Assert.DoesNotContain(Process.Output, line => line.Contains(message));
3941

4042
public void AssertOutputContains(MessageDescriptor descriptor, string projectDisplay = null)
4143
=> AssertOutputContains(GetLinePrefix(descriptor, projectDisplay));
4244

43-
public async ValueTask WaitUntilOutputContains(string message)
45+
public async ValueTask WaitUntilOutputContains(string message, [CallerFilePath] string testPath = null, [CallerLineNumber] int testLine = 0)
4446
{
4547
if (!Process.Output.Any(line => line.Contains(message)))
4648
{
47-
Logger.WriteLine($"[TEST] Test waiting for output: '{message}'");
49+
Logger.Log($"Test waiting for output: '{message}'", testPath, testLine);
4850
_ = await AssertOutputLine(line => line.Contains(message));
4951
}
5052
}
5153

52-
public Task<string> AssertOutputLineStartsWith(MessageDescriptor descriptor, string projectDisplay = null, Predicate<string> failure = null)
53-
=> AssertOutputLineStartsWith(GetLinePrefix(descriptor, projectDisplay), failure);
54+
public Task<string> AssertOutputLineStartsWith(MessageDescriptor descriptor, string projectDisplay = null, Predicate<string> failure = null, [CallerFilePath] string testPath = null, [CallerLineNumber] int testLine = 0)
55+
=> AssertOutputLineStartsWith(GetLinePrefix(descriptor, projectDisplay), failure, testPath, testLine);
5456

5557
/// <summary>
5658
/// Asserts that the watched process outputs a line starting with <paramref name="expectedPrefix"/> and returns the remainder of that line.
5759
/// </summary>
58-
public async Task<string> AssertOutputLineStartsWith(string expectedPrefix, Predicate<string> failure = null)
60+
public async Task<string> AssertOutputLineStartsWith(string expectedPrefix, Predicate<string> failure = null, [CallerFilePath] string testPath = null, [CallerLineNumber] int testLine = 0)
5961
{
60-
Logger.WriteLine($"[TEST] Test waiting for output: '{expectedPrefix}'");
62+
Logger.Log($"Test waiting for output: '{expectedPrefix}'", testPath, testLine);
6163

6264
var line = await Process.GetOutputLineAsync(
6365
success: line => line.StartsWith(expectedPrefix, StringComparison.Ordinal),

0 commit comments

Comments
 (0)