Skip to content

Commit 9b2c451

Browse files
committed
Add pytest tests for CLI
1 parent 49d1a5c commit 9b2c451

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tests/test_igzip_cli.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright (c) 2020 Leiden University Medical Center
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
# SOFTWARE.
20+
21+
import gzip
22+
import io
23+
import sys
24+
25+
from isal import igzip
26+
27+
import pytest
28+
29+
DATA = b'This is a simple test with igzip'
30+
COMPRESSED_DATA = gzip.compress(DATA)
31+
32+
33+
@pytest.mark.parametrize("level", range(1, 10))
34+
def test_decompress_stdin_stdout(capsysbinary, level):
35+
"""Test if the command line can decompress data that has been compressed
36+
by gzip at all levels."""
37+
mock_stdin = io.BytesIO(gzip.compress(DATA, level))
38+
sys.stdin = io.TextIOWrapper(mock_stdin)
39+
sys.argv = ["", "-d"]
40+
igzip.main()
41+
out, err = capsysbinary.readouterr()
42+
assert out == DATA
43+
44+
45+
@pytest.mark.parametrize("level", [str(x) for x in range(4)])
46+
def test_compress_stdin_stdout(capsysbinary, level):
47+
mock_stdin = io.BytesIO(DATA)
48+
sys.stdin = io.TextIOWrapper(mock_stdin)
49+
sys.argv = ["", f"-{level}"]
50+
igzip.main()
51+
out, err = capsysbinary.readouterr()
52+
assert gzip.decompress(out) == DATA
53+
54+
55+
def test_decompress_infile_outfile(tmp_path):
56+
test_file = tmp_path / "test"
57+
compressed_temp = test_file.with_suffix(".gz")
58+
compressed_temp.write_bytes(gzip.compress(DATA))
59+
sys.argv = ['', '-d', str(compressed_temp)]
60+
igzip.main()
61+
assert test_file.read_bytes() == DATA
62+
63+
64+
def test_compress_infile_outfile(tmp_path):
65+
test_file = tmp_path / "test"
66+
test_file.write_bytes(DATA)
67+
sys.argv = ['', str(test_file)]
68+
igzip.main()
69+
assert gzip.decompress(test_file.with_suffix(".gz").read_bytes()) == DATA

0 commit comments

Comments
 (0)