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
- 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
25
21
26
22
max_worker_restart_0 = '--max-worker-restart=0'
27
23
@@ -168,7 +164,7 @@ def test_foo(cov):
168
164
def adjust_sys_path ():
169
165
"""Adjust PYTHONPATH during tests to make "helper" importable in SCRIPT."""
170
166
orig_path = os .environ .get ('PYTHONPATH' , None )
171
- new_path = os . path . dirname ( __file__ )
167
+ new_path = str ( Path ( __file__ ). parent )
172
168
if orig_path is not None :
173
169
new_path = os .pathsep .join ([new_path , orig_path ])
174
170
os .environ ['PYTHONPATH' ] = new_path
@@ -191,7 +187,7 @@ def adjust_sys_path():
191
187
ids = ['branch2x' , 'branch1c' , 'branch1a' , 'nobranch' ],
192
188
)
193
189
def prop (request ):
194
- return Namespace (
190
+ return SimpleNamespace (
195
191
code = SCRIPT ,
196
192
code2 = SCRIPT2 ,
197
193
conf = request .param [0 ],
@@ -348,7 +344,7 @@ def test_xml_output_dir(testdir):
348
344
def test_json_output_dir (testdir ):
349
345
script = testdir .makepyfile (SCRIPT )
350
346
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 )
352
348
353
349
result .stdout .fnmatch_lines (
354
350
[
@@ -364,7 +360,7 @@ def test_json_output_dir(testdir):
364
360
def test_markdown_output_dir (testdir ):
365
361
script = testdir .makepyfile (SCRIPT )
366
362
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 )
368
364
369
365
result .stdout .fnmatch_lines (
370
366
[
@@ -380,7 +376,7 @@ def test_markdown_output_dir(testdir):
380
376
def test_markdown_append_output_dir (testdir ):
381
377
script = testdir .makepyfile (SCRIPT )
382
378
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 )
384
380
385
381
result .stdout .fnmatch_lines (
386
382
[
@@ -398,7 +394,7 @@ def test_markdown_and_markdown_append_work_together(testdir):
398
394
399
395
result = testdir .runpytest (
400
396
'-v' ,
401
- '--cov=%s' % script .dirpath (),
397
+ f '--cov={ script .dirpath ()} ' ,
402
398
'--cov-report=markdown:' + MARKDOWN_REPORT_NAME ,
403
399
'--cov-report=markdown-append:' + MARKDOWN_APPEND_REPORT_NAME ,
404
400
script ,
@@ -421,7 +417,7 @@ def test_markdown_and_markdown_append_pointing_to_same_file_throws_error(testdir
421
417
422
418
result = testdir .runpytest (
423
419
'-v' ,
424
- '--cov=%s' % script .dirpath (),
420
+ f '--cov={ script .dirpath ()} ' ,
425
421
'--cov-report=markdown:' + MARKDOWN_REPORT_NAME ,
426
422
'--cov-report=markdown-append:' + MARKDOWN_REPORT_NAME ,
427
423
script ,
@@ -467,7 +463,7 @@ def test_term_missing_output_dir(testdir):
467
463
468
464
result .stderr .fnmatch_lines (
469
465
[
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 } "*' ,
471
467
]
472
468
)
473
469
assert result .ret != 0
@@ -754,7 +750,7 @@ def test_add_task(celery_worker):
754
750
result .stdout .fnmatch_lines (
755
751
[
756
752
'*_ coverage: platform *, python * _*' ,
757
- f 'small_celery* 100%*' ,
753
+ 'small_celery* 100%*' ,
758
754
]
759
755
)
760
756
assert result .ret == 0
@@ -1337,7 +1333,7 @@ def test_run():
1337
1333
'-v' , '--assert=plain' , f'--cov={ script .dirpath ()} ' , '--cov-report=term-missing' , '--cov-report=html' , script
1338
1334
)
1339
1335
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*' ])
1341
1337
assert result .ret == 0
1342
1338
1343
1339
@@ -1644,7 +1640,7 @@ def test_dist_bare_cov(testdir):
1644
1640
1645
1641
def test_not_started_plugin_does_not_fail (testdir ):
1646
1642
class ns :
1647
- cov_source = [ True ]
1643
+ cov_source = ( True ,)
1648
1644
cov_report = ''
1649
1645
1650
1646
plugin = pytest_cov .plugin .CovPlugin (ns , None , start = False )
@@ -1692,14 +1688,13 @@ def test_external_data_file(testdir):
1692
1688
testdir .tmpdir .join ('.coveragerc' ).write (
1693
1689
"""
1694
1690
[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 ())
1698
1693
)
1699
1694
1700
1695
result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , script )
1701
1696
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
1703
1698
1704
1699
1705
1700
@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
@@ -1709,14 +1704,13 @@ def test_external_data_file_xdist(testdir):
1709
1704
"""
1710
1705
[run]
1711
1706
parallel = 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 ())
1715
1709
)
1716
1710
1717
1711
result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , '-n' , '1' , max_worker_restart_0 , script )
1718
1712
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
1720
1714
1721
1715
1722
1716
@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
@@ -1743,7 +1737,7 @@ def test_external_data_file_negative(testdir):
1743
1737
1744
1738
result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , script )
1745
1739
assert result .ret == 0
1746
- assert glob .glob (str (testdir .tmpdir .join ('.coverage*' )))
1740
+ assert glob .glob (str (testdir .tmpdir .join ('.coverage*' ))) # noqa: PTH207
1747
1741
1748
1742
1749
1743
@xdist_params
@@ -1851,7 +1845,7 @@ def test_do_not_append_coverage(pytester, testdir, opts, prop):
1851
1845
1852
1846
@pytest .mark .skipif ('sys.platform == "win32" and platform.python_implementation() == "PyPy"' )
1853
1847
def test_append_coverage_subprocess (testdir ):
1854
- testdir .makepyprojecttoml (f """
1848
+ testdir .makepyprojecttoml ("""
1855
1849
[tool.coverage.run]
1856
1850
patch = ["subprocess"]
1857
1851
""" )
@@ -1965,7 +1959,7 @@ def find_labels(text, pattern):
1965
1959
1966
1960
@xdist_params
1967
1961
def 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 :
1969
1963
contextful_tests = f .read ()
1970
1964
script = testdir .makepyfile (contextful_tests )
1971
1965
result = testdir .runpytest ('-v' , f'--cov={ script .dirpath ()} ' , '--cov-context=test' , script , * opts .split ())
@@ -1982,7 +1976,7 @@ def test_contexts(pytester, testdir, opts):
1982
1976
measured = data .measured_files ()
1983
1977
assert len (measured ) == 1
1984
1978
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
1986
1980
1987
1981
line_data = find_labels (contextful_tests , r'[crst]\d+(?:-\d+)?' )
1988
1982
for context , label in EXPECTED_CONTEXTS .items ():
0 commit comments