Skip to content

Commit

Permalink
test both str and Path
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jul 19, 2024
1 parent 0ae0869 commit 5bd0f39
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions tests/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,19 @@ def test_source_for_file_windows(tmp_path: pathlib.Path) -> None:
class RunpyTest(CoverageTest):
"""Tests using runpy."""

def test_runpy_path(self) -> None:
"""Ensure runpy.run_path(path) works when path is pathlib.Path.
runpy.run_path(pathlib.Path(...)) causes __file__ to be a Path,
which may make source_for_file() stumble (#1819).
"""

self.check_coverage("""\
@pytest.mark.parametrize("convert_to", ["str", "Path"])
def test_runpy_path(self, convert_to) -> None:
# Ensure runpy.run_path(path) works when path is pathlib.Path or str.
#
# runpy.run_path(pathlib.Path(...)) causes __file__ to be a Path,
# which may make source_for_file() stumble (#1819) with:
#
# AttributeError: 'PosixPath' object has no attribute 'endswith'

self.check_coverage(f"""\
import runpy
from pathlib import Path
pyfile = Path('script.py')
pyfile.write_text('')
runpy.run_path(pyfile)
runpy.run_path({convert_to}(pyfile))
""")

0 comments on commit 5bd0f39

Please sign in to comment.