10
10
import traceback
11
11
from collections .abc import Sequence
12
12
from fnmatch import fnmatch
13
+ from typing import Union
13
14
from weakref import WeakKeyDictionary
14
15
15
16
import py
@@ -361,7 +362,7 @@ class RunResult:
361
362
:ivar duration: duration in seconds
362
363
"""
363
364
364
- def __init__ (self , ret , outlines , errlines , duration ):
365
+ def __init__ (self , ret : Union [ int , ExitCode ] , outlines , errlines , duration ):
365
366
self .ret = ret
366
367
self .outlines = outlines
367
368
self .errlines = errlines
@@ -479,11 +480,7 @@ def __init__(self, request, tmpdir_factory):
479
480
self ._sys_modules_snapshot = self .__take_sys_modules_snapshot ()
480
481
self .chdir ()
481
482
self .request .addfinalizer (self .finalize )
482
- method = self .request .config .getoption ("--runpytest" )
483
- if method == "inprocess" :
484
- self ._runpytest_method = self .runpytest_inprocess
485
- elif method == "subprocess" :
486
- self ._runpytest_method = self .runpytest_subprocess
483
+ self ._method = self .request .config .getoption ("--runpytest" )
487
484
488
485
mp = self .monkeypatch = MonkeyPatch ()
489
486
mp .setenv ("PYTEST_DEBUG_TEMPROOT" , str (self .test_tmproot ))
@@ -831,7 +828,7 @@ def pytest_configure(x, config):
831
828
reprec = rec .pop ()
832
829
else :
833
830
834
- class reprec :
831
+ class reprec : # type: ignore
835
832
pass
836
833
837
834
reprec .ret = ret
@@ -847,7 +844,7 @@ class reprec:
847
844
for finalizer in finalizers :
848
845
finalizer ()
849
846
850
- def runpytest_inprocess (self , * args , ** kwargs ):
847
+ def runpytest_inprocess (self , * args , ** kwargs ) -> RunResult :
851
848
"""Return result of running pytest in-process, providing a similar
852
849
interface to what self.runpytest() provides.
853
850
"""
@@ -862,15 +859,20 @@ def runpytest_inprocess(self, *args, **kwargs):
862
859
try :
863
860
reprec = self .inline_run (* args , ** kwargs )
864
861
except SystemExit as e :
862
+ ret = e .args [0 ]
863
+ try :
864
+ ret = ExitCode (e .args [0 ])
865
+ except ValueError :
866
+ pass
865
867
866
- class reprec :
867
- ret = e . args [ 0 ]
868
+ class reprec : # type: ignore
869
+ ret = ret
868
870
869
871
except Exception :
870
872
traceback .print_exc ()
871
873
872
- class reprec :
873
- ret = 3
874
+ class reprec : # type: ignore
875
+ ret = ExitCode ( 3 )
874
876
875
877
finally :
876
878
out , err = capture .readouterr ()
@@ -879,16 +881,19 @@ class reprec:
879
881
sys .stderr .write (err )
880
882
881
883
res = RunResult (reprec .ret , out .split ("\n " ), err .split ("\n " ), time .time () - now )
882
- res .reprec = reprec
884
+ res .reprec = reprec # type: ignore
883
885
return res
884
886
885
- def runpytest (self , * args , ** kwargs ):
887
+ def runpytest (self , * args , ** kwargs ) -> RunResult :
886
888
"""Run pytest inline or in a subprocess, depending on the command line
887
889
option "--runpytest" and return a :py:class:`RunResult`.
888
890
889
891
"""
890
892
args = self ._ensure_basetemp (args )
891
- return self ._runpytest_method (* args , ** kwargs )
893
+ if self ._method == "inprocess" :
894
+ return self .runpytest_inprocess (* args , ** kwargs )
895
+ assert self ._method == "subprocess"
896
+ return self .runpytest_subprocess (* args , ** kwargs )
892
897
893
898
def _ensure_basetemp (self , args ):
894
899
args = list (args )
@@ -1045,7 +1050,7 @@ def popen(
1045
1050
1046
1051
return popen
1047
1052
1048
- def run (self , * cmdargs , timeout = None , stdin = CLOSE_STDIN ):
1053
+ def run (self , * cmdargs , timeout = None , stdin = CLOSE_STDIN ) -> RunResult :
1049
1054
"""Run a command with arguments.
1050
1055
1051
1056
Run a process using subprocess.Popen saving the stdout and stderr.
@@ -1063,9 +1068,9 @@ def run(self, *cmdargs, timeout=None, stdin=CLOSE_STDIN):
1063
1068
"""
1064
1069
__tracebackhide__ = True
1065
1070
1066
- cmdargs = [
1071
+ cmdargs = tuple (
1067
1072
str (arg ) if isinstance (arg , py .path .local ) else arg for arg in cmdargs
1068
- ]
1073
+ )
1069
1074
p1 = self .tmpdir .join ("stdout" )
1070
1075
p2 = self .tmpdir .join ("stderr" )
1071
1076
print ("running:" , * cmdargs )
@@ -1116,6 +1121,10 @@ def handle_timeout():
1116
1121
f2 .close ()
1117
1122
self ._dump_lines (out , sys .stdout )
1118
1123
self ._dump_lines (err , sys .stderr )
1124
+ try :
1125
+ ret = ExitCode (ret )
1126
+ except ValueError :
1127
+ pass
1119
1128
return RunResult (ret , out , err , time .time () - now )
1120
1129
1121
1130
def _dump_lines (self , lines , fp ):
@@ -1128,7 +1137,7 @@ def _dump_lines(self, lines, fp):
1128
1137
def _getpytestargs (self ):
1129
1138
return sys .executable , "-mpytest"
1130
1139
1131
- def runpython (self , script ):
1140
+ def runpython (self , script ) -> RunResult :
1132
1141
"""Run a python script using sys.executable as interpreter.
1133
1142
1134
1143
Returns a :py:class:`RunResult`.
@@ -1140,7 +1149,7 @@ def runpython_c(self, command):
1140
1149
"""Run python -c "command", return a :py:class:`RunResult`."""
1141
1150
return self .run (sys .executable , "-c" , command )
1142
1151
1143
- def runpytest_subprocess (self , * args , timeout = None ):
1152
+ def runpytest_subprocess (self , * args , timeout = None ) -> RunResult :
1144
1153
"""Run pytest as a subprocess with given arguments.
1145
1154
1146
1155
Any plugins added to the :py:attr:`plugins` list will be added using the
0 commit comments