Skip to content

Commit

Permalink
Implement publishruntimeidentifier and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nagilson committed Nov 2, 2022
1 parent 32c6ffa commit 5166175
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Cli/dotnet/commands/dotnet-publish/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static PublishCommand FromParseResult(ParseResult parseResult, string msb
var msbuildArgs = new List<string>()
{
"-target:Publish",
"--property:_IsPublishing=true"
"--property:_IsPublishing=true" // This property will not hold true for MSBuild /t:Publish. VS should also inject this property in the future.
};

IEnumerable<string> slnOrProjectArgs = parseResult.GetValueForArgument(PublishCommandParser.SlnOrProjectArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Copyright (c) .NET Foundation. All rights reserved.
<RuntimeIdentifier Condition="'$(PlatformTarget)' == 'x86' or '$(PlatformTarget)' == ''">win7-x86</RuntimeIdentifier>
</PropertyGroup>

<PropertyGroup Condition="'$(_IsPublishing)' == 'true' and '$(PublishRuntimeIdentifier)' != ''">
<RuntimeIdentifier>$(PublishRuntimeIdentifier)</RuntimeIdentifier>
</PropertyGroup>

<PropertyGroup Condition="'$(UseCurrentRuntimeIdentifier)' == ''">
<UseCurrentRuntimeIdentifier Condition="
'$(RuntimeIdentifier)' == '' and
Expand Down
68 changes: 66 additions & 2 deletions src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Linq;
using FluentAssertions;
Expand All @@ -15,6 +16,7 @@
using Microsoft.NET.TestFramework.ProjectConstruction;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Microsoft.NET.Publish.Tests
{
Expand Down Expand Up @@ -94,8 +96,6 @@ public void BuildWithUseCurrentRuntimeIdentifier()
IsExe = true
};

var compatibleRid = EnvironmentInfo.GetCompatibleRid(testProject.TargetFrameworks);

testProject.AdditionalProperties["UseCurrentRuntimeIdentifier"] = "True";

// Use a test-specific packages folder
Expand Down Expand Up @@ -194,6 +194,70 @@ public void PublishWithRuntimeIdentifier(bool publishNoBuild)
}
}

[WindowsOnlyTheory]
[InlineData("net7.0", "win-x64", "win-x86", false, false)]
[InlineData("net7.0", "win-x64", "win-x86", true, false)]
[InlineData("net7.0", "win-x64", "win-x86", true, true)]
public void PublishRuntimeIdentifierSetsRuntimeIdentifierAndDoesOrDoesntOverrideRID(string tfm, string publishRuntimeIdentifier, string runtimeIdentifier, bool runtimeIdentifierIsGlobal, bool publishRuntimeIdentifierIsGlobal)
{
var testProject = new TestProject()
{
IsExe = true,
TargetFrameworks = tfm
};
testProject.AdditionalProperties["RuntimeIdentifier"] = runtimeIdentifier;
testProject.AdditionalProperties["PublishRuntimeIdentifier"] = publishRuntimeIdentifier;
testProject.RecordProperties("RuntimeIdentifier");

List<string> args = new List<string>
{
$"/p:_IsPublishing=true", // Normally this would be set by the CLI (OR VS Soon TM), but this calls directly into t:/Publish.
runtimeIdentifierIsGlobal ? $"/p:RuntimeIdentifier={runtimeIdentifier}" : "",
publishRuntimeIdentifierIsGlobal ? $"/p:PublishRuntimeIdentifier={publishRuntimeIdentifier}" : ""
};

var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: $"{publishRuntimeIdentifierIsGlobal}-{runtimeIdentifierIsGlobal}");
var publishCommand = new PublishCommand(testAsset);
publishCommand
.Execute(args.ToArray())
.Should()
.Pass();

string expectedRid = runtimeIdentifierIsGlobal ? runtimeIdentifier : publishRuntimeIdentifier;
var properties = testProject.GetPropertyValues(testAsset.TestRoot, targetFramework: tfm, runtimeIdentifier: expectedRid);
var finalRid = properties["RuntimeIdentifier"];

Assert.True(finalRid == expectedRid); // This assert is theoretically worthless as the above code will fail if the RID path is wrong.
}

[WindowsOnlyFact]
[InlineData("net7.0", "tizen")] // tizen is an arbitrary nonwindows rid, picked because it will be different from a windows rid.
public void PublishRuntimeIdentifierDoesNotOverrideUseCurrentRuntime(string tfm, string publishRid)
{
var testProject = new TestProject()
{
IsExe = true,
TargetFrameworks = tfm
};

testProject.AdditionalProperties["UseCurrentRuntimeIdentifier"] = "true";
testProject.AdditionalProperties["PublishRuntimeIdentifier"] = publishRid;
testProject.RecordProperties("RuntimeIdentifier");

var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: "UCR_PUBLISH_RID_OVERRIDES");
var publishCommand = new PublishCommand(testAsset);
publishCommand
.Execute($"/p:_IsPublishing=true")
.Should()
.Pass();

var currentRid = EnvironmentInfo.GetCompatibleRid(testProject.TargetFrameworks);
var properties = testProject.GetPropertyValues(testAsset.TestRoot, targetFramework: tfm, runtimeIdentifier: currentRid);
var finalRid = properties["RuntimeIdentifier"];

Assert.True(finalRid == currentRid); // This assert is theoretically worthless as the above code will fail if the RID path is wrong.
}

[Fact]
public void ImplicitRuntimeIdentifierOptOutCorrecltyOptsOut()
{
Expand Down

0 comments on commit 5166175

Please sign in to comment.