Skip to content

Commit

Permalink
Capture output in some BuildCheck tests
Browse files Browse the repository at this point in the history
This should help identify failures like dotnet#10836 (comment).
  • Loading branch information
rainersigwald committed Oct 24, 2024
1 parent 69b3e7a commit 01a4f1c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/BuildCheck.UnitTests/BuildCheckManagerProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class BuildCheckManagerTests
public BuildCheckManagerTests(ITestOutputHelper output)
{
_loggingService = LoggingService.CreateLoggingService(LoggerMode.Synchronous, 1);
_logger = new MockLogger();
_logger = new MockLogger(output);
_loggingService.RegisterLogger(_logger);
_testedInstance = new BuildCheckManager();
}
Expand Down
14 changes: 10 additions & 4 deletions src/BuildCheck.UnitTests/TaskInvocationAnalysisDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.Build.Utilities;
using Shouldly;
using Xunit;
using Xunit.Abstractions;
using static Microsoft.Build.Experimental.BuildCheck.Infrastructure.BuildCheckManagerProvider;

namespace Microsoft.Build.BuildCheck.UnitTests
Expand Down Expand Up @@ -51,15 +52,19 @@ private void TaskInvocationAction(BuildCheckDataContext<TaskInvocationCheckData>
}
}

private ITestOutputHelper _output;

private static TestCheck? s_testCheck;

public TaskInvocationCheckDataTests()
public TaskInvocationCheckDataTests(ITestOutputHelper output)
{
_output = output;

BuildCheckManager.s_testFactoriesPerDataSource =
[
// BuildCheckDataSource.EventArgs
[
([TestCheck.SupportedRule.Id], true, () => (s_testCheck = new TestCheck())),
([TestCheck.SupportedRule.Id], true, () => s_testCheck = new TestCheck()),
],
// BuildCheckDataSource.Execution
[],
Expand All @@ -75,7 +80,7 @@ public void Dispose()

private void BuildProject(string taskInvocation)
{
using (var env = TestEnvironment.Create())
using (var env = TestEnvironment.Create(_output))
{
var testProject = env.CreateTestProjectWithFiles($"<Project><Target Name=\"Build\">{taskInvocation}</Target></Project>");

Expand All @@ -84,6 +89,7 @@ private void BuildProject(string taskInvocation)
var request = new BuildRequestData(testProject.ProjectFile, new Dictionary<string, string?>(), MSBuildConstants.CurrentToolsVersion, [], null, BuildRequestDataFlags.None);
var parameters = new BuildParameters
{
Loggers = [new MockLogger(_output)],
LogTaskInputs = true,
IsBuildCheckEnabled = true,
ShutdownInProcNodeOnBuildFinish = true,
Expand All @@ -94,7 +100,7 @@ private void BuildProject(string taskInvocation)
result.OverallResult.ShouldBe(BuildResultCode.Success);
}

foreach (var data in s_testCheck!.CheckData)
foreach (var data in s_testCheck.ShouldNotBeNull().CheckData)
{
data.ProjectFilePath.ShouldBe(testProject.ProjectFile);
data.TaskInvocationLocation.Line.ShouldBeGreaterThan(0);
Expand Down

0 comments on commit 01a4f1c

Please sign in to comment.