Skip to content

Commit

Permalink
refactor(test): use tmp_path instead of tmpdir
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 3, 2023
1 parent 0b05b45 commit c3ee30c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -2,7 +2,7 @@
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

[tool:pytest]
addopts = -q -n auto --strict-markers --no-flaky-report -rfEX --failed-first
addopts = -q -n auto -p no:legacypath --strict-markers --no-flaky-report -rfEX --failed-first
python_classes = *Test
markers =
expensive: too slow to run during "make smoke"
Expand Down
3 changes: 2 additions & 1 deletion tests/balance_xdist_plugin.py
Expand Up @@ -29,6 +29,7 @@
import os
import shutil
import time

from pathlib import Path

import pytest
Expand Down Expand Up @@ -64,7 +65,7 @@ def pytest_sessionstart(self, session):
if not self.running_all:
return

tests_csv_dir = Path(session.startdir).resolve() / "tmp/tests_csv"
tests_csv_dir = session.startpath.resolve() / "tmp/tests_csv"
self.tests_csv = tests_csv_dir / f"{self.worker}.csv"

if self.worker == "none":
Expand Down
6 changes: 3 additions & 3 deletions tests/mixins.py
Expand Up @@ -12,7 +12,7 @@
import os.path
import sys

from typing import Tuple
from typing import Iterator, Tuple

import pytest

Expand Down Expand Up @@ -57,10 +57,10 @@ class TempDirMixin:
run_in_temp_dir = True

@pytest.fixture(autouse=True)
def _temp_dir(self, tmpdir_factory):
def _temp_dir(self, tmp_path_factory: pytest.TempPathFactory) -> Iterator[None]:
"""Create a temp dir for the tests, if they want it."""
if self.run_in_temp_dir:
tmpdir = tmpdir_factory.mktemp("t")
tmpdir = tmp_path_factory.mktemp("t")
self.temp_dir = str(tmpdir)
with change_dir(self.temp_dir):
# Modules should be importable from this temp directory. We don't
Expand Down
10 changes: 6 additions & 4 deletions tests/test_concurrency.py
Expand Up @@ -6,6 +6,7 @@
import glob
import multiprocessing
import os
import pathlib
import random
import re
import sys
Expand Down Expand Up @@ -626,21 +627,22 @@ def run_thread(): # pragma: nested
assert has_stopped_coverage == [t.ident]


def test_thread_safe_save_data(tmpdir):
def test_thread_safe_save_data(tmp_path: pathlib.Path) -> None:
# Non-regression test for: https://github.com/nedbat/coveragepy/issues/581

# Create some Python modules and put them in the path
modules_dir = tmpdir.mkdir('test_modules')
modules_dir = tmp_path / "test_modules"
modules_dir.mkdir()
module_names = [f"m{i:03d}" for i in range(1000)]
for module_name in module_names:
modules_dir.join(module_name + ".py").write("def f(): pass\n")
(modules_dir / (module_name + ".py")).write_text("def f(): pass\n")

# Shared variables for threads
should_run = [True]
imported = []

old_dir = os.getcwd()
os.chdir(modules_dir.strpath)
os.chdir(modules_dir)
try:
# Make sure that all dummy modules can be imported.
for module_name in module_names:
Expand Down
17 changes: 9 additions & 8 deletions tests/test_python.py
Expand Up @@ -3,6 +3,7 @@

"""Tests of coverage/python.py"""

import pathlib
import sys

import pytest
Expand Down Expand Up @@ -37,9 +38,8 @@ def test_get_encoded_zip_files(self, encoding):
assert mod.encoding == encoding


def test_source_for_file(tmpdir):
path = tmpdir.join("a.py")
src = str(path)
def test_source_for_file(tmp_path: pathlib.Path) -> None:
src = str(tmp_path / "a.py")
assert source_for_file(src) == src
assert source_for_file(src + 'c') == src
assert source_for_file(src + 'o') == src
Expand All @@ -48,14 +48,15 @@ def test_source_for_file(tmpdir):


@pytest.mark.skipif(not env.WINDOWS, reason="not windows")
def test_source_for_file_windows(tmpdir):
path = tmpdir.join("a.py")
src = str(path)
def test_source_for_file_windows(tmp_path: pathlib.Path) -> None:
a_py = tmp_path / "a.py"
src = str(a_py)

# On windows if a pyw exists, it is an acceptable source
path_windows = tmpdir.ensure("a.pyw")
path_windows = tmp_path / "a.pyw"
path_windows.write_text("")
assert str(path_windows) == source_for_file(src + 'c')

# If both pyw and py exist, py is preferred
path.ensure(file=True)
a_py.write_text("")
assert source_for_file(src + 'c') == src

0 comments on commit c3ee30c

Please sign in to comment.