diff --git a/.github/workflows/wheel.yml b/.github/workflows/wheel.yml index f36ea56..f9e543f 100644 --- a/.github/workflows/wheel.yml +++ b/.github/workflows/wheel.yml @@ -38,7 +38,7 @@ jobs: run: pip install dist/python-snappy*.tar.gz - name: Pytest - run: pytest --verbose test_snappy.py + run: pytest --verbose test_snappy.py test_formats.py - name: Archive sdist uses: actions/upload-artifact@v4 diff --git a/src/snappy/__main__.py b/src/snappy/__main__.py index 07c95f7..c558b48 100644 --- a/src/snappy/__main__.py +++ b/src/snappy/__main__.py @@ -71,20 +71,21 @@ def cmdline_main(): if isinstance(args.outfile, io.TextIOWrapper): args.outfile = stdout - additional_args = {} if args.compress: method = formats.get_compress_function(args.target_format) else: try: - method, read_chunk = formats.get_decompress_function( + method = formats.get_decompress_function( args.target_format, args.infile ) except UncompressError as err: sys.exit("Failed to get decompress function: {}".format(err)) - additional_args['start_chunk'] = read_chunk + if args.target_format == "auto": + # guessing the format reads from the input, so rewind it + args.infile.seek(0) - method(args.infile, args.outfile, **additional_args) + method(args.infile, args.outfile) if __name__ == "__main__": diff --git a/test_formats.py b/test_formats.py index 6453b1e..fa6893b 100644 --- a/test_formats.py +++ b/test_formats.py @@ -1,5 +1,8 @@ import io import os +import subprocess +import sys +import tempfile from unittest import TestCase from snappy import snappy_formats as formats @@ -72,6 +75,30 @@ class TestFormatRawAuto(TestFormatBase): success = True +class TestCommandLine(TestCase): + def test_compress_decompress_roundtrip(self): + data = os.urandom(1024 * 256 * 2) + os.urandom(13245 * 2) + with tempfile.TemporaryDirectory() as tmp_dir: + plain = os.path.join(tmp_dir, "plain") + compressed = os.path.join(tmp_dir, "compressed.snappy") + with open(plain, "wb") as f: + f.write(data) + subprocess.run( + [sys.executable, "-m", "snappy", "-c", plain, compressed], + check=True, + ) + for target_format in formats.ALL_SUPPORTED_FORMATS: + with self.subTest(target_format=target_format): + decompressed = os.path.join(tmp_dir, target_format) + subprocess.run( + [sys.executable, "-m", "snappy", "-d", + "-t", target_format, compressed, decompressed], + check=True, + ) + with open(decompressed, "rb") as f: + self.assertEqual(data, f.read()) + + if __name__ == "__main__": import unittest unittest.main()