|
| 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 | +"""Tests for the igzip CLI. Uses pytest which works better than unittest for |
| 22 | +these sort of tests. Meant to complement the gzip module compliance tests. |
| 23 | +It should improve coverage as well.""" |
| 24 | + |
| 25 | +import gzip |
| 26 | +import io |
| 27 | +import sys |
| 28 | + |
| 29 | +from isal import igzip |
| 30 | + |
| 31 | +import pytest |
| 32 | + |
| 33 | +DATA = b'This is a simple test with igzip' |
| 34 | +COMPRESSED_DATA = gzip.compress(DATA) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize("level", range(1, 10)) |
| 38 | +def test_decompress_stdin_stdout(capsysbinary, level): |
| 39 | + """Test if the command line can decompress data that has been compressed |
| 40 | + by gzip at all levels.""" |
| 41 | + mock_stdin = io.BytesIO(gzip.compress(DATA, level)) |
| 42 | + sys.stdin = io.TextIOWrapper(mock_stdin) |
| 43 | + sys.argv = ["", "-d"] |
| 44 | + igzip.main() |
| 45 | + out, err = capsysbinary.readouterr() |
| 46 | + assert err == b'' |
| 47 | + assert out == DATA |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("level", [str(x) for x in range(4)]) |
| 51 | +def test_compress_stdin_stdout(capsysbinary, level): |
| 52 | + mock_stdin = io.BytesIO(DATA) |
| 53 | + sys.stdin = io.TextIOWrapper(mock_stdin) |
| 54 | + sys.argv = ["", f"-{level}"] |
| 55 | + igzip.main() |
| 56 | + out, err = capsysbinary.readouterr() |
| 57 | + assert err == b'' |
| 58 | + assert gzip.decompress(out) == DATA |
| 59 | + |
| 60 | + |
| 61 | +def test_decompress_infile_outfile(tmp_path, capsysbinary): |
| 62 | + test_file = tmp_path / "test" |
| 63 | + compressed_temp = test_file.with_suffix(".gz") |
| 64 | + compressed_temp.write_bytes(gzip.compress(DATA)) |
| 65 | + sys.argv = ['', '-d', str(compressed_temp)] |
| 66 | + igzip.main() |
| 67 | + out, err = capsysbinary.readouterr() |
| 68 | + assert err == b'' |
| 69 | + assert out == b'' |
| 70 | + assert test_file.exists() |
| 71 | + assert test_file.read_bytes() == DATA |
| 72 | + |
| 73 | + |
| 74 | +def test_compress_infile_outfile(tmp_path, capsysbinary): |
| 75 | + test_file = tmp_path / "test" |
| 76 | + test_file.write_bytes(DATA) |
| 77 | + sys.argv = ['', str(test_file)] |
| 78 | + igzip.main() |
| 79 | + out_file = test_file.with_suffix(".gz") |
| 80 | + out, err = capsysbinary.readouterr() |
| 81 | + assert err == b'' |
| 82 | + assert out == b'' |
| 83 | + assert out_file.exists() |
| 84 | + assert gzip.decompress(out_file.read_bytes()) == DATA |
| 85 | + |
| 86 | + |
| 87 | +def test_decompress_infile_outfile_error(capsysbinary): |
| 88 | + sys.argv = ['', '-d', 'thisisatest.out'] |
| 89 | + with pytest.raises(ValueError) as error: |
| 90 | + igzip.main() |
| 91 | + assert error.match("filename doesn't end") |
| 92 | + out, err = capsysbinary.readouterr() |
| 93 | + assert out == b'' |
| 94 | + |
| 95 | + |
| 96 | +def test_decompress_infile_stdout(capsysbinary, tmp_path): |
| 97 | + test_gz = tmp_path / "test.gz" |
| 98 | + test_gz.write_bytes(gzip.compress(DATA)) |
| 99 | + sys.argv = ['', '-cd', str(test_gz)] |
| 100 | + igzip.main() |
| 101 | + out, err = capsysbinary.readouterr() |
| 102 | + assert out == DATA |
| 103 | + assert err == b'' |
| 104 | + |
| 105 | + |
| 106 | +def test_compress_infile_stdout(capsysbinary, tmp_path): |
| 107 | + test = tmp_path / "test" |
| 108 | + test.write_bytes(DATA) |
| 109 | + sys.argv = ['', '-c', str(test)] |
| 110 | + igzip.main() |
| 111 | + out, err = capsysbinary.readouterr() |
| 112 | + assert gzip.decompress(out) == DATA |
| 113 | + assert err == b'' |
0 commit comments