65
65
import click .testing
66
66
import pytest # nodep
67
67
from coincidence .regressions import check_file_regression # nodep
68
+ from domdf_python_tools .compat import importlib_metadata
68
69
from pytest_regressions .file_regression import FileRegressionFixture # nodep
69
70
from typing_extensions import Literal
70
71
71
72
__all__ = ("CliRunner" , "Result" , "cli_runner" )
72
73
73
- _click_major = int ( click . __version__ . split ('.' )[ 0 ] )
74
+ _click_version = tuple ( map ( int , importlib_metadata . version ( " click" ). split ('.' )) )
74
75
75
76
76
77
class Result (click .testing .Result ):
@@ -101,9 +102,21 @@ def __init__(
101
102
exit_code : int ,
102
103
exception : Optional [BaseException ],
103
104
exc_info : Optional [Tuple [Type [BaseException ], BaseException , TracebackType ]] = None ,
105
+ output_bytes : Optional [bytes ] = None ,
104
106
) -> None :
105
107
106
- if _click_major >= 8 :
108
+ if _click_version >= (8 , 2 ):
109
+ super ().__init__ (
110
+ runner = runner ,
111
+ stdout_bytes = stdout_bytes ,
112
+ stderr_bytes = stderr_bytes ,
113
+ output_bytes = output_bytes , # type: ignore[call-arg]
114
+ exit_code = exit_code ,
115
+ exception = exception ,
116
+ exc_info = exc_info ,
117
+ return_value = None ,
118
+ )
119
+ elif _click_version [0 ] >= 8 :
107
120
super ().__init__ (
108
121
runner = runner ,
109
122
stdout_bytes = stdout_bytes ,
@@ -129,6 +142,10 @@ def output(self) -> str:
129
142
The (standard) output as a string.
130
143
"""
131
144
145
+ if _click_version >= (8 , 2 ):
146
+ ob = self .output_bytes # type: ignore[attr-defined]
147
+ return ob .decode (self .runner .charset , "replace" ).replace ("\r \n " , '\n ' )
148
+
132
149
return super ().output
133
150
134
151
@property
@@ -149,13 +166,19 @@ def stderr(self) -> str:
149
166
150
167
@classmethod
151
168
def _from_click_result (cls , result : click .testing .Result ) -> "Result" :
169
+ if _click_version >= (8 , 2 ):
170
+ output_bytes = result .output_bytes # type: ignore[attr-defined]
171
+ else :
172
+ output_bytes = None
173
+
152
174
return cls (
153
175
runner = result .runner ,
154
176
stdout_bytes = result .stdout_bytes ,
155
177
stderr_bytes = result .stderr_bytes ,
156
178
exit_code = result .exit_code ,
157
179
exception = result .exception ,
158
180
exc_info = result .exc_info ,
181
+ output_bytes = output_bytes ,
159
182
)
160
183
161
184
def check_stdout (
@@ -174,7 +197,10 @@ def check_stdout(
174
197
175
198
__tracebackhide__ = True
176
199
177
- check_file_regression (self .stdout .rstrip (), file_regression , extension = extension , ** kwargs )
200
+ if _click_version >= (8 , 2 ) and self .runner .mix_stderr :
201
+ check_file_regression (self .output .rstrip (), file_regression , extension = extension , ** kwargs )
202
+ else :
203
+ check_file_regression (self .stdout .rstrip (), file_regression , extension = extension , ** kwargs )
178
204
179
205
return True
180
206
@@ -206,11 +232,15 @@ def __init__(
206
232
echo_stdin : bool = False ,
207
233
mix_stderr : bool = True ,
208
234
) -> None :
209
- super ().__init__ (charset , env , echo_stdin , mix_stderr )
235
+ if _click_version >= (8 , 2 ):
236
+ super ().__init__ (charset , env , echo_stdin )
237
+ self .mix_stderr = mix_stderr
238
+ else :
239
+ super ().__init__ (charset , env , echo_stdin , mix_stderr )
210
240
211
241
def invoke ( # type: ignore[override]
212
242
self ,
213
- cli : click .BaseCommand ,
243
+ cli : click .Command ,
214
244
args : Optional [Union [str , Iterable [str ]]] = None ,
215
245
input : Optional [Union [bytes , str , IO ]] = None , # noqa: A002 # pylint: disable=redefined-builtin
216
246
env : Optional [Mapping [str , str ]] = None ,
0 commit comments