Skip to content

Commit 435669c

Browse files
committed
Add tests for cli
1 parent 9b2c451 commit 435669c

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

src/isal/igzip.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ def main():
296296
else:
297297
base, extension = os.path.splitext(args.file)
298298
if extension != ".gz":
299-
print(f"filename doesn't end in .gz: {args.file}")
300-
return
299+
raise ValueError(f"filename doesn't end in .gz: {args.file}")
301300
in_file = open(args.file, "rb")
302301
out_file = io.open(base, "wb")
303302
try:

tests/test_gzip_compliance.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,17 @@ def test_decompress_infile_outfile(self):
816816
self.assertTrue(os.path.exists(igzipname))
817817

818818
def test_decompress_infile_outfile_error(self):
819-
rc, out, err = assert_python_ok('-m', 'isal.igzip', '-d',
820-
'thisisatest.out')
821-
self.assertIn(b"filename doesn't end in .gz:", out)
822-
self.assertEqual(rc, 0)
823-
self.assertEqual(err, b'')
819+
rc, out, err = assert_python_failure('-m', 'isal.igzip', '-d',
820+
'thisisatest.out')
821+
# We take a divide from the original gzip module here. Error messages
822+
# should be printed in stderr. Also exit code should not be 0!
823+
# in python -m gzip -d mycompressedfile > decompressed
824+
# will simply make decompressed contents 'filename doesn't end in .gz'
825+
# without throwing an error. Crazy!
826+
# TODO: Report a bug in CPython for gzip module
827+
self.assertIn(b"filename doesn't end in .gz:", err)
828+
self.assertNotEqual(rc, 0)
829+
self.assertEqual(out, b'')
824830

825831
@create_and_remove_directory(TEMPDIR)
826832
def test_compress_stdin_outfile(self):

tests/test_igzip_cli.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
# SOFTWARE.
2020

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+
2125
import gzip
2226
import io
2327
import sys
@@ -39,6 +43,7 @@ def test_decompress_stdin_stdout(capsysbinary, level):
3943
sys.argv = ["", "-d"]
4044
igzip.main()
4145
out, err = capsysbinary.readouterr()
46+
assert err == b''
4247
assert out == DATA
4348

4449

@@ -49,21 +54,40 @@ def test_compress_stdin_stdout(capsysbinary, level):
4954
sys.argv = ["", f"-{level}"]
5055
igzip.main()
5156
out, err = capsysbinary.readouterr()
57+
assert err == b''
5258
assert gzip.decompress(out) == DATA
5359

5460

55-
def test_decompress_infile_outfile(tmp_path):
61+
def test_decompress_infile_outfile(tmp_path, capsysbinary):
5662
test_file = tmp_path / "test"
5763
compressed_temp = test_file.with_suffix(".gz")
5864
compressed_temp.write_bytes(gzip.compress(DATA))
5965
sys.argv = ['', '-d', str(compressed_temp)]
6066
igzip.main()
67+
out, err = capsysbinary.readouterr()
68+
assert err == b''
69+
assert out == b''
70+
assert test_file.exists()
6171
assert test_file.read_bytes() == DATA
6272

6373

64-
def test_compress_infile_outfile(tmp_path):
74+
def test_compress_infile_outfile(tmp_path, capsysbinary):
6575
test_file = tmp_path / "test"
6676
test_file.write_bytes(DATA)
6777
sys.argv = ['', str(test_file)]
6878
igzip.main()
69-
assert gzip.decompress(test_file.with_suffix(".gz").read_bytes()) == DATA
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''

0 commit comments

Comments
 (0)