|
| 1 | +using FluentAssertions; |
| 2 | +using NuGet.Frameworks; |
| 3 | +using NuGet.Packaging; |
| 4 | +using RestApia.Shared.Common; |
| 5 | +using RestApia.Shared.Common.Enums; |
| 6 | +using RestApia.Shared.Common.Services; |
| 7 | +namespace RestApia.Experiments.Tests.Extensions; |
| 8 | + |
| 9 | +public class ValidateMyExtension |
| 10 | +{ |
| 11 | + [TestCase(@"X:\projects\RestApia3\repos\RestApia.Shared\.local\builds\RestApia.Extensions.Import.Postman\RestApia.Extensions.Import.Postman.*.nupkg")] |
| 12 | + [TestCase(@"X:\projects\RestApia3\repos\RestApia.Shared\.local\builds\RestApia.Extensions.Auth.OAuth2\RestApia.Extensions.Auth.OAuth2.*.nupkg")] |
| 13 | + [TestCase(@"X:\projects\RestApia3\repos\RestApia.Shared\.local\builds\RestApia.Extensions.Auth.Basic\RestApia.Extensions.Auth.Basic.*.nupkg")] |
| 14 | + public void ValidateNugetPackage(string fileSearchPath) |
| 15 | + { |
| 16 | + var filePath = Directory.GetFiles(Directory.GetParent(fileSearchPath)!.FullName, Path.GetFileName(fileSearchPath)).FirstOrDefault(); |
| 17 | + filePath.Should().NotBeEmpty(); |
| 18 | + File.Exists(filePath).Should().BeTrue("Nuget package should exist"); |
| 19 | + |
| 20 | + var targetFramework = NuGetFramework.ParseFrameworkName(".NETCoreApp,Version=v8.0", DefaultFrameworkNameProvider.Instance); |
| 21 | + using var package = new PackageArchiveReader(filePath); |
| 22 | + var packageIdentity = package.GetIdentity(); |
| 23 | + |
| 24 | + // check identity |
| 25 | + packageIdentity.Id.Should().NotBeEmpty("Package should have an Id"); |
| 26 | + packageIdentity.Version.Should().NotBeNull("Version should not be null"); |
| 27 | + Console.WriteLine($"Package Id: {packageIdentity.Id}"); |
| 28 | + Console.WriteLine($"Version: {packageIdentity.Version}"); |
| 29 | + |
| 30 | + // search assembly |
| 31 | + var files = package |
| 32 | + .GetFiles() |
| 33 | + .Where(x => |
| 34 | + x.StartsWith($"lib/{targetFramework.GetShortFolderName()}/", StringComparison.Ordinal) && |
| 35 | + Path.GetFileName(x).Equals($"{packageIdentity.Id}.dll", StringComparison.Ordinal)) |
| 36 | + .ToList(); |
| 37 | + |
| 38 | + files.Should().NotBeEmpty($"Package should contain a '{packageIdentity.Id}.dll' file"); |
| 39 | + files.Should().HaveCount(1, $"Package should contain only one '{packageIdentity.Id}.dll' file"); |
| 40 | + Console.WriteLine($"Assembly found: {files[0]}"); |
| 41 | + |
| 42 | + // check dependencies, must include shared library |
| 43 | + var dependencies = package.GetPackageDependencies().FirstOrDefault(x => NuGetFrameworkUtility.IsCompatibleWithFallbackCheck(targetFramework, x.TargetFramework)); |
| 44 | + dependencies.Should().NotBeNull("Package should have dependencies"); |
| 45 | + dependencies!.Packages.Should().NotBeEmpty("Package should have dependencies"); |
| 46 | + |
| 47 | + var sharedLibrary = dependencies.Packages.FirstOrDefault(x => x.Id.Equals("RestApia.Shared", StringComparison.Ordinal)); |
| 48 | + sharedLibrary.Should().NotBeNull("Package should have a dependency on 'RestApia.Shared'"); |
| 49 | + |
| 50 | + // check metadata |
| 51 | + var owners = package.NuspecReader.GetOwners().IfEmpty(package.NuspecReader.GetAuthors()); |
| 52 | + var description = package.NuspecReader.GetDescription(); |
| 53 | + var projectUrl = package.NuspecReader.GetProjectUrl(); |
| 54 | + |
| 55 | + owners.Should().NotBeEmpty("Package should have owners"); |
| 56 | + description.Should().NotBeEmpty("Package should have a description"); |
| 57 | + Console.WriteLine($"Description: {description}"); |
| 58 | + Console.WriteLine($"Owners: {owners}"); |
| 59 | + |
| 60 | + if (owners.Split(',').Select(x => x.Trim()).Any(x => x.Equals("RestApia", StringComparison.OrdinalIgnoreCase))) |
| 61 | + Console.WriteLine("[WARN] Package owners should not include 'RestApia'"); |
| 62 | + |
| 63 | + if (projectUrl.IsEmpty()) Console.WriteLine("[WARN] It is recommended to have a project URL in the package metadata"); |
| 64 | + else Console.WriteLine($"Project URL: {projectUrl}"); |
| 65 | + |
| 66 | + // check requirements |
| 67 | + var requirements = PackageReader.RequirementsRead(package.NuspecReader.GetReleaseNotes()); |
| 68 | + if (!requirements.TryGetValue(PackageRequirement.MinAppVersion, out var requirement)) Console.WriteLine("[WARN] It is recommended to have a 'MinAppVersion' requirement"); |
| 69 | + else Console.WriteLine($"Minimum application version: {requirement}"); |
| 70 | + } |
| 71 | +} |
0 commit comments