Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix license display when it comes from a File #5296

Merged
merged 4 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using System.Windows.Documents;
using Microsoft.ServiceHub.Framework;
using NuGet.PackageManagement.VisualStudio;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Packaging.Licenses;
Expand Down Expand Up @@ -109,6 +110,9 @@ internal static IReadOnlyList<IText> GenerateLicenseLinks(LicenseMetadata metada
break;

case LicenseType.File:
NuGetPackageFileService.AddLicenseToCache(
packageIdentity,
CreateEmbeddedLicenseUri(packagePath, metadata));
list.Add(new LicenseFileText(Resources.Text_ViewLicense, licenseFileHeader, packagePath, metadata.License, packageIdentity));
break;

Expand All @@ -119,6 +123,18 @@ internal static IReadOnlyList<IText> GenerateLicenseLinks(LicenseMetadata metada
return list;
}

private static Uri CreateEmbeddedLicenseUri(string packagePath, LicenseMetadata licenseMetadata)
{
Uri baseUri = new Uri(packagePath, UriKind.Absolute);

var builder = new UriBuilder(baseUri)
{
Fragment = licenseMetadata.License
};

return builder.Uri;
}

private static void PopulateLicenseIdentifiers(NuGetLicenseExpression expression, IList<string> identifiers)
{
switch (expression.Type)
Expand Down
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 @@ -144,23 +156,76 @@ public void PackageLicenseUtility_BadUnlicensedGeneratesNoLinksAndAWarning()
[Fact]
public void PackageLicenseUtility_GeneratesLinkForFiles()
{
// Setup
var licenseFileLocation = "License.txt";
var licenseFileHeader = "header";
var licenseData = new LicenseMetadata(LicenseType.File, licenseFileLocation, null, null, LicenseMetadata.CurrentVersion);
using (var pathContext = new SimpleTestPathContext())
{
// Setup
var packageA1 = new SimpleTestPackageContext("AddLicenseToCache", "1.0.0");
string licenseFileLocation = "License.txt";
string licenseFileHeader = "header";
LicenseMetadata licenseData = new LicenseMetadata(LicenseType.File, licenseFileLocation, null, null, LicenseMetadata.CurrentVersion);
packageA1.AddFile(licenseFileLocation, StreamLicenseContents);

PackageIdentity packageIdentity = new PackageIdentity("AddLicenseToCache", NuGetVersion.Parse("1.0.0"));

// Act
IReadOnlyList<IText> links = PackageLicenseUtilities.GenerateLicenseLinks(
licenseData,
licenseFileHeader,
pathContext.SolutionRoot,
packageIdentity: packageIdentity);

Assert.Equal(1, links.Count);
Assert.True(links[0] is LicenseFileText);
LicenseFileText 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);
}
}

// Act
var links = PackageLicenseUtilities.GenerateLicenseLinks(
licenseData,
licenseFileHeader,
licenseFileLocation,
packageIdentity: null);
private Mock<INuGetTelemetryProvider> _telemetryProvider = new Mock<INuGetTelemetryProvider>(MockBehavior.Strict);

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);
[Fact]
public async Task PackageLicenseUtility_GeneratesLinkForFiles_And_CacheIsUpdated()
{
using (var pathContext = new SimpleTestPathContext())
{
// Setup
var package = new SimpleTestPackageContext("AddLicenseToCache", "1.0.0");
string licenseFileLocation = "License.txt";
string licenseFileHeader = "header";
LicenseMetadata licenseData = new LicenseMetadata(LicenseType.File, licenseFileLocation, null, null, LicenseMetadata.CurrentVersion);
package.AddFile(licenseFileLocation, StreamLicenseContents);

NuGetPackageFileService packageFileService = new NuGetPackageFileService(
default(ServiceActivationOptions),
Mock.Of<IServiceBroker>(),
new AuthorizationServiceClient(Mock.Of<IAuthorizationService>()),
_telemetryProvider.Object);

await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
PackageSaveMode.Defaultv3,
package);

PackageIdentity packageIdentity = new PackageIdentity("AddLicenseToCache", NuGetVersion.Parse("1.0.0"));

// Act
IReadOnlyList<IText> links = PackageLicenseUtilities.GenerateLicenseLinks(
licenseData,
licenseFileHeader,
Path.Combine(pathContext.PackageSource, packageIdentity.Id, packageIdentity.Version.ToString(), package.PackageName),
packageIdentity);

Assert.Equal(1, links.Count);
Assert.True(links[0] is LicenseFileText);
LicenseFileText 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]
Expand All @@ -181,5 +246,7 @@ public void PackageLicenseUtility_GenerateCorrectLink()
Assert.Equal(license, licenseText.Text);
Assert.Equal("https://licenses.nuget.org/MIT", licenseText.Link.AbsoluteUri);
}

private const string StreamLicenseContents = "I am a license";
}
}