Skip to content

Commit

Permalink
Move VerifyClosure tasks into M.D.PackageTesting (#7692)
Browse files Browse the repository at this point in the history
As we plan to deprecate the M.D.B.Tasks.Packaging project in the future,
moving the tasks into the PackageTesting project.

Fixes #7474
  • Loading branch information
ViktorHofer authored Aug 2, 2021
1 parent 093cd72 commit a50c276
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
- SplitDependenciesBySupport
- SplitReferences
- UpdatePackageIndex
- ValidationTask
- VerifyClosure
- VerifyTypes</PackageDescription>
- ValidationTask</PackageDescription>

<DefaultItemExcludes Condition="'$(TargetFramework)' != 'net472'">**/*.Desktop.*</DefaultItemExcludes>
<BeforePack>$(BeforePack);AddRuntimeJson</BeforePack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,5 @@
<UsingTask TaskName="UpdatePackageIndex" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
<UsingTask TaskName="ValidatePackage" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
<UsingTask TaskName="ValidateFrameworkPackage" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
<UsingTask TaskName="VerifyClosure" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
<UsingTask TaskName="VerifyTypes" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
<UsingTask TaskName="GetSupportedPackagesFromPackageReports" AssemblyFile="$(PackagingTaskDir)Microsoft.DotNet.Build.Tasks.Packaging.dll"/>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildUtilitiesCoreVersion)" />
<PackageReference Include="NuGet.Frameworks" Version="$(NuGetVersion)" />
<PackageReference Include="NuGet.Packaging" Version="$(NuGetVersion)" />
<!-- Remove when targeting >= net5.0. -->
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.DotNet.PackageTesting
{
public class GetCompatiblePackageTargetFrameworks : BuildTask
{
private static List<NuGetFramework> allTargetFrameworks = allTargetFrameworks = new();
private static List<NuGetFramework> allTargetFrameworks = new();
private static Dictionary<NuGetFramework, HashSet<NuGetFramework>> packageTfmMapping = new();

[Required]
Expand All @@ -27,12 +27,15 @@ public class GetCompatiblePackageTargetFrameworks : BuildTask

public override bool Execute()
{
bool result = true;
List<ITaskItem> testProjects = new List<ITaskItem>();
List<ITaskItem> testProjects = new();

try
{
Initialize(SupportedTestFrameworks);
string minDotnetTargetFramework = allTargetFrameworks.Where(t => t.Framework == ".NETCoreApp").OrderBy(t => t.Version).FirstOrDefault()?.GetShortFolderName();
string minDotnetTargetFramework = allTargetFrameworks.Where(t => t.Framework == ".NETCoreApp")
.OrderBy(t => t.Version)
.FirstOrDefault()?
.GetShortFolderName();

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

return result && !Log.HasLoggedErrors;
return !Log.HasLoggedErrors;
}

public static IEnumerable<NuGetFramework> GetTestFrameworks(Package package, string minDotnetTargetFramework)
{
List<NuGetFramework> frameworksToTest= new List<NuGetFramework>();
List<NuGetFramework> frameworksToTest= new();
IEnumerable<NuGetFramework> packageTargetFrameworks = package.FrameworksInPackage;

// Testing the package installation on all tfms linked with package targetframeworks.
foreach (var item in packageTargetFrameworks)
{
if (packageTfmMapping.ContainsKey(item))
{
frameworksToTest.AddRange(packageTfmMapping[item]);
}

// Adding the frameworks in the packages to the test matrix.
frameworksToTest.Add(item);
}

if (!string.IsNullOrEmpty(minDotnetTargetFramework) && frameworksToTest.Any(t => t.Framework == ".NETStandard"))
{
frameworksToTest.Add(NuGetFramework.Parse(minDotnetTargetFramework));
}

return frameworksToTest.Where(tfm => allTargetFrameworks.Contains(tfm)).Distinct();
}
Expand Down Expand Up @@ -97,12 +105,12 @@ public static void Initialize(string targetFrameworks)
}
}

public List<ITaskItem> CreateItemFromTestFramework(string packageId, string version, IEnumerable<NuGetFramework> testFrameworks)
private static List<ITaskItem> CreateItemFromTestFramework(string packageId, string version, IEnumerable<NuGetFramework> testFrameworks)
{
List<ITaskItem> testprojects = new List<ITaskItem>();
List<ITaskItem> testprojects = new();
foreach (var framework in testFrameworks)
{
var supportedPackage = new TaskItem(packageId);
TaskItem supportedPackage = new(packageId);
supportedPackage.SetMetadata("Version", version);
supportedPackage.SetMetadata("TargetFramework", framework.ToString());
supportedPackage.SetMetadata("TargetFrameworkShort", framework.GetShortFolderName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net472</TargetFrameworks>
<ExcludeFromSourceBuild>false</ExcludeFromSourceBuild>
<PackageType>MSBuildSdk</PackageType>
<IncludeBuildOutput>false</IncludeBuildOutput>
<IsPackable>true</IsPackable>
Expand Down
13 changes: 9 additions & 4 deletions src/Microsoft.DotNet.PackageTesting/NupkgParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using System.Collections.Generic;
using NuGet.Frameworks;
using NuGet.Packaging;

namespace Microsoft.DotNet.PackageTesting
{
public class NupkgParser
class NupkgParser
{
public static Package CreatePackageObject(string packagePath)
{
PackageArchiveReader nupkgReader = new PackageArchiveReader(packagePath);
PackageArchiveReader nupkgReader = new(packagePath);
NuspecReader nuspecReader = nupkgReader.NuspecReader;

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

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

return new Package(packageId, version, nupkgReader.GetFiles()?.Where(t => t.EndsWith(packageId + ".dll")), dependencyFrameworks);
return new Package(packageId, version, files, dependencyFrameworks);
}
}
}
10 changes: 5 additions & 5 deletions src/Microsoft.DotNet.PackageTesting/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ namespace Microsoft.DotNet.PackageTesting
{
public class Package
{
public IEnumerable<NuGetFramework> FrameworksInPackage { get; }
public string PackageId { get; }
public string Version { get; }

public Package(string packageId, string version, IEnumerable<string> packageAssetPaths, IEnumerable<NuGetFramework> dependencyFrameworks)
{
PackageId = packageId;
Version = version;

ContentItemCollection packageAssets = new();
packageAssets.Load(packageAssetPaths);
ManagedCodeConventions conventions = new ManagedCodeConventions(null);
ManagedCodeConventions conventions = new(null);

IEnumerable<ContentItem> RefAssets = packageAssets.FindItems(conventions.Patterns.CompileRefAssemblies);
IEnumerable<ContentItem> LibAssets = packageAssets.FindItems(conventions.Patterns.CompileLibAssemblies);
Expand All @@ -30,9 +34,5 @@ public Package(string packageId, string version, IEnumerable<string> packageAsse
FrameworksInPackageList.AddRange(dependencyFrameworks);
FrameworksInPackage = FrameworksInPackageList.Distinct();
}

public string PackageId { get; set; }
public string Version { get; set; }
public IEnumerable<NuGetFramework> FrameworksInPackage { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Framework;
using Microsoft.DotNet.Build.Tasks;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -12,7 +13,7 @@
using System.Text;
using System.Xml.Linq;

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

private void LoadIgnoredReferences()
{
foreach (var ignoredReference in IgnoredReferences.NullAsEmpty())
foreach (var ignoredReference in IgnoredReferences.DefaultIfEmpty())
{
var name = ignoredReference.ItemSpec;
var versionString = ignoredReference.GetMetadata("Version");
Expand Down Expand Up @@ -175,7 +176,7 @@ void CheckDependencies(Stack<AssemblyInfo> depStack)
// check module references
if (assm.State == CheckState.Unchecked && CheckModuleReferences)
{
foreach(var moduleReference in assm.ModuleReferences.NullAsEmpty())
foreach(var moduleReference in assm.ModuleReferences.DefaultIfEmpty())
{
if (ShouldIgnore(moduleReference))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Framework;
using Microsoft.DotNet.Build.Tasks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;

namespace Microsoft.DotNet.Build.Tasks.Packaging
namespace Microsoft.DotNet.PackageTesting
{
/// <summary>
/// Verifies no type overlap in a set of DLLs
/// </summary>
public class VerifyTypes : BuildTask
{

/// <summary>
/// Sources to scan. Items can be directories or files.
/// </summary>
Expand Down Expand Up @@ -124,7 +125,7 @@ private void AddSourceFile(string file)

private void LoadIgnoredTypes()
{
foreach(var ignoredType in IgnoredTypes.NullAsEmpty())
foreach(var ignoredType in IgnoredTypes.DefaultIfEmpty())
{
ignoredTypes.Add(ignoredType.ItemSpec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<DotNetPackageTestingAssembly Condition="'$(MSBuildRuntimeType)' == 'Core'">$(MSBuildThisFileDirectory)..\tools\netcoreapp3.1\Microsoft.DotNet.PackageTesting.dll</DotNetPackageTestingAssembly>
</PropertyGroup>

<UsingTask TaskName="GetCompatiblePackageTargetFrameworks" AssemblyFile="$(DotNetPackageTestingAssembly)" />
<UsingTask TaskName="VerifyClosure" AssemblyFile="$(DotNetPackageTestingAssembly)" />
<UsingTask TaskName="VerifyTypes" AssemblyFile="$(DotNetPackageTestingAssembly)"/>

<ItemGroup>
<SupportedTestFramework Include="netcoreapp3.1" />
<SupportedTestFramework Include="net5.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\Microsoft.DotNet.Build.Tasks.Packaging\src\VerifyClosure.cs" Link="src/Packaging/VerifyClosure.cs" />
<Compile Include="..\Microsoft.DotNet.Build.Tasks.Packaging\src\VerifyTypes.cs" Link="src/Packaging/VerifyTypes.cs" />
<Compile Include="..\Microsoft.DotNet.PackageTesting\VerifyClosure.cs" Link="src/Packaging/VerifyClosure.cs" />
<Compile Include="..\Microsoft.DotNet.PackageTesting\VerifyTypes.cs" Link="src/Packaging/VerifyTypes.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
Expand Down

This file was deleted.

0 comments on commit a50c276

Please sign in to comment.