Skip to content

Commit 8b57afd

Browse files
authored
Only use lowercase boolean values in .nuspec XML (#271)
1 parent 5176116 commit 8b57afd

File tree

5 files changed

+71
-13
lines changed

5 files changed

+71
-13
lines changed

src/Microsoft.Build.Utilities.ProjectCreation.UnitTests/PackageFeedTests/PackageFeedFileTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void FileCustom()
7171
GetFileContents(packageA.FullPath, relativePath)
7272
.ShouldBe("585B55DD5AC54A10B841B3D9A00129D8");
7373

74-
GetNuspec(packageA)
74+
GetNuspecReader(packageA)
7575
.DependencyGroups.Select(i => i.TargetFramework).ToList()
7676
.ShouldContain("net46");
7777
}
@@ -105,7 +105,7 @@ public void FileText()
105105
GetFileContents(packageA.FullPath, relativePath)
106106
.ShouldBe("607779BADE3645F8A288543213BFE948");
107107

108-
GetNuspec(packageA)
108+
GetNuspecReader(packageA)
109109
.DependencyGroups
110110
.ShouldBeEmpty();
111111
}
@@ -126,7 +126,7 @@ private void VerifyContentFile(Package package, string relativePath, string expe
126126

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

129-
NuspecReader nuspecReader = GetNuspec(package);
129+
NuspecReader nuspecReader = GetNuspecReader(package);
130130

131131
PackageContentFileEntry file = nuspecReader.ContentFiles.ShouldHaveSingleItem();
132132

src/Microsoft.Build.Utilities.ProjectCreation.UnitTests/PackageFeedTests/PackageFeedTestBase.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,16 @@ protected string GetFileContents(string packageFullPath, Func<string, bool> file
5959
});
6060
}
6161

62-
protected NuspecReader GetNuspec(Package package)
62+
protected string GetNuspec(Package package)
6363
{
6464
package.FullPath.ShouldNotBeNull();
6565

66-
return new NuspecReader(GetFileContents(package.FullPath, filePath => filePath.EndsWith($"{package.Id}.nuspec", StringComparison.OrdinalIgnoreCase)));
66+
return GetFileContents(package.FullPath, filePath => filePath.EndsWith($"{package.Id}.nuspec", StringComparison.OrdinalIgnoreCase));
67+
}
68+
69+
protected NuspecReader GetNuspecReader(Package package)
70+
{
71+
return new NuspecReader(GetNuspec(package));
6772
}
6873

6974
protected Assembly? LoadAssembly(string packageFullPath, string filePath)

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Licensed under the MIT license.
44

55
using Shouldly;
6-
using System;
76
using System.IO;
87
using System.Linq;
98
using Xunit;
@@ -28,7 +27,7 @@ public void BasicPackage()
2827
packageA.Id.ShouldBe("PackageA");
2928
packageA.Version.ShouldBe("1.0.0");
3029

31-
NuspecReader nuspec = GetNuspec(packageA);
30+
NuspecReader nuspec = GetNuspecReader(packageA);
3231

3332
nuspec.Authors.ShouldBe("John Smith");
3433
nuspec.Description.ShouldBe("Custom Description");
@@ -42,6 +41,44 @@ public void BasicPackage()
4241
dependencies.ShouldBeEmpty();
4342
}
4443

44+
[Fact]
45+
public void NuspecXmlSerializesCorrectly()
46+
{
47+
using PackageFeed packageFeed = PackageFeed.Create(FeedRootPath)
48+
.Package(
49+
id: "PackageD",
50+
version: "1.2.3-beta",
51+
out Package package,
52+
developmentDependency: false)
53+
.Library("net45")
54+
.ContentFileText("file.txt", "584717ec-6132-4418-853c-1fa72778f52a", "net45", "None", copyToOutput: true, flatten: false)
55+
.Save();
56+
57+
package.ShouldNotBeNull();
58+
59+
char sep = Path.DirectorySeparatorChar;
60+
GetNuspec(package).ShouldBe(
61+
$@"<package xmlns=""http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"">
62+
<metadata minClientVersion=""2.12"">
63+
<id>PackageD</id>
64+
<version>1.2.3-beta</version>
65+
<authors>Author</authors>
66+
<description>Description</description>
67+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
68+
<developmentDependency>false</developmentDependency>
69+
<serviceable>false</serviceable>
70+
<dependencies>
71+
<group targetFramework=""net45"" />
72+
<group targetFramework=""any"" />
73+
</dependencies>
74+
<contentFiles>
75+
<files include=""any{sep}net45{sep}file.txt"" copyToOutput=""true"" flatten=""false"" buildAction=""None"" />
76+
</contentFiles>
77+
</metadata>
78+
</package>",
79+
StringCompareShould.IgnoreLineEndings);
80+
}
81+
4582
[Fact]
4683
public void BasicPackageWithDependency()
4784
{
@@ -59,7 +96,7 @@ public void BasicPackageWithDependency()
5996
packageA.Id.ShouldBe("PackageA");
6097
packageA.Version.ShouldBe("1.0.0");
6198

62-
NuspecReader nuspec = GetNuspec(packageA);
99+
NuspecReader nuspec = GetNuspecReader(packageA);
63100

64101
nuspec.Authors.ShouldBe("John Smith");
65102
nuspec.Description.ShouldBe("Custom Description");

src/Microsoft.Build.Utilities.ProjectCreation/ExtensionMethods.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,28 @@ internal static void WriteAttributeStringIfNotNull(this XmlWriter writer, string
118118
}
119119
}
120120

121+
internal static void WriteAttributeStringIfNotNull(this XmlWriter writer, string localName, bool? value)
122+
{
123+
if (value.HasValue)
124+
{
125+
writer.WriteAttributeString(localName, value.Value ? "true" : "false");
126+
}
127+
}
128+
121129
internal static void WriteElementStringIfNotNull(this XmlWriter writer, string localName, string? value)
122130
{
123131
if (!string.IsNullOrWhiteSpace(value))
124132
{
125133
writer.WriteElementString(localName, value);
126134
}
127135
}
136+
137+
internal static void WriteElementStringIfNotNull(this XmlWriter writer, string localName, bool? value)
138+
{
139+
if (value.HasValue)
140+
{
141+
writer.WriteElementString(localName, value.Value ? "true" : "false");
142+
}
143+
}
128144
}
129145
}

src/Microsoft.Build.Utilities.ProjectCreation/Package.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,18 +462,18 @@ internal void WriteNuspec(Stream stream)
462462
writer.WriteElementStringIfNotNull("copyright", Copyright);
463463
writer.WriteElementStringIfNotNull("tags", Tags);
464464
writer.WriteElementStringIfNotNull("projectUrl", ProjectUrl);
465-
writer.WriteElementStringIfNotNull("requireLicenseAcceptance", RequireLicenseAcceptance.ToString());
465+
writer.WriteElementStringIfNotNull("requireLicenseAcceptance", RequireLicenseAcceptance);
466466
writer.WriteElementStringIfNotNull("licenseUrl", LicenseUrl);
467467
writer.WriteElementStringIfNotNull("icon", Icon);
468468
writer.WriteElementStringIfNotNull("iconUrl", IconUrl);
469-
writer.WriteElementStringIfNotNull("developmentDependency", DevelopmentDependency.ToString());
469+
writer.WriteElementStringIfNotNull("developmentDependency", DevelopmentDependency);
470470
writer.WriteElementStringIfNotNull("language", Language);
471471
writer.WriteElementStringIfNotNull("title", Title);
472472
writer.WriteElementStringIfNotNull("tags", Tags);
473473
writer.WriteElementStringIfNotNull("summary", Summary);
474474
writer.WriteElementStringIfNotNull("owners", Owners);
475475
writer.WriteElementStringIfNotNull("releaseNotes", ReleaseNotes);
476-
writer.WriteElementStringIfNotNull("serviceable", Serviceable.ToString());
476+
writer.WriteElementStringIfNotNull("serviceable", Serviceable);
477477

478478
if (PackageTypes != null && PackageTypes.Any())
479479
{
@@ -544,8 +544,8 @@ internal void WriteNuspec(Stream stream)
544544
writer.WriteStartElement("files");
545545
writer.WriteAttributeStringIfNotNull("include", contentFilesEntry.Include);
546546
writer.WriteAttributeStringIfNotNull("exclude", contentFilesEntry.Exclude);
547-
writer.WriteAttributeString("copyToOutput", contentFilesEntry.CopyToOutput.ToString());
548-
writer.WriteAttributeStringIfNotNull("flatten", contentFilesEntry.Flatten.ToString());
547+
writer.WriteAttributeStringIfNotNull("copyToOutput", contentFilesEntry.CopyToOutput);
548+
writer.WriteAttributeStringIfNotNull("flatten", contentFilesEntry.Flatten);
549549
writer.WriteAttributeStringIfNotNull("buildAction", contentFilesEntry.BuildAction);
550550
writer.WriteEndElement();
551551
}

0 commit comments

Comments
 (0)