Skip to content

Commit

Permalink
Merge pull request #2306 from eerovaher/refactor-systools
Browse files Browse the repository at this point in the history
Refactor `astroquery/utils/system_tools.py` and its tests
  • Loading branch information
bsipocz authored Feb 24, 2022
2 parents 9eb2278 + 2920b77 commit 843f9da
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 48 deletions.
17 changes: 3 additions & 14 deletions astroquery/utils/system_tools.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import gzip
import os

# Import DEVNULL for py3 or py3
try:
from subprocess import DEVNULL
except ImportError:
DEVNULL = open(os.devnull, 'wb')

# Check availability of some system tools
# Exceptions are raised if not found
import shutil


def gunzip(filename):
Expand All @@ -19,17 +12,13 @@ def gunzip(filename):
----------
filename : str
Fully qualified path of the file to decompress.
Returns
-------
filename : str
Name of the decompressed file (or input filename if gzip is not
available).
"""
import shutil
import gzip

# system-wide 'gzip' was removed, Python gzip used instead.
# See #1538 : https://github.com/astropy/astroquery/issues/1538

# ".fz" denotes RICE rather than gzip compression
if not filename.endswith('.fz'):
Expand Down
42 changes: 8 additions & 34 deletions astroquery/utils/tests/test_system_tools.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,18 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

try:
import gzip

HAS_GZIP = True
except ImportError:
HAS_GZIP = False

import shutil
import os
from os.path import exists
import tempfile

import pytest
import gzip

from ..system_tools import gunzip


@pytest.mark.skipif("not HAS_GZIP")
def test_gunzip():

temp_dir = tempfile.mkdtemp()
filename = f"{temp_dir}{os.sep}test_gunzip.txt.gz"
unziped_filename = filename.rsplit(".", 1)[0]

def test_gunzip(tmp_path):
filename = tmp_path / 'test_gunzip.txt.gz'
# First create a gzip file
content = b"Bla"
with gzip.open(filename, "wb") as f:
f.write(content)

try:
# Then test our gunzip command works and creates an unziped file
gunzip(filename)
assert exists(unziped_filename)

# Check content is the same
with open(unziped_filename, "rb") as f:
new_content = f.read()
assert new_content == content

finally:
# Clean
shutil.rmtree(temp_dir)
# Then test our gunzip command works
gunzip(str(filename))
with open(filename.with_suffix(''), "rb") as f:
new_content = f.read()
assert new_content == content

0 comments on commit 843f9da

Please sign in to comment.