Skip to content

Commit b409eb2

Browse files
authored
ZipDirectory task supports CompressionLevel (#11975)
1 parent 232755c commit b409eb2

17 files changed

+277
-19
lines changed

src/MSBuild/MSBuild/Microsoft.Build.CommonTypes.xsd

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6287,9 +6287,24 @@ elementFormDefault="qualified">
62876287
<xs:complexType>
62886288
<xs:complexContent>
62896289
<xs:extension base="msb:TaskType">
6290+
<xs:attribute name="CompressionLevel">
6291+
<xs:annotation>
6292+
<xs:documentation>
6293+
<!-- _locID_text="CompressionLevel" _locComment="" -->Specify the compression level to apply. Possible values are Optimal, Fastest, NoCompression and SmallestSize. In the .NET Framework version of MSBuild, the SmallestSize option is unavailable. Using it will on .NET Framework will log warning MSB3945 and use the default compression level instead.
6294+
</xs:documentation>
6295+
</xs:annotation>
6296+
<xs:simpleType>
6297+
<xs:restriction base="xs:string">
6298+
<xs:enumeration value="Optimal" />
6299+
<xs:enumeration value="Fastest" />
6300+
<xs:enumeration value="NoCompression" />
6301+
<xs:enumeration value="SmallestSize" />
6302+
</xs:restriction>
6303+
</xs:simpleType>
6304+
</xs:attribute>
62906305
<xs:attribute name="DestinationFile" use="required" />
62916306
<xs:attribute name="Overwrite" type="msb:boolean" />
6292-
<xs:attribute name="SourceDirectory" type="msb:boolean" />
6307+
<xs:attribute name="SourceDirectory" use="required" />
62936308
</xs:extension>
62946309
</xs:complexContent>
62956310
</xs:complexType>

src/Tasks.UnitTests/ZipDirectory_Tests.cs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,31 @@
1111
using Shouldly;
1212
using Xunit;
1313

14-
#nullable disable
15-
1614
namespace Microsoft.Build.Tasks.UnitTests
1715
{
1816
public class ZipDirectory_Tests
1917
{
2018
private readonly MockEngine _mockEngine = new MockEngine();
2119

22-
[Fact]
23-
public void CanZipDirectory()
20+
public enum CompressionSupportKind
21+
{
22+
NotSupported,
23+
NotSupportedOnNetFramework,
24+
Supported,
25+
}
26+
27+
[Theory]
28+
[InlineData(null, CompressionSupportKind.Supported)]
29+
[InlineData("Optimal", CompressionSupportKind.Supported)]
30+
[InlineData("Fastest", CompressionSupportKind.Supported)]
31+
[InlineData("NoCompression", CompressionSupportKind.Supported)]
32+
#if NET
33+
[InlineData("SmallestSize", CompressionSupportKind.Supported)]
34+
#elif NETFRAMEWORK
35+
[InlineData("SmallestSize", CompressionSupportKind.NotSupportedOnNetFramework)]
36+
#endif
37+
[InlineData("RandomUnsupportedValue", CompressionSupportKind.NotSupported)]
38+
public void CanZipDirectory(string? compressionLevel, CompressionSupportKind compressionSupportKind)
2439
{
2540
using (TestEnvironment testEnvironment = TestEnvironment.Create())
2641
{
@@ -34,34 +49,51 @@ public void CanZipDirectory()
3449
ZipDirectory zipDirectory = new ZipDirectory
3550
{
3651
BuildEngine = _mockEngine,
52+
CompressionLevel = compressionLevel,
3753
DestinationFile = new TaskItem(zipFilePath),
38-
SourceDirectory = new TaskItem(sourceFolder.Path)
54+
SourceDirectory = new TaskItem(sourceFolder.Path),
3955
};
4056

4157
zipDirectory.Execute().ShouldBeTrue(_mockEngine.Log);
4258

4359
_mockEngine.Log.ShouldContain(sourceFolder.Path, customMessage: _mockEngine.Log);
4460
_mockEngine.Log.ShouldContain(zipFilePath, customMessage: _mockEngine.Log);
4561

62+
if (compressionSupportKind == CompressionSupportKind.NotSupported)
63+
{
64+
_mockEngine.Log.ShouldContain("MSB3944", customMessage: _mockEngine.Log);
65+
}
66+
else if (compressionSupportKind == CompressionSupportKind.NotSupportedOnNetFramework)
67+
{
68+
_mockEngine.Log.ShouldContain("MSB3945", customMessage: _mockEngine.Log);
69+
}
70+
else
71+
{
72+
Assert.Equal(CompressionSupportKind.Supported, compressionSupportKind);
73+
74+
// Should not contain any warnings between MSB3941 - MSB3950
75+
_mockEngine.Log.ShouldNotContain("MSB394", customMessage: _mockEngine.Log); // Prefix
76+
_mockEngine.Log.ShouldNotContain("MSB3950", customMessage: _mockEngine.Log);
77+
}
78+
4679
using (FileStream stream = new FileStream(zipFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
4780
using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read))
4881
{
4982
archive.Entries
5083
.Select(i => i.FullName)
5184
.ToList()
5285
.ShouldBe(
53-
new List<string>
54-
{
86+
[
5587
"6DE6060259C44DB6B145159376751C22.txt",
5688
"CDA3DD8C25A54A7CAC638A444CB1EAD0.txt"
57-
},
89+
],
5890
ignoreOrder: true);
5991
}
6092
}
6193
}
6294

6395
[Fact]
64-
public void CanOvewriteExistingFile()
96+
public void CanOverwriteExistingFile()
6597
{
6698
using (TestEnvironment testEnvironment = TestEnvironment.Create())
6799
{
@@ -92,11 +124,10 @@ public void CanOvewriteExistingFile()
92124
.Select(i => i.FullName)
93125
.ToList()
94126
.ShouldBe(
95-
new List<string>
96-
{
127+
[
97128
"F1C22D660B0D4DAAA296C1B980320B03.txt",
98129
"AA825D1CB154492BAA58E1002CE1DFEB.txt"
99-
},
130+
],
100131
ignoreOrder: true);
101132
}
102133
}

src/Tasks/Resources/Strings.resx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,6 +2923,14 @@
29232923
<value>MSB3943: Failed to zip directory "{0}" to file "{1}". {2} {3}</value>
29242924
<comment>{StrBegin="MSB3943: "}</comment>
29252925
</data>
2926+
<data name="ZipDirectory.ErrorInvalidCompressionLevel">
2927+
<value>MSB3944: Unsupported compression level '{0}'. Using default compression level instead.</value>
2928+
<comment>{StrBegin="MSB3944: "}</comment>
2929+
</data>
2930+
<data name="ZipDirectory.WarningCompressionLevelUnsupportedOnFramework">
2931+
<value>MSB3945: Compression level '{0}' is not supported on the .NET Framework version of MSBuild. Using default compression level instead.</value>
2932+
<comment>{StrBegin="MSB3945: "}</comment>
2933+
</data>
29262934
<data name="ZipDirectory.Comment">
29272935
<value>Zipping directory "{0}" to "{1}".</value>
29282936
</data>

src/Tasks/Resources/xlf/Strings.cs.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Tasks/Resources/xlf/Strings.de.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Tasks/Resources/xlf/Strings.es.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Tasks/Resources/xlf/Strings.fr.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Tasks/Resources/xlf/Strings.it.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Tasks/Resources/xlf/Strings.ja.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Tasks/Resources/xlf/Strings.ko.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)