Skip to content

Commit d101de8

Browse files
author
John Luo
authored
Add sharedfx and targeting pack tests (#23045) (#23070)
* Add sharedfx and targeting pack tests (#23045) * Add test for assembly versions * Add test for framework list * Add some hardcoded lists for sharedfx and targeting pack content * Fix failing tests * Fix targeting pack tests * Feedback
1 parent d45e6b2 commit d101de8

File tree

4 files changed

+420
-3
lines changed

4 files changed

+420
-3
lines changed

src/Framework/test/Microsoft.AspNetCore.App.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
<ItemGroup>
8787
<AssemblyAttribute Include="Microsoft.AspNetCore.TestData">
8888
<_Parameter1>TargetingPackDependencies</_Parameter1>
89-
<_Parameter2>@(_TargetingPackDependencies)</_Parameter2>
89+
<_Parameter2>@(_TargetingPackDependencies->'%(FileName)')</_Parameter2>
9090
</AssemblyAttribute>
9191

9292
<AssemblyAttribute Include="Microsoft.AspNetCore.TestData">

src/Framework/test/SharedFxTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.IO;
76
using System.Linq;
87
using Newtonsoft.Json.Linq;
@@ -26,6 +25,30 @@ public SharedFxTests(ITestOutputHelper output)
2625
_sharedFxRoot = Path.Combine(TestData.GetTestDataValue("SharedFrameworkLayoutRoot"), "shared", "Microsoft.AspNetCore.App", TestData.GetTestDataValue("RuntimePackageVersion"));
2726
}
2827

28+
[Fact]
29+
public void SharedFrameworkContainsListedAssemblies()
30+
{
31+
var actualAssemblies = Directory.GetFiles(_sharedFxRoot, "*.dll")
32+
.Select(Path.GetFileNameWithoutExtension)
33+
.ToHashSet();
34+
35+
_output.WriteLine("==== actual assemblies ====");
36+
_output.WriteLine(string.Join('\n', actualAssemblies.OrderBy(i => i)));
37+
_output.WriteLine("==== expected assemblies ====");
38+
_output.WriteLine(string.Join('\n', TestData.ListedSharedFxAssemblies.OrderBy(i => i)));
39+
40+
var missing = TestData.ListedSharedFxAssemblies.Except(actualAssemblies);
41+
var unexpected = actualAssemblies.Except(TestData.ListedSharedFxAssemblies);
42+
43+
_output.WriteLine("==== missing assemblies from the framework ====");
44+
_output.WriteLine(string.Join('\n', missing));
45+
_output.WriteLine("==== unexpected assemblies in the framework ====");
46+
_output.WriteLine(string.Join('\n', unexpected));
47+
48+
Assert.Empty(missing);
49+
Assert.Empty(unexpected);
50+
}
51+
2952
[Fact]
3053
public void SharedFrameworkContainsExpectedFiles()
3154
{

src/Framework/test/TargetingPackTests.cs

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Reflection.Metadata;
1010
using System.Reflection.PortableExecutable;
1111
using System.Runtime.CompilerServices;
12-
using Newtonsoft.Json.Linq;
12+
using System.Xml.Linq;
1313
using Xunit;
1414
using Xunit.Abstractions;
1515

@@ -18,6 +18,7 @@ namespace Microsoft.AspNetCore
1818
public class TargetingPackTests
1919
{
2020
private readonly string _expectedRid;
21+
private readonly string _targetingPackTfm;
2122
private readonly string _targetingPackRoot;
2223
private readonly ITestOutputHelper _output;
2324
private readonly bool _isTargetingPackBuilding;
@@ -26,10 +27,66 @@ public TargetingPackTests(ITestOutputHelper output)
2627
{
2728
_output = output;
2829
_expectedRid = TestData.GetSharedFxRuntimeIdentifier();
30+
_targetingPackTfm = "netcoreapp" + TestData.GetSharedFxVersion().Substring(0, 3);
2931
_targetingPackRoot = Path.Combine(TestData.GetTestDataValue("TargetingPackLayoutRoot"), "packs", "Microsoft.AspNetCore.App.Ref", TestData.GetTestDataValue("TargetingPackVersion"));
3032
_isTargetingPackBuilding = bool.Parse(TestData.GetTestDataValue("IsTargetingPackBuilding"));
3133
}
3234

35+
[Fact]
36+
public void TargetingPackContainsListedAssemblies()
37+
{
38+
if (!_isTargetingPackBuilding)
39+
{
40+
return;
41+
}
42+
43+
var actualAssemblies = Directory.GetFiles(Path.Combine(_targetingPackRoot, "ref", _targetingPackTfm), "*.dll")
44+
.Select(Path.GetFileNameWithoutExtension)
45+
.ToHashSet();
46+
var listedTargetingPackAssemblies = TestData.ListedTargetingPackAssemblies.Keys.ToHashSet();
47+
48+
_output.WriteLine("==== actual assemblies ====");
49+
_output.WriteLine(string.Join('\n', actualAssemblies.OrderBy(i => i)));
50+
_output.WriteLine("==== expected assemblies ====");
51+
_output.WriteLine(string.Join('\n', listedTargetingPackAssemblies.OrderBy(i => i)));
52+
53+
var missing = listedTargetingPackAssemblies.Except(actualAssemblies);
54+
var unexpected = actualAssemblies.Except(listedTargetingPackAssemblies);
55+
56+
_output.WriteLine("==== missing assemblies from the framework ====");
57+
_output.WriteLine(string.Join('\n', missing));
58+
_output.WriteLine("==== unexpected assemblies in the framework ====");
59+
_output.WriteLine(string.Join('\n', unexpected));
60+
61+
Assert.Empty(missing);
62+
Assert.Empty(unexpected);
63+
}
64+
65+
[Fact]
66+
public void RefAssembliesHaveExpectedAssemblyVersions()
67+
{
68+
if (!_isTargetingPackBuilding)
69+
{
70+
return;
71+
}
72+
73+
IEnumerable<string> dlls = Directory.GetFiles(Path.Combine(_targetingPackRoot, "ref", _targetingPackTfm), "*.dll", SearchOption.AllDirectories);
74+
Assert.NotEmpty(dlls);
75+
76+
Assert.All(dlls, path =>
77+
{
78+
var fileName = Path.GetFileNameWithoutExtension(path);
79+
var assemblyName = AssemblyName.GetAssemblyName(path);
80+
using var fileStream = File.OpenRead(path);
81+
using var peReader = new PEReader(fileStream, PEStreamOptions.Default);
82+
var reader = peReader.GetMetadataReader(MetadataReaderOptions.Default);
83+
var assemblyDefinition = reader.GetAssemblyDefinition();
84+
85+
TestData.ListedTargetingPackAssemblies.TryGetValue(fileName, out var expectedVersion);
86+
Assert.Equal(expectedVersion, assemblyDefinition.Version.ToString());
87+
});
88+
}
89+
3390
[Fact]
3491
public void AssembliesAreReferenceAssemblies()
3592
{
@@ -131,5 +188,61 @@ public void PlatformManifestListsAllFiles()
131188
Assert.True(Version.TryParse(parts[3], out _), "File version must be convertable to System.Version");
132189
});
133190
}
191+
192+
[Fact]
193+
public void FrameworkListListsContainsCorrectEntries()
194+
{
195+
if (!_isTargetingPackBuilding)
196+
{
197+
return;
198+
}
199+
200+
var frameworkListPath = Path.Combine(_targetingPackRoot, "data", "FrameworkList.xml");
201+
var expectedAssemblies = TestData.GetTargetingPackDependencies()
202+
.Split(';', StringSplitOptions.RemoveEmptyEntries)
203+
.ToHashSet();
204+
expectedAssemblies.Remove("aspnetcorev2_inprocess");
205+
206+
AssertEx.FileExists(frameworkListPath);
207+
208+
var frameworkListDoc = XDocument.Load(frameworkListPath);
209+
var frameworkListEntries = frameworkListDoc.Root.Descendants();
210+
211+
_output.WriteLine("==== file contents ====");
212+
_output.WriteLine(string.Join('\n', frameworkListEntries.Select(i => i.Attribute("Path").Value).OrderBy(i => i)));
213+
_output.WriteLine("==== expected assemblies ====");
214+
_output.WriteLine(string.Join('\n', expectedAssemblies.OrderBy(i => i)));
215+
216+
var actualAssemblies = frameworkListEntries
217+
.Select(i =>
218+
{
219+
var fileName = i.Attribute("Path").Value;
220+
return fileName.EndsWith(".dll", StringComparison.Ordinal)
221+
? fileName.Substring(0, fileName.Length - 4)
222+
: fileName;
223+
})
224+
.ToHashSet();
225+
226+
var missing = expectedAssemblies.Except(actualAssemblies);
227+
var unexpected = actualAssemblies.Except(expectedAssemblies);
228+
229+
_output.WriteLine("==== missing assemblies from the framework list ====");
230+
_output.WriteLine(string.Join('\n', missing));
231+
_output.WriteLine("==== unexpected assemblies in the framework list ====");
232+
_output.WriteLine(string.Join('\n', unexpected));
233+
234+
Assert.Empty(missing);
235+
Assert.Empty(unexpected);
236+
237+
Assert.All(frameworkListEntries, i =>
238+
{
239+
var assemblyPath = i.Attribute("Path").Value;
240+
var assemblyVersion = i.Attribute("AssemblyVersion").Value;
241+
var fileVersion = i.Attribute("FileVersion").Value;
242+
243+
Assert.True(Version.TryParse(assemblyVersion, out _), $"{assemblyPath} has assembly version {assemblyVersion}. Assembly version must be convertable to System.Version");
244+
Assert.True(Version.TryParse(fileVersion, out _), $"{assemblyPath} has file version {fileVersion}. File version must be convertable to System.Version");
245+
});
246+
}
134247
}
135248
}

0 commit comments

Comments
 (0)