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- coverage , platform # required for skipif mark on test_cov_min_from_coveragerc
19+ # required for skipif mark on test_cov_min_from_coveragerc
20+ coverage , platform # noqa: B018
2521
2622max_worker_restart_0 = '--max-worker-restart=0'
2723
@@ -168,7 +164,7 @@ def test_foo(cov):
168164def adjust_sys_path ():
169165 """Adjust PYTHONPATH during tests to make "helper" importable in SCRIPT."""
170166 orig_path = os .environ .get ('PYTHONPATH' , None )
171- new_path = os . path . dirname ( __file__ )
167+ new_path = str ( Path ( __file__ ). parent )
172168 if orig_path is not None :
173169 new_path = os .pathsep .join ([new_path , orig_path ])
174170 os .environ ['PYTHONPATH' ] = new_path
@@ -191,7 +187,7 @@ def adjust_sys_path():
191187 ids = ['branch2x' , 'branch1c' , 'branch1a' , 'nobranch' ],
192188)
193189def prop (request ):
194- return Namespace (
190+ return SimpleNamespace (
195191 code = SCRIPT ,
196192 code2 = SCRIPT2 ,
197193 conf = request .param [0 ],
@@ -348,7 +344,7 @@ def test_xml_output_dir(testdir):
348344def test_json_output_dir (testdir ):
349345 script = testdir .makepyfile (SCRIPT )
350346
351- result = testdir .runpytest ('-v' , '--cov=%s' % script .dirpath (), '--cov-report=json:' + JSON_REPORT_NAME , script )
347+ result = testdir .runpytest ('-v' , f '--cov={ script .dirpath ()} ' , '--cov-report=json:' + JSON_REPORT_NAME , script )
352348
353349 result .stdout .fnmatch_lines (
354350 [
@@ -364,7 +360,7 @@ def test_json_output_dir(testdir):
364360def test_markdown_output_dir (testdir ):
365361 script = testdir .makepyfile (SCRIPT )
366362
367- result = testdir .runpytest ('-v' , '--cov=%s' % script .dirpath (), '--cov-report=markdown:' + MARKDOWN_REPORT_NAME , script )
363+ result = testdir .runpytest ('-v' , f '--cov={ script .dirpath ()} ' , '--cov-report=markdown:' + MARKDOWN_REPORT_NAME , script )
368364
369365 result .stdout .fnmatch_lines (
370366 [
@@ -380,7 +376,7 @@ def test_markdown_output_dir(testdir):
380376def test_markdown_append_output_dir (testdir ):
381377 script = testdir .makepyfile (SCRIPT )
382378
383- result = testdir .runpytest ('-v' , '--cov=%s' % script .dirpath (), '--cov-report=markdown-append:' + MARKDOWN_APPEND_REPORT_NAME , script )
379+ result = testdir .runpytest ('-v' , f '--cov={ script .dirpath ()} ' , '--cov-report=markdown-append:' + MARKDOWN_APPEND_REPORT_NAME , script )
384380
385381 result .stdout .fnmatch_lines (
386382 [
@@ -398,7 +394,7 @@ def test_markdown_and_markdown_append_work_together(testdir):
398394
399395 result = testdir .runpytest (
400396 '-v' ,
401- '--cov=%s' % script .dirpath (),
397+ f '--cov={ script .dirpath ()} ' ,
402398 '--cov-report=markdown:' + MARKDOWN_REPORT_NAME ,
403399 '--cov-report=markdown-append:' + MARKDOWN_APPEND_REPORT_NAME ,
404400 script ,
@@ -421,7 +417,7 @@ def test_markdown_and_markdown_append_pointing_to_same_file_throws_error(testdir
421417
422418 result = testdir .runpytest (
423419 '-v' ,
424- '--cov=%s' % script .dirpath (),
420+ f '--cov={ script .dirpath ()} ' ,
425421 '--cov-report=markdown:' + MARKDOWN_REPORT_NAME ,
426422 '--cov-report=markdown-append:' + MARKDOWN_REPORT_NAME ,
427423 script ,
@@ -467,7 +463,7 @@ def test_term_missing_output_dir(testdir):
467463
468464 result .stderr .fnmatch_lines (
469465 [
470- '*argument --cov-report: output specifier not supported for: "term-missing:%s "*' % DEST_DIR ,
466+ f '*argument --cov-report: output specifier not supported for: "term-missing:{ DEST_DIR } "*' ,
471467 ]
472468 )
473469 assert result .ret != 0
@@ -754,7 +750,7 @@ def test_add_task(celery_worker):
754750 result .stdout .fnmatch_lines (
755751 [
756752 '*_ coverage: platform *, python * _*' ,
757- f 'small_celery* 100%*' ,
753+ 'small_celery* 100%*' ,
758754 ]
759755 )
760756 assert result .ret == 0
@@ -1337,7 +1333,7 @@ def test_run():
13371333 '-v' , '--assert=plain' , f'--cov={ script .dirpath ()} ' , '--cov-report=term-missing' , '--cov-report=html' , script
13381334 )
13391335
1340- result .stdout .fnmatch_lines (['*_ coverage: platform *, python * _*' , f 'test_cleanup_on_sigterm* 88% * 18-19' , '*1 passed*' ])
1336+ result .stdout .fnmatch_lines (['*_ coverage: platform *, python * _*' , 'test_cleanup_on_sigterm* 88% * 18-19' , '*1 passed*' ])
13411337 assert result .ret == 0
13421338
13431339
@@ -1644,7 +1640,7 @@ def test_dist_bare_cov(testdir):
16441640
16451641def test_not_started_plugin_does_not_fail (testdir ):
16461642 class ns :
1647- cov_source = [ True ]
1643+ cov_source = ( True ,)
16481644 cov_report = ''
16491645
16501646 plugin = pytest_cov .plugin .CovPlugin (ns , None , start = False )
@@ -1692,14 +1688,13 @@ def test_external_data_file(testdir):
16921688 testdir .tmpdir .join ('.coveragerc' ).write (
16931689 """
16941690[run]
1695- data_file = %s
1696- """
1697- % testdir .tmpdir .join ('some/special/place/coverage-data' ).ensure ()
1691+ data_file = {}
1692+ """ .format (testdir .tmpdir .join ('some/special/place/coverage-data' ).ensure ())
16981693 )
16991694
17001695 result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , script )
17011696 assert result .ret == 0
1702- assert glob .glob (str (testdir .tmpdir .join ('some/special/place/coverage-data*' )))
1697+ assert glob .glob (str (testdir .tmpdir .join ('some/special/place/coverage-data*' ))) # noqa: PTH207
17031698
17041699
17051700@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
@@ -1709,14 +1704,13 @@ def test_external_data_file_xdist(testdir):
17091704 """
17101705[run]
17111706parallel = true
1712- data_file = %s
1713- """
1714- % testdir .tmpdir .join ('some/special/place/coverage-data' ).ensure ()
1707+ data_file = {}
1708+ """ .format (testdir .tmpdir .join ('some/special/place/coverage-data' ).ensure ())
17151709 )
17161710
17171711 result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , '-n' , '1' , max_worker_restart_0 , script )
17181712 assert result .ret == 0
1719- assert glob .glob (str (testdir .tmpdir .join ('some/special/place/coverage-data*' )))
1713+ assert glob .glob (str (testdir .tmpdir .join ('some/special/place/coverage-data*' ))) # noqa: PTH207
17201714
17211715
17221716@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
@@ -1743,7 +1737,7 @@ def test_external_data_file_negative(testdir):
17431737
17441738 result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , script )
17451739 assert result .ret == 0
1746- assert glob .glob (str (testdir .tmpdir .join ('.coverage*' )))
1740+ assert glob .glob (str (testdir .tmpdir .join ('.coverage*' ))) # noqa: PTH207
17471741
17481742
17491743@xdist_params
@@ -1851,7 +1845,7 @@ def test_do_not_append_coverage(pytester, testdir, opts, prop):
18511845
18521846@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
18531847def test_append_coverage_subprocess (testdir ):
1854- testdir .makepyprojecttoml (f """
1848+ testdir .makepyprojecttoml ("""
18551849[tool.coverage.run]
18561850patch = ["subprocess"]
18571851""" )
@@ -1965,7 +1959,7 @@ def find_labels(text, pattern):
19651959
19661960@xdist_params
19671961def test_contexts (pytester , testdir , opts ):
1968- with open ( os . path . join ( os . path . dirname ( __file__ ), 'contextful.py' )) as f :
1962+ with Path ( __file__ ). parent . joinpath ( 'contextful.py' ). open ( ) as f :
19691963 contextful_tests = f .read ()
19701964 script = testdir .makepyfile (contextful_tests )
19711965 result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , '--cov-context=test' , script , * opts .split ())
@@ -1982,7 +1976,7 @@ def test_contexts(pytester, testdir, opts):
19821976 measured = data .measured_files ()
19831977 assert len (measured ) == 1
19841978 test_context_path = next (iter (measured ))
1985- assert test_context_path .lower () == os .path .abspath ('test_contexts.py' ).lower ()
1979+ assert test_context_path .lower () == os .path .abspath ('test_contexts.py' ).lower () # noqa: PTH100
19861980
19871981 line_data = find_labels (contextful_tests , r'[crst]\d+(?:-\d+)?' )
19881982 for context , label in EXPECTED_CONTEXTS .items ():
0 commit comments