5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
32 _python_ = TestSCons._python_
34 test = TestSCons.TestSCons()
39 zip = test.where_is('zip')
41 x = "Python version has no 'ziplib' module nor 'zip' utility; skipping tests.\n"
46 test.write('myzip.py', r"""\
50 def process(outfile, name):
51 if os.path.isdir(name):
52 ## TODO 2.5: the next three lines can be replaced by
53 #for entry in sorted(os.listdir(name)):
54 list = os.listdir(name)
57 process(outfile, os.path.join(name, entry))
59 outfile.write(open(name, 'rb').read())
60 outfile = open(sys.argv[1], 'wb')
61 for infile in sys.argv[2:]:
62 process(outfile, infile)
67 test.write('SConstruct', """
68 env = Environment(tools = ['zip'],
69 ZIPCOM = r'%(_python_)s myzip.py $TARGET $SOURCES')
70 env.Zip(target = 'aaa.zip', source = ['file1', 'file2'])
71 env.Zip(target = 'aaa.zip', source = 'file3')
72 env.Zip(target = 'bbb', source = 'sub1')
73 env.Zip(target = 'bbb', source = 'file4')
76 test.write('file1', "file1\n")
77 test.write('file2', "file2\n")
78 test.write('file3', "file3\n")
79 test.write('file4', "file4\n")
81 test.write(['sub1', 'file5'], "sub1/file5\n")
82 test.write(['sub1', 'file6'], "sub1/file6\n")
84 test.run(arguments = 'aaa.zip', stderr = None)
86 test.fail_test(test.read('aaa.zip') != "file1\nfile2\nfile3\n")
88 test.run(arguments = 'bbb.zip', stderr = None)
90 test.fail_test(test.read('bbb.zip') != "sub1/file5\nsub1/file6\nfile4\n")
98 zf = zipfile.ZipFile(fname, 'r')
99 return [x.filename for x in zf.infolist()]
103 zip = test.detect('ZIP', 'zip')
104 unzip = test.where_is('unzip')
106 def files(fname, test=test, unzip=unzip):
107 test.run(program = unzip, arguments = "-l -qq %s" % fname)
108 lines = test.stdout().split("\n")[:-1]
110 return line.split()[-1]
111 return list(map(lastword, lines))
115 marker_out = test.workpath('marker.out').replace('\\', '\\\\')
117 test.write('SConstruct', """\
118 def marker(target, source, env):
119 open(r'%s', 'wb').write("marker\\n")
121 zipcom = f1.Dictionary('ZIPCOM')
122 if not isinstance(zipcom, list):
124 f2 = Environment(ZIPCOM = [Action(marker)] + zipcom)
125 f3 = Environment(ZIPSUFFIX = '.xyzzy')
126 f1.Zip(target = 'f1.zip', source = ['file10', 'file11'])
127 f1.Zip(target = 'f1.zip', source = 'file12')
128 f2.Zip(target = 'f2.zip', source = ['file13', 'file14'])
129 f2.Zip(target = 'f2.zip', source = 'file15')
130 f3.Zip(target = 'f3', source = 'file16')
131 f3.Zip(target = 'f3', source = ['file17', 'file18'])
134 sources = ['file10', 'file11', 'file12', 'file13', 'file14', 'file15']
135 f1.Zip(target = 'f4.zip', source = sources)
136 f1.Zip(target = 'f4stored.zip', source = sources,
137 ZIPCOMPRESSION = zipfile.ZIP_STORED)
138 f1.Zip(target = 'f4deflated.zip', source = sources,
139 ZIPCOMPRESSION = zipfile.ZIP_DEFLATED)
144 for f in ['file10', 'file11', 'file12',
145 'file13', 'file14', 'file15',
146 'file16', 'file17', 'file18']:
147 test.write(f, f + "\n")
149 test.run(arguments = 'f1.zip', stderr = None)
151 test.fail_test(os.path.exists(test.workpath('marker.out')))
153 test.fail_test(not os.path.exists(test.workpath('f1.zip')))
155 test.run(arguments = 'f2.zip', stderr = None)
157 test.fail_test(test.read('marker.out') != 'marker\n')
159 test.fail_test(not os.path.exists(test.workpath('f2.zip')))
161 test.run(arguments = '.', stderr = None)
163 test.fail_test(os.path.exists(test.workpath('f3.zip')))
164 test.fail_test(not os.path.exists(test.workpath('f3.xyzzy')))
166 test.fail_test(files("f1.zip") != ['file10', 'file11', 'file12'])
168 test.fail_test(files("f2.zip") != ['file13', 'file14', 'file15'])
170 test.fail_test(files("f3.xyzzy") != ['file16', 'file17', 'file18'])
173 f4_size = os.stat('f4.zip')[stat.ST_SIZE]
174 f4stored_size = os.stat('f4stored.zip')[stat.ST_SIZE]
175 f4deflated_size = os.stat('f4deflated.zip')[stat.ST_SIZE]
177 test.fail_test(f4_size != f4deflated_size)
178 test.fail_test(f4stored_size == f4deflated_size)
184 # indent-tabs-mode:nil
186 # vim: set expandtab tabstop=4 shiftwidth=4: