Skip to content

Commit a7af855

Browse files
committed
refactor tests to provide details on failure
1 parent 64a8cb7 commit a7af855

File tree

8 files changed

+185
-126
lines changed

8 files changed

+185
-126
lines changed

test/ICSharpCode.SharpZipLib.Tests/TestSupport/Utils.cs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.IO;
44
using System.Linq;
5-
using System.Text;
65
using System.Threading.Tasks;
76

87
namespace ICSharpCode.SharpZipLib.Tests.TestSupport
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System;
21
using ICSharpCode.SharpZipLib.Zip;
3-
using System.IO;
2+
using NUnit.Framework.Constraints;
43
using NUnit.Framework;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
57

68
namespace ICSharpCode.SharpZipLib.Tests.TestSupport
79
{
@@ -12,7 +14,13 @@ internal static class ZipTesting
1214
{
1315
public static void AssertValidZip(Stream stream, string password = null, bool usesAes = true)
1416
{
15-
Assert.That(TestArchive(stream, password), "Archive did not pass ZipFile.TestArchive");
17+
using var zipFile = new ZipFile(stream)
18+
{
19+
IsStreamOwner = false,
20+
Password = password,
21+
};
22+
23+
Assert.That(zipFile, Does.PassTestArchive());
1624

1725
if (!string.IsNullOrEmpty(password) && usesAes)
1826
{
@@ -30,37 +38,86 @@ public static void AssertValidZip(Stream stream, string password = null, bool us
3038
}
3139
}, "Archive could not be read by ZipInputStream");
3240
}
41+
}
42+
43+
public class TestArchiveReport
44+
{
45+
internal const string PassingArchive = "Passing Archive";
46+
47+
readonly List<string> _messages = new List<string>();
48+
public void HandleTestResults(TestStatus status, string message)
49+
{
50+
if (string.IsNullOrWhiteSpace(message)) return;
51+
_messages.Add(message);
52+
}
3353

34-
/// <summary>
35-
/// Tests the archive.
36-
/// </summary>
37-
/// <param name="data">The data.</param>
38-
/// <param name="password">The password.</param>
39-
/// <returns></returns>
40-
public static bool TestArchive(byte[] data, string password = null)
54+
public override string ToString() => _messages.Any() ? string.Join(", ", _messages) : PassingArchive;
55+
}
56+
57+
public class PassesTestArchiveConstraint : Constraint
58+
{
59+
private readonly string _password;
60+
private readonly bool _testData;
61+
62+
public PassesTestArchiveConstraint(string password = null, bool testData = true)
4163
{
42-
using var ms = new MemoryStream(data);
43-
return TestArchive(new MemoryStream(data), password);
64+
_password = password;
65+
_testData = testData;
4466
}
4567

46-
/// <summary>
47-
/// Tests the archive.
48-
/// </summary>
49-
/// <param name="stream">The data.</param>
50-
/// <param name="password">The password.</param>
51-
/// <returns>true if archive tests ok; false otherwise.</returns>
52-
public static bool TestArchive(Stream stream, string password = null)
68+
public override string Description => TestArchiveReport.PassingArchive;
69+
70+
public override ConstraintResult ApplyTo<TActual>(TActual actual)
5371
{
54-
using var zipFile = new ZipFile(stream)
72+
MemoryStream ms = null;
73+
try
5574
{
56-
IsStreamOwner = false,
57-
Password = password,
58-
};
59-
60-
return zipFile.TestArchive(true, TestStrategy.FindAllErrors, (status, message) =>
75+
if (!(actual is ZipFile zipFile))
76+
{
77+
if (!(actual is byte[] rawArchive))
78+
{
79+
return new ConstraintResult(this, actual, ConstraintStatus.Failure);
80+
}
81+
82+
ms = new MemoryStream(rawArchive);
83+
zipFile = new ZipFile(ms){Password = _password};
84+
}
85+
86+
var report = new TestArchiveReport();
87+
88+
return new ConstraintResult(
89+
this, report, zipFile.TestArchive(
90+
_testData,
91+
TestStrategy.FindAllErrors,
92+
report.HandleTestResults
93+
)
94+
? ConstraintStatus.Success
95+
: ConstraintStatus.Failure);
96+
}
97+
finally
6198
{
62-
if (!string.IsNullOrWhiteSpace(message)) TestContext.Out.WriteLine(message);
63-
});
99+
ms?.Dispose();
100+
}
64101
}
65102
}
103+
104+
public static class ZipTestingConstraintExtensions
105+
{
106+
public static IResolveConstraint PassTestArchive(this ConstraintExpression expression, string password = null, bool testData = true)
107+
{
108+
var constraint = new PassesTestArchiveConstraint(password, testData);
109+
expression.Append(constraint);
110+
return constraint;
111+
}
112+
}
113+
114+
/// <inheritdoc />
115+
public class Does: NUnit.Framework.Does
116+
{
117+
public static IResolveConstraint PassTestArchive(string password = null, bool testData = true)
118+
=> new PassesTestArchiveConstraint(password, testData);
119+
120+
public static IResolveConstraint PassTestArchive(bool testData)
121+
=> new PassesTestArchiveConstraint(password: null, testData);
122+
}
66123
}

test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs

+33-32
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Linq;
88
using System.Text;
9+
using Does = ICSharpCode.SharpZipLib.Tests.TestSupport.Does;
910
using TimeSetting = ICSharpCode.SharpZipLib.Zip.ZipEntryFactory.TimeSetting;
1011

1112
namespace ICSharpCode.SharpZipLib.Tests.Zip
@@ -40,7 +41,7 @@ public void Basics()
4041
ZipEntry entry = zf[0];
4142
Assert.AreEqual(tempName1, entry.Name);
4243
Assert.AreEqual(1, entry.Size);
43-
Assert.IsTrue(zf.TestArchive(true));
44+
Assert.That(zf, Does.PassTestArchive());
4445

4546
zf.Close();
4647
}
@@ -128,7 +129,7 @@ public void CreateEmptyDirectories(string password)
128129
var folderEntry = zipFile.GetEntry("floyd/");
129130
Assert.That(folderEntry.IsDirectory, Is.True, "The entry must be a folder");
130131

131-
Assert.IsTrue(zipFile.TestArchive(testData: true));
132+
Assert.That(zipFile, Does.PassTestArchive());
132133
}
133134
}
134135
}
@@ -166,14 +167,15 @@ public void ContentEqualAfterAfterArchived([Values(0, 1, 64)]int contentSize)
166167
public void Encryption(ZipEncryptionMethod encryptionMethod)
167168
{
168169
const string tempName1 = "a.dat";
170+
const int tempSize = 1;
169171

170172
var target = new MemoryStream();
171173

172174
string tempFilePath = GetTempFilePath();
173175
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");
174176

175177
string addFile = Path.Combine(tempFilePath, tempName1);
176-
MakeTempFile(addFile, 1);
178+
MakeTempFile(addFile, tempSize);
177179

178180
try
179181
{
@@ -189,17 +191,13 @@ public void Encryption(ZipEncryptionMethod encryptionMethod)
189191
using (ZipFile zf = new ZipFile(archive))
190192
{
191193
zf.Password = "Ahoy";
192-
Assert.AreEqual(1, zf.Count);
193-
ZipEntry entry = zf[0];
194-
Assert.AreEqual(tempName1, entry.Name);
195-
Assert.AreEqual(1, entry.Size);
196-
Assert.IsTrue(zf.TestArchive(true, TestStrategy.FindFirstError, (status, message) =>
197-
{
198-
if(!string.IsNullOrEmpty(message)) {
199-
Console.WriteLine($"{message} ({status.Entry?.Name ?? "-"})");
200-
}
201-
}));
202-
Assert.IsTrue(entry.IsCrypted);
194+
Assert.That(zf.Count, Is.EqualTo(1));
195+
var entry = zf[0];
196+
Assert.That(entry.Name, Is.EqualTo(tempName1));
197+
Assert.That(entry.Size, Is.EqualTo(tempSize));
198+
Assert.That(entry.IsCrypted);
199+
200+
Assert.That(zf, Does.PassTestArchive());
203201

204202
switch (encryptionMethod)
205203
{
@@ -363,14 +361,15 @@ public void ExtractExceptions()
363361
public void ReadingOfLockedDataFiles()
364362
{
365363
const string tempName1 = "a.dat";
364+
const int tempSize = 1;
366365

367366
var target = new MemoryStream();
368367

369368
string tempFilePath = GetTempFilePath();
370369
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");
371370

372371
string addFile = Path.Combine(tempFilePath, tempName1);
373-
MakeTempFile(addFile, 1);
372+
MakeTempFile(addFile, tempSize);
374373

375374
try
376375
{
@@ -383,11 +382,11 @@ public void ReadingOfLockedDataFiles()
383382
var archive = new MemoryStream(target.ToArray());
384383
using (ZipFile zf = new ZipFile(archive))
385384
{
386-
Assert.AreEqual(1, zf.Count);
387-
ZipEntry entry = zf[0];
388-
Assert.AreEqual(tempName1, entry.Name);
389-
Assert.AreEqual(1, entry.Size);
390-
Assert.IsTrue(zf.TestArchive(true));
385+
Assert.That(zf.Count, Is.EqualTo(1));
386+
var entry = zf[0];
387+
Assert.That(entry.Name, Is.EqualTo(tempName1));
388+
Assert.That(entry.Size, Is.EqualTo(tempSize));
389+
Assert.That(zf, Does.PassTestArchive());
391390

392391
zf.Close();
393392
}
@@ -404,14 +403,15 @@ public void ReadingOfLockedDataFiles()
404403
public void NonAsciiPasswords()
405404
{
406405
const string tempName1 = "a.dat";
406+
const int tempSize = 1;
407407

408408
var target = new MemoryStream();
409409

410410
string tempFilePath = GetTempFilePath();
411411
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");
412412

413413
string addFile = Path.Combine(tempFilePath, tempName1);
414-
MakeTempFile(addFile, 1);
414+
MakeTempFile(addFile, tempSize);
415415

416416
string password = "abc\u0066\u0393";
417417
try
@@ -425,12 +425,12 @@ public void NonAsciiPasswords()
425425
using (ZipFile zf = new ZipFile(archive))
426426
{
427427
zf.Password = password;
428-
Assert.AreEqual(1, zf.Count);
429-
ZipEntry entry = zf[0];
430-
Assert.AreEqual(tempName1, entry.Name);
431-
Assert.AreEqual(1, entry.Size);
432-
Assert.IsTrue(zf.TestArchive(true));
433-
Assert.IsTrue(entry.IsCrypted);
428+
Assert.That(zf.Count, Is.EqualTo(1));
429+
var entry = zf[0];
430+
Assert.That(entry.Name, Is.EqualTo(tempName1));
431+
Assert.That(entry.Size, Is.EqualTo(tempSize));
432+
Assert.That(zf, Does.PassTestArchive());
433+
Assert.That(entry.IsCrypted);
434434
}
435435
}
436436
finally
@@ -636,10 +636,11 @@ public void SetDirectoryModifiedDate()
636636
public void CreateZipShouldLeaveOutputStreamOpenIfRequested(bool leaveOpen)
637637
{
638638
const string tempFileName = "a(2).dat";
639+
const int tempSize = 16;
639640

640641
using var tempFolder = Utils.GetTempDir();
641642
// Create test input file
642-
tempFolder.CreateDummyFile(tempFileName, size: 16);
643+
tempFolder.CreateDummyFile(tempFileName, tempSize);
643644

644645
// Create the zip with fast zip
645646
var target = new TrackedMemoryStream();
@@ -653,11 +654,11 @@ public void CreateZipShouldLeaveOutputStreamOpenIfRequested(bool leaveOpen)
653654
// Check that the file contents are correct in both cases
654655
var archive = new MemoryStream(target.ToArray());
655656
using var zf = new ZipFile(archive);
656-
Assert.AreEqual(expected: 1, zf.Count);
657+
Assert.That(zf.Count, Is.EqualTo(1));
657658
var entry = zf[0];
658-
Assert.AreEqual(tempFileName, entry.Name);
659-
Assert.AreEqual(expected: 16, entry.Size);
660-
Assert.IsTrue(zf.TestArchive(testData: true));
659+
Assert.That(entry.Name, Is.EqualTo(tempFileName));
660+
Assert.That(entry.Size, Is.EqualTo(tempSize));
661+
Assert.That(zf, Does.PassTestArchive());
661662
}
662663

663664
[Category("Zip")]

0 commit comments

Comments
 (0)