Skip to content

Commit 1f797a3

Browse files
committed
Make it possible to run tests in parallel
1 parent e1f3d25 commit 1f797a3

1 file changed

Lines changed: 31 additions & 24 deletions

File tree

test.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from glob import glob
2-
from pathlib import Path
3-
from subprocess import check_call, check_output
41
import filecmp
52
import os
63
import sys
4+
from pathlib import Path
5+
from subprocess import STDOUT, CalledProcessError, check_call, check_output
76

87
import pytest
98

@@ -18,28 +17,21 @@ def fixed_filename(original_filename):
1817
return original_filename.with_suffix(".fixed" + original_filename.suffix)
1918

2019

21-
@pytest.fixture(autouse=True)
22-
def clean():
23-
# run test first
24-
yield
25-
for fixed_file in glob(
26-
str(Path("test") / "**" / "*.fixed.*"), recursive=True
27-
):
28-
os.unlink(fixed_file)
29-
30-
3120
def generate_test_data():
32-
python_stdlib = glob(str(Path("test") / "python_stdlib" / "*" / "*.pyc"))
33-
pure_marshal = glob(str(Path("test") / "pure_marshal" / "*"))
34-
renamed_pycs = glob(str(Path("test") / "renamed_pycs" / "*"))
35-
return pure_marshal + renamed_pycs + python_stdlib
21+
yield from (Path("test") / "python_stdlib").glob("**/*.pyc")
22+
yield from (Path("test") / "pure_marshal").glob("*")
23+
yield from (Path("test") / "renamed_pycs").glob("*")
3624

3725

38-
test_data = generate_test_data()
26+
@pytest.mark.parametrize("original_filename", generate_test_data())
27+
def test_complete(original_filename, tmp_path):
28+
# To be able to run tests in parallel, we create
29+
# a symlink for each test file so the fixed file appears
30+
# next to the symlink in the temp dir instead of the
31+
# test folder next to the original files.
32+
filename = tmp_path / original_filename.name
33+
filename.symlink_to(original_filename.absolute())
3934

40-
41-
@pytest.mark.parametrize("filename", test_data)
42-
def test_complete(filename):
4335
# This command uses the Python we are testing with
4436
# because for example we can run marshalparser with
4537
# Python 3.9 and fix pyc file for Python 3.6
@@ -92,9 +84,24 @@ def test_complete(filename):
9284
)
9385

9486

95-
three_doubles = [test_data[i : i + 2] for i in range(0, 6, 2)]
87+
test_files = sorted((Path("test") / "renamed_pycs").glob("*"))
88+
three_doubles = [(test_files[n], test_files[n + 1]) for n in range(0, 6, 2)]
9689

9790

98-
@pytest.mark.parametrize("filenames", three_doubles)
99-
def test_run_with_more_than_one_file(filenames):
91+
@pytest.mark.parametrize("original_filenames", three_doubles)
92+
def test_run_with_more_than_one_file(original_filenames, tmp_path):
93+
filenames = []
94+
for original_filename in original_filenames:
95+
filename = tmp_path / original_filename.name
96+
filename.symlink_to(original_filename.absolute())
97+
filenames.append(filename)
10098
check_call(CMD + [*filenames])
99+
100+
101+
def test_empty_file(tmp_path):
102+
filename = tmp_path / "empty_file.pyc"
103+
filename.touch()
104+
with pytest.raises(CalledProcessError) as e:
105+
check_output(CMD + [filename], encoding="utf-8", stderr=STDOUT)
106+
assert e.value.returncode == 1
107+
assert f"File {filename} is empty!" in e.value.output

0 commit comments

Comments
 (0)