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

1"""Tests for the zipx module.""" 

2import io 

3import os 

4import zipfile 

5 

6import gws 

7import gws.test.util as u 

8import gws.lib.zipx as zipx 

9 

10 

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') 

18 

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)) 

26 

27 

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 

36 

37 

38# test dir as src 

39def test_zip_dir(tmp_path): 

40 d = tmp_path / 'test' 

41 d.mkdir() 

42 

43 z = d / 'zip' 

44 dr = d / 'dir' 

45 dr.mkdir() 

46 

47 f = dr / 'f1.txt' 

48 f.write_text('foo') 

49 f2 = dr / 'f2.txt' 

50 f2.write_text('bar') 

51 

52 ret = zipx.zip_to_path(str(z), str(dr)) 

53 assert sorted(os.listdir(str(d))) == sorted(['zip', 'dir']) 

54 assert ret == 2 

55 

56 

57# test zip with empty dir 

58def test_zip_dir_empty(tmp_path): 

59 d = tmp_path / 'test' 

60 d.mkdir() 

61 

62 z = d / 'zip' 

63 dr = d / 'dir' 

64 dr.mkdir() 

65 

66 ret = zipx.zip_to_path(str(z), str(dr)) 

67 assert sorted(os.listdir(str(d))) == sorted(['dir']) 

68 assert ret == 0 

69 

70 

71def test_zip_to_bytes(tmp_path): 

72 d = tmp_path / 'test' 

73 d.mkdir() 

74 

75 f = d / 'f1.txt' 

76 f.write_text('foo') 

77 f2 = d / 'f2.txt' 

78 f2.write_text('bar') 

79 

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']) 

86 

87 

88# test zip_to_bytes with no src 

89def test_zip_to_bytes_empty(tmp_path): 

90 d = tmp_path / 'test' 

91 d.mkdir() 

92 

93 ret = zipx.zip_to_bytes() 

94 assert not os.listdir(str(d)) 

95 assert not sorted(os.listdir(d)) 

96 assert ret == b'' 

97 

98 

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() 

103 

104 z = d / 'zip' 

105 dr = d / 'dir' 

106 dr.mkdir() 

107 

108 f = dr / 'f1.txt' 

109 f.write_text('foo') 

110 f2 = dr / 'f2.txt' 

111 f2.write_text('bar') 

112 

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']) 

117 

118 

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() 

123 

124 dr = d / 'dir' 

125 dr.mkdir() 

126 

127 ret = zipx.zip_to_bytes(str(dr)) 

128 assert sorted(os.listdir(d)) == sorted(['dir']) 

129 assert ret == b'' 

130 

131 

132# Has no return although return type is int 

133def test_unzip(tmp_path): 

134 d = tmp_path / 'test' 

135 d.mkdir() 

136 

137 z = d / 'zip' 

138 

139 f = d / 'f1.txt' 

140 f.write_text('foo') 

141 f2 = d / 'f2.txt' 

142 f2.write_text('bar') 

143 

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 

151 

152 

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() 

157 

158 z = d / 'zip' 

159 

160 f = d / 'f1.txt' 

161 f.write_text('foo') 

162 f2 = d / 'f2.txt' 

163 f2.write_text('bar') 

164 

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 

172 

173 

174def test_unzip_bytes(tmp_path): 

175 d = tmp_path / 'test' 

176 d.mkdir() 

177 

178 z = d / 'zip' 

179 

180 f = d / 'f1.txt' 

181 f.write_text('foo') 

182 f2 = d / 'f2.txt' 

183 f2.write_text('bar') 

184 

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']) 

191 

192 

193def test_unzip_to_dict(tmp_path): 

194 d = tmp_path / 'test' 

195 d.mkdir() 

196 

197 z = d / 'zip' 

198 

199 f = d / 'f1.txt' 

200 f.write_text('foo') 

201 f2 = d / 'f2.txt' 

202 f2.write_text('bar') 

203 

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']) 

210 

211 

212def test_unzip_bytes_to_dict(tmp_path): 

213 d = tmp_path / 'test' 

214 d.mkdir() 

215 

216 z = d / 'zip' 

217 

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']