Skip to content

Commit

Permalink
Only use lowercase boolean values in .nuspec XML (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKotsenas authored Mar 7, 2024
1 parent 5176116 commit 8b57afd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void FileCustom()
GetFileContents(packageA.FullPath, relativePath)
.ShouldBe("585B55DD5AC54A10B841B3D9A00129D8");

GetNuspec(packageA)
GetNuspecReader(packageA)
.DependencyGroups.Select(i => i.TargetFramework).ToList()
.ShouldContain("net46");
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public void FileText()
GetFileContents(packageA.FullPath, relativePath)
.ShouldBe("607779BADE3645F8A288543213BFE948");

GetNuspec(packageA)
GetNuspecReader(packageA)
.DependencyGroups
.ShouldBeEmpty();
}
Expand All @@ -126,7 +126,7 @@ private void VerifyContentFile(Package package, string relativePath, string expe

GetFileContents(package.FullPath, Path.Combine("contentFiles", expectedLanguage, expectedTargetFramework, relativePath)).ShouldBe(expectedContents);

NuspecReader nuspecReader = GetNuspec(package);
NuspecReader nuspecReader = GetNuspecReader(package);

PackageContentFileEntry file = nuspecReader.ContentFiles.ShouldHaveSingleItem();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ protected string GetFileContents(string packageFullPath, Func<string, bool> file
});
}

protected NuspecReader GetNuspec(Package package)
protected string GetNuspec(Package package)
{
package.FullPath.ShouldNotBeNull();

return new NuspecReader(GetFileContents(package.FullPath, filePath => filePath.EndsWith($"{package.Id}.nuspec", StringComparison.OrdinalIgnoreCase)));
return GetFileContents(package.FullPath, filePath => filePath.EndsWith($"{package.Id}.nuspec", StringComparison.OrdinalIgnoreCase));
}

protected NuspecReader GetNuspecReader(Package package)
{
return new NuspecReader(GetNuspec(package));
}

protected Assembly? LoadAssembly(string packageFullPath, string filePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Licensed under the MIT license.

using Shouldly;
using System;
using System.IO;
using System.Linq;
using Xunit;
Expand All @@ -28,7 +27,7 @@ public void BasicPackage()
packageA.Id.ShouldBe("PackageA");
packageA.Version.ShouldBe("1.0.0");

NuspecReader nuspec = GetNuspec(packageA);
NuspecReader nuspec = GetNuspecReader(packageA);

nuspec.Authors.ShouldBe("John Smith");
nuspec.Description.ShouldBe("Custom Description");
Expand All @@ -42,6 +41,44 @@ public void BasicPackage()
dependencies.ShouldBeEmpty();
}

[Fact]
public void NuspecXmlSerializesCorrectly()
{
using PackageFeed packageFeed = PackageFeed.Create(FeedRootPath)
.Package(
id: "PackageD",
version: "1.2.3-beta",
out Package package,
developmentDependency: false)
.Library("net45")
.ContentFileText("file.txt", "584717ec-6132-4418-853c-1fa72778f52a", "net45", "None", copyToOutput: true, flatten: false)
.Save();

package.ShouldNotBeNull();

char sep = Path.DirectorySeparatorChar;
GetNuspec(package).ShouldBe(
$@"<package xmlns=""http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"">
<metadata minClientVersion=""2.12"">
<id>PackageD</id>
<version>1.2.3-beta</version>
<authors>Author</authors>
<description>Description</description>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<developmentDependency>false</developmentDependency>
<serviceable>false</serviceable>
<dependencies>
<group targetFramework=""net45"" />
<group targetFramework=""any"" />
</dependencies>
<contentFiles>
<files include=""any{sep}net45{sep}file.txt"" copyToOutput=""true"" flatten=""false"" buildAction=""None"" />
</contentFiles>
</metadata>
</package>",
StringCompareShould.IgnoreLineEndings);
}

[Fact]
public void BasicPackageWithDependency()
{
Expand All @@ -59,7 +96,7 @@ public void BasicPackageWithDependency()
packageA.Id.ShouldBe("PackageA");
packageA.Version.ShouldBe("1.0.0");

NuspecReader nuspec = GetNuspec(packageA);
NuspecReader nuspec = GetNuspecReader(packageA);

nuspec.Authors.ShouldBe("John Smith");
nuspec.Description.ShouldBe("Custom Description");
Expand Down
16 changes: 16 additions & 0 deletions src/Microsoft.Build.Utilities.ProjectCreation/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,28 @@ internal static void WriteAttributeStringIfNotNull(this XmlWriter writer, string
}
}

internal static void WriteAttributeStringIfNotNull(this XmlWriter writer, string localName, bool? value)
{
if (value.HasValue)
{
writer.WriteAttributeString(localName, value.Value ? "true" : "false");
}
}

internal static void WriteElementStringIfNotNull(this XmlWriter writer, string localName, string? value)
{
if (!string.IsNullOrWhiteSpace(value))
{
writer.WriteElementString(localName, value);
}
}

internal static void WriteElementStringIfNotNull(this XmlWriter writer, string localName, bool? value)
{
if (value.HasValue)
{
writer.WriteElementString(localName, value.Value ? "true" : "false");
}
}
}
}
10 changes: 5 additions & 5 deletions src/Microsoft.Build.Utilities.ProjectCreation/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,18 +462,18 @@ internal void WriteNuspec(Stream stream)
writer.WriteElementStringIfNotNull("copyright", Copyright);
writer.WriteElementStringIfNotNull("tags", Tags);
writer.WriteElementStringIfNotNull("projectUrl", ProjectUrl);
writer.WriteElementStringIfNotNull("requireLicenseAcceptance", RequireLicenseAcceptance.ToString());
writer.WriteElementStringIfNotNull("requireLicenseAcceptance", RequireLicenseAcceptance);
writer.WriteElementStringIfNotNull("licenseUrl", LicenseUrl);
writer.WriteElementStringIfNotNull("icon", Icon);
writer.WriteElementStringIfNotNull("iconUrl", IconUrl);
writer.WriteElementStringIfNotNull("developmentDependency", DevelopmentDependency.ToString());
writer.WriteElementStringIfNotNull("developmentDependency", DevelopmentDependency);
writer.WriteElementStringIfNotNull("language", Language);
writer.WriteElementStringIfNotNull("title", Title);
writer.WriteElementStringIfNotNull("tags", Tags);
writer.WriteElementStringIfNotNull("summary", Summary);
writer.WriteElementStringIfNotNull("owners", Owners);
writer.WriteElementStringIfNotNull("releaseNotes", ReleaseNotes);
writer.WriteElementStringIfNotNull("serviceable", Serviceable.ToString());
writer.WriteElementStringIfNotNull("serviceable", Serviceable);

if (PackageTypes != null && PackageTypes.Any())
{
Expand Down Expand Up @@ -544,8 +544,8 @@ internal void WriteNuspec(Stream stream)
writer.WriteStartElement("files");
writer.WriteAttributeStringIfNotNull("include", contentFilesEntry.Include);
writer.WriteAttributeStringIfNotNull("exclude", contentFilesEntry.Exclude);
writer.WriteAttributeString("copyToOutput", contentFilesEntry.CopyToOutput.ToString());
writer.WriteAttributeStringIfNotNull("flatten", contentFilesEntry.Flatten.ToString());
writer.WriteAttributeStringIfNotNull("copyToOutput", contentFilesEntry.CopyToOutput);
writer.WriteAttributeStringIfNotNull("flatten", contentFilesEntry.Flatten);
writer.WriteAttributeStringIfNotNull("buildAction", contentFilesEntry.BuildAction);
writer.WriteEndElement();
}
Expand Down

0 comments on commit 8b57afd

Please sign in to comment.