File: test_cli.py

package info (click to toggle)
python-cramjam 2.7.0.1%2Bds1-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 3,048 kB
  • sloc: python: 622; makefile: 41
file content (69 lines) | stat: -rw-r--r-- 2,244 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import subprocess
import tempfile
import pathlib
from datetime import timedelta

import pytest
from hypothesis import strategies as st, given, settings

import cramjam

VARIANTS = ("snappy", "brotli", "bzip2", "lz4", "gzip", "deflate", "zstd")

# Some OS can be slow or have higher variability in their runtimes on CI
settings.register_profile("local", deadline=timedelta(milliseconds=1000))
settings.register_profile("CI", deadline=None, max_examples=10)
if os.getenv("CI"):
    settings.load_profile("CI")
else:
    settings.load_profile("local")


def run_command(cmd) -> bytes:
    return subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)


@given(data=st.binary(min_size=1))
@pytest.mark.parametrize("variant", VARIANTS)
def test_cli_file_to_file(data, variant):

    with tempfile.TemporaryDirectory() as tmpdir:
        infile = pathlib.Path(tmpdir).joinpath("input.txt")
        infile.write_bytes(data)

        compressed_file = pathlib.Path(tmpdir).joinpath(f"input.txt.{variant}")

        cmd = f"cramjam-cli {variant} compress --input {infile} --output {compressed_file}"
        run_command(cmd)

        expected = bytes(getattr(cramjam, variant).compress(data))
        assert expected == compressed_file.read_bytes()

        decompressed_file = pathlib.Path(tmpdir).joinpath("decompressed.txt")
        run_command(
            f"cramjam-cli {variant} decompress --input {compressed_file} --output {decompressed_file}"
        )
        assert data == decompressed_file.read_bytes()


@given(data=st.binary(min_size=1))
@pytest.mark.parametrize("variant", VARIANTS)
def test_cli_file_to_stdout(data, variant):

    with tempfile.TemporaryDirectory() as tmpdir:
        infile = pathlib.Path(tmpdir).joinpath("input.txt")
        infile.write_bytes(data)

        cmd = f"cramjam-cli {variant} compress --input {infile}"
        out = run_command(cmd)

        expected = bytes(getattr(cramjam, variant).compress(data))
        assert expected == out

        compressed = pathlib.Path(tmpdir).joinpath(f"compressed.txt.{variant}")
        compressed.write_bytes(expected)

        cmd = f"cramjam-cli {variant} decompress --input {compressed}"
        out = run_command(cmd)
        assert out == data