Skip to content

Commit

Permalink
Deprecate binfmt in build event args (#8917)
Browse files Browse the repository at this point in the history
* Fix to properly serialize TargetFinishedEventArgs.TargetOutput
* New extended EventArgs for custom events data
* Make ExternalProject*EventArgs serialize without BinaryFormatter
* Issue warnings only on dotnetcore runtime.
* Make deser constructors internal

---------

Co-authored-by: YuliiaKovalova <ykovalova@microsoft.com>
Co-authored-by: YuliiaKovalova <95473390+YuliiaKovalova@users.noreply.github.com>
Co-authored-by: Ladi Prosek <ladi.prosek@gmail.com>
  • Loading branch information
4 people authored Aug 9, 2023
1 parent 60363d0 commit f121098
Show file tree
Hide file tree
Showing 47 changed files with 1,798 additions and 369 deletions.
3 changes: 3 additions & 0 deletions eng/dependabot/Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<PackageVersion Include="BenchmarkDotNet" Version="0.13.1" />
<PackageVersion Update="BenchmarkDotNet" Condition="'$(BenchmarkDotNetVersion)' != ''" Version="$(BenchmarkDotNetVersion)" />

<PackageVersion Include="FluentAssertions" Version="6.11.0" />
<PackageVersion Update="FluentAssertions" Condition="'$(FluentAssertionsVersion)' != ''" Version="$(FluentAssertionsVersion)" />

<PackageVersion Include="LargeAddressAware" Version="1.0.5" />
<PackageVersion Update="LargeAddressAware" Condition="'$(LargeAddressAwareVersion)' != ''" Version="$(LargeAddressAwareVersion)" />

Expand Down
163 changes: 163 additions & 0 deletions src/Build.UnitTests/BackEnd/BuildManager_Logging_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.UnitTests;
using Shouldly;
using Xunit;
using Xunit.Abstractions;
using Xunit.NetCore.Extensions;
using static Microsoft.Build.UnitTests.ObjectModelHelpers;

#nullable disable

namespace Microsoft.Build.Engine.UnitTests.BackEnd
{
public class BuildManager_Logging_Tests : IDisposable
{
private string _mainProject = @"
<Project>
<Target Name=`MainTarget`>
<MSBuild Projects=`{0}` Targets=`ChildTarget` />
</Target>
</Project>";

private string _childProjectWithCustomBuildEvent = $@"
<Project>
<UsingTask TaskName=""CustomBuildEventTask"" AssemblyFile=""{Assembly.GetExecutingAssembly().Location}"" />
<Target Name=`ChildTarget`>
<CustomBuildEventTask />
</Target>
</Project>";


/// <summary>
/// The mock logger for testing.
/// </summary>
private readonly MockLogger _logger;

/// <summary>
/// The standard build manager for each test.
/// </summary>
private readonly BuildManager _buildManager;

/// <summary>
/// The project collection used.
/// </summary>
private readonly ProjectCollection _projectCollection;

private readonly TestEnvironment _env;
private readonly ITestOutputHelper _output;

/// <summary>
/// SetUp
/// </summary>
public BuildManager_Logging_Tests(ITestOutputHelper output)
{
_output = output;
// Ensure that any previous tests which may have been using the default BuildManager do not conflict with us.
BuildManager.DefaultBuildManager.Dispose();

_logger = new MockLogger(output);
_buildManager = new BuildManager();
_projectCollection = new ProjectCollection();

_env = TestEnvironment.Create(output);
}

[DotNetOnlyTheory]
[InlineData("1", true)]
[InlineData("0", false)]
[InlineData(null, true)]
public void Build_WithCustomBuildArgs_NetCore(string envVariableValue, bool isWarningExpected)
=> TestCustomEventWarning<BuildErrorEventArgs>(envVariableValue, isWarningExpected);

[WindowsFullFrameworkOnlyTheory]
[InlineData("1", true)]
[InlineData("0", false)]
[InlineData(null, false)]
public void Build_WithCustomBuildArgs_Framework(string envVariableValue, bool isWarningExpected) =>
TestCustomEventWarning<BuildWarningEventArgs>(envVariableValue, isWarningExpected);

private void TestCustomEventWarning<T>(string envVariableValue, bool isWarningExpected) where T : LazyFormattedBuildEventArgs
{
var testFiles = _env.CreateTestProjectWithFiles(string.Empty, new[] { "main", "child1" }, string.Empty);

ILoggingService service = LoggingService.CreateLoggingService(LoggerMode.Synchronous, 1);
service.RegisterLogger(_logger);

_env.SetEnvironmentVariable("MSBUILDCUSTOMBUILDEVENTWARNING", envVariableValue);
_env.SetEnvironmentVariable("MSBUILDNOINPROCNODE", "1");

_buildManager.BeginBuild(BuildParameters);

try
{
var child1ProjectPath = testFiles.CreatedFiles[1];
var cleanedUpChildContents = CleanupFileContents(_childProjectWithCustomBuildEvent);
File.WriteAllText(child1ProjectPath, cleanedUpChildContents);

var mainProjectPath = testFiles.CreatedFiles[0];
var cleanedUpMainContents = CleanupFileContents(string.Format(_mainProject, child1ProjectPath));
File.WriteAllText(mainProjectPath, cleanedUpMainContents);

var buildRequestData = new BuildRequestData(
mainProjectPath,
new Dictionary<string, string>(),
MSBuildConstants.CurrentToolsVersion,
new[] { "MainTarget" },
null);

var submission = _buildManager.PendBuildRequest(buildRequestData);
var result = submission.Execute();
var allEvents = _logger.AllBuildEvents;

if (isWarningExpected)
{
allEvents.OfType<T>().ShouldHaveSingleItem();
allEvents.First(x => x is T).Message.ShouldContain(
string.Format(ResourceUtilities.GetResourceString("DeprecatedEventSerialization"),
"MyCustomBuildEventArgs"));
}
else
{
allEvents.OfType<T>().ShouldBeEmpty();
}
}
finally
{
_buildManager.EndBuild();
}
}

private BuildParameters BuildParameters => new BuildParameters(_projectCollection)
{
DisableInProcNode = true,
EnableNodeReuse = false,
Loggers = new ILogger[] { _logger }
};

/// <summary>
/// TearDown
/// </summary>
public void Dispose()
{
_buildManager.Dispose();
_projectCollection.Dispose();
_env.Dispose();
}
}
}
25 changes: 25 additions & 0 deletions src/Build.UnitTests/BackEnd/CustomBuildEventTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

#nullable disable

namespace Microsoft.Build.UnitTests
{
public class CustomBuildEventTask : Task
{
public override bool Execute()
{
MyCustomBuildEventArgs customBuildEvent = new() { RawMessage = "A message from MyCustomBuildEventArgs" };
BuildEngine.LogCustomEvent(customBuildEvent);

return true;
}

[Serializable]
public sealed class MyCustomBuildEventArgs : CustomBuildEventArgs { }
}
}
Loading

0 comments on commit f121098

Please sign in to comment.