File: ZipTaskTest.cs

package info (click to toggle)
nant 0.85.dfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 8,364 kB
  • ctags: 6,756
  • sloc: cs: 50,420; sh: 1,014; makefile: 87; cpp: 70; xml: 40
file content (266 lines) | stat: -rw-r--r-- 10,952 bytes parent folder | download | duplicates (3)
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
255
256
257
258
259
260
261
262
263
264
265
266
// NAnt - A .NET build tool
// Copyright (C) 2002 Scott Hernandez (ScottHernandez@hotmail.com)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// Scott Hernandez (ScottHernandez@hotmail.com)

using System;
using System.IO;
using System.Text;
using System.Xml;

using NUnit.Framework;

using NAnt.Core;

using Tests.NAnt.Core;

namespace Tests.NAnt.Compression.Tasks {
    [TestFixture]
    public class ZipTaskTest : BuildTestBase {
        /// <summary>
        /// Test to make sure a simple zip file can be created.
        /// </summary>
        [Test]
        public void Test_SimpleZip() {
            const string projectXML = @"<?xml version='1.0'?>
                <project>
                    <zip zipfile='test.zip'>
                        <fileset basedir='src'>
                            <include name='**'/>
                        </fileset>
                    </zip>
                </project>";

            CreateTempDir("src");
            CreateTempFile(Path.Combine("src", "temp1.file"),"hello");
            RunBuild(projectXML);
            Assert.IsTrue(File.Exists(Path.Combine(TempDirName,"test.zip")),
                "Zip File not created.");
        }

        /// <summary>
        /// Test to make sure an empty zip file can be created.
        /// </summary>
        [Test]
        public void Test_CreateEmptyZipFile() {
            const string projectXML = @"<?xml version='1.0'?>
                <project>
                    <zip zipfile='test.zip' />
                </project>";

            RunBuild(projectXML);
            Assert.IsTrue(File.Exists(Path.Combine(TempDirName, "test.zip")),
                "Zip File not created.");
        }

        /// <summary>
        /// Ensures a <see cref="BuildException" /> is thrown when attempting 
        /// to add a non-existing file to the zip file.
        /// </summary>
        [Test]
        public void Test_NonExistingFile() {
            const string projectXML = @"<?xml version='1.0'?>
                <project>
                    <zip zipfile='test.zip'>
                        <fileset prefix='bin'>
                            <include name='whatever/test.txt' asis='true' />
                        </fileset>
                    </zip>
                </project>";

            try {
                RunBuild(projectXML);
                Assert.Fail("#1");
            } catch (TestBuildException ex) {
                Assert.IsNotNull(ex.InnerException, "#2");
                Assert.IsTrue(ex.InnerException is BuildException, "#3");
                Assert.IsNotNull(ex.InnerException.InnerException, "#4");
                Assert.IsTrue(ex.InnerException.InnerException is BuildException, "#5");

                BuildException be = (BuildException) ex.InnerException.InnerException;
                // error message should contain path of file that does not exist
                Assert.IsTrue(be.RawMessage.IndexOf("whatever/test.txt") != -1, "#6");
            }
        }

        [Test]
        public void Duplicate_Add() {
            const string projectXML = @"<?xml version='1.0'?>
                <project>
                    <zip zipfile='test.zip' duplicate='Add'>
                        <fileset basedir='folder1' prefix='bin'>
                            <include name='test.txt' />
                        </fileset>
                        <fileset basedir='folder2' prefix='bin'>
                            <include name='test.txt' />
                        </fileset>
                    </zip>
                    <unzip zipfile='test.zip' todir='extract' />
                </project>";

            CreateTempDir("folder1");
            CreateTempFile(Path.Combine("folder1", "test.txt"), "folder1");
            CreateTempDir("folder2");
            CreateTempFile(Path.Combine("folder2", "test.txt"), "folder2");

            RunBuild(projectXML);

            string extractDir = Path.Combine(TempDirName, "extract");
            Assert.IsTrue(Directory.Exists(extractDir), "#1");
            string binDir = Path.Combine(extractDir, "bin");
            Assert.IsTrue(Directory.Exists(binDir), "#2");
            string testFile = Path.Combine(binDir, "test.txt");
            Assert.IsTrue(File.Exists(testFile), "#3");

            // finally check whether second entry (from folder2) was indeed
            // added to zip file (and as such extracted from it)
            using (StreamReader sr = new StreamReader(testFile, true)) {
                Assert.AreEqual("folder2", sr.ReadToEnd(), "#4");
                sr.Close();
            }
        }

        /// <summary>
        /// Verifies whether a build error is reported if an invalid value is
        /// specified for the "duplicate" attribute.
        /// </summary>
        [Test]
        [ExpectedException(typeof(TestBuildException))]
        public void Duplicate_Invalid() {
            const string projectXML = @"<?xml version='1.0'?>
                <project>
                    <zip zipfile='test.zip' duplicate='invalid'>
                        <fileset basedir='folder1' prefix='bin'>
                            <include name='test.txt' />
                        </fileset>
                        <fileset basedir='folder2' prefix='bin'>
                            <include name='test.txt' />
                        </fileset>
                    </zip>
                    <unzip zipfile='test.zip' todir='extract' />
                </project>";

            CreateTempDir("folder1");
            CreateTempFile(Path.Combine("folder1", "test.txt"), "folder1");
            CreateTempDir("folder2");
            CreateTempFile(Path.Combine("folder2", "test.txt"), "folder2");

            RunBuild(projectXML);
        }

        /// <summary>
        /// Verifies whether Add is the default value for the "duplicate"
        /// attribute.
        /// </summary>
        [Test]
        public void Duplicate_Add_Default() {
            const string projectXML = @"<?xml version='1.0'?>
                <project>
                    <zip zipfile='test.zip'>
                        <fileset basedir='folder1' prefix='bin'>
                            <include name='test.txt' />
                        </fileset>
                        <fileset basedir='folder2' prefix='bin'>
                            <include name='test.txt' />
                        </fileset>
                    </zip>
                    <unzip zipfile='test.zip' todir='extract' />
                </project>";

            CreateTempDir("folder1");
            CreateTempFile(Path.Combine("folder1", "test.txt"), "folder1");
            CreateTempDir("folder2");
            CreateTempFile(Path.Combine("folder2", "test.txt"), "folder2");

            RunBuild(projectXML);

            string extractDir = Path.Combine(TempDirName, "extract");
            Assert.IsTrue(Directory.Exists(extractDir), "#1");
            string binDir = Path.Combine(extractDir, "bin");
            Assert.IsTrue(Directory.Exists(binDir), "#2");
            string testFile = Path.Combine(binDir, "test.txt");
            Assert.IsTrue(File.Exists(testFile), "#3");

            // finally check whether second entry (from folder2) was indeed
            // added to zip file (and as such extracted from it)
            using (StreamReader sr = new StreamReader(testFile, true)) {
                Assert.AreEqual("folder2", sr.ReadToEnd(), "#4");
                sr.Close();
            }
        }

        [Test]
        public void Duplicate_Preserve() {
            const string projectXML = @"<?xml version='1.0'?>
                <project>
                    <zip zipfile='test.zip' duplicate='Preserve'>
                        <fileset basedir='folder1' prefix='bin'>
                            <include name='test.txt' />
                        </fileset>
                        <fileset basedir='folder2' prefix='bin'>
                            <include name='test.txt' />
                        </fileset>
                    </zip>
                    <unzip zipfile='test.zip' todir='extract' />
                </project>";

            CreateTempDir("folder1");
            CreateTempFile(Path.Combine("folder1", "test.txt"), "folder1");
            CreateTempDir("folder2");
            CreateTempFile(Path.Combine("folder2", "test.txt"), "folder2");

            RunBuild(projectXML);

            string extractDir = Path.Combine(TempDirName, "extract");
            Assert.IsTrue(Directory.Exists(extractDir), "#1");
            string binDir = Path.Combine(extractDir, "bin");
            Assert.IsTrue(Directory.Exists(binDir), "#2");
            string testFile = Path.Combine(binDir, "test.txt");
            Assert.IsTrue(File.Exists(testFile), "#3");

            // finally check whether second entry (from folder2) was indeed
            // added to zip file (and as such extracted from it)
            using (StreamReader sr = new StreamReader(testFile, true)) {
                Assert.AreEqual("folder1", sr.ReadToEnd(), "#4");
                sr.Close();
            }
        }

        [Test]
        [ExpectedException(typeof(TestBuildException))]
        public void Duplicate_Fail() {
            const string projectXML = @"<?xml version='1.0'?>
                <project>
                    <zip zipfile='test.zip' duplicate='Fail'>
                        <fileset basedir='folder1' prefix='bin'>
                            <include name='test.txt' />
                        </fileset>
                        <fileset basedir='folder2' prefix='bin'>
                            <include name='test.txt' />
                        </fileset>
                    </zip>
                </project>";

            CreateTempDir("folder1");
            CreateTempFile(Path.Combine("folder1", "test.txt"), "folder1");
            CreateTempDir("folder2");
            CreateTempFile(Path.Combine("folder2", "test.txt"), "folder2");

            RunBuild(projectXML);
        }
    }
}