1
1
import importlib
2
2
import io
3
3
import operator
4
- import os
5
4
import queue
6
5
import sys
7
6
import textwrap
12
11
from typing import TYPE_CHECKING
13
12
from typing import Union
14
13
15
- import py
16
-
17
14
import _pytest
18
15
import pytest
19
16
from _pytest ._code .code import ExceptionChainRepr
20
17
from _pytest ._code .code import ExceptionInfo
21
18
from _pytest ._code .code import FormattedExcinfo
22
19
from _pytest ._io import TerminalWriter
20
+ from _pytest .monkeypatch import MonkeyPatch
21
+ from _pytest .pathlib import bestrelpath
23
22
from _pytest .pathlib import import_path
24
23
from _pytest .pytester import LineMatcher
25
24
from _pytest .pytester import Pytester
@@ -150,9 +149,10 @@ def xyz():
150
149
" except somenoname: # type: ignore[name-defined] # noqa: F821" ,
151
150
]
152
151
153
- def test_traceback_cut (self ):
152
+ def test_traceback_cut (self ) -> None :
154
153
co = _pytest ._code .Code .from_function (f )
155
154
path , firstlineno = co .path , co .firstlineno
155
+ assert isinstance (path , Path )
156
156
traceback = self .excinfo .traceback
157
157
newtraceback = traceback .cut (path = path , firstlineno = firstlineno )
158
158
assert len (newtraceback ) == 1
@@ -163,11 +163,11 @@ def test_traceback_cut_excludepath(self, pytester: Pytester) -> None:
163
163
p = pytester .makepyfile ("def f(): raise ValueError" )
164
164
with pytest .raises (ValueError ) as excinfo :
165
165
import_path (p ).f () # type: ignore[attr-defined]
166
- basedir = py . path . local (pytest .__file__ ).dirpath ()
166
+ basedir = Path (pytest .__file__ ).parent
167
167
newtraceback = excinfo .traceback .cut (excludepath = basedir )
168
168
for x in newtraceback :
169
- if hasattr ( x , "path" ):
170
- assert not py . path . local ( x .path ). relto ( basedir )
169
+ assert isinstance ( x . path , Path )
170
+ assert basedir not in x .path . parents
171
171
assert newtraceback [- 1 ].frame .code .path == p
172
172
173
173
def test_traceback_filter (self ):
@@ -376,7 +376,7 @@ def test_excinfo_no_python_sourcecode(tmpdir):
376
376
for item in excinfo .traceback :
377
377
print (item ) # XXX: for some reason jinja.Template.render is printed in full
378
378
item .source # shouldn't fail
379
- if isinstance (item .path , py . path . local ) and item .path .basename == "test.txt" :
379
+ if isinstance (item .path , Path ) and item .path .name == "test.txt" :
380
380
assert str (item .source ) == "{{ h()}}:"
381
381
382
382
@@ -392,16 +392,16 @@ def test_entrysource_Queue_example():
392
392
assert s .startswith ("def get" )
393
393
394
394
395
- def test_codepath_Queue_example ():
395
+ def test_codepath_Queue_example () -> None :
396
396
try :
397
397
queue .Queue ().get (timeout = 0.001 )
398
398
except queue .Empty :
399
399
excinfo = _pytest ._code .ExceptionInfo .from_current ()
400
400
entry = excinfo .traceback [- 1 ]
401
401
path = entry .path
402
- assert isinstance (path , py . path . local )
403
- assert path .basename .lower () == "queue.py"
404
- assert path .check ()
402
+ assert isinstance (path , Path )
403
+ assert path .name .lower () == "queue.py"
404
+ assert path .exists ()
405
405
406
406
407
407
def test_match_succeeds ():
@@ -805,21 +805,21 @@ def entry():
805
805
806
806
raised = 0
807
807
808
- orig_getcwd = os . getcwd
808
+ orig_path_cwd = Path . cwd
809
809
810
810
def raiseos ():
811
811
nonlocal raised
812
812
upframe = sys ._getframe ().f_back
813
813
assert upframe is not None
814
- if upframe .f_code .co_name == "checked_call " :
814
+ if upframe .f_code .co_name == "_makepath " :
815
815
# Only raise with expected calls, but not via e.g. inspect for
816
816
# py38-windows.
817
817
raised += 1
818
818
raise OSError (2 , "custom_oserror" )
819
- return orig_getcwd ()
819
+ return orig_path_cwd ()
820
820
821
- monkeypatch .setattr (os , "getcwd " , raiseos )
822
- assert p ._makepath (__file__ ) == __file__
821
+ monkeypatch .setattr (Path , "cwd " , raiseos )
822
+ assert p ._makepath (Path ( __file__ ) ) == __file__
823
823
assert raised == 1
824
824
repr_tb = p .repr_traceback (excinfo )
825
825
@@ -1015,33 +1015,32 @@ def f():
1015
1015
assert line .endswith ("mod.py" )
1016
1016
assert tw_mock .lines [10 ] == ":3: ValueError"
1017
1017
1018
- def test_toterminal_long_filenames (self , importasmod , tw_mock ):
1018
+ def test_toterminal_long_filenames (
1019
+ self , importasmod , tw_mock , monkeypatch : MonkeyPatch
1020
+ ) -> None :
1019
1021
mod = importasmod (
1020
1022
"""
1021
1023
def f():
1022
1024
raise ValueError()
1023
1025
"""
1024
1026
)
1025
1027
excinfo = pytest .raises (ValueError , mod .f )
1026
- path = py .path .local (mod .__file__ )
1027
- old = path .dirpath ().chdir ()
1028
- try :
1029
- repr = excinfo .getrepr (abspath = False )
1030
- repr .toterminal (tw_mock )
1031
- x = py .path .local ().bestrelpath (path )
1032
- if len (x ) < len (str (path )):
1033
- msg = tw_mock .get_write_msg (- 2 )
1034
- assert msg == "mod.py"
1035
- assert tw_mock .lines [- 1 ] == ":3: ValueError"
1036
-
1037
- repr = excinfo .getrepr (abspath = True )
1038
- repr .toterminal (tw_mock )
1028
+ path = Path (mod .__file__ )
1029
+ monkeypatch .chdir (path .parent )
1030
+ repr = excinfo .getrepr (abspath = False )
1031
+ repr .toterminal (tw_mock )
1032
+ x = bestrelpath (Path .cwd (), path )
1033
+ if len (x ) < len (str (path )):
1039
1034
msg = tw_mock .get_write_msg (- 2 )
1040
- assert msg == path
1041
- line = tw_mock .lines [- 1 ]
1042
- assert line == ":3: ValueError"
1043
- finally :
1044
- old .chdir ()
1035
+ assert msg == "mod.py"
1036
+ assert tw_mock .lines [- 1 ] == ":3: ValueError"
1037
+
1038
+ repr = excinfo .getrepr (abspath = True )
1039
+ repr .toterminal (tw_mock )
1040
+ msg = tw_mock .get_write_msg (- 2 )
1041
+ assert msg == str (path )
1042
+ line = tw_mock .lines [- 1 ]
1043
+ assert line == ":3: ValueError"
1045
1044
1046
1045
@pytest .mark .parametrize (
1047
1046
"reproptions" ,
0 commit comments