Skip to content

Commit a50c276

Browse files
authored
Move VerifyClosure tasks into M.D.PackageTesting (#7692)
As we plan to deprecate the M.D.B.Tasks.Packaging project in the future, moving the tasks into the PackageTesting project. Fixes #7474
1 parent 093cd72 commit a50c276

File tree

12 files changed

+48
-68
lines changed

12 files changed

+48
-68
lines changed

src/Microsoft.DotNet.Build.Tasks.Packaging/src/Microsoft.DotNet.Build.Tasks.Packaging.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
- SplitDependenciesBySupport
3939
- SplitReferences
4040
- UpdatePackageIndex
41-
- ValidationTask
42-
- VerifyClosure
43-
- VerifyTypes</PackageDescription>
41+
- ValidationTask</PackageDescription>
4442

4543
<DefaultItemExcludes Condition="'$(TargetFramework)' != 'net472'">**/*.Desktop.*</DefaultItemExcludes>
4644
<BeforePack>$(BeforePack);AddRuntimeJson</BeforePack>

src/Microsoft.DotNet.Build.Tasks.Packaging/src/build/Packaging.common.targets

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,5 @@
4444
<UsingTask TaskName="UpdatePackageIndex" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
4545
<UsingTask TaskName="ValidatePackage" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
4646
<UsingTask TaskName="ValidateFrameworkPackage" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
47-
<UsingTask TaskName="VerifyClosure" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
48-
<UsingTask TaskName="VerifyTypes" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
4947
<UsingTask TaskName="GetSupportedPackagesFromPackageReports" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
5048
</Project>

src/Microsoft.DotNet.PackageTesting.Tests/Microsoft.DotNet.PackageTesting.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildUtilitiesCoreVersion)" />
1010
<PackageReference Include="NuGet.Frameworks" Version="$(NuGetVersion)" />
1111
<PackageReference Include="NuGet.Packaging" Version="$(NuGetVersion)" />
12+
<!-- Remove when targeting >= net5.0. -->
13+
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
1214
</ItemGroup>
1315

1416
<ItemGroup>

src/Microsoft.DotNet.PackageTesting/GetCompatiblePackageTargetFrameworks.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.DotNet.PackageTesting
1313
{
1414
public class GetCompatiblePackageTargetFrameworks : BuildTask
1515
{
16-
private static List<NuGetFramework> allTargetFrameworks = allTargetFrameworks = new();
16+
private static List<NuGetFramework> allTargetFrameworks = new();
1717
private static Dictionary<NuGetFramework, HashSet<NuGetFramework>> packageTfmMapping = new();
1818

1919
[Required]
@@ -27,12 +27,15 @@ public class GetCompatiblePackageTargetFrameworks : BuildTask
2727

2828
public override bool Execute()
2929
{
30-
bool result = true;
31-
List<ITaskItem> testProjects = new List<ITaskItem>();
30+
List<ITaskItem> testProjects = new();
31+
3232
try
3333
{
3434
Initialize(SupportedTestFrameworks);
35-
string minDotnetTargetFramework = allTargetFrameworks.Where(t => t.Framework == ".NETCoreApp").OrderBy(t => t.Version).FirstOrDefault()?.GetShortFolderName();
35+
string minDotnetTargetFramework = allTargetFrameworks.Where(t => t.Framework == ".NETCoreApp")
36+
.OrderBy(t => t.Version)
37+
.FirstOrDefault()?
38+
.GetShortFolderName();
3639

3740
foreach (var packagePath in PackagePaths)
3841
{
@@ -50,25 +53,30 @@ public override bool Execute()
5053
Log.LogErrorFromException(e, showStackTrace: false);
5154
}
5255

53-
return result && !Log.HasLoggedErrors;
56+
return !Log.HasLoggedErrors;
5457
}
5558

5659
public static IEnumerable<NuGetFramework> GetTestFrameworks(Package package, string minDotnetTargetFramework)
5760
{
58-
List<NuGetFramework> frameworksToTest= new List<NuGetFramework>();
61+
List<NuGetFramework> frameworksToTest= new();
5962
IEnumerable<NuGetFramework> packageTargetFrameworks = package.FrameworksInPackage;
6063

6164
// Testing the package installation on all tfms linked with package targetframeworks.
6265
foreach (var item in packageTargetFrameworks)
6366
{
6467
if (packageTfmMapping.ContainsKey(item))
68+
{
6569
frameworksToTest.AddRange(packageTfmMapping[item]);
70+
}
71+
6672
// Adding the frameworks in the packages to the test matrix.
6773
frameworksToTest.Add(item);
6874
}
6975

7076
if (!string.IsNullOrEmpty(minDotnetTargetFramework) && frameworksToTest.Any(t => t.Framework == ".NETStandard"))
77+
{
7178
frameworksToTest.Add(NuGetFramework.Parse(minDotnetTargetFramework));
79+
}
7280

7381
return frameworksToTest.Where(tfm => allTargetFrameworks.Contains(tfm)).Distinct();
7482
}
@@ -97,12 +105,12 @@ public static void Initialize(string targetFrameworks)
97105
}
98106
}
99107

100-
public List<ITaskItem> CreateItemFromTestFramework(string packageId, string version, IEnumerable<NuGetFramework> testFrameworks)
108+
private static List<ITaskItem> CreateItemFromTestFramework(string packageId, string version, IEnumerable<NuGetFramework> testFrameworks)
101109
{
102-
List<ITaskItem> testprojects = new List<ITaskItem>();
110+
List<ITaskItem> testprojects = new();
103111
foreach (var framework in testFrameworks)
104112
{
105-
var supportedPackage = new TaskItem(packageId);
113+
TaskItem supportedPackage = new(packageId);
106114
supportedPackage.SetMetadata("Version", version);
107115
supportedPackage.SetMetadata("TargetFramework", framework.ToString());
108116
supportedPackage.SetMetadata("TargetFrameworkShort", framework.GetShortFolderName());

src/Microsoft.DotNet.PackageTesting/Microsoft.DotNet.PackageTesting.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netcoreapp3.1;net472</TargetFrameworks>
5-
<ExcludeFromSourceBuild>false</ExcludeFromSourceBuild>
65
<PackageType>MSBuildSdk</PackageType>
76
<IncludeBuildOutput>false</IncludeBuildOutput>
87
<IsPackable>true</IsPackable>

src/Microsoft.DotNet.PackageTesting/NupkgParser.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,29 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Linq;
5+
using System.Collections.Generic;
56
using NuGet.Frameworks;
67
using NuGet.Packaging;
78

89
namespace Microsoft.DotNet.PackageTesting
910
{
10-
public class NupkgParser
11+
class NupkgParser
1112
{
1213
public static Package CreatePackageObject(string packagePath)
1314
{
14-
PackageArchiveReader nupkgReader = new PackageArchiveReader(packagePath);
15+
PackageArchiveReader nupkgReader = new(packagePath);
1516
NuspecReader nuspecReader = nupkgReader.NuspecReader;
1617

1718
string packageId = nuspecReader.GetId();
1819
string version = nuspecReader.GetVersion().ToString();
1920

20-
NuGetFramework[] dependencyFrameworks = nuspecReader.GetDependencyGroups().Select(dg => dg.TargetFramework).Where(tfm => tfm != null).ToArray();
21+
NuGetFramework[] dependencyFrameworks = nuspecReader.GetDependencyGroups()
22+
.Select(dg => dg.TargetFramework)
23+
.Where(tfm => tfm != null)
24+
.ToArray();
25+
IEnumerable<string> files = nupkgReader.GetFiles()?.Where(t => t.EndsWith(packageId + ".dll"));
2126

22-
return new Package(packageId, version, nupkgReader.GetFiles()?.Where(t => t.EndsWith(packageId + ".dll")), dependencyFrameworks);
27+
return new Package(packageId, version, files, dependencyFrameworks);
2328
}
2429
}
2530
}

src/Microsoft.DotNet.PackageTesting/Package.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ namespace Microsoft.DotNet.PackageTesting
1111
{
1212
public class Package
1313
{
14+
public IEnumerable<NuGetFramework> FrameworksInPackage { get; }
15+
public string PackageId { get; }
16+
public string Version { get; }
17+
1418
public Package(string packageId, string version, IEnumerable<string> packageAssetPaths, IEnumerable<NuGetFramework> dependencyFrameworks)
1519
{
1620
PackageId = packageId;
1721
Version = version;
1822

1923
ContentItemCollection packageAssets = new();
2024
packageAssets.Load(packageAssetPaths);
21-
ManagedCodeConventions conventions = new ManagedCodeConventions(null);
25+
ManagedCodeConventions conventions = new(null);
2226

2327
IEnumerable<ContentItem> RefAssets = packageAssets.FindItems(conventions.Patterns.CompileRefAssemblies);
2428
IEnumerable<ContentItem> LibAssets = packageAssets.FindItems(conventions.Patterns.CompileLibAssemblies);
@@ -30,9 +34,5 @@ public Package(string packageId, string version, IEnumerable<string> packageAsse
3034
FrameworksInPackageList.AddRange(dependencyFrameworks);
3135
FrameworksInPackage = FrameworksInPackageList.Distinct();
3236
}
33-
34-
public string PackageId { get; set; }
35-
public string Version { get; set; }
36-
public IEnumerable<NuGetFramework> FrameworksInPackage { get; private set; }
3737
}
3838
}

src/Microsoft.DotNet.Build.Tasks.Packaging/src/VerifyClosure.cs renamed to src/Microsoft.DotNet.PackageTesting/VerifyClosure.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.Build.Framework;
5+
using Microsoft.DotNet.Build.Tasks;
56
using System;
67
using System.Collections.Generic;
78
using System.IO;
@@ -12,7 +13,7 @@
1213
using System.Text;
1314
using System.Xml.Linq;
1415

15-
namespace Microsoft.DotNet.Build.Tasks.Packaging
16+
namespace Microsoft.DotNet.PackageTesting
1617
{
1718
/// <summary>
1819
/// Verifies the closure of a set of DLLs, making sure all files are present and no cycles exist
@@ -125,7 +126,7 @@ private void AddSourceFile(string file)
125126

126127
private void LoadIgnoredReferences()
127128
{
128-
foreach (var ignoredReference in IgnoredReferences.NullAsEmpty())
129+
foreach (var ignoredReference in IgnoredReferences.DefaultIfEmpty())
129130
{
130131
var name = ignoredReference.ItemSpec;
131132
var versionString = ignoredReference.GetMetadata("Version");
@@ -175,7 +176,7 @@ void CheckDependencies(Stack<AssemblyInfo> depStack)
175176
// check module references
176177
if (assm.State == CheckState.Unchecked && CheckModuleReferences)
177178
{
178-
foreach(var moduleReference in assm.ModuleReferences.NullAsEmpty())
179+
foreach(var moduleReference in assm.ModuleReferences.DefaultIfEmpty())
179180
{
180181
if (ShouldIgnore(moduleReference))
181182
{

src/Microsoft.DotNet.Build.Tasks.Packaging/src/VerifyTypes.cs renamed to src/Microsoft.DotNet.PackageTesting/VerifyTypes.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.Build.Framework;
5+
using Microsoft.DotNet.Build.Tasks;
56
using System;
67
using System.Collections.Generic;
78
using System.IO;
9+
using System.Linq;
810
using System.Reflection;
911
using System.Reflection.Metadata;
1012
using System.Reflection.PortableExecutable;
1113

12-
namespace Microsoft.DotNet.Build.Tasks.Packaging
14+
namespace Microsoft.DotNet.PackageTesting
1315
{
1416
/// <summary>
1517
/// Verifies no type overlap in a set of DLLs
1618
/// </summary>
1719
public class VerifyTypes : BuildTask
1820
{
19-
2021
/// <summary>
2122
/// Sources to scan. Items can be directories or files.
2223
/// </summary>
@@ -124,7 +125,7 @@ private void AddSourceFile(string file)
124125

125126
private void LoadIgnoredTypes()
126127
{
127-
foreach(var ignoredType in IgnoredTypes.NullAsEmpty())
128+
foreach(var ignoredType in IgnoredTypes.DefaultIfEmpty())
128129
{
129130
ignoredTypes.Add(ignoredType.ItemSpec);
130131
}

src/Microsoft.DotNet.PackageTesting/build/Microsoft.DotNet.PackageTesting.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<DotNetPackageTestingAssembly Condition="'$(MSBuildRuntimeType)' == 'Core'">$(MSBuildThisFileDirectory)..\tools\netcoreapp3.1\Microsoft.DotNet.PackageTesting.dll</DotNetPackageTestingAssembly>
55
</PropertyGroup>
66

7+
<UsingTask TaskName="GetCompatiblePackageTargetFrameworks" AssemblyFile="$(DotNetPackageTestingAssembly)" />
8+
<UsingTask TaskName="VerifyClosure" AssemblyFile="$(DotNetPackageTestingAssembly)" />
9+
<UsingTask TaskName="VerifyTypes" AssemblyFile="$(DotNetPackageTestingAssembly)"/>
10+
711
<ItemGroup>
812
<SupportedTestFramework Include="netcoreapp3.1" />
913
<SupportedTestFramework Include="net5.0" />

0 commit comments

Comments
 (0)