Skip to content

Commit d94ac75

Browse files
authored
Add LicenseUrl and LicenseExpression to NuspecReader (#273)
1 parent 9dd25ae commit d94ac75

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

src/Microsoft.Build.Utilities.ProjectCreation.UnitTests/NuspecReader.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,25 @@ public NuspecReader(FileInfo fileInfo)
6262

6363
public string? License => GetElement("license");
6464

65+
public string? LicenseExpression
66+
{
67+
get
68+
{
69+
string? licenseType = LicenseType;
70+
71+
if (licenseType != null && licenseType.Equals("expression", StringComparison.OrdinalIgnoreCase))
72+
{
73+
return License;
74+
}
75+
76+
return null;
77+
}
78+
}
79+
6580
public string? LicenseType => GetAttribute("license", "type");
6681

82+
public string? LicenseUrl => GetElement("licenseUrl");
83+
6784
public string? LicenseVersion => GetAttribute("license", "version");
6885

6986
public string? Owners => GetElement("owners");
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Jeff Kluge. All rights reserved.
2+
//
3+
// Licensed under the MIT license.
4+
5+
using Shouldly;
6+
using Xunit;
7+
8+
namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests
9+
{
10+
public class NuspecReaderTests
11+
{
12+
[Fact]
13+
public void LicenseExpressionIsNullForFileLicenses()
14+
{
15+
string contents =
16+
@"<package xmlns=""http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"">
17+
<metadata>
18+
<id>PackageL</id>
19+
<version>16.4.60</version>
20+
<authors>UserA</authors>
21+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
22+
<license type=""file"">LICENSE</license>
23+
<packageTypes>
24+
<packageType name=""Dependency"" />
25+
</packageTypes>
26+
</metadata>
27+
</package>";
28+
29+
NuspecReader nuspec = new NuspecReader(contents);
30+
31+
nuspec.License.ShouldBe("LICENSE");
32+
nuspec.LicenseExpression.ShouldBeNull();
33+
nuspec.LicenseType.ShouldBe("file");
34+
nuspec.LicenseVersion.ShouldBeNull();
35+
nuspec.LicenseUrl.ShouldBeNull();
36+
}
37+
}
38+
}

src/Microsoft.Build.Utilities.ProjectCreation.UnitTests/PackageRepositoryTests/RepositoryTests.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,17 @@ public void CanSetAllPackageProperties()
113113
copyright: "Copyright 2000",
114114
developmentDependency: true,
115115
icon: Path.Combine("some", "icon.jpg"),
116-
iconUrl: "https://icon.url",
116+
iconUrl: "https://icon.invalid/url",
117117
language: "Pig latin",
118+
licenseUrl: "https://license.invalid/url",
118119
licenseExpression: "MIT",
119120
licenseVersion: "1.0.0",
120121
owners: "Owner1;Owner2",
121122
packageTypes: new List<string> { "Dependency", "DotnetCliTool" },
122-
projectUrl: "https://project.url",
123+
projectUrl: "https://project.invalid/url",
123124
releaseNotes: "Release notes for PackageD",
124125
repositoryType: "Git",
125-
repositoryUrl: "https://repository.url",
126+
repositoryUrl: "https://repository.invalid/url",
126127
repositoryBranch: "Branch1000",
127128
repositoryCommit: "Commit14",
128129
requireLicenseAcceptance: true,
@@ -145,20 +146,22 @@ public void CanSetAllPackageProperties()
145146
nuspec.Description.ShouldBe("Custom description");
146147
nuspec.DevelopmentDependency.ShouldBeTrue();
147148
nuspec.Icon.ShouldBe(Path.Combine("some", "icon.jpg"));
148-
nuspec.IconUrl.ShouldBe("https://icon.url");
149+
nuspec.IconUrl.ShouldBe("https://icon.invalid/url");
149150
nuspec.Id.ShouldBe("PackageD");
150151
nuspec.Language.ShouldBe("Pig latin");
151152
nuspec.License.ShouldBe("MIT");
153+
nuspec.LicenseExpression.ShouldBe("MIT");
152154
nuspec.LicenseType.ShouldBe("expression");
155+
nuspec.LicenseUrl.ShouldBe("https://license.invalid/url");
153156
nuspec.LicenseVersion.ShouldBe("1.0.0");
154157
nuspec.Owners.ShouldBe("Owner1;Owner2");
155158
nuspec.PackageTypes.ShouldBe(new[] { "Dependency", "DotnetCliTool" });
156-
nuspec.ProjectUrl.ShouldBe("https://project.url");
159+
nuspec.ProjectUrl.ShouldBe("https://project.invalid/url");
157160
nuspec.ReleaseNotes.ShouldBe("Release notes for PackageD");
158161
nuspec.RepositoryBranch.ShouldBe("Branch1000");
159162
nuspec.RepositoryCommit.ShouldBe("Commit14");
160163
nuspec.RepositoryType.ShouldBe("Git");
161-
nuspec.RepositoryUrl.ShouldBe("https://repository.url");
164+
nuspec.RepositoryUrl.ShouldBe("https://repository.invalid/url");
162165
nuspec.RequireLicenseAcceptance.ShouldBeTrue();
163166
nuspec.Serviceable.ShouldBeTrue();
164167
nuspec.Summary.ShouldBe("Summary of PackageD");
@@ -167,6 +170,32 @@ public void CanSetAllPackageProperties()
167170
}
168171
}
169172

173+
[Fact]
174+
public void LicenseExpressionPackagePropertiesCanBeNull()
175+
{
176+
using (PackageRepository packageRepository = PackageRepository.Create(TestRootPath)
177+
.Package(
178+
id: "PackageD",
179+
version: "1.2.3",
180+
out Package package,
181+
licenseUrl: "https://license.invalid/url",
182+
licenseExpression: null,
183+
licenseVersion: null))
184+
{
185+
package.ShouldNotBeNull();
186+
187+
FileInfo nuspecFileInfo = new FileInfo(packageRepository.GetManifestFilePath(package.Id, package.Version)).ShouldExist();
188+
189+
NuspecReader nuspec = new NuspecReader(nuspecFileInfo);
190+
191+
nuspec.License.ShouldBeNull();
192+
nuspec.LicenseExpression.ShouldBeNull();
193+
nuspec.LicenseType.ShouldBeNull();
194+
nuspec.LicenseUrl.ShouldBe("https://license.invalid/url");
195+
nuspec.LicenseVersion.ShouldBeNull();
196+
}
197+
}
198+
170199
[Fact]
171200
public void CanUseNuGetSdkResolver()
172201
{

0 commit comments

Comments
 (0)