Skip to content

Commit 5f4d71c

Browse files
committed
Fix test and formatting
1 parent 921c924 commit 5f4d71c

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

python_files/tests/pytestadapter/helpers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030
CONTENT_TYPE: str = "Content-Type:"
3131

3232

33+
@contextlib.contextmanager
34+
def text_to_python_file(text_file_path: pathlib.Path):
35+
try:
36+
contents = text_file_path.read_text(encoding="utf-8")
37+
python_file = text_file_path.with_suffix(".py")
38+
python_file.write_text(contents, encoding="utf-8")
39+
yield python_file
40+
finally:
41+
os.unlink(os.fspath(python_file))
42+
43+
3344
@contextlib.contextmanager
3445
def create_symlink(root: pathlib.Path, target_ext: str, destination_ext: str):
3546
destination = None

python_files/tests/pytestadapter/test_discovery.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
from . import expected_discovery_test_output, helpers # noqa: E402
1414

15-
def test_import_error(tmp_path):
15+
16+
def test_import_error():
1617
"""Test pytest discovery on a file that has a pytest marker but does not import pytest.
1718
1819
Copies the contents of a .txt file to a .py file in the temporary directory
@@ -23,15 +24,10 @@ def test_import_error(tmp_path):
2324
Keyword arguments:
2425
tmp_path -- pytest fixture that creates a temporary directory.
2526
"""
26-
# Saving some files as .txt to avoid that file displaying a syntax error for
27-
# the extension as a whole. Instead, rename it before running this test
28-
# in order to test the error handling.
2927
file_path = helpers.TEST_DATA_PATH / "error_pytest_import.txt"
30-
temp_dir = tmp_path / "temp_data"
31-
temp_dir.mkdir()
32-
p = temp_dir / "error_pytest_import.py"
33-
shutil.copyfile(file_path, p)
34-
actual: Optional[List[Dict[str, Any]]] = helpers.runner(["--collect-only", os.fspath(p)])
28+
with helpers.text_to_python_file(file_path) as p:
29+
actual: Optional[List[Dict[str, Any]]] = helpers.runner(["--collect-only", os.fspath(p)])
30+
3531
assert actual
3632
actual_list: List[Dict[str, Any]] = actual
3733
if actual_list is not None:
@@ -65,11 +61,9 @@ def test_syntax_error(tmp_path):
6561
# the extension as a whole. Instead, rename it before running this test
6662
# in order to test the error handling.
6763
file_path = helpers.TEST_DATA_PATH / "error_syntax_discovery.txt"
68-
temp_dir = tmp_path / "temp_data"
69-
temp_dir.mkdir()
70-
p = temp_dir / "error_syntax_discovery.py"
71-
shutil.copyfile(file_path, p)
72-
actual = helpers.runner(["--collect-only", os.fspath(p)])
64+
with helpers.text_to_python_file(file_path) as p:
65+
actual = helpers.runner(["--collect-only", os.fspath(p)])
66+
7367
assert actual
7468
actual_list: List[Dict[str, Any]] = actual
7569
if actual_list is not None:

0 commit comments

Comments
 (0)