Skip to content

Commit eec03a6

Browse files
committed
Add tests for runtime packs with labels
1 parent 96b6408 commit eec03a6

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Xml.Linq;
7+
using FluentAssertions;
8+
using Microsoft.NET.TestFramework;
9+
using Microsoft.NET.TestFramework.Assertions;
10+
using Microsoft.NET.TestFramework.Commands;
11+
using Microsoft.NET.TestFramework.ProjectConstruction;
12+
using Xunit;
13+
using Xunit.Abstractions;
14+
15+
namespace Microsoft.NET.Build.Tests
16+
{
17+
public class KnownRuntimePackTests : SdkTest
18+
{
19+
public KnownRuntimePackTests(ITestOutputHelper log) : base(log)
20+
{
21+
}
22+
23+
[Fact]
24+
public void BuildSucceedsWithRuntimePackWithDifferentLabel()
25+
{
26+
var testProject = new TestProject()
27+
{
28+
TargetFrameworks = "net5.0",
29+
IsSdkProject = true,
30+
IsExe = true,
31+
RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid()
32+
};
33+
34+
var testAsset = _testAssetsManager.CreateTestProject(testProject);
35+
36+
var knownRuntimePack = CreateTestKnownRuntimePack();
37+
38+
AddItem(testAsset, knownRuntimePack);
39+
40+
var buildCommand = new BuildCommand(testAsset);
41+
42+
buildCommand
43+
.Execute()
44+
.Should()
45+
.Pass();
46+
}
47+
48+
[Fact]
49+
public void DuplicateRuntimePackCausesFailure()
50+
{
51+
var testProject = new TestProject()
52+
{
53+
TargetFrameworks = "net5.0",
54+
IsSdkProject = true,
55+
IsExe = true,
56+
RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid()
57+
};
58+
59+
var testAsset = _testAssetsManager.CreateTestProject(testProject);
60+
61+
var knownRuntimePack = CreateTestKnownRuntimePack();
62+
knownRuntimePack.Attribute("RuntimePackLabels").Value = "";
63+
64+
AddItem(testAsset, knownRuntimePack);
65+
66+
var buildCommand = new BuildCommand(testAsset);
67+
68+
buildCommand
69+
.Execute()
70+
.Should()
71+
.Fail()
72+
.And
73+
.HaveStdOutContaining("NETSDK1133");
74+
}
75+
76+
[Fact]
77+
public void RuntimePackWithLabelIsSelected()
78+
{
79+
var testProject = new TestProject()
80+
{
81+
TargetFrameworks = "net5.0",
82+
IsSdkProject = true,
83+
IsExe = true,
84+
RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid()
85+
};
86+
87+
var testAsset = _testAssetsManager.CreateTestProject(testProject);
88+
89+
var knownRuntimePack = CreateTestKnownRuntimePack();
90+
91+
AddItem(testAsset, knownRuntimePack);
92+
93+
var frameworkReferenceUpdate = new XElement("FrameworkReference",
94+
new XAttribute("Update", "Microsoft.NETCore.App"),
95+
new XAttribute("RuntimePackLabels", "Mono"));
96+
97+
AddItem(testAsset, frameworkReferenceUpdate);
98+
99+
var getValuesCommand = new GetValuesCommand(testAsset, "RuntimePack", GetValuesCommand.ValueType.Item)
100+
{
101+
DependsOnTargets = "ProcessFrameworkReferences",
102+
ShouldRestore = false
103+
};
104+
105+
getValuesCommand
106+
.Execute()
107+
.Should()
108+
.Pass();
109+
110+
// StartsWith instead of exact match because current RID is likely to be more specific than the runtime pack RID
111+
getValuesCommand.GetValues().Should().Contain(rp => rp.StartsWith("Microsoft.NETCore.App.Runtime.Mono."));
112+
113+
}
114+
115+
private XElement CreateTestKnownRuntimePack()
116+
{
117+
var knownRuntimePack = new XElement("KnownRuntimePack",
118+
new XAttribute("Include", "Microsoft.NETCore.App"),
119+
new XAttribute("TargetFramework", "net5.0"),
120+
new XAttribute("RuntimeFrameworkName", "Microsoft.NETCore.App"),
121+
new XAttribute("DefaultRuntimeFrameworkVersion", "5.0.0-preview1"),
122+
new XAttribute("LatestRuntimeFrameworkVersion", "5.0.0-preview1.1"),
123+
new XAttribute("RuntimePackNamePatterns", "Microsoft.NETCore.App.Runtime.Mono.**RID**"),
124+
new XAttribute("RuntimePackRuntimeIdentifiers", "linux-arm;linux-arm64;linux-musl-arm64;linux-musl-x64;linux-x64;osx-x64;rhel.6-x64;tizen.4.0.0-armel;tizen.5.0.0-armel;win-arm;win-arm64;win-x64;win-x86;ios-arm64;ios-arm;ios-x64;ios-x86;tvos-arm64;tvos-x64;android-arm64;android-arm;android-x64;android-x86;browser-wasm"),
125+
new XAttribute("IsTrimmable", "true"),
126+
new XAttribute("RuntimePackLabels", "Mono"));
127+
128+
return knownRuntimePack;
129+
}
130+
131+
private void AddItem(TestAsset testAsset, XElement item)
132+
{
133+
testAsset.WithProjectChanges(project =>
134+
{
135+
var ns = project.Root.Name.Namespace;
136+
137+
var itemGroup = new XElement(ns + "ItemGroup");
138+
project.Root.Add(itemGroup);
139+
itemGroup.Add(item);
140+
});
141+
}
142+
}
143+
}

src/Tests/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public enum ValueType
3131

3232
public List<string> MetadataNames { get; set; } = new List<string>();
3333

34+
public bool ShouldRestore { get; set; } = true;
35+
36+
protected override bool ExecuteWithRestoreByDefault => ShouldRestore;
37+
3438
public GetValuesCommand(ITestOutputHelper log, string projectPath, string targetFramework,
3539
string valueName, ValueType valueType = ValueType.Property)
3640
: base(log, "WriteValuesToFile", projectPath, relativePathToProject: null)
@@ -41,6 +45,17 @@ public GetValuesCommand(ITestOutputHelper log, string projectPath, string target
4145
_valueType = valueType;
4246
}
4347

48+
public GetValuesCommand(TestAsset testAsset,
49+
string valueName, ValueType valueType = ValueType.Property,
50+
string targetFramework = null)
51+
: base(testAsset, "WriteValuesToFile", relativePathToProject: null)
52+
{
53+
_targetFramework = targetFramework ?? testAsset.TestProject?.TargetFrameworks;
54+
55+
_valueName = valueName;
56+
_valueType = valueType;
57+
}
58+
4459
protected override SdkCommandSpec CreateCommand(IEnumerable<string> args)
4560
{
4661
var newArgs = new List<string>();

0 commit comments

Comments
 (0)