forked from fedora-python/marshalparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
87 lines (65 loc) · 2.5 KB
/
test.py
File metadata and controls
87 lines (65 loc) · 2.5 KB
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
77
78
79
80
81
82
83
84
85
86
87
from glob import glob
from pathlib import Path
from subprocess import check_call
import filecmp
import os
import sys
import pytest
from marshalparser.magic import get_pyc_python_version
PYTHON_VERSION = "{}.{}".format(*sys.version_info[:2])
CMD = ["python" + PYTHON_VERSION, "-m", "marshalparser", "-f"]
def fixed_filename(original_filename):
original_filename = Path(original_filename)
return original_filename.with_suffix(".fixed" + original_filename.suffix)
@pytest.fixture(autouse=True)
def clean():
# run test first
yield
for fixed_file in glob(
str(Path("test") / "**" / "*.fixed.*"), recursive=True
):
os.unlink(fixed_file)
def generate_test_data():
python_stdlib = glob(str(Path("test") / "python_stdlib" / "*" / "*.pyc"))
pure_marshal = glob(str(Path("test") / "pure_marshal" / "*"))
renamed_pycs = glob(str(Path("test") / "renamed_pycs" / "*"))
return pure_marshal + renamed_pycs + python_stdlib
test_data = generate_test_data()
@pytest.mark.parametrize("filename", test_data)
def test_complete(filename):
# This command uses the Python we are testing with
# because for example we can run marshalparser with
# Python 3.9 and fix pyc file for Python 3.6
check_call(CMD + [filename])
assert os.path.isfile(fixed_filename(filename))
if filecmp.cmp(filename, fixed_filename(filename), shallow=False):
pytest.skip("Fixed file is the same as original, nothing to check")
# To compare two pyc files, we need to use Python
# they were compiled by
python_version = get_pyc_python_version(filename=filename)
if python_version:
python_version_str = "{}.{}".format(*python_version)
# pass this to marshal_content_check.py so we don't have to
# check the header there again
check_pyc = True
else:
# For non-pyc files, we use the current Python for content check
python_version_str = PYTHON_VERSION
check_pyc = False
CHECK_CMD = [
"python" + python_version_str,
"marshal_content_check.py",
str(int(check_pyc)),
filename,
fixed_filename(filename),
]
try:
check_call(CHECK_CMD)
except FileNotFoundError:
pytest.skip(
f"python{python_version_str} not found! Cannot check the result."
)
three_doubles = [test_data[i : i + 2] for i in range(0, 6, 2)]
@pytest.mark.parametrize("filenames", three_doubles)
def test_run_with_more_than_one_file(filenames):
check_call(CMD + [*filenames])