Skip to content
Closed
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 tests, but some are NET 8.0 so they break
  • Loading branch information
nagilson committed Oct 7, 2022
commit 04e421b798802c8f10532f29b5b0a495e4517ae5
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Copyright (c) .NET Foundation. All rights reserved.
</PropertyGroup>

<PropertyGroup Condition="'$(RuntimeSpecific)' == 'true' and '$(RuntimeIdentifier)' == '' and '$(RuntimeIdentifiers)' == ''">
<RuntimeIdentifier>$(NETCoreSdkPortableRuntimeIdentifier)</RuntimeIdentifier>
<RuntimeIdentifier>$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier>
</PropertyGroup>

<PropertyGroup Condition="'$(UseCurrentRuntimeIdentifier)' == 'true' or
Expand Down Expand Up @@ -207,7 +207,7 @@ Copyright (c) .NET Foundation. All rights reserved.

<!-- Previously, RuntimeIdentifier (RID) implied SelfContained (SC). A breaking change in 8.0 made it so RID did not activate SC by default.
So we warn older TFM users before they upgrade to TFM 8.0 or above that they need to add <SelfContained>true</SelfContained> now to keep the same behavior.-->
<NETSdkWarning Condition="'$(RuntimeIdentifier)' != '' and $(_TargetFrameworkVersionWithoutV)' != '' and '$(_TargetFrameworkVersionWithoutV)' &lt; '8.0' and '$(_SelfContainedWasSpecified)' != 'true'"
<NETSdkWarning Condition="'$(RuntimeIdentifier)' != '' and '$(_TargetFrameworkVersionWithoutV)' != '' and '$(_TargetFrameworkVersionWithoutV)' &lt; '8.0' and '$(_SelfContainedWasSpecified)' != 'true'"
ResourceName="RuntimeIdentifierWillNoLongerImplySelfContained" />

<!-- Generate Trimming warnings for WinForms and Wpf applications-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,63 +99,63 @@ public void It_errors_out_when_RuntimeIdentifier_architecture_and_PlatformTarget
const string PlatformTarget = "x86";

var testAsset = _testAssetsManager
.CopyTestAsset("HelloWorld")
.WithSource()
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "RuntimeIdentifier", RuntimeIdentifier));
.CopyTestAsset("HelloWorld")
.WithSource()
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "RuntimeIdentifier", RuntimeIdentifier));
propertyGroup.Add(new XElement(ns + "PlatformTarget", PlatformTarget));
});
});

var buildCommand = new BuildCommand(testAsset);
var buildCommand = new BuildCommand(testAsset);

buildCommand
.Execute()
.Should()
buildCommand
.Execute()
.Should()
.Fail()
.And.HaveStdOutContaining(string.Format(
Strings.CannotHaveRuntimeIdentifierPlatformMismatchPlatformTarget,
RuntimeIdentifier,
PlatformTarget));
}

[Fact]
public void It_succeeds_when_RuntimeIdentifier_and_PlatformTarget_mismatch_but_PT_is_AnyCPU()
{
var targetFramework = ToolsetInfo.CurrentTargetFramework;
var runtimeIdentifier = EnvironmentInfo.GetCompatibleRid(targetFramework);
var testAsset = _testAssetsManager
.CopyTestAsset("HelloWorld")
.WithSource()
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "RuntimeIdentifier", runtimeIdentifier));
propertyGroup.Add(new XElement(ns + "PlatformTarget", "AnyCPU"));
});

var buildCommand = new BuildCommand(testAsset);

buildCommand
.Execute()
.Should()
.Pass();

var outputDirectory = buildCommand.GetOutputDirectory(targetFramework, runtimeIdentifier: runtimeIdentifier);
var selfContainedExecutable = $"HelloWorld{Constants.ExeSuffix}";

string selfContainedExecutableFullPath = Path.Combine(outputDirectory.FullName, selfContainedExecutable);
[Fact]
public void It_succeeds_when_RuntimeIdentifier_and_PlatformTarget_mismatch_but_PT_is_AnyCPU()
{
var targetFramework = ToolsetInfo.CurrentTargetFramework;
var runtimeIdentifier = EnvironmentInfo.GetCompatibleRid(targetFramework);
var testAsset = _testAssetsManager
.CopyTestAsset("HelloWorld")
.WithSource()
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "RuntimeIdentifier", runtimeIdentifier));
propertyGroup.Add(new XElement(ns + "PlatformTarget", "AnyCPU"));
});

var buildCommand = new BuildCommand(testAsset);

buildCommand
.Execute()
.Should()
.Pass();

var outputDirectory = buildCommand.GetOutputDirectory(targetFramework, runtimeIdentifier: runtimeIdentifier);
var selfContainedExecutable = $"HelloWorld{Constants.ExeSuffix}";

string selfContainedExecutableFullPath = Path.Combine(outputDirectory.FullName, selfContainedExecutable);

new RunExeCommand(Log, selfContainedExecutableFullPath)
.Execute()
.Should()
.Pass()
.And
.HaveStdOutContaining("Hello World!");
}
.Execute()
.Should()
.Pass()
.And
.HaveStdOutContaining("Hello World!");
}

[RequiresMSBuildVersionFact("17.0.0.32901")]
public void It_resolves_runtimepack_from_packs_folder()
Expand Down Expand Up @@ -317,7 +317,7 @@ static int Last2DigitsTo0(int versionBuild)
");

// Verify correct targeting pack version is resolved
var getValuesCommand = (GetValuesCommand) new GetValuesCommand(testAsset, "TargetingPack", GetValuesCommand.ValueType.Item)
var getValuesCommand = (GetValuesCommand)new GetValuesCommand(testAsset, "TargetingPack", GetValuesCommand.ValueType.Item)
.WithEnvironmentVariable(EnvironmentVariableNames.WORKLOAD_MANIFEST_ROOTS, manifestRoot);
getValuesCommand.MetadataNames = new List<string>() { "NuGetPackageId", "NuGetPackageVersion" };
getValuesCommand.DependsOnTargets = "ProcessFrameworkReferences";
Expand Down Expand Up @@ -351,27 +351,105 @@ static int Last2DigitsTo0(int versionBuild)

[Theory]
[InlineData("net6.0")]
public void It_can_publish_runtime_specific_apps_with_library_dependencies_self_contained(string targetFramework) {
public void It_can_publish_runtime_specific_apps_with_library_dependencies_self_contained(string targetFramework)
{

// create a basic library and a basic app, reference the library from the app and then
// publish the app with a RID specified and self-contained.
// verify that no warnings about missing the --self-contained flag are emitted.
var rid = EnvironmentInfo.GetCompatibleRid(targetFramework);
var libProject = new TestProject("RidSelfContainedLib"){
var libProject = new TestProject("RidSelfContainedLib")
{
IsExe = false,
TargetFrameworks = targetFramework,
IsSdkProject = true
};
var createdLibProject = _testAssetsManager.CreateTestProject(libProject);
var appProject = new TestProject("RidSelfContainedApp") {
var appProject = new TestProject("RidSelfContainedApp")
{
IsExe = true,
TargetFrameworks = targetFramework,
IsSdkProject = true
};
appProject.ReferencedProjects.Add(libProject);
var createdAppProject = _testAssetsManager.CreateTestProject(appProject);
var publishCommand = new PublishCommand(createdAppProject);
publishCommand.Execute(new [] {"-property:SelfContained=true", "-property:_CommandLineDefinedSelfContained=true", $"-property:RuntimeIdentifier={rid}", "-property:_CommandLineDefinedRuntimeIdentifier=true" }).Should().Pass().And.NotHaveStdOutContaining("warning");
publishCommand.Execute(new[] { "-property:SelfContained=true", "-property:_CommandLineDefinedSelfContained=true", $"-property:RuntimeIdentifier={rid}", "-property:_CommandLineDefinedRuntimeIdentifier=true" }).Should().Pass().And.NotHaveStdOutContaining("warning");
}

[Theory]
[InlineData("net7.0")]
[InlineData("net8.0")]
public void It_does_or_doesnt_imply_SelfContained_based_on_RuntimeIdentifier_and_TargetFramework(string targetFramework)
{
var runtimeIdentifier = EnvironmentInfo.GetCompatibleRid(targetFramework);
var testAsset = _testAssetsManager
.CopyTestAsset("HelloWorld")
.WithSource()
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "RuntimeIdentifier", runtimeIdentifier));
});

var buildCommand = new BuildCommand(testAsset);
buildCommand
.Execute()
.Should()
.Pass();

var outputDirectory = buildCommand.GetOutputDirectory(targetFramework, runtimeIdentifier: runtimeIdentifier);
var selfContainedExecutable = $"HelloWorld{Constants.ExeSuffix}";
string selfContainedExecutableFullPath = Path.Combine(outputDirectory.FullName, selfContainedExecutable);

if (targetFramework == "net7.0")
{
Assert.True(File.Exists(selfContainedExecutableFullPath));
}
else
{
Assert.False(File.Exists(selfContainedExecutableFullPath)); // RuntimeIdentifier no longer implies SelfContained for TFM >= 8
}
}

[Theory]
[InlineData("net7.0", true)]
[InlineData("net7.0", false)]
[InlineData("net8.0", false)]
public void It_does_or_doesnt_warn_based_on_SelfContained_and_TargetFramework_breaking_RID_change(string targetFramework, bool defineSelfContained)
{
var runtimeIdentifier = EnvironmentInfo.GetCompatibleRid(targetFramework);
var testAsset = _testAssetsManager
.CopyTestAsset("HelloWorld")
.WithSource()
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "RuntimeIdentifier", runtimeIdentifier));
propertyGroup.Add(new XElement(ns + "SelfContained", defineSelfContained ? "true" : ""));
});

var buildCommand = new BuildCommand(testAsset);
var commandResult = buildCommand.Execute();

if (targetFramework == "net7.0" && !defineSelfContained)
{
commandResult
.Should()
.Pass()
.And
.HaveStdOutContaining(Strings.RuntimeIdentifierWillNoLongerImplySelfContained);
}
else
{
commandResult
.Should()
.Pass()
.And
.NotHaveStdOutContaining(Strings.RuntimeIdentifierWillNoLongerImplySelfContained);
}
}

[Theory]
Expand Down
74 changes: 74 additions & 0 deletions src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

using System;
using System.Collections.Generic;
using System.Formats.Asn1;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.NET.TestFramework;
Expand All @@ -14,13 +16,24 @@
using Microsoft.NET.TestFramework.ProjectConstruction;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Microsoft.NET.Publish.Tests
{
public class RuntimeIdentifiersTests : SdkTest
{

TestProject _testProject;

public RuntimeIdentifiersTests(ITestOutputHelper log) : base(log)
{
_testProject = new TestProject("TestProject")
{
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
IsExe = true
};

_testProject.RecordProperties("RuntimeIdentifier", "SelfContained", "RuntimeIdentifiers");
}

// Run on core MSBuild only as using a local packages folder hits long path issues on full MSBuild
Expand Down Expand Up @@ -218,5 +231,66 @@ public void DuplicateRuntimeIdentifiers()
.Pass();

}

[Theory]
[InlineData("net7.0", "true")]
[InlineData("net7.0", "")]
[InlineData("net8.0", "")]
[InlineData("net8.0", "true")]
[InlineData("net8.0", "false")]
public void RuntimeSpecificEnablesOrDisablesRuntimeIdentifierByDefaultBasedOnValueAndTargetFramework(string targetFramework, string runtimeSpecific)
{
TestAsset testAsset = _testAssetsManager.CreateTestProject(_testProject, identifier: $"{targetFramework}_{runtimeSpecific}")
.WithTargetFramework(targetFramework)
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "RuntimeSpecific", runtimeSpecific));
});

var expectedRuntimeIdentifier = EnvironmentInfo.GetCompatibleRid(targetFramework);
new DotnetBuildCommand(testAsset)
.Execute($"/p:NETCoreSdkRuntimeIdentifier={expectedRuntimeIdentifier}")
.Should()
.Pass();

if ((runtimeSpecific != "false" && targetFramework == "net8.0") || runtimeSpecific == "true" && targetFramework == "net7.0")
{
var properties = _testProject.GetPropertyValues(testAsset.TestRoot, targetFramework: targetFramework, runtimeIdentifier: expectedRuntimeIdentifier);
var resolvedRid = properties["RuntimeIdentifier"];
Assert.True(resolvedRid == expectedRuntimeIdentifier);
}
else
{
var properties = _testProject.GetPropertyValues(testAsset.TestRoot, targetFramework: targetFramework);
var resolvedRid = properties["RuntimeIdentifier"];
Assert.True(resolvedRid == "");
}
}

[Fact]
public void RuntimeIdentifiersDisablesRuntimeSpecificFDDBehavior()
{
string targetFramework = "net8.0";
var expectedRuntimeIdentifier = EnvironmentInfo.GetCompatibleRid(targetFramework);
TestAsset testAsset = _testAssetsManager.CreateTestProject(_testProject, identifier: $"RuntimeIdentifierS_{targetFramework}") // the S is intentional to call atention to it
.WithTargetFramework(targetFramework)
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "RuntimeIdentifiers", $"{expectedRuntimeIdentifier};{expectedRuntimeIdentifier}"));
});

new DotnetBuildCommand(testAsset)
.Execute($"/p:NETCoreSdkRuntimeIdentifier={expectedRuntimeIdentifier}")
.Should()
.Pass();

var properties = _testProject.GetPropertyValues(testAsset.TestRoot, targetFramework: targetFramework, runtimeIdentifier: expectedRuntimeIdentifier);
var resolvedRid = properties["RuntimeIdentifier"];
Assert.True(resolvedRid == "");
}
}
}