diff --git a/src/UnityNuGet.Tests/PlatformDefinitionTests.cs b/src/UnityNuGet.Tests/PlatformDefinitionTests.cs index 7ac0b41..9309194 100644 --- a/src/UnityNuGet.Tests/PlatformDefinitionTests.cs +++ b/src/UnityNuGet.Tests/PlatformDefinitionTests.cs @@ -1,24 +1,24 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using NUnit.Framework; - -namespace UnityNuGet.Tests -{ - public class PlatformDefinitionTests - { - [Test] - public void CanFindDefinitions() - { - var platformDefs = PlatformDefinition.CreateAllPlatforms(); - - // Look-up by OS should return the most general configuration - var win = platformDefs.Find(UnityOs.Windows); - Assert.That(win, Is.Not.Null); - Assert.That(win!.Cpu, Is.EqualTo(UnityCpu.AnyCpu)); - - // Look-up explicit configuration +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using NUnit.Framework; + +namespace UnityNuGet.Tests +{ + public class PlatformDefinitionTests + { + [Test] + public void CanFindDefinitions() + { + var platformDefs = PlatformDefinition.CreateAllPlatforms(); + + // Look-up by OS should return the most general configuration + var win = platformDefs.Find(UnityOs.Windows); + Assert.That(win, Is.Not.Null); + Assert.That(win!.Cpu, Is.EqualTo(UnityCpu.AnyCpu)); + + // Look-up explicit configuration var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); Assert.Multiple(() => { @@ -27,118 +27,118 @@ public void CanFindDefinitions() }); Assert.Multiple(() => { - Assert.That(win64.Cpu, Is.EqualTo(UnityCpu.X64)); + Assert.That(win64?.Cpu, Is.EqualTo(UnityCpu.X64)); Assert.That(win.Children, Does.Contain(win64)); - }); - - // Look-up invalid configuration - var and = platformDefs.Find(UnityOs.Android, UnityCpu.None); - Assert.That(and, Is.Null); - } - - [Test] - public void RemainingPlatforms_NoneVisited() - { - var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var visited = new HashSet(); - - // If no platform was visited, the remaining platforms should be the (AnyOS, AnyCPU) config. - var remaining = platformDefs.GetRemainingPlatforms(visited); + }); + + // Look-up invalid configuration + var and = platformDefs.Find(UnityOs.Android, UnityCpu.None); + Assert.That(and, Is.Null); + } + + [Test] + public void RemainingPlatforms_NoneVisited() + { + var platformDefs = PlatformDefinition.CreateAllPlatforms(); + var visited = new HashSet(); + + // If no platform was visited, the remaining platforms should be the (AnyOS, AnyCPU) config. + var remaining = platformDefs.GetRemainingPlatforms(visited); Assert.That(remaining, Is.Not.Null); Assert.Multiple(() => { Assert.That(remaining, Has.Count.EqualTo(1)); Assert.That(platformDefs, Is.EqualTo(remaining.First())); - }); - } - - [Test] - public void RemainingPlatforms_OneVisited() - { - var platformDefs = PlatformDefinition.CreateAllPlatforms(); - - foreach (var child in platformDefs.Children) - { - var visited = new HashSet() { child }; - var remaining = platformDefs.GetRemainingPlatforms(visited); - - // We should get all other children, except the one already visited - Assert.That(remaining.Count + 1, Is.EqualTo(platformDefs.Children.Count)); - foreach (var r in remaining) + }); + } + + [Test] + public void RemainingPlatforms_OneVisited() + { + var platformDefs = PlatformDefinition.CreateAllPlatforms(); + + foreach (var child in platformDefs.Children) + { + var visited = new HashSet() { child }; + var remaining = platformDefs.GetRemainingPlatforms(visited); + + // We should get all other children, except the one already visited + Assert.That(remaining.Count + 1, Is.EqualTo(platformDefs.Children.Count)); + foreach (var r in remaining) { Assert.Multiple(() => { Assert.That(child, Is.Not.EqualTo(r)); Assert.That(platformDefs.Children, Does.Contain(r)); - }); - } - } - } - - [Test] - public void RemainingPlatforms_LeafVisited() - { - var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); - var visited = new HashSet() { win64! }; - - // The remaining platforms should be all non-windows, as well as all !x64 windows - var expected = platformDefs.Children - .Except(new[] { win64!.Parent }) - .Concat( - win64.Parent!.Children - .Except(new[] { win64 })) - .ToHashSet(); - var actual = platformDefs.GetRemainingPlatforms(visited); - Assert.That(expected.SetEquals(actual), Is.True); - } - - [TestCase("")] - [TestCase("base")] - public void TestConfigPath_Root(string basePath) - { - var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var file = new PlatformFile("a/b/c.dll", platformDefs); - - // We don't use extra paths for the (AnyOS, AnyCPU) configuration - var actual = file.GetDestinationPath(basePath); - var expected = Path.Combine( - basePath, - Path.GetFileName(file.SourcePath)); - Assert.That(expected, Is.EqualTo(actual)); - } - - [TestCase("")] - [TestCase("base")] - public void TestConfigPath_OsOnly(string basePath) - { - var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var win = platformDefs.Find(UnityOs.Windows); - var file = new PlatformFile("a/b/c.dll", win!); - - var actual = file.GetDestinationPath(basePath); - var expected = Path.Combine( - basePath, - "Windows", - Path.GetFileName(file.SourcePath)); - Assert.That(expected, Is.EqualTo(actual)); - } - - [TestCase("")] - [TestCase("base")] - public void TestConfigPath_Full(string basePath) - { - var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); - var file = new PlatformFile("a/b/c.dll", win64!); - - var actual = file.GetDestinationPath(basePath); - var expected = Path.Combine( - basePath, - "Windows", - "x86_64", - Path.GetFileName(file.SourcePath)); - Assert.That(expected, Is.EqualTo(actual)); - } - } -} + }); + } + } + } + + [Test] + public void RemainingPlatforms_LeafVisited() + { + var platformDefs = PlatformDefinition.CreateAllPlatforms(); + var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); + var visited = new HashSet() { win64! }; + + // The remaining platforms should be all non-windows, as well as all !x64 windows + var expected = platformDefs.Children + .Except(new[] { win64!.Parent }) + .Concat( + win64.Parent!.Children + .Except(new[] { win64 })) + .ToHashSet(); + var actual = platformDefs.GetRemainingPlatforms(visited); + Assert.That(expected.SetEquals(actual), Is.True); + } + + [TestCase("")] + [TestCase("base")] + public void TestConfigPath_Root(string basePath) + { + var platformDefs = PlatformDefinition.CreateAllPlatforms(); + var file = new PlatformFile("a/b/c.dll", platformDefs); + + // We don't use extra paths for the (AnyOS, AnyCPU) configuration + var actual = file.GetDestinationPath(basePath); + var expected = Path.Combine( + basePath, + Path.GetFileName(file.SourcePath)); + Assert.That(expected, Is.EqualTo(actual)); + } + + [TestCase("")] + [TestCase("base")] + public void TestConfigPath_OsOnly(string basePath) + { + var platformDefs = PlatformDefinition.CreateAllPlatforms(); + var win = platformDefs.Find(UnityOs.Windows); + var file = new PlatformFile("a/b/c.dll", win!); + + var actual = file.GetDestinationPath(basePath); + var expected = Path.Combine( + basePath, + "Windows", + Path.GetFileName(file.SourcePath)); + Assert.That(expected, Is.EqualTo(actual)); + } + + [TestCase("")] + [TestCase("base")] + public void TestConfigPath_Full(string basePath) + { + var platformDefs = PlatformDefinition.CreateAllPlatforms(); + var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); + var file = new PlatformFile("a/b/c.dll", win64!); + + var actual = file.GetDestinationPath(basePath); + var expected = Path.Combine( + basePath, + "Windows", + "x86_64", + Path.GetFileName(file.SourcePath)); + Assert.That(expected, Is.EqualTo(actual)); + } + } +} diff --git a/src/UnityNuGet.Tests/RegistryCacheTests.cs b/src/UnityNuGet.Tests/RegistryCacheTests.cs index 212aea3..1acd027 100644 --- a/src/UnityNuGet.Tests/RegistryCacheTests.cs +++ b/src/UnityNuGet.Tests/RegistryCacheTests.cs @@ -7,7 +7,7 @@ namespace UnityNuGet.Tests { public class RegistryCacheTests { - [Test] + [Test,Order(99)] public async Task TestBuild() { var errorsTriggered = false; diff --git a/src/UnityNuGet.Tests/RegistryTests.cs b/src/UnityNuGet.Tests/RegistryTests.cs index 008d9e4..a2a7f75 100644 --- a/src/UnityNuGet.Tests/RegistryTests.cs +++ b/src/UnityNuGet.Tests/RegistryTests.cs @@ -92,8 +92,7 @@ public async Task CanParse_PackageWithRuntimes() Assert.That(libFiles.SetEquals(runtimeFiles), Is.True); } - [Test] - public async Task Ensure_Min_Version_Is_Correct_Ignoring_Analyzers_And_Native_Libs() + static async Task AllRegistries() { var registry = Registry.GetInstance(); @@ -153,110 +152,57 @@ public async Task Ensure_Min_Version_Is_Correct_Ignoring_Analyzers_And_Native_Li var excludedPackagesRegex = new Regex(@$"^{string.Join('|', excludedPackages)}$"); - foreach (var registryKvp in registry.Where(r => !r.Value.Analyzer && !r.Value.Ignored)) - { - var packageId = registryKvp.Key; - - if (excludedPackagesRegex.IsMatch(packageId)) - { - continue; - } - - var versionRange = registryKvp.Value.Version; - - var dependencyPackageMetas = await resource.GetMetadataAsync( - packageId, - includePrerelease: false, - includeUnlisted: false, - cache, - logger, - cancellationToken); - - var packageIdentity = NuGetHelper.GetMinimumCompatiblePackageIdentity(dependencyPackageMetas, nuGetFrameworks, includeAny: false); - - if (packageIdentity != null) - { - Assert.That(versionRange!.MinVersion, Is.EqualTo(packageIdentity.Version), $"Package {packageId}"); - } - else - { - var settings = Settings.LoadDefaultSettings(root: null); - - var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( - new SourceRepository[] { repository }, - new PackageIdentity(registryKvp.Key, registryKvp.Value.Version!.MinVersion), - new PackageDownloadContext(cache), - SettingsUtility.GetGlobalPackagesFolder(settings), - logger, cancellationToken); - - var hasNativeLib = await NativeLibraries.GetSupportedNativeLibsAsync(downloadResult.PackageReader, logger).AnyAsync(); - - if (hasNativeLib) - { - continue; - } - else - { - Assert.Fail(packageId); - } - } - } + return registry.Where(r => !r.Value.Analyzer && !r.Value.Ignored).OrderBy((pair) => pair.Key).Select((pair) => { + return new TestCaseData(resource,logger,cache,repository,excludedPackagesRegex,nuGetFrameworks,pair.Key,pair.Value.Version).SetArgDisplayNames(pair.Key,pair.Value.Version!.ToString()); + }).ToArray(); } - [Test] - public async Task Ensure_Do_Not_Exceed_The_Maximum_Number_Of_Allowed_Versions() + const int maxAllowedVersions = 100; + + [TestCaseSource(nameof(AllRegistries))] + public async Task Ensure_Min_Version_Is_Correct_Ignoring_Analyzers_And_Native_Libs(PackageMetadataResource resource, + NuGetConsoleTestLogger logger, + SourceCacheContext cache, + SourceRepository repository, + Regex excludedPackagesRegex, + RegistryTargetFramework[] nuGetFrameworks, + string packageId, + NuGet.Versioning.VersionRange versionRange) { - const int maxAllowedVersions = 100; + var dependencyPackageMetas = await resource.GetMetadataAsync( + packageId, + includePrerelease: false, + includeUnlisted: false, + cache, + logger, + CancellationToken.None); - var registry = Registry.GetInstance(); + var versions = dependencyPackageMetas.Where(v => versionRange!.Satisfies(v.Identity.Version)).ToArray(); + Warn.If(versions,Has.Length.GreaterThan(maxAllowedVersions)); - var logger = new NuGetConsoleTestLogger(); - var cancellationToken = CancellationToken.None; - - var cache = new SourceCacheContext(); - var repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); - var resource = await repository.GetResourceAsync(); - - List<(string packageId, int versionCount)> packages = []; - - foreach (var registryKvp in registry.Where(r => !r.Value.Analyzer && !r.Value.Ignored)) - { - var packageId = registryKvp.Key; - - var versionRange = registryKvp.Value.Version; + if(excludedPackagesRegex.IsMatch(packageId)) + return; - var dependencyPackageMetas = await resource.GetMetadataAsync( - packageId, - includePrerelease: false, - includeUnlisted: false, - cache, - logger, - cancellationToken); + var packageIdentity = NuGetHelper.GetMinimumCompatiblePackageIdentity(dependencyPackageMetas, nuGetFrameworks, includeAny: false); - var versions = dependencyPackageMetas.Where(v => versionRange!.Satisfies(v.Identity.Version)).ToArray(); - - if (versions.Length > maxAllowedVersions) - { - packages.Add((registryKvp.Key, versions.Length)); - } - } - - StringBuilder stringBuilder = new(); - - foreach (var (packageId, versionCount) in packages.OrderByDescending(p => p.versionCount)) + if (packageIdentity != null) { - stringBuilder.AppendLine($"{packageId} -> {versionCount}"); - } - - if (stringBuilder.Length == 0) - { - const bool trueConstant = true; - - Assert.That(trueConstant, Is.True); + Assert.That(versionRange!.MinVersion, Is.EqualTo(packageIdentity.Version), $"Package {packageId}"); } else { - Assert.Inconclusive(stringBuilder.ToString()); + var settings = Settings.LoadDefaultSettings(root: null); + + var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( + new [] { repository }, + new PackageIdentity(packageId,versionRange!.MinVersion), + new PackageDownloadContext(cache), + SettingsUtility.GetGlobalPackagesFolder(settings), + logger, CancellationToken.None); + + var hasNativeLib = await NativeLibraries.GetSupportedNativeLibsAsync(downloadResult.PackageReader, logger).AnyAsync(); + if (!hasNativeLib) + Assert.Fail(packageId); } } } diff --git a/src/UnityNuGet.Tests/UnityNuGet.Tests.csproj b/src/UnityNuGet.Tests/UnityNuGet.Tests.csproj index 3b8c848..5c2b22a 100644 --- a/src/UnityNuGet.Tests/UnityNuGet.Tests.csproj +++ b/src/UnityNuGet.Tests/UnityNuGet.Tests.csproj @@ -9,15 +9,15 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - \ No newline at end of file