Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jan 20, 2025
1 parent 879b716 commit 99f8dc6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
16 changes: 5 additions & 11 deletions test/dotnet-watch.Tests/MsBuildFileSetFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MsBuildFileSetFactoryTest(ITestOutputHelper output)
private readonly TestReporter _reporter = new(output);
private readonly TestAssetsManager _testAssets = new(output);

private string MuxerPath
private static string MuxerPath
=> TestContext.Current.ToolsetUnderTest.DotNetHostPath;

private static string InspectPath(string path, string rootDir)
Expand Down Expand Up @@ -329,9 +329,6 @@ public async Task ProjectReferences_Graph()
MuxerPath: MuxerPath,
WorkingDirectory: testDirectory);

var output = new List<string>();
_reporter.OnProcessOutput += line => output.Add(line.Content);

var filesetFactory = new MSBuildFileSetFactory(projectA, buildArguments: ["/p:_DotNetWatchTraceOutput=true"], options, _reporter);

var result = await filesetFactory.TryCreateAsync(requireProjectGraph: null, CancellationToken.None);
Expand Down Expand Up @@ -367,7 +364,7 @@ public async Task ProjectReferences_Graph()
"Collecting watch items from 'F'",
"Collecting watch items from 'G'",
],
output.Where(l => l.Contains("Collecting watch items from")).Select(l => l.Trim()).Order());
_reporter.Messages.Where(l => l.text.Contains("Collecting watch items from")).Select(l => l.text.Trim()).Order());
}

[Fact]
Expand All @@ -390,17 +387,14 @@ public async Task MsbuildOutput()
MuxerPath: MuxerPath,
WorkingDirectory: Path.GetDirectoryName(project1Path)!);

var output = new List<string>();
_reporter.OnProcessOutput += line => output.Add($"{(line.IsError ? "[stderr]" : "[stdout]")} {line.Content}");

var factory = new MSBuildFileSetFactory(project1Path, buildArguments: [], options, _reporter);
var result = await factory.TryCreateAsync(requireProjectGraph: null, CancellationToken.None);
Assert.Null(result);

// note: msbuild prints errors to stdout:
// note: msbuild prints errors to stdout, we match the pattern and report as error:
AssertEx.Equal(
$"[stdout] {project1Path} : error NU1201: Project Project2 is not compatible with net462 (.NETFramework,Version=v4.6.2). Project Project2 supports: netstandard2.1 (.NETStandard,Version=v2.1)",
output.Single(l => l.Contains("error NU1201")));
(MessageSeverity.Error, $"{project1Path} : error NU1201: Project Project2 is not compatible with net462 (.NETFramework,Version=v4.6.2). Project Project2 supports: netstandard2.1 (.NETStandard,Version=v2.1)"),
_reporter.Messages.Single(l => l.text.Contains("error NU1201")));
}

private Task<EvaluationResult> Evaluate(TestAsset projectPath)
Expand Down
11 changes: 5 additions & 6 deletions test/dotnet-watch.Tests/Utilities/TestReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ internal class TestReporter(ITestOutputHelper output) : IReporter
{
private readonly Dictionary<int, Action> _actions = [];
public readonly List<string> ProcessOutput = [];

public bool EnableProcessOutputReporting
=> true;
public readonly List<(MessageSeverity severity, string text)> Messages = [];

public bool IsVerbose
=> true;
Expand All @@ -26,7 +24,6 @@ public void ReportProcessOutput(OutputLine line)
ProcessOutput.Add(line.Content);

OnProcessOutput?.Invoke(line);

}

public SemaphoreSlim RegisterSemaphore(MessageDescriptor descriptor)
Expand Down Expand Up @@ -56,6 +53,8 @@ public void Report(MessageDescriptor descriptor, string prefix, object?[] args)
{
if (descriptor.TryGetMessage(prefix, args, out var message))
{
Messages.Add((descriptor.Severity, message));

WriteTestOutput($"{ToString(descriptor.Severity)} {descriptor.Emoji} {message}");
}

Expand All @@ -65,11 +64,11 @@ public void Report(MessageDescriptor descriptor, string prefix, object?[] args)
}
}

private void WriteTestOutput(string message)
private void WriteTestOutput(string line)
{
try
{
output.WriteLine(message);
output.WriteLine(line);
}
catch (InvalidOperationException)
{
Expand Down

0 comments on commit 99f8dc6

Please sign in to comment.