diff --git a/src/UnityNuGet.Tests/PlatformDefinitionTests.cs b/src/UnityNuGet.Tests/PlatformDefinitionTests.cs index 001c5ca..e8ec684 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 - PlatformDefinition? 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 + PlatformDefinition? win = platformDefs.Find(UnityOs.Windows); + Assert.That(win, Is.Not.Null); + Assert.That(win!.Cpu, Is.EqualTo(UnityCpu.AnyCpu)); + + // Look-up explicit configuration PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); Assert.Multiple(() => { @@ -29,116 +29,116 @@ public void CanFindDefinitions() { Assert.That(win64?.Cpu, Is.EqualTo(UnityCpu.X64)); Assert.That(win.Children, Does.Contain(win64)); - }); - - // Look-up invalid configuration - PlatformDefinition? 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. - HashSet remaining = platformDefs.GetRemainingPlatforms(visited); + }); + + // Look-up invalid configuration + PlatformDefinition? 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. + HashSet 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 (PlatformDefinition child in platformDefs.Children) - { - var visited = new HashSet() { child }; - HashSet 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 (PlatformDefinition r in remaining) + }); + } + + [Test] + public void RemainingPlatforms_OneVisited() + { + var platformDefs = PlatformDefinition.CreateAllPlatforms(); + + foreach (var child in platformDefs.Children) + { + var visited = new HashSet() { child }; + HashSet 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 (PlatformDefinition 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(); - PlatformDefinition? 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([win64!.Parent]) - .Concat( - win64.Parent!.Children - .Except([win64])) - .ToHashSet(); - HashSet 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 - string actual = file.GetDestinationPath(basePath); - string 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(); - PlatformDefinition? win = platformDefs.Find(UnityOs.Windows); - var file = new PlatformFile("a/b/c.dll", win!); - - string actual = file.GetDestinationPath(basePath); - string 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(); - PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); - var file = new PlatformFile("a/b/c.dll", win64!); - - string actual = file.GetDestinationPath(basePath); - string 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(); + PlatformDefinition? 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([win64!.Parent]) + .Concat( + win64.Parent!.Children + .Except([win64])) + .ToHashSet(); + HashSet 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 + string actual = file.GetDestinationPath(basePath); + string 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(); + PlatformDefinition? win = platformDefs.Find(UnityOs.Windows); + var file = new PlatformFile("a/b/c.dll", win!); + + string actual = file.GetDestinationPath(basePath); + string 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(); + PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); + var file = new PlatformFile("a/b/c.dll", win64!); + + string actual = file.GetDestinationPath(basePath); + string 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 c9337a3..230240a 100644 --- a/src/UnityNuGet.Tests/RegistryCacheTests.cs +++ b/src/UnityNuGet.Tests/RegistryCacheTests.cs @@ -14,7 +14,7 @@ namespace UnityNuGet.Tests { public class RegistryCacheTests { - [Test] + [Test,Order(99)] public async Task TestBuild() { bool errorsTriggered = false; diff --git a/src/UnityNuGet.Tests/RegistryTests.cs b/src/UnityNuGet.Tests/RegistryTests.cs index e80b8ed..e5035a8 100644 --- a/src/UnityNuGet.Tests/RegistryTests.cs +++ b/src/UnityNuGet.Tests/RegistryTests.cs @@ -24,7 +24,7 @@ namespace UnityNuGet.Tests { public class RegistryTests { - private readonly RegistryOptions _registryOptions = new() { RegistryFilePath = "registry.json" }; + private static readonly RegistryOptions _registryOptions = new() { RegistryFilePath = "registry.json" }; [Test] [TestCase("scriban")] @@ -45,7 +45,7 @@ public async Task Make_Sure_That_The_Registry_Is_Case_Insensitive(string package { Assert.That(registry.TryGetValue(packageName, out RegistryEntry? result), Is.True); Assert.That(result, Is.Not.Null); - }); + }); } [Test] @@ -140,8 +140,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 hostEnvironmentMock = new Mock(); hostEnvironmentMock.Setup(h => h.EnvironmentName).Returns(Environments.Development); @@ -211,118 +210,56 @@ public async Task Ensure_Min_Version_Is_Correct_Ignoring_Analyzers_And_Native_Li var excludedPackagesRegex = new Regex(@$"^{string.Join('|', excludedPackages)}$"); - foreach (KeyValuePair registryKvp in registry.Where(r => !r.Value.Analyzer && !r.Value.Ignored)) - { - string packageId = registryKvp.Key; - - if (excludedPackagesRegex.IsMatch(packageId)) - { - continue; - } - - VersionRange? versionRange = registryKvp.Value.Version; - - IEnumerable dependencyPackageMetas = await resource.GetMetadataAsync( - packageId, - includePrerelease: false, - includeUnlisted: false, - cache, - logger, - cancellationToken); - - PackageIdentity? packageIdentity = NuGetHelper.GetMinimumCompatiblePackageIdentity(dependencyPackageMetas, nuGetFrameworks, includeAny: false); - - if (packageIdentity != null) - { - Assert.That(versionRange!.MinVersion, Is.EqualTo(packageIdentity.Version), $"Package {packageId}"); - } - else - { - ISettings settings = Settings.LoadDefaultSettings(root: null); - - DownloadResourceResult downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( - [repository], - new PackageIdentity(registryKvp.Key, registryKvp.Value.Version!.MinVersion), - new PackageDownloadContext(cache), - SettingsUtility.GetGlobalPackagesFolder(settings), - logger, cancellationToken); - - bool 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, + VersionRange versionRange) { - const int maxAllowedVersions = 100; + var dependencyPackageMetas = await resource.GetMetadataAsync( + packageId, + includePrerelease: false, + includeUnlisted: false, + cache, + logger, + CancellationToken.None); - var hostEnvironmentMock = new Mock(); - hostEnvironmentMock.Setup(h => h.EnvironmentName).Returns(Environments.Development); + var versions = dependencyPackageMetas.Where(v => versionRange!.Satisfies(v.Identity.Version)).ToArray(); + Warn.If(versions,Has.Length.GreaterThan(maxAllowedVersions)); - var loggerFactory = new LoggerFactory(); - loggerFactory.AddProvider(new FakeLoggerProvider()); - - var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(_registryOptions)); - - var logger = new NuGetConsoleTestLogger(); - CancellationToken cancellationToken = CancellationToken.None; + if(excludedPackagesRegex.IsMatch(packageId)) + return; - await registry.StartAsync(cancellationToken); + var packageIdentity = NuGetHelper.GetMinimumCompatiblePackageIdentity(dependencyPackageMetas, nuGetFrameworks, includeAny: false); - var cache = new SourceCacheContext(); - SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); - PackageMetadataResource resource = await repository.GetResourceAsync(); - - List<(string packageId, int versionCount)> packages = []; - - foreach (KeyValuePair registryKvp in registry.Where(r => !r.Value.Analyzer && !r.Value.Ignored)) + if (packageIdentity != null) { - string packageId = registryKvp.Key; - - VersionRange? versionRange = registryKvp.Value.Version; - - IEnumerable dependencyPackageMetas = await resource.GetMetadataAsync( - packageId, - includePrerelease: false, - includeUnlisted: false, - cache, - logger, - cancellationToken); - - IPackageSearchMetadata[] versions = dependencyPackageMetas.Where(v => versionRange!.Satisfies(v.Identity.Version)).ToArray(); - - if (versions.Length > maxAllowedVersions) - { - packages.Add((registryKvp.Key, versions.Length)); - } + Assert.That(versionRange!.MinVersion, Is.EqualTo(packageIdentity.Version), $"Package {packageId}"); } - - StringBuilder stringBuilder = new(); - - foreach ((string packageId, int versionCount) in packages.OrderByDescending(p => p.versionCount)) + else { - stringBuilder.AppendLine($"{packageId} -> {versionCount}"); - } + ISettings settings = Settings.LoadDefaultSettings(root: null); - if (stringBuilder.Length == 0) - { - const bool trueConstant = true; + var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( + [repository], + new PackageIdentity(packageId, versionRange!.MinVersion), + new PackageDownloadContext(cache), + SettingsUtility.GetGlobalPackagesFolder(settings), + logger, CancellationToken.None); - Assert.That(trueConstant, Is.True); - } - else - { - Assert.Inconclusive(stringBuilder.ToString()); + bool hasNativeLib = await NativeLibraries.GetSupportedNativeLibsAsync(downloadResult.PackageReader, logger).AnyAsync(); + Assert.That(hasNativeLib,packageId); } } } diff --git a/src/UnityNuGet.Tests/UnityNuGet.Tests.csproj b/src/UnityNuGet.Tests/UnityNuGet.Tests.csproj index d026452..23763ac 100644 --- a/src/UnityNuGet.Tests/UnityNuGet.Tests.csproj +++ b/src/UnityNuGet.Tests/UnityNuGet.Tests.csproj @@ -11,15 +11,15 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - \ No newline at end of file