|
| 1 | +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. |
| 2 | + |
| 3 | +namespace Microsoft.CST.OpenSource.Tests.Helpers; |
| 4 | + |
| 5 | +using Contracts; |
| 6 | +using Model.Metadata; |
| 7 | +using Moq; |
| 8 | +using NuGet.Packaging; |
| 9 | +using PackageUrl; |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Linq; |
| 13 | +using System.Threading; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Helper class for working with NuGet metadata in tests |
| 17 | +/// </summary> |
| 18 | +public static class NuGetTestHelper |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Creates a NuGetPackageVersionMetadata object for use in tests with |
| 22 | + /// proper initialization of all required properties. |
| 23 | + /// </summary> |
| 24 | + public static NuGetPackageVersionMetadata CreateMetadata( |
| 25 | + string name, |
| 26 | + string version, |
| 27 | + string description = "", |
| 28 | + string authors = "", |
| 29 | + Uri? catalogUri = null, |
| 30 | + string tags = "") |
| 31 | + { |
| 32 | + // Instead of directly creating the metadata object with the problematic IEnumerable property, |
| 33 | + // we'll create a TestNuGetPackageVersionMetadata that uses a List instead |
| 34 | + return new TestNuGetPackageVersionMetadata |
| 35 | + { |
| 36 | + Name = name, |
| 37 | + Version = version, |
| 38 | + Description = description, |
| 39 | + Authors = authors, |
| 40 | + Tags = tags, |
| 41 | + CatalogUri = catalogUri ?? new Uri($"https://api.nuget.org/v3/catalog0/data/2022.03.11.23.17.27/{name}.{version}.json"), |
| 42 | + DependencySets = new List<PackageDependencyGroup>() // Empty list to avoid serialization issues |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Sets up a mock package actions object for testing NuGet package operations |
| 48 | + /// </summary> |
| 49 | + /// <returns>A mocked IManagerPackageActions that returns the specified metadata and versions</returns> |
| 50 | + public static IManagerPackageActions<NuGetPackageVersionMetadata> SetupPackageActions( |
| 51 | + PackageURL purl, |
| 52 | + NuGetPackageVersionMetadata metadata, |
| 53 | + IEnumerable<string>? versions = null) |
| 54 | + { |
| 55 | + Mock<IManagerPackageActions<NuGetPackageVersionMetadata>> mockActions = new(); |
| 56 | + |
| 57 | + // Set up GetMetadataAsync to return our metadata |
| 58 | + mockActions.Setup(m => m.GetMetadataAsync( |
| 59 | + It.Is<PackageURL>(p => p.Name.Equals(purl.Name, StringComparison.OrdinalIgnoreCase) && |
| 60 | + (purl.Version == null || p.Version == purl.Version)), |
| 61 | + It.IsAny<bool>(), |
| 62 | + It.IsAny<CancellationToken>())) |
| 63 | + .ReturnsAsync(metadata); |
| 64 | + |
| 65 | + // If versions are provided, set up GetAllVersionsAsync and GetLatestVersionAsync |
| 66 | + if (versions != null) |
| 67 | + { |
| 68 | + string[] versionArray = versions as string[] ?? versions.ToArray(); |
| 69 | + |
| 70 | + mockActions.Setup(m => m.GetAllVersionsAsync( |
| 71 | + It.Is<PackageURL>(p => p.Name.Equals(purl.Name, StringComparison.OrdinalIgnoreCase)), |
| 72 | + It.IsAny<bool>(), |
| 73 | + It.IsAny<bool>(), |
| 74 | + It.IsAny<CancellationToken>())) |
| 75 | + .ReturnsAsync(versionArray); |
| 76 | + |
| 77 | + if (versionArray.Length > 0) |
| 78 | + { |
| 79 | + mockActions.Setup(m => m.GetLatestVersionAsync( |
| 80 | + It.Is<PackageURL>(p => p.Name.Equals(purl.Name, StringComparison.OrdinalIgnoreCase)), |
| 81 | + It.IsAny<bool>(), |
| 82 | + It.IsAny<bool>(), |
| 83 | + It.IsAny<CancellationToken>())) |
| 84 | + .ReturnsAsync(versionArray[0]); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Set up DoesPackageExistAsync to return true for this package |
| 89 | + mockActions.Setup(m => m.DoesPackageExistAsync( |
| 90 | + It.Is<PackageURL>(p => p.Name.Equals(purl.Name, StringComparison.OrdinalIgnoreCase)), |
| 91 | + It.IsAny<bool>(), |
| 92 | + It.IsAny<CancellationToken>())) |
| 93 | + .ReturnsAsync(true); |
| 94 | + |
| 95 | + return mockActions.Object; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// <summary> |
| 100 | +/// A test-specific implementation of NuGetPackageVersionMetadata that uses a List for DependencySets |
| 101 | +/// to avoid serialization issues |
| 102 | +/// </summary> |
| 103 | +public record TestNuGetPackageVersionMetadata : NuGetPackageVersionMetadata |
| 104 | +{ |
| 105 | + // Override the problematic property with a concrete List implementation |
| 106 | + public new List<PackageDependencyGroup> DependencySets { get; set; } = new(); |
| 107 | + |
| 108 | + // This property is accessed in some places so we need to ensure it returns our concrete List |
| 109 | + public IEnumerable<PackageDependencyGroup> DependencySetsInternal => DependencySets; |
| 110 | +} |
0 commit comments