6565import click .testing
6666import pytest # nodep
6767from coincidence .regressions import check_file_regression # nodep
68+ from domdf_python_tools .compat import importlib_metadata
6869from pytest_regressions .file_regression import FileRegressionFixture # nodep
6970from typing_extensions import Literal
7071
7172__all__ = ("CliRunner" , "Result" , "cli_runner" )
7273
73- _click_major = int ( click . __version__ . split ('.' )[ 0 ] )
74+ _click_version = tuple ( map ( int , importlib_metadata . version ( " click" ). split ('.' )) )
7475
7576
7677class Result (click .testing .Result ):
@@ -101,9 +102,21 @@ def __init__(
101102 exit_code : int ,
102103 exception : Optional [BaseException ],
103104 exc_info : Optional [Tuple [Type [BaseException ], BaseException , TracebackType ]] = None ,
105+ output_bytes : Optional [bytes ] = None ,
104106 ) -> None :
105107
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 :
107120 super ().__init__ (
108121 runner = runner ,
109122 stdout_bytes = stdout_bytes ,
@@ -129,6 +142,10 @@ def output(self) -> str:
129142 The (standard) output as a string.
130143 """
131144
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+
132149 return super ().output
133150
134151 @property
@@ -149,13 +166,19 @@ def stderr(self) -> str:
149166
150167 @classmethod
151168 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+
152174 return cls (
153175 runner = result .runner ,
154176 stdout_bytes = result .stdout_bytes ,
155177 stderr_bytes = result .stderr_bytes ,
156178 exit_code = result .exit_code ,
157179 exception = result .exception ,
158180 exc_info = result .exc_info ,
181+ output_bytes = output_bytes ,
159182 )
160183
161184 def check_stdout (
@@ -174,7 +197,10 @@ def check_stdout(
174197
175198 __tracebackhide__ = True
176199
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 )
178204
179205 return True
180206
@@ -206,11 +232,15 @@ def __init__(
206232 echo_stdin : bool = False ,
207233 mix_stderr : bool = True ,
208234 ) -> 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 )
210240
211241 def invoke ( # type: ignore[override]
212242 self ,
213- cli : click .BaseCommand ,
243+ cli : click .Command ,
214244 args : Optional [Union [str , Iterable [str ]]] = None ,
215245 input : Optional [Union [bytes , str , IO ]] = None , # noqa: A002 # pylint: disable=redefined-builtin
216246 env : Optional [Mapping [str , str ]] = None ,
0 commit comments