This repository was archived by the owner on Oct 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathZipTest.cs
More file actions
254 lines (196 loc) · 10.2 KB
/
ZipTest.cs
File metadata and controls
254 lines (196 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Ionic.Zip;
using Microsoft.Build.Utilities;
using NUnit.Framework;
namespace MSBuild.Community.Tasks.Tests
{
/// <summary>
/// Summary description for ZipTest
/// </summary>
[TestFixture]
public class ZipTest
{
public const string ZIP_FILE_NAME = @"MSBuild.Community.Tasks.zip";
public const string ZIP_WITH_FOLDERS_FILE_NAME = @"MSBuild.Community.Tasks.WithFolders.zip";
public const string ZIP_WITH_RELATIVE_FILE_NAME = @"MSBuild.Community.Tasks.WithReltive.zip";
public const string ZIP_128KB_FILE_NAME = @"MSBuild.Community.Tasks.128KB.zip";
[Test(Description = "Zip files into a zip archive")]
public void ZipExecute()
{
var task = new Zip();
task.BuildEngine = new MockBuild();
string testDir = TaskUtility.TestDirectory;
string prjRootPath = TaskUtility.GetProjectRootDirectory(true);
string workingDir = Path.Combine(prjRootPath, @"Source\MSBuild.Community.Tasks.Tests");
string[] files = Directory.GetFiles(workingDir, "*.*", SearchOption.TopDirectoryOnly);
TaskItem[] items = TaskUtility.StringArrayToItemArray(files);
task.Files = items;
task.WorkingDirectory = workingDir;
task.ZipFileName = Path.Combine(testDir, ZIP_FILE_NAME);
if (File.Exists(task.ZipFileName))
File.Delete(task.ZipFileName);
Assert.IsTrue(task.Execute(), "Execute Failed");
Assert.IsTrue(File.Exists(task.ZipFileName), "Zip file not found");
}
[Test(Description = "Zip files and empty folders into a zip archive")]
public void ZipExecuteWithEmptyFolders()
{
var task = new Zip();
task.BuildEngine = new MockBuild();
string testDir = TaskUtility.TestDirectory;
string prjRootPath = TaskUtility.GetProjectRootDirectory(true);
string workingDir = Path.Combine(prjRootPath, @"Source\MSBuild.Community.Tasks.Tests");
string[] files = Directory.GetFiles(workingDir, "*.*", SearchOption.TopDirectoryOnly);
string[] directories = Directory.GetDirectories(workingDir, "*.*", SearchOption.TopDirectoryOnly);
string[] filesFromXmlDirectory = new string[0]; //Directory.GetFiles(workingDir + "\\" + "Xml", "*.*", SearchOption.TopDirectoryOnly);
var filesAndDirectories = new string[files.Length + directories.Length + filesFromXmlDirectory.Length];
files.CopyTo(filesAndDirectories, 0);
directories.CopyTo(filesAndDirectories, files.Length + filesFromXmlDirectory.Length);
filesFromXmlDirectory.CopyTo(filesAndDirectories, files.Length);
TaskItem[] items = TaskUtility.StringArrayToItemArray(filesAndDirectories);
task.Files = items;
task.WorkingDirectory = workingDir;
task.ZipFileName = Path.Combine(testDir, ZIP_WITH_FOLDERS_FILE_NAME);
if (File.Exists(task.ZipFileName))
File.Delete(task.ZipFileName);
Assert.IsTrue(task.Execute(), "Execute Failed");
Assert.IsTrue(File.Exists(task.ZipFileName), "Zip file not found");
}
[Test]
public void ZipNoRoot()
{
var task = new Zip();
task.BuildEngine = new MockBuild();
string testDir = TaskUtility.TestDirectory;
string prjRootPath = TaskUtility.GetProjectRootDirectory(true);
string workingDir = Path.Combine(prjRootPath, @"Source\MSBuild.Community.Tasks.Tests");
string[] files = Directory.GetFiles(workingDir, "*.*", SearchOption.AllDirectories);
TaskItem[] items = TaskUtility.StringArrayToItemArray(files);
task.Files = items;
task.ZipFileName = Path.Combine(testDir, ZIP_FILE_NAME);
if (File.Exists(task.ZipFileName))
File.Delete(task.ZipFileName);
Assert.IsTrue(task.Execute(), "Execute Failed");
Assert.IsTrue(File.Exists(task.ZipFileName), "Zip file not found");
}
[Test(Description = "Zip files with relative path")]
public void ZipRelativeExecute()
{
var task = new Zip();
task.BuildEngine = new MockBuild();
string testDir = TaskUtility.TestDirectory;
string prjRootPath = TaskUtility.GetProjectRootDirectory(true);
string workingDir = Path.Combine(prjRootPath, @"Source\MSBuild.Community.Tasks.Tests");
List<string> files = Directory.GetFiles(workingDir, "*.*", SearchOption.TopDirectoryOnly).ToList();
files.Add(Path.Combine(workingDir, "..\\..\\readme.md"));
TaskItem[] items = TaskUtility.StringArrayToItemArray(files.ToArray());
task.Files = items;
task.WorkingDirectory = workingDir;
task.ZipFileName = Path.Combine(testDir, ZIP_WITH_RELATIVE_FILE_NAME);
if (File.Exists(task.ZipFileName))
File.Delete(task.ZipFileName);
Assert.IsTrue(task.Execute(), "Execute Failed");
Assert.IsTrue(ZipFile.Read(task.ZipFileName).ContainsEntry("readme.md"), "The zip doesnt contains the readme.md file");
Assert.IsTrue(File.Exists(task.ZipFileName), "Zip file not found");
}
[Test(Description = "Zip 9 * 128kb with parallel compression")]
public void ZipWithParallelCompression()
{
var task = new Zip();
task.BuildEngine = new MockBuild();
string testDir = TaskUtility.TestDirectory;
string testFile = Path.Combine(testDir, "zip-128kb-test-1.dat");
const int numberOfBytes = 1024*128*9;
if (File.Exists(testFile)) File.Delete(testFile);
CreateTestFile(numberOfBytes, testFile);
task.Files = new[] {new TaskItem(testFile)};
task.WorkingDirectory = testDir;
task.ParallelCompression = true;
task.ZipFileName = Path.Combine(testDir, ZIP_128KB_FILE_NAME);
if (File.Exists(task.ZipFileName)) File.Delete(task.ZipFileName);
// First zip up the file
Assert.IsTrue(task.Execute(), "Execute Failed");
Assert.IsTrue(File.Exists(task.ZipFileName), "Zip file not found");
// Then delete the original, and try to unzip the file
File.Delete(testFile);
Unzip unzipTask = new Unzip();
unzipTask.BuildEngine = new MockBuild();
unzipTask.ZipFileName = task.ZipFileName;
unzipTask.TargetDirectory = testDir;
Assert.IsTrue(unzipTask.Execute());
}
[Test(Description = "Zip file update instead of overwrite")]
public void ZipFileUpdate()
{
var task = new Zip();
task.BuildEngine = new MockBuild();
string testDir = TaskUtility.TestDirectory;
const string fileName = "zip-128kb-test-1.dat";
string testFile = Path.Combine(testDir, fileName);
const int numberOfBytes = 1024 * 128 * 9;
if (File.Exists(testFile))
File.Delete(testFile);
CreateTestFile(numberOfBytes, testFile);
task.Files = new[] { new TaskItem(testFile) };
task.WorkingDirectory = testDir;
task.ZipFileName = Path.Combine(testDir, ZIP_FILE_NAME);
task.Overwrite = false;
if (File.Exists(task.ZipFileName))
File.Delete(task.ZipFileName);
// First zip up the file
Assert.IsTrue(task.Execute(), "Execute Failed");
Assert.IsTrue(File.Exists(task.ZipFileName), "Zip file not found");
// Then delete the original, and try to unzip the file
File.Delete(testFile);
Unzip unzipTask = new Unzip();
unzipTask.BuildEngine = new MockBuild();
unzipTask.ZipFileName = task.ZipFileName;
unzipTask.TargetDirectory = testDir;
Assert.IsTrue(unzipTask.Execute());
long length = new FileInfo(Path.Combine(testDir, fileName)).Length;
Assert.AreEqual(numberOfBytes, length, "File length does not match");
if (File.Exists(testFile))
File.Delete(testFile);
CreateTestFile(numberOfBytes * 2, testFile);
// Update the zip file
Assert.IsTrue(task.Execute(), "Execute Failed");
Assert.IsTrue(File.Exists(task.ZipFileName), "Zip file not found");
// Then delete the original, and try to unzip the file
File.Delete(testFile);
unzipTask = new Unzip();
unzipTask.BuildEngine = new MockBuild();
unzipTask.ZipFileName = task.ZipFileName;
unzipTask.TargetDirectory = testDir;
Assert.IsTrue(unzipTask.Execute());
length = new FileInfo(Path.Combine(testDir, fileName)).Length;
Assert.AreEqual(numberOfBytes * 2, length, "File length does not match");
if (File.Exists(testFile))
File.Delete(testFile);
CreateTestFile(numberOfBytes, testFile);
// Update the zip file again
Assert.IsTrue(task.Execute(), "Execute Failed");
Assert.IsTrue(File.Exists(task.ZipFileName), "Zip file not found");
// Then delete the original, and try to unzip the file
File.Delete(testFile);
unzipTask = new Unzip();
unzipTask.BuildEngine = new MockBuild();
unzipTask.ZipFileName = task.ZipFileName;
unzipTask.TargetDirectory = testDir;
Assert.IsTrue(unzipTask.Execute());
length = new FileInfo(Path.Combine(testDir, fileName)).Length;
Assert.AreEqual(numberOfBytes, length, "File length does not match");
}
public void CreateTestFile(int fileSize, string filePath)
{
byte[] testFileBytes = new byte[fileSize];
new Random().NextBytes(testFileBytes);
using (FileStream filestream = File.Create(filePath))
{
filestream.Write(testFileBytes, 0, fileSize);
}
}
}
}