Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/wheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/snappy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
27 changes: 27 additions & 0 deletions test_formats.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()