1515import re
1616import shutil
1717import struct
18+ import subprocess
1819import sys
1920import tempfile
2021import zlib
2829DATA = b'This is a simple test with igzip'
2930COMPRESSED_DATA = gzip .compress (DATA )
3031TEST_FILE = str ((Path (__file__ ).parent / "data" / "test.fastq.gz" ))
32+ PYPY = sys .implementation .name == "pypy"
33+
34+
35+ def run_isal_igzip (* args , stdin = None ):
36+ """Calling isal.igzip externally seems to solve some issues on PyPy where
37+ files would not be written properly when igzip.main() was called. This is
38+ probably due to some out of order execution that PyPy tries to pull.
39+ Running the process externally is detrimental to the coverage report,
40+ so this is only done for PyPy."""
41+ process = subprocess .Popen (["python" , "-m" , "isal.igzip" , * args ],
42+ stdout = subprocess .PIPE ,
43+ stderr = subprocess .PIPE ,
44+ stdin = subprocess .PIPE )
45+
46+ return process .communicate (stdin )
3147
3248
3349def test_wrong_compresslevel_igzipfile ():
@@ -112,10 +128,13 @@ def test_decompress_infile_outfile(tmp_path, capsysbinary):
112128def test_compress_infile_outfile (tmp_path , capsysbinary ):
113129 test_file = tmp_path / "test"
114130 test_file .write_bytes (DATA )
115- sys .argv = ['' , str (test_file )]
116- igzip .main ()
131+ if PYPY :
132+ out , err = run_isal_igzip (str (test_file ))
133+ else :
134+ sys .argv = ['' , str (test_file )]
135+ igzip .main ()
136+ out , err = capsysbinary .readouterr ()
117137 out_file = test_file .with_suffix (".gz" )
118- out , err = capsysbinary .readouterr ()
119138 assert err == b''
120139 assert out == b''
121140 assert out_file .exists ()
@@ -176,9 +195,13 @@ def test_compress_infile_out_file(tmp_path, capsysbinary):
176195 test = tmp_path / "test"
177196 test .write_bytes (DATA )
178197 out_file = tmp_path / "compressed.gz"
179- sys .argv = ['' , '-o' , str (out_file ), str (test )]
180- igzip .main ()
181- out , err = capsysbinary .readouterr ()
198+ args = ['-o' , str (out_file ), str (test )]
199+ if PYPY :
200+ out , err = run_isal_igzip (* args )
201+ else :
202+ sys .argv = ['' , * args ]
203+ igzip .main ()
204+ out , err = capsysbinary .readouterr ()
182205 assert gzip .decompress (out_file .read_bytes ()) == DATA
183206 assert err == b''
184207 assert out == b''
@@ -189,9 +212,13 @@ def test_compress_infile_out_file_force(tmp_path, capsysbinary):
189212 test .write_bytes (DATA )
190213 out_file = tmp_path / "compressed.gz"
191214 out_file .touch ()
192- sys .argv = ['' , '-f' , '-o' , str (out_file ), str (test )]
193- igzip .main ()
194- out , err = capsysbinary .readouterr ()
215+ args = ['-f' , '-o' , str (out_file ), str (test )]
216+ if PYPY :
217+ out , err = run_isal_igzip (* args )
218+ else :
219+ sys .argv = ['' , * args ]
220+ igzip .main ()
221+ out , err = capsysbinary .readouterr ()
195222 assert gzip .decompress (out_file .read_bytes ()) == DATA
196223 assert err == b''
197224 assert out == b''
@@ -234,23 +261,30 @@ def test_compress_infile_out_file_inmplicit_name_prompt_accept(
234261 test .write_bytes (DATA )
235262 out_file = tmp_path / "test.gz"
236263 out_file .touch ()
237- sys .argv = ['' , str (test )]
238- mock_stdin = io .BytesIO (b"y" )
239- sys .stdin = io .TextIOWrapper (mock_stdin )
240- igzip .main ()
241- out , err = capsysbinary .readouterr ()
242- assert gzip .decompress (out_file .read_bytes ()) == DATA
264+ if PYPY :
265+ out , err = run_isal_igzip (str (test ), stdin = b"y\n " )
266+ else :
267+ sys .argv = ['' , str (test )]
268+ mock_stdin = io .BytesIO (b"y" )
269+ sys .stdin = io .TextIOWrapper (mock_stdin )
270+ igzip .main ()
271+ out , err = capsysbinary .readouterr ()
243272 assert b"already exists; do you wish to overwrite" in out
244273 assert err == b""
274+ assert gzip .decompress (out_file .read_bytes ()) == DATA
245275
246276
247277def test_compress_infile_out_file_no_name (tmp_path , capsysbinary ):
248278 test = tmp_path / "test"
249279 test .write_bytes (DATA )
250280 out_file = tmp_path / "compressed.gz"
251- sys .argv = ['' , '-n' , '-o' , str (out_file ), str (test )]
252- igzip .main ()
253- out , err = capsysbinary .readouterr ()
281+ args = ['-n' , '-o' , str (out_file ), str (test )]
282+ if PYPY :
283+ out , err = run_isal_igzip (* args )
284+ else :
285+ sys .argv = ['' , '-n' , '-o' , str (out_file ), str (test )]
286+ igzip .main ()
287+ out , err = capsysbinary .readouterr ()
254288 output = out_file .read_bytes ()
255289 assert gzip .decompress (output ) == DATA
256290 assert err == b''
0 commit comments