|
2 | 2 |
|
3 | 3 | try:
|
4 | 4 | import gzip
|
| 5 | + |
5 | 6 | HAS_GZIP = True
|
6 | 7 | except ImportError:
|
7 | 8 | HAS_GZIP = False
|
8 | 9 |
|
| 10 | +import shutil |
9 | 11 | import os
|
10 | 12 | from os.path import exists
|
11 | 13 | import tempfile
|
|
15 | 17 | from ..system_tools import gunzip
|
16 | 18 |
|
17 | 19 |
|
18 |
| -@pytest.mark.skipif('not HAS_GZIP') |
| 20 | +@pytest.mark.skipif("not HAS_GZIP") |
19 | 21 | def test_gunzip():
|
20 |
| - filehandle = tempfile.NamedTemporaryFile(suffix='.txt.gz') |
21 |
| - filename = filehandle.name |
| 22 | + |
| 23 | + temp_dir = tempfile.mkdtemp() |
| 24 | + filename = temp_dir + os.sep + "test_gunzip.txt.gz" |
22 | 25 | unziped_filename = filename.rsplit(".", 1)[0]
|
23 |
| - for f in [filename, unziped_filename]: |
24 |
| - if exists(f): |
25 |
| - os.remove(f) |
26 | 26 |
|
27 | 27 | # First create a gzip file
|
28 | 28 | content = b"Bla"
|
29 |
| - with gzip.open(filename, 'wb') as f: |
| 29 | + with gzip.open(filename, "wb") as f: |
30 | 30 | f.write(content)
|
31 | 31 |
|
32 |
| - # Then test our gunzip command works and creates an unziped file |
33 |
| - gunzip(filename) |
34 |
| - assert exists(unziped_filename) |
| 32 | + try: |
| 33 | + # Then test our gunzip command works and creates an unziped file |
| 34 | + gunzip(filename) |
| 35 | + assert exists(unziped_filename) |
35 | 36 |
|
36 |
| - # Check content is the same |
37 |
| - with open(unziped_filename, 'rb') as f: |
38 |
| - new_content = f.read() |
39 |
| - assert new_content == content |
| 37 | + # Check content is the same |
| 38 | + with open(unziped_filename, "rb") as f: |
| 39 | + new_content = f.read() |
| 40 | + assert new_content == content |
40 | 41 |
|
41 |
| - # Clean |
42 |
| - for f in [filename, unziped_filename]: |
43 |
| - if exists(f): |
44 |
| - os.remove(f) |
| 42 | + finally: |
| 43 | + # Clean |
| 44 | + shutil.rmtree(temp_dir) |
0 commit comments