1- # ruff: noqa
21import collections
32import glob
43import os
54import platform
65import re
7- import subprocess
86import sys
9- from io import StringIO
107from itertools import chain
8+ from pathlib import Path
9+ from types import SimpleNamespace
1110
1211import coverage
13- import py
1412import pytest
15- import virtualenv
16- import xdist
17- from fields import Namespace
1813from process_tests import TestProcess as _TestProcess
1914from process_tests import dump_on_error
2015from process_tests import wait_for_strings
2116
2217import pytest_cov .plugin
2318
24-
2519max_worker_restart_0 = '--max-worker-restart=0'
2620
2721SCRIPT = """
@@ -167,7 +161,7 @@ def test_foo(cov):
167161def adjust_sys_path ():
168162 """Adjust PYTHONPATH during tests to make "helper" importable in SCRIPT."""
169163 orig_path = os .environ .get ('PYTHONPATH' , None )
170- new_path = os . path . dirname ( __file__ )
164+ new_path = str ( Path ( __file__ ). parent )
171165 if orig_path is not None :
172166 new_path = os .pathsep .join ([new_path , orig_path ])
173167 os .environ ['PYTHONPATH' ] = new_path
@@ -190,7 +184,7 @@ def adjust_sys_path():
190184 ids = ['branch2x' , 'branch1c' , 'branch1a' , 'nobranch' ],
191185)
192186def prop (request ):
193- return Namespace (
187+ return SimpleNamespace (
194188 code = SCRIPT ,
195189 code2 = SCRIPT2 ,
196190 conf = request .param [0 ],
@@ -347,7 +341,7 @@ def test_xml_output_dir(testdir):
347341def test_json_output_dir (testdir ):
348342 script = testdir .makepyfile (SCRIPT )
349343
350- result = testdir .runpytest ('-v' , '--cov=%s' % script .dirpath (), '--cov-report=json:' + JSON_REPORT_NAME , script )
344+ result = testdir .runpytest ('-v' , f '--cov={ script .dirpath ()} ' , '--cov-report=json:' + JSON_REPORT_NAME , script )
351345
352346 result .stdout .fnmatch_lines (
353347 [
@@ -363,7 +357,7 @@ def test_json_output_dir(testdir):
363357def test_markdown_output_dir (testdir ):
364358 script = testdir .makepyfile (SCRIPT )
365359
366- result = testdir .runpytest ('-v' , '--cov=%s' % script .dirpath (), '--cov-report=markdown:' + MARKDOWN_REPORT_NAME , script )
360+ result = testdir .runpytest ('-v' , f '--cov={ script .dirpath ()} ' , '--cov-report=markdown:' + MARKDOWN_REPORT_NAME , script )
367361
368362 result .stdout .fnmatch_lines (
369363 [
@@ -379,7 +373,7 @@ def test_markdown_output_dir(testdir):
379373def test_markdown_append_output_dir (testdir ):
380374 script = testdir .makepyfile (SCRIPT )
381375
382- result = testdir .runpytest ('-v' , '--cov=%s' % script .dirpath (), '--cov-report=markdown-append:' + MARKDOWN_APPEND_REPORT_NAME , script )
376+ result = testdir .runpytest ('-v' , f '--cov={ script .dirpath ()} ' , '--cov-report=markdown-append:' + MARKDOWN_APPEND_REPORT_NAME , script )
383377
384378 result .stdout .fnmatch_lines (
385379 [
@@ -397,7 +391,7 @@ def test_markdown_and_markdown_append_work_together(testdir):
397391
398392 result = testdir .runpytest (
399393 '-v' ,
400- '--cov=%s' % script .dirpath (),
394+ f '--cov={ script .dirpath ()} ' ,
401395 '--cov-report=markdown:' + MARKDOWN_REPORT_NAME ,
402396 '--cov-report=markdown-append:' + MARKDOWN_APPEND_REPORT_NAME ,
403397 script ,
@@ -420,7 +414,7 @@ def test_markdown_and_markdown_append_pointing_to_same_file_throws_error(testdir
420414
421415 result = testdir .runpytest (
422416 '-v' ,
423- '--cov=%s' % script .dirpath (),
417+ f '--cov={ script .dirpath ()} ' ,
424418 '--cov-report=markdown:' + MARKDOWN_REPORT_NAME ,
425419 '--cov-report=markdown-append:' + MARKDOWN_REPORT_NAME ,
426420 script ,
@@ -466,7 +460,7 @@ def test_term_missing_output_dir(testdir):
466460
467461 result .stderr .fnmatch_lines (
468462 [
469- '*argument --cov-report: output specifier not supported for: "term-missing:%s "*' % DEST_DIR ,
463+ f '*argument --cov-report: output specifier not supported for: "term-missing:{ DEST_DIR } "*' ,
470464 ]
471465 )
472466 assert result .ret != 0
@@ -755,7 +749,7 @@ def test_add_task(celery_worker):
755749 result .stdout .fnmatch_lines (
756750 [
757751 '*_ coverage: platform *, python * _*' ,
758- f 'small_celery* 100%*' ,
752+ 'small_celery* 100%*' ,
759753 ]
760754 )
761755 assert result .ret == 0
@@ -1353,7 +1347,7 @@ def test_run():
13531347 '-v' , '--assert=plain' , f'--cov={ script .dirpath ()} ' , '--cov-report=term-missing' , '--cov-report=html' , script
13541348 )
13551349
1356- result .stdout .fnmatch_lines (['*_ coverage: platform *, python * _*' , f 'test_cleanup_on_sigterm* 88% * 18-19' , '*1 passed*' ])
1350+ result .stdout .fnmatch_lines (['*_ coverage: platform *, python * _*' , 'test_cleanup_on_sigterm* 88% * 18-19' , '*1 passed*' ])
13571351 assert result .ret == 0
13581352
13591353
@@ -1649,7 +1643,7 @@ def test_dist_bare_cov(testdir):
16491643
16501644def test_not_started_plugin_does_not_fail (testdir ):
16511645 class ns :
1652- cov_source = [ True ]
1646+ cov_source = ( True ,)
16531647 cov_report = ''
16541648
16551649 plugin = pytest_cov .plugin .CovPlugin (ns , None , start = False )
@@ -1697,14 +1691,13 @@ def test_external_data_file(testdir):
16971691 testdir .tmpdir .join ('.coveragerc' ).write (
16981692 """
16991693[run]
1700- data_file = %s
1701- """
1702- % testdir .tmpdir .join ('some/special/place/coverage-data' ).ensure ()
1694+ data_file = {}
1695+ """ .format (testdir .tmpdir .join ('some/special/place/coverage-data' ).ensure ())
17031696 )
17041697
17051698 result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , script )
17061699 assert result .ret == 0
1707- assert glob .glob (str (testdir .tmpdir .join ('some/special/place/coverage-data*' )))
1700+ assert glob .glob (str (testdir .tmpdir .join ('some/special/place/coverage-data*' ))) # noqa: PTH207
17081701
17091702
17101703@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
@@ -1714,14 +1707,13 @@ def test_external_data_file_xdist(testdir):
17141707 """
17151708[run]
17161709parallel = true
1717- data_file = %s
1718- """
1719- % testdir .tmpdir .join ('some/special/place/coverage-data' ).ensure ()
1710+ data_file = {}
1711+ """ .format (testdir .tmpdir .join ('some/special/place/coverage-data' ).ensure ())
17201712 )
17211713
17221714 result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , '-n' , '1' , max_worker_restart_0 , script )
17231715 assert result .ret == 0
1724- assert glob .glob (str (testdir .tmpdir .join ('some/special/place/coverage-data*' )))
1716+ assert glob .glob (str (testdir .tmpdir .join ('some/special/place/coverage-data*' ))) # noqa: PTH207
17251717
17261718
17271719@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
@@ -1748,7 +1740,7 @@ def test_external_data_file_negative(testdir):
17481740
17491741 result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , script )
17501742 assert result .ret == 0
1751- assert glob .glob (str (testdir .tmpdir .join ('.coverage*' )))
1743+ assert glob .glob (str (testdir .tmpdir .join ('.coverage*' ))) # noqa: PTH207
17521744
17531745
17541746@xdist_params
@@ -1856,7 +1848,7 @@ def test_do_not_append_coverage(pytester, testdir, opts, prop):
18561848
18571849@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
18581850def test_append_coverage_subprocess (testdir ):
1859- testdir .makepyprojecttoml (f """
1851+ testdir .makepyprojecttoml ("""
18601852[tool.coverage.run]
18611853patch = ["subprocess"]
18621854""" )
@@ -1970,7 +1962,7 @@ def find_labels(text, pattern):
19701962
19711963@xdist_params
19721964def test_contexts (pytester , testdir , opts ):
1973- with open ( os . path . join ( os . path . dirname ( __file__ ), 'contextful.py' )) as f :
1965+ with Path ( __file__ ). parent . joinpath ( 'contextful.py' ). open ( ) as f :
19741966 contextful_tests = f .read ()
19751967 script = testdir .makepyfile (contextful_tests )
19761968 result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , '--cov-context=test' , script , * opts .split ())
@@ -1987,7 +1979,7 @@ def test_contexts(pytester, testdir, opts):
19871979 measured = data .measured_files ()
19881980 assert len (measured ) == 1
19891981 test_context_path = next (iter (measured ))
1990- assert test_context_path .lower () == os .path .abspath ('test_contexts.py' ).lower ()
1982+ assert test_context_path .lower () == os .path .abspath ('test_contexts.py' ).lower () # noqa: PTH100
19911983
19921984 line_data = find_labels (contextful_tests , r'[crst]\d+(?:-\d+)?' )
19931985 for context , label in EXPECTED_CONTEXTS .items ():
0 commit comments