Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrrm committed Jul 5, 2023
1 parent e09b809 commit 629ec93
Showing 1 changed file with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<INuGetTelemetryProvider> _telemetryProvider = new Mock<INuGetTelemetryProvider>(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<IServiceBroker>(),
new AuthorizationServiceClient(Mock.Of<IAuthorizationService>()),
_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()
{
Expand All @@ -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";
}
}

0 comments on commit 629ec93

Please sign in to comment.