11
11
from collections .abc import Sequence
12
12
from fnmatch import fnmatch
13
13
from io import StringIO
14
+ from typing import Union
14
15
from weakref import WeakKeyDictionary
15
16
16
17
import py
@@ -362,9 +363,9 @@ class RunResult:
362
363
:ivar duration: duration in seconds
363
364
"""
364
365
365
- def __init__ (self , ret , outlines , errlines , duration ):
366
+ def __init__ (self , ret : Union [ int , ExitCode ], outlines , errlines , duration ) -> None :
366
367
try :
367
- self .ret = pytest .ExitCode (ret )
368
+ self .ret = pytest .ExitCode (ret ) # type: Union[int, ExitCode]
368
369
except ValueError :
369
370
self .ret = ret
370
371
self .outlines = outlines
@@ -483,11 +484,7 @@ def __init__(self, request, tmpdir_factory):
483
484
self ._sys_modules_snapshot = self .__take_sys_modules_snapshot ()
484
485
self .chdir ()
485
486
self .request .addfinalizer (self .finalize )
486
- method = self .request .config .getoption ("--runpytest" )
487
- if method == "inprocess" :
488
- self ._runpytest_method = self .runpytest_inprocess
489
- elif method == "subprocess" :
490
- self ._runpytest_method = self .runpytest_subprocess
487
+ self ._method = self .request .config .getoption ("--runpytest" )
491
488
492
489
mp = self .monkeypatch = MonkeyPatch ()
493
490
mp .setenv ("PYTEST_DEBUG_TEMPROOT" , str (self .test_tmproot ))
@@ -835,7 +832,7 @@ def pytest_configure(x, config):
835
832
reprec = rec .pop ()
836
833
else :
837
834
838
- class reprec :
835
+ class reprec : # type: ignore
839
836
pass
840
837
841
838
reprec .ret = ret
@@ -851,7 +848,7 @@ class reprec:
851
848
for finalizer in finalizers :
852
849
finalizer ()
853
850
854
- def runpytest_inprocess (self , * args , ** kwargs ):
851
+ def runpytest_inprocess (self , * args , ** kwargs ) -> RunResult :
855
852
"""Return result of running pytest in-process, providing a similar
856
853
interface to what self.runpytest() provides.
857
854
"""
@@ -866,15 +863,20 @@ def runpytest_inprocess(self, *args, **kwargs):
866
863
try :
867
864
reprec = self .inline_run (* args , ** kwargs )
868
865
except SystemExit as e :
866
+ ret = e .args [0 ]
867
+ try :
868
+ ret = ExitCode (e .args [0 ])
869
+ except ValueError :
870
+ pass
869
871
870
- class reprec :
871
- ret = e . args [ 0 ]
872
+ class reprec : # type: ignore
873
+ ret = ret
872
874
873
875
except Exception :
874
876
traceback .print_exc ()
875
877
876
- class reprec :
877
- ret = 3
878
+ class reprec : # type: ignore
879
+ ret = ExitCode ( 3 )
878
880
879
881
finally :
880
882
out , err = capture .readouterr ()
@@ -885,16 +887,20 @@ class reprec:
885
887
res = RunResult (
886
888
reprec .ret , out .splitlines (), err .splitlines (), time .time () - now
887
889
)
888
- res .reprec = reprec
890
+ res .reprec = reprec # type: ignore
889
891
return res
890
892
891
- def runpytest (self , * args , ** kwargs ):
893
+ def runpytest (self , * args , ** kwargs ) -> RunResult :
892
894
"""Run pytest inline or in a subprocess, depending on the command line
893
895
option "--runpytest" and return a :py:class:`RunResult`.
894
896
895
897
"""
896
898
args = self ._ensure_basetemp (args )
897
- return self ._runpytest_method (* args , ** kwargs )
899
+ if self ._method == "inprocess" :
900
+ return self .runpytest_inprocess (* args , ** kwargs )
901
+ elif self ._method == "subprocess" :
902
+ return self .runpytest_subprocess (* args , ** kwargs )
903
+ raise RuntimeError ("Unrecognized runpytest option: {}" .format (self ._method ))
898
904
899
905
def _ensure_basetemp (self , args ):
900
906
args = list (args )
@@ -1051,7 +1057,7 @@ def popen(
1051
1057
1052
1058
return popen
1053
1059
1054
- def run (self , * cmdargs , timeout = None , stdin = CLOSE_STDIN ):
1060
+ def run (self , * cmdargs , timeout = None , stdin = CLOSE_STDIN ) -> RunResult :
1055
1061
"""Run a command with arguments.
1056
1062
1057
1063
Run a process using subprocess.Popen saving the stdout and stderr.
@@ -1069,9 +1075,9 @@ def run(self, *cmdargs, timeout=None, stdin=CLOSE_STDIN):
1069
1075
"""
1070
1076
__tracebackhide__ = True
1071
1077
1072
- cmdargs = [
1078
+ cmdargs = tuple (
1073
1079
str (arg ) if isinstance (arg , py .path .local ) else arg for arg in cmdargs
1074
- ]
1080
+ )
1075
1081
p1 = self .tmpdir .join ("stdout" )
1076
1082
p2 = self .tmpdir .join ("stderr" )
1077
1083
print ("running:" , * cmdargs )
@@ -1122,6 +1128,10 @@ def handle_timeout():
1122
1128
f2 .close ()
1123
1129
self ._dump_lines (out , sys .stdout )
1124
1130
self ._dump_lines (err , sys .stderr )
1131
+ try :
1132
+ ret = ExitCode (ret )
1133
+ except ValueError :
1134
+ pass
1125
1135
return RunResult (ret , out , err , time .time () - now )
1126
1136
1127
1137
def _dump_lines (self , lines , fp ):
@@ -1134,7 +1144,7 @@ def _dump_lines(self, lines, fp):
1134
1144
def _getpytestargs (self ):
1135
1145
return sys .executable , "-mpytest"
1136
1146
1137
- def runpython (self , script ):
1147
+ def runpython (self , script ) -> RunResult :
1138
1148
"""Run a python script using sys.executable as interpreter.
1139
1149
1140
1150
Returns a :py:class:`RunResult`.
@@ -1146,7 +1156,7 @@ def runpython_c(self, command):
1146
1156
"""Run python -c "command", return a :py:class:`RunResult`."""
1147
1157
return self .run (sys .executable , "-c" , command )
1148
1158
1149
- def runpytest_subprocess (self , * args , timeout = None ):
1159
+ def runpytest_subprocess (self , * args , timeout = None ) -> RunResult :
1150
1160
"""Run pytest as a subprocess with given arguments.
1151
1161
1152
1162
Any plugins added to the :py:attr:`plugins` list will be added using the
0 commit comments