Coverage for gws-app/gws/lib/zipx/_test.py: 0%
160 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1"""Tests for the zipx module."""
2import io
3import os
4import zipfile
6import gws
7import gws.test.util as u
8import gws.lib.zipx as zipx
11def test_zip(tmp_path):
12 d = tmp_path / "test"
13 d.mkdir()
14 z = d / 'zip'
15 z2 = d / 'zip2'
16 f = d / 'f1.txt'
17 f.write_text('foobar')
19 ret = zipx.zip_to_path(str(z), str(f), flat=False)
20 ret2 = zipx.zip_to_path(str(z2), str(f), flat=True)
21 assert zipfile.is_zipfile(z)
22 assert sorted(os.listdir(d)) == sorted(['zip', 'zip2', 'f1.txt'])
23 assert ret == 1
24 assert ret2 == 1
25 assert zipx.unzip_path_to_dict(str(z)) != zipx.unzip_path_to_dict(str(z2))
28# test zip with no src
29def test_zip_empty(tmp_path):
30 d = tmp_path / 'test'
31 d.mkdir()
32 z = d / 'zip'
33 ret = zipx.zip_to_path(str(z))
34 assert not os.listdir(str(d))
35 assert ret == 0
38# test dir as src
39def test_zip_dir(tmp_path):
40 d = tmp_path / 'test'
41 d.mkdir()
43 z = d / 'zip'
44 dr = d / 'dir'
45 dr.mkdir()
47 f = dr / 'f1.txt'
48 f.write_text('foo')
49 f2 = dr / 'f2.txt'
50 f2.write_text('bar')
52 ret = zipx.zip_to_path(str(z), str(dr))
53 assert sorted(os.listdir(str(d))) == sorted(['zip', 'dir'])
54 assert ret == 2
57# test zip with empty dir
58def test_zip_dir_empty(tmp_path):
59 d = tmp_path / 'test'
60 d.mkdir()
62 z = d / 'zip'
63 dr = d / 'dir'
64 dr.mkdir()
66 ret = zipx.zip_to_path(str(z), str(dr))
67 assert sorted(os.listdir(str(d))) == sorted(['dir'])
68 assert ret == 0
71def test_zip_to_bytes(tmp_path):
72 d = tmp_path / 'test'
73 d.mkdir()
75 f = d / 'f1.txt'
76 f.write_text('foo')
77 f2 = d / 'f2.txt'
78 f2.write_text('bar')
80 ret = zipx.zip_to_bytes(str(f), str(f2), flat=False)
81 # assert ret = 'someByteString'
82 os.remove(str(f))
83 os.remove(str(f2))
84 zipx.unzip_bytes(ret, str(d), flat=True)
85 assert sorted(os.listdir(str(d))) == sorted(['f2.txt', 'f1.txt'])
88# test zip_to_bytes with no src
89def test_zip_to_bytes_empty(tmp_path):
90 d = tmp_path / 'test'
91 d.mkdir()
93 ret = zipx.zip_to_bytes()
94 assert not os.listdir(str(d))
95 assert not sorted(os.listdir(d))
96 assert ret == b''
99# test zip_to_bytes with dir as src
100def test_zip_to_bytes_dir(tmp_path):
101 d = tmp_path / 'test'
102 d.mkdir()
104 z = d / 'zip'
105 dr = d / 'dir'
106 dr.mkdir()
108 f = dr / 'f1.txt'
109 f.write_text('foo')
110 f2 = dr / 'f2.txt'
111 f2.write_text('bar')
113 ret = zipx.zip_to_bytes(str(dr), flat=False)
114 # assert ret = 'someByteString'
115 zipx.unzip_bytes(ret, str(d), flat=True)
116 assert sorted(os.listdir(str(d))) == sorted(['f1.txt', 'f2.txt', 'dir'])
119# test zip_to_bytes with empty dir
120def test_zip_to_bytes_dir_empty(tmp_path):
121 d = tmp_path / 'test'
122 d.mkdir()
124 dr = d / 'dir'
125 dr.mkdir()
127 ret = zipx.zip_to_bytes(str(dr))
128 assert sorted(os.listdir(d)) == sorted(['dir'])
129 assert ret == b''
132# Has no return although return type is int
133def test_unzip(tmp_path):
134 d = tmp_path / 'test'
135 d.mkdir()
137 z = d / 'zip'
139 f = d / 'f1.txt'
140 f.write_text('foo')
141 f2 = d / 'f2.txt'
142 f2.write_text('bar')
144 zipx.zip_to_path(str(z), str(f), str(f2), flat=True)
145 os.remove(str(f))
146 os.remove(str(f2))
147 ret = zipx.unzip_path(str(z), str(d), flat=False)
148 os.remove(str(z))
149 assert sorted(os.listdir(str(d))) == sorted(['f2.txt', 'f1.txt'])
150 assert ret == 2
153# test for unzipping files into dir containing same named files
154def test_unzip_name(tmp_path):
155 d = tmp_path / 'test'
156 d.mkdir()
158 z = d / 'zip'
160 f = d / 'f1.txt'
161 f.write_text('foo')
162 f2 = d / 'f2.txt'
163 f2.write_text('bar')
165 zipx.zip_to_path(str(z), str(f), str(f2), flat=False)
166 os.remove(str(f))
167 os.remove(str(f2))
168 ret = zipx.unzip_path(str(z), str(d), flat=True)
169 os.remove(str(z))
170 assert sorted(os.listdir(str(d))) == sorted(['f2.txt', 'f1.txt'])
171 assert ret == 2
174def test_unzip_bytes(tmp_path):
175 d = tmp_path / 'test'
176 d.mkdir()
178 z = d / 'zip'
180 f = d / 'f1.txt'
181 f.write_text('foo')
182 f2 = d / 'f2.txt'
183 f2.write_text('bar')
185 z = zipx.zip_to_bytes(str(f), str(f2), flat=False)
186 os.remove(str(f))
187 os.remove(str(f2))
188 ret = zipx.unzip_bytes(z, str(d), flat=True)
189 assert ret == 2
190 assert sorted(os.listdir(str(d))) == sorted(['f1.txt', 'f2.txt'])
193def test_unzip_to_dict(tmp_path):
194 d = tmp_path / 'test'
195 d.mkdir()
197 z = d / 'zip'
199 f = d / 'f1.txt'
200 f.write_text('foo')
201 f2 = d / 'f2.txt'
202 f2.write_text('bar')
204 zipx.zip_to_path(str(z), str(f), str(f2))
205 os.remove(str(f))
206 os.remove(str(f2))
207 ret = zipx.unzip_path_to_dict(str(z), flat=True)
208 assert ret == {'f1.txt': b'foo', 'f2.txt': b'bar'}
209 assert sorted(os.listdir(str(d))) == sorted(['zip'])
212def test_unzip_bytes_to_dict(tmp_path):
213 d = tmp_path / 'test'
214 d.mkdir()
216 z = d / 'zip'
218 f = d / 'f1.txt'
219 f.write_text('foo')
220 byt = zipx.zip_to_bytes(str(f))
221 ret = zipx.unzip_bytes_to_dict(byt)
222 zipx.zip_to_path(str(z), str(f))
223 os.remove(str(f))
224 ret2 = zipx.unzip_path_to_dict(str(z))
225 assert ret == ret2
226 assert sorted(os.listdir(d)) == ['zip']