Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for GenerateRuntimeGraph
  • Loading branch information
ericstj committed Mar 31, 2021
commit f7ce36b812425d5cc90dce27eb6e13394e765e01
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31004.24
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NETCore.Platforms", "src\Microsoft.NETCore.Platforms.csproj", "{BFFF96CC-06AA-4291-9F93-3E77F23DBB11}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NETCore.Platforms.Tests", "tests\Microsoft.NETCore.Platforms.Tests.csproj", "{0C60F372-5C73-4BFA-9B91-5659C88F9750}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BFFF96CC-06AA-4291-9F93-3E77F23DBB11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFFF96CC-06AA-4291-9F93-3E77F23DBB11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFFF96CC-06AA-4291-9F93-3E77F23DBB11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFFF96CC-06AA-4291-9F93-3E77F23DBB11}.Release|Any CPU.Build.0 = Release|Any CPU
{0C60F372-5C73-4BFA-9B91-5659C88F9750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C60F372-5C73-4BFA-9B91-5659C88F9750}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C60F372-5C73-4BFA-9B91-5659C88F9750}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C60F372-5C73-4BFA-9B91-5659C88F9750}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E946A528-C3E7-48EC-AD6D-AE84ED2B11AC}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.NETCore.Platforms.BuildTasks.Tests
{
public class GenerateRuntimeGraphTests
{
private Log _log;
private TestBuildEngine _engine;

public GenerateRuntimeGraphTests(ITestOutputHelper output)
{
_log = new Log(output);
_engine = new TestBuildEngine(_log);
}

[Fact]
public void CanCreateRuntimeGraph()
{
string runtimeFile = "runtime.json";

Project runtimeGroupProps = new Project("runtimeGroups.props");

ITaskItem[] runtimeGroups = runtimeGroupProps.GetItems("RuntimeGroupWithQualifiers")
.Select(i => CreateItem(i)).ToArray();

Assert.NotEmpty(runtimeGroups);

// will generate and compare to existing file.
GenerateRuntimeGraph task = new GenerateRuntimeGraph()
{
BuildEngine = _engine,
RuntimeGroups = runtimeGroups,
RuntimeJson = runtimeFile
};
task.Execute();

_log.AssertNoErrorsOrWarnings();
}

private static ITaskItem CreateItem(ProjectItem projectItem)
{
TaskItem item = new TaskItem(projectItem.EvaluatedInclude);
foreach(var metadatum in projectItem.Metadata)
{
item.SetMetadata(metadatum.Name, metadatum.EvaluatedValue);
}
return item;
}
}
}
56 changes: 56 additions & 0 deletions src/libraries/Microsoft.NETCore.Platforms/tests/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;
using Xunit.Abstractions;

namespace Microsoft.NETCore.Platforms.BuildTasks.Tests
{
internal class Log : ILog
{
private readonly ITestOutputHelper _output;

public Log(ITestOutputHelper output)
{
_output = output;
Reset();
}

public int ErrorsLogged { get; set; }
public int WarningsLogged { get; set; }

public void LogError(string message, params object[] messageArgs)
{
ErrorsLogged++;
_output.WriteLine("Error: " + message, messageArgs);
}

public void LogMessage(string message, params object[] messageArgs)
{
_output.WriteLine(message, messageArgs);
}

public void LogMessage(LogImportance importance, string message, params object[] messageArgs)
{
_output.WriteLine(message, messageArgs);
}

public void LogWarning(string message, params object[] messageArgs)
{
WarningsLogged++;
_output.WriteLine("Warning: " + message, messageArgs);
}

public void Reset()
{
ErrorsLogged = 0;
WarningsLogged = 0;
}

public void AssertNoErrorsOrWarnings()
{
Assert.Equal(0, ErrorsLogged);
Assert.Equal(0, WarningsLogged);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);net472</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\Microsoft.NETCore.Platforms.csproj" />
<!-- Workaround NuGet promoting this to a ProjectReference -->
<PackageReference Include="System.Memory" Condition="'$(TargetFramework)' == 'net472'" Version="$(SystemMemoryVersion)" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please elaborate? Why does NuGet add a P2P for System.Memory?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NuGet tried to restore System.Memory.csproj for net472 without this and restore failed. I believe it's because it's in the closure for the project added by the test targets. I think it's the same issue as NuGet/Home#9354.

</ItemGroup>

<ItemGroup>
<Compile Include="GenerateRuntimeGraphTests.cs" />
<Compile Include="Log.cs" />
<Compile Include="TestBuildEngine.cs" />
</ItemGroup>

<ItemGroup>
<Content Include="..\src\runtimeGroups.props" CopyToOutputDirectory="PreserveNewest" />
<Content Include="..\src\runtime.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
56 changes: 56 additions & 0 deletions src/libraries/Microsoft.NETCore.Platforms/tests/TestBuildEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Framework;
using System;
using System.Collections;

namespace Microsoft.NETCore.Platforms.BuildTasks.Tests
{
public class TestBuildEngine : IBuildEngine
{
private ILog _log;

public TestBuildEngine(ILog log)
{
ColumnNumberOfTaskNode = 0;
ContinueOnError = true;
LineNumberOfTaskNode = 0;
ProjectFileOfTaskNode = "test";
_log = log;
}

public int ColumnNumberOfTaskNode { get; set; }

public bool ContinueOnError { get; set; }

public int LineNumberOfTaskNode { get; set; }

public string ProjectFileOfTaskNode { get; set; }

public bool BuildProjectFile(string projectFileName, string[] targetNames, IDictionary globalProperties, IDictionary targetOutputs)
{
throw new NotImplementedException();
}

public void LogCustomEvent(CustomBuildEventArgs e)
{
_log.LogMessage(e.Message);
}

public void LogErrorEvent(BuildErrorEventArgs e)
{
_log.LogError(e.Message);
}

public void LogMessageEvent(BuildMessageEventArgs e)
{
_log.LogMessage((LogImportance)e.Importance, e.Message);
}

public void LogWarningEvent(BuildWarningEventArgs e)
{
_log.LogWarning(e.Message);
}
}
}