1
- # ruff: noqa
2
1
import collections
3
2
import glob
4
3
import os
5
4
import platform
6
5
import re
7
- import subprocess
8
6
import sys
9
- from io import StringIO
10
7
from itertools import chain
8
+ from pathlib import Path
9
+ from types import SimpleNamespace
11
10
12
11
import coverage
13
- import py
14
12
import pytest
15
- import virtualenv
16
- import xdist
17
- from fields import Namespace
18
13
from process_tests import TestProcess as _TestProcess
19
14
from process_tests import dump_on_error
20
15
from process_tests import wait_for_strings
21
16
22
17
import pytest_cov .plugin
23
18
24
-
25
19
max_worker_restart_0 = '--max-worker-restart=0'
26
20
27
21
SCRIPT = """
@@ -167,7 +161,7 @@ def test_foo(cov):
167
161
def adjust_sys_path ():
168
162
"""Adjust PYTHONPATH during tests to make "helper" importable in SCRIPT."""
169
163
orig_path = os .environ .get ('PYTHONPATH' , None )
170
- new_path = os . path . dirname ( __file__ )
164
+ new_path = str ( Path ( __file__ ). parent )
171
165
if orig_path is not None :
172
166
new_path = os .pathsep .join ([new_path , orig_path ])
173
167
os .environ ['PYTHONPATH' ] = new_path
@@ -190,7 +184,7 @@ def adjust_sys_path():
190
184
ids = ['branch2x' , 'branch1c' , 'branch1a' , 'nobranch' ],
191
185
)
192
186
def prop (request ):
193
- return Namespace (
187
+ return SimpleNamespace (
194
188
code = SCRIPT ,
195
189
code2 = SCRIPT2 ,
196
190
conf = request .param [0 ],
@@ -347,7 +341,7 @@ def test_xml_output_dir(testdir):
347
341
def test_json_output_dir (testdir ):
348
342
script = testdir .makepyfile (SCRIPT )
349
343
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 )
351
345
352
346
result .stdout .fnmatch_lines (
353
347
[
@@ -363,7 +357,7 @@ def test_json_output_dir(testdir):
363
357
def test_markdown_output_dir (testdir ):
364
358
script = testdir .makepyfile (SCRIPT )
365
359
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 )
367
361
368
362
result .stdout .fnmatch_lines (
369
363
[
@@ -379,7 +373,7 @@ def test_markdown_output_dir(testdir):
379
373
def test_markdown_append_output_dir (testdir ):
380
374
script = testdir .makepyfile (SCRIPT )
381
375
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 )
383
377
384
378
result .stdout .fnmatch_lines (
385
379
[
@@ -397,7 +391,7 @@ def test_markdown_and_markdown_append_work_together(testdir):
397
391
398
392
result = testdir .runpytest (
399
393
'-v' ,
400
- '--cov=%s' % script .dirpath (),
394
+ f '--cov={ script .dirpath ()} ' ,
401
395
'--cov-report=markdown:' + MARKDOWN_REPORT_NAME ,
402
396
'--cov-report=markdown-append:' + MARKDOWN_APPEND_REPORT_NAME ,
403
397
script ,
@@ -420,7 +414,7 @@ def test_markdown_and_markdown_append_pointing_to_same_file_throws_error(testdir
420
414
421
415
result = testdir .runpytest (
422
416
'-v' ,
423
- '--cov=%s' % script .dirpath (),
417
+ f '--cov={ script .dirpath ()} ' ,
424
418
'--cov-report=markdown:' + MARKDOWN_REPORT_NAME ,
425
419
'--cov-report=markdown-append:' + MARKDOWN_REPORT_NAME ,
426
420
script ,
@@ -466,7 +460,7 @@ def test_term_missing_output_dir(testdir):
466
460
467
461
result .stderr .fnmatch_lines (
468
462
[
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 } "*' ,
470
464
]
471
465
)
472
466
assert result .ret != 0
@@ -755,7 +749,7 @@ def test_add_task(celery_worker):
755
749
result .stdout .fnmatch_lines (
756
750
[
757
751
'*_ coverage: platform *, python * _*' ,
758
- f 'small_celery* 100%*' ,
752
+ 'small_celery* 100%*' ,
759
753
]
760
754
)
761
755
assert result .ret == 0
@@ -1353,7 +1347,7 @@ def test_run():
1353
1347
'-v' , '--assert=plain' , f'--cov={ script .dirpath ()} ' , '--cov-report=term-missing' , '--cov-report=html' , script
1354
1348
)
1355
1349
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*' ])
1357
1351
assert result .ret == 0
1358
1352
1359
1353
@@ -1649,7 +1643,7 @@ def test_dist_bare_cov(testdir):
1649
1643
1650
1644
def test_not_started_plugin_does_not_fail (testdir ):
1651
1645
class ns :
1652
- cov_source = [ True ]
1646
+ cov_source = ( True ,)
1653
1647
cov_report = ''
1654
1648
1655
1649
plugin = pytest_cov .plugin .CovPlugin (ns , None , start = False )
@@ -1697,14 +1691,13 @@ def test_external_data_file(testdir):
1697
1691
testdir .tmpdir .join ('.coveragerc' ).write (
1698
1692
"""
1699
1693
[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 ())
1703
1696
)
1704
1697
1705
1698
result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , script )
1706
1699
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
1708
1701
1709
1702
1710
1703
@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
@@ -1714,14 +1707,13 @@ def test_external_data_file_xdist(testdir):
1714
1707
"""
1715
1708
[run]
1716
1709
parallel = 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 ())
1720
1712
)
1721
1713
1722
1714
result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , '-n' , '1' , max_worker_restart_0 , script )
1723
1715
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
1725
1717
1726
1718
1727
1719
@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
@@ -1748,7 +1740,7 @@ def test_external_data_file_negative(testdir):
1748
1740
1749
1741
result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , script )
1750
1742
assert result .ret == 0
1751
- assert glob .glob (str (testdir .tmpdir .join ('.coverage*' )))
1743
+ assert glob .glob (str (testdir .tmpdir .join ('.coverage*' ))) # noqa: PTH207
1752
1744
1753
1745
1754
1746
@xdist_params
@@ -1856,7 +1848,7 @@ def test_do_not_append_coverage(pytester, testdir, opts, prop):
1856
1848
1857
1849
@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
1858
1850
def test_append_coverage_subprocess (testdir ):
1859
- testdir .makepyprojecttoml (f """
1851
+ testdir .makepyprojecttoml ("""
1860
1852
[tool.coverage.run]
1861
1853
patch = ["subprocess"]
1862
1854
""" )
@@ -1970,7 +1962,7 @@ def find_labels(text, pattern):
1970
1962
1971
1963
@xdist_params
1972
1964
def 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 :
1974
1966
contextful_tests = f .read ()
1975
1967
script = testdir .makepyfile (contextful_tests )
1976
1968
result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , '--cov-context=test' , script , * opts .split ())
@@ -1987,7 +1979,7 @@ def test_contexts(pytester, testdir, opts):
1987
1979
measured = data .measured_files ()
1988
1980
assert len (measured ) == 1
1989
1981
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
1991
1983
1992
1984
line_data = find_labels (contextful_tests , r'[crst]\d+(?:-\d+)?' )
1993
1985
for context , label in EXPECTED_CONTEXTS .items ():
0 commit comments