From 629ec930a61f1a52dfdd3c6713a4f6abd48a02df Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Wed, 5 Jul 2023 11:50:46 -0700 Subject: [PATCH] added tests --- .../PackageLicenseUtilitiesTests.cs | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/test/NuGet.Clients.Tests/NuGet.PackageManagement.UI.Test/PackageLicenseUtilitiesTests.cs b/test/NuGet.Clients.Tests/NuGet.PackageManagement.UI.Test/PackageLicenseUtilitiesTests.cs index 76715686ece..92b98e28eee 100644 --- a/test/NuGet.Clients.Tests/NuGet.PackageManagement.UI.Test/PackageLicenseUtilitiesTests.cs +++ b/test/NuGet.Clients.Tests/NuGet.PackageManagement.UI.Test/PackageLicenseUtilitiesTests.cs @@ -3,10 +3,22 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; using System.Windows.Documents; +using Microsoft.ServiceHub.Framework; +using Microsoft.ServiceHub.Framework.Services; +using Moq; +using NuGet.PackageManagement.VisualStudio; using NuGet.Packaging; +using NuGet.Packaging.Core; using NuGet.Packaging.Licenses; +using NuGet.Test.Utility; +using NuGet.Versioning; +using NuGet.VisualStudio.Telemetry; using Xunit; namespace NuGet.PackageManagement.UI.Test @@ -163,6 +175,49 @@ public void PackageLicenseUtility_GeneratesLinkForFiles() Assert.Equal(Resources.LicenseFile_Loading, ((Run)((Paragraph)licenseFileText.LicenseText.Blocks.AsEnumerable().First()).Inlines.First()).Text); } + private Mock _telemetryProvider = new Mock(MockBehavior.Strict); + + [Fact] + public async Task PackageLicenseUtility_GeneratesLinkForFiles_And_CacheIsUpdated() + { + using (TestDirectory testDir = TestDirectory.Create()) + { + // Setup + // Create decoy nuget package + var zipPath = Path.Combine(testDir.Path, "file.nupkg"); + CreateDummyPackage(zipPath: zipPath, licenseFile: "License.txt", licenseFileTargetElement: ""); + + var packageFileService = new NuGetPackageFileService( + default(ServiceActivationOptions), + Mock.Of(), + new AuthorizationServiceClient(Mock.Of()), + _telemetryProvider.Object); + + var licenseFileLocation = "License.txt"; + var licenseFileHeader = "header"; + var licenseData = new LicenseMetadata(LicenseType.File, licenseFileLocation, null, null, LicenseMetadata.CurrentVersion); + + var packageIdentity = new PackageIdentity("AddLicenseToCache", NuGetVersion.Parse("1.0.0")); + + // Act + var links = PackageLicenseUtilities.GenerateLicenseLinks( + licenseData, + licenseFileHeader, + zipPath, + packageIdentity); + + Assert.Equal(1, links.Count); + Assert.True(links[0] is LicenseFileText); + var licenseFileText = links[0] as LicenseFileText; + Assert.Equal(Resources.Text_ViewLicense, licenseFileText.Text); + Assert.Equal(Resources.LicenseFile_Loading, ((Run)((Paragraph)licenseFileText.LicenseText.Blocks.AsEnumerable().First()).Inlines.First()).Text); + + Stream licenseStream = await packageFileService.GetEmbeddedLicenseAsync(packageIdentity, CancellationToken.None); + Assert.NotNull(licenseStream); + Assert.Equal(StreamLicenseContents.Length, licenseStream.Length); + } + } + [Fact] public void PackageLicenseUtility_GenerateCorrectLink() { @@ -181,5 +236,43 @@ public void PackageLicenseUtility_GenerateCorrectLink() Assert.Equal(license, licenseText.Text); Assert.Equal("https://licenses.nuget.org/MIT", licenseText.Link.AbsoluteUri); } + + private static void CreateDummyPackage( + string zipPath, + string licenseFile = "License.txt", + string licenseFileTargetElement = "") + { + var dir = Path.GetDirectoryName(zipPath); + var holdDir = "pkg"; + var folderPath = Path.Combine(dir, holdDir); + + // base dir + Directory.CreateDirectory(folderPath); + + // create nuspec + var nuspec = NuspecBuilder.Create() + .WithFile(licenseFile, licenseFileTargetElement); + + // create license file + var licensePath = Path.Combine(folderPath, licenseFile); + var licenseDir = Path.GetDirectoryName(licensePath); + Directory.CreateDirectory(licenseDir); + + File.WriteAllText(licensePath, StreamLicenseContents); + + // Create nuget package + using (var nuspecStream = new MemoryStream()) + using (FileStream nupkgStream = File.Create(zipPath)) + using (var writer = new StreamWriter(nuspecStream)) + { + nuspec.Write(writer); + writer.Flush(); + nuspecStream.Position = 0; + var pkgBuilder = new PackageBuilder(stream: nuspecStream, basePath: folderPath); + pkgBuilder.Save(nupkgStream); + } + } + + private static string StreamLicenseContents = "I am license"; } }