Skip to content

Commit c0cbfd9

Browse files
committed
Tests to prove the any RID works
1 parent 28ef8c5 commit c0cbfd9

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

test/Microsoft.DotNet.PackageInstall.Tests/EndToEndToolTests.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,94 @@ public void PackagesFrameworkDependentRidSpecificPackagesCorrectly()
223223
foundRids.Should().BeEquivalentTo(expectedRids, "The top-level package should declare all of the RIDs for the tools it contains");
224224
}
225225

226+
[Fact]
227+
public void PackageToolWithAnyRid()
228+
{
229+
var toolSettings = new TestToolBuilder.TestToolSettings()
230+
{
231+
RidSpecific = true,
232+
IncludeAnyRid = true
233+
};
234+
235+
string toolPackagesPath = ToolBuilder.CreateTestTool(Log, toolSettings);
236+
237+
var packages = Directory.GetFiles(toolPackagesPath, "*.nupkg");
238+
var packageIdentifier = toolSettings.ToolPackageId;
239+
var expectedRids = ToolsetInfo.LatestRuntimeIdentifiers.Split(';');
240+
241+
packages.Length.Should().Be(expectedRids.Length + 1 + 1, "There should be one package for the tool-wrapper, one for the top-level manifest, and one for each RID");
242+
foreach (string rid in expectedRids)
243+
{
244+
var packageName = $"{toolSettings.ToolPackageId}.{rid}.{toolSettings.ToolPackageVersion}";
245+
var package = packages.FirstOrDefault(p => p.EndsWith(packageName + ".nupkg"));
246+
package.Should().NotBeNull($"Package {packageName} should be present in the tool packages directory")
247+
.And.Satisfy<string>(EnsurePackageIsAnExecutable);
248+
}
249+
250+
// Ensure that the package with the "any" RID is present
251+
var anyRidPackage = packages.FirstOrDefault(p => p.EndsWith($"{packageIdentifier}.any.{toolSettings.ToolPackageVersion}.nupkg"));
252+
anyRidPackage.Should().NotBeNull($"Package {packageIdentifier}.any.{toolSettings.ToolPackageVersion}.nupkg should be present in the tool packages directory")
253+
.And.Satisfy<string>(EnsurePackageIsFdd);
254+
255+
// top-level package should declare all of the rids
256+
var topLevelPackage = packages.First(p => p.EndsWith($"{packageIdentifier}.{toolSettings.ToolPackageVersion}.nupkg"));
257+
var settingsXml = GetToolSettingsFile(topLevelPackage);
258+
var packageNodes = GetRidsInSettingsFile(settingsXml);
259+
260+
packageNodes.Should().BeEquivalentTo([.. expectedRids, "any"], "The top-level package should declare all of the RIDs for the tools it contains");
261+
}
262+
263+
[Fact]
264+
public void InstallAndRunToolFromAnyRid()
265+
{
266+
var toolSettings = new TestToolBuilder.TestToolSettings()
267+
{
268+
IncludeAnyRid = true // will make one package with the "any" RID
269+
};
270+
string toolPackagesPath = ToolBuilder.CreateTestTool(Log, toolSettings);
271+
var packages = Directory.GetFiles(toolPackagesPath, "*.nupkg").Select(p => Path.GetFileName(p)).ToArray();
272+
packages.Should().BeEquivalentTo([
273+
$"{toolSettings.ToolPackageId}.{toolSettings.ToolPackageVersion}.nupkg",
274+
$"{toolSettings.ToolPackageId}.any.{toolSettings.ToolPackageVersion}.nupkg"
275+
], "There should be two packages: one for the tool-wrapper and one for the 'any' RID");
276+
var testDirectory = _testAssetsManager.CreateTestDirectory();
277+
var homeFolder = Path.Combine(testDirectory.Path, "home");
278+
279+
new DotnetToolCommand(Log, "exec", toolSettings.ToolPackageId, "--yes", "--add-source", toolPackagesPath)
280+
.WithEnvironmentVariables(homeFolder)
281+
.WithWorkingDirectory(testDirectory.Path)
282+
.Execute()
283+
.Should().Pass()
284+
.And.HaveStdOutContaining("Hello Tool!");
285+
}
286+
287+
[Fact]
288+
public void InstallAndRunToolFromAnyRidWhenOtherRidsArePresentButIncompatible()
289+
{
290+
var toolSettings = new TestToolBuilder.TestToolSettings()
291+
{
292+
IncludeCurrentRid = false,
293+
RidSpecific = true, // will make one package for each RID except the current RID
294+
IncludeAnyRid = true // will make one package with the "any" RID
295+
};
296+
List<string> expectedRids = [ .. ToolsetInfo.LatestRuntimeIdentifiers.Split(';').Where(rid => rid != RuntimeInformation.RuntimeIdentifier), "any"];
297+
298+
string toolPackagesPath = ToolBuilder.CreateTestTool(Log, toolSettings);
299+
var packages = Directory.GetFiles(toolPackagesPath, "*.nupkg").Select(p => Path.GetFileName(p)).ToArray();
300+
packages.Should().BeEquivalentTo([
301+
$"{toolSettings.ToolPackageId}.{toolSettings.ToolPackageVersion}.nupkg",
302+
.. expectedRids.Select(rid => $"{toolSettings.ToolPackageId}.{rid}.{toolSettings.ToolPackageVersion}.nupkg"),
303+
], $"There should be { 1 + expectedRids.Count } packages: one for the tool-wrapper and one for each RID except the current RID");
304+
var testDirectory = _testAssetsManager.CreateTestDirectory();
305+
var homeFolder = Path.Combine(testDirectory.Path, "home");
306+
307+
new DotnetToolCommand(Log, "exec", toolSettings.ToolPackageId, "--yes", "--add-source", toolPackagesPath)
308+
.WithEnvironmentVariables(homeFolder)
309+
.WithWorkingDirectory(testDirectory.Path)
310+
.Execute()
311+
.Should().Pass()
312+
.And.HaveStdOutContaining("Hello Tool!");
313+
}
226314

227315
private void EnsurePackageIsFdd(string packagePath)
228316
{

test/Microsoft.DotNet.PackageInstall.Tests/TestToolBuilder.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ public class TestToolSettings
3131
public bool NativeAOT { get; set { field = value; this.RidSpecific = value; } } = false;
3232
public bool SelfContained { get; set { field = value; this.RidSpecific = value; } } = false;
3333
public bool Trimmed { get; set { field = value; this.RidSpecific = value; } } = false;
34-
public bool IncludeAnyRid { get; set { field = value; this.RidSpecific = value; } } = false;
34+
public bool IncludeAnyRid { get; set { field = value; } } = false;
3535
public bool RidSpecific { get; set; } = false;
36+
public bool IncludeCurrentRid { get; set; } = true;
3637

37-
public string GetIdentifier() => $"{ToolPackageId}-{ToolPackageVersion}-{ToolCommandName}-{(NativeAOT ? "nativeaot" : SelfContained ? "selfcontained" : Trimmed ? "trimmed" : "managed")}{(RidSpecific ? "-specific" : "")}{(IncludeAnyRid ? "-anyrid" : "")}";
38+
public string GetIdentifier() => $"{ToolPackageId}-{ToolPackageVersion}-{ToolCommandName}-{(NativeAOT ? "nativeaot" : SelfContained ? "selfcontained" : Trimmed ? "trimmed" : "managed")}{(RidSpecific ? "-specific" : "")}{(IncludeAnyRid ? "-anyrid" : "")}{(IncludeCurrentRid ? "" : "-no-current-rid")}";
3839
}
3940

4041

@@ -53,13 +54,18 @@ public string CreateTestTool(ITestOutputHelper log, TestToolSettings toolSetting
5354
testProject.AdditionalProperties["ImplicitUsings"] = "enable";
5455
testProject.AdditionalProperties["Version"] = toolSettings.ToolPackageVersion;
5556

56-
var singleRid = RuntimeInformation.RuntimeIdentifier;
57-
var multiRid = toolSettings.IncludeAnyRid ? $"{ToolsetInfo.LatestRuntimeIdentifiers};any" : ToolsetInfo.LatestRuntimeIdentifiers;
57+
var multiRid = toolSettings.IncludeCurrentRid ? ToolsetInfo.LatestRuntimeIdentifiers : ToolsetInfo.LatestRuntimeIdentifiers.Replace(RuntimeInformation.RuntimeIdentifier, string.Empty).Trim(';');
5858

5959
if (toolSettings.RidSpecific)
6060
{
6161
testProject.AdditionalProperties["RuntimeIdentifiers"] = multiRid;
6262
}
63+
if (toolSettings.IncludeAnyRid)
64+
{
65+
testProject.AdditionalProperties["RuntimeIdentifiers"] = testProject.AdditionalProperties.TryGetValue("RuntimeIdentifiers", out var existingRids)
66+
? $"{existingRids};any"
67+
: "any";
68+
}
6369

6470
if (toolSettings.NativeAOT)
6571
{

0 commit comments

Comments
 (0)