File: test_misc.py

package info (click to toggle)
py7zr 0.11.3%2Bdfsg-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,740 kB
  • sloc: python: 6,786; makefile: 198; ansic: 35
file content (76 lines) | stat: -rw-r--r-- 2,843 bytes parent folder | download | duplicates (2)
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
70
71
72
73
74
75
76
import os
import pathlib
import shutil
import subprocess
import sys

import pytest

import py7zr

from . import libarchive_extract, p7zip_test

try:
    import multivolumefile as MVF
except ImportError:
    MVF = None

testdata_path = pathlib.Path(os.path.dirname(__file__)).joinpath('data')
os.umask(0o022)


@pytest.mark.misc
@pytest.mark.skipif(MVF is None, reason="multivolume support is not loaded.")
def test_extract_multi_volume(tmp_path):
    with testdata_path.joinpath('lzma2bcj.7z').open('rb') as src:
        with tmp_path.joinpath('lzma2bcj.7z.001').open('wb') as tgt:
            tgt.write(src.read(25000))
        with tmp_path.joinpath('lzma2bcj.7z.002').open('wb') as tgt:
            tgt.write(src.read(27337))
    with MVF.open(tmp_path.joinpath('lzma2bcj.7z'), mode='rb') as tgt:
        with py7zr.SevenZipFile(tgt) as arc:
            arc.extractall(tmp_path.joinpath('tgt'))


@pytest.mark.misc
@pytest.mark.skipif(MVF is None, reason="multivolume support is not loaded.")
def test_compress_to_multi_volume(tmp_path):
    tmp_path.joinpath('src').mkdir()
    py7zr.unpack_7zarchive(os.path.join(testdata_path, 'lzma2bcj.7z'), path=tmp_path.joinpath('src'))
    with MVF.open(tmp_path.joinpath('target.7z'), mode='wb', volume=10240) as tgt:
        with py7zr.SevenZipFile(tgt, 'w') as arc:
            arc.writeall(tmp_path.joinpath('src'), 'src')
    target = tmp_path.joinpath('target.7z.0001')
    assert target.exists()
    assert target.stat().st_size == 10240


@pytest.mark.misc
def test_copy_bcj_file(tmp_path):
    with py7zr.SevenZipFile(testdata_path.joinpath('copy_bcj_1.7z').open(mode='rb')) as ar:
        ar.extractall(tmp_path)


@pytest.mark.misc
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Case uses gcc and posix path on linux/mac")
def test_bcj_file(tmp_path):
    tmp_path.joinpath('src').mkdir()
    # build test target data
    if shutil.which('gcc'):
        result = subprocess.run(['gcc', '-std=c99', '-fPIC',
                                 '-o', tmp_path.joinpath('src').joinpath('bcj_test').as_posix(),
                                 '-c', testdata_path.joinpath('bcj_test.c').as_posix()],
                                stdout=subprocess.PIPE)
        if result.returncode != 0:
            return 0
        #
        tmp_path.joinpath('tgt').mkdir()
        tmp_path.joinpath('tgt2').mkdir()
        my_filters = [{"id": py7zr.FILTER_X86}, {"id": py7zr.FILTER_COPY}]
        with py7zr.SevenZipFile(tmp_path.joinpath('target.7z'), filters=my_filters, mode='w') as ar:
            ar.write(tmp_path.joinpath('src/bcj_test'), 'bcj_test')
        target = tmp_path.joinpath('target.7z')
        with py7zr.SevenZipFile(target, 'r') as ar:
            ar.extractall(tmp_path.joinpath('tgt'))
        p7zip_test(target)
        libarchive_extract(target, tmp_path / 'tgt2')