Skip to content

Commit bf09762

Browse files
committed
Refactor utils/tests/test_system_tools.py
1. There is no need to check for the availability of `gzip` because it is in the Python standard library. 2. There is no need to write code for managing temporary directories because `pytest` provides the `tmp_path` fixture. 3. A separate test for checking the existance of a file produced by the tests is not needed because if it does not exist the following tests fail anyways.
1 parent f99e1ba commit bf09762

File tree

1 file changed

+8
-34
lines changed

1 file changed

+8
-34
lines changed
Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,18 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3-
try:
4-
import gzip
5-
6-
HAS_GZIP = True
7-
except ImportError:
8-
HAS_GZIP = False
9-
10-
import shutil
11-
import os
12-
from os.path import exists
13-
import tempfile
14-
15-
import pytest
3+
import gzip
164

175
from ..system_tools import gunzip
186

197

20-
@pytest.mark.skipif("not HAS_GZIP")
21-
def test_gunzip():
22-
23-
temp_dir = tempfile.mkdtemp()
24-
filename = f"{temp_dir}{os.sep}test_gunzip.txt.gz"
25-
unziped_filename = filename.rsplit(".", 1)[0]
26-
8+
def test_gunzip(tmp_path):
9+
filename = tmp_path / 'test_gunzip.txt.gz'
2710
# First create a gzip file
2811
content = b"Bla"
2912
with gzip.open(filename, "wb") as f:
3013
f.write(content)
31-
32-
try:
33-
# Then test our gunzip command works and creates an unziped file
34-
gunzip(filename)
35-
assert exists(unziped_filename)
36-
37-
# Check content is the same
38-
with open(unziped_filename, "rb") as f:
39-
new_content = f.read()
40-
assert new_content == content
41-
42-
finally:
43-
# Clean
44-
shutil.rmtree(temp_dir)
14+
# Then test our gunzip command works
15+
gunzip(str(filename))
16+
with open(filename.with_suffix(''), "rb") as f:
17+
new_content = f.read()
18+
assert new_content == content

0 commit comments

Comments
 (0)