2626import click
2727import pytest
2828from click import unstyle
29- from click .testing import CliRunner
29+ from click .testing import CliRunner as _CliRunner
3030from packaging .version import Version
3131from pathspec import GitIgnoreSpec
3232
@@ -112,7 +112,7 @@ def __init__(self) -> None:
112112 pass
113113
114114
115- class BlackRunner (CliRunner ):
115+ class BlackRunner (_CliRunner ):
116116 """Make sure STDOUT and STDERR are kept separate when testing Black via its CLI."""
117117
118118 def __init__ (self ) -> None :
@@ -1170,7 +1170,7 @@ def test_multi_file_force_pyi(self) -> None:
11701170
11711171 def test_pipe_force_pyi (self ) -> None :
11721172 source , expected = read_data ("miscellaneous" , "force_pyi" )
1173- result = CliRunner ().invoke (
1173+ result = BlackRunner ().invoke (
11741174 black .main , ["-" , "-q" , "--pyi" ], input = BytesIO (source .encode ("utf-8" ))
11751175 )
11761176 self .assertEqual (result .exit_code , 0 )
@@ -1218,7 +1218,7 @@ def test_multi_file_force_py36(self) -> None:
12181218
12191219 def test_pipe_force_py36 (self ) -> None :
12201220 source , expected = read_data ("miscellaneous" , "force_py36" )
1221- result = CliRunner ().invoke (
1221+ result = BlackRunner ().invoke (
12221222 black .main ,
12231223 ["-" , "-q" , "--target-version=py36" ],
12241224 input = BytesIO (source .encode ("utf-8" )),
@@ -1853,7 +1853,7 @@ def test_code_option(self) -> None:
18531853 """Test the code option with no changes."""
18541854 code = 'print("Hello world")\n '
18551855 args = ["--code" , code ]
1856- result = CliRunner ().invoke (black .main , args )
1856+ result = BlackRunner ().invoke (black .main , args )
18571857
18581858 self .compare_results (result , code , 0 )
18591859
@@ -1863,20 +1863,20 @@ def test_code_option_changed(self) -> None:
18631863 formatted = black .format_str (code , mode = DEFAULT_MODE )
18641864
18651865 args = ["--code" , code ]
1866- result = CliRunner ().invoke (black .main , args )
1866+ result = BlackRunner ().invoke (black .main , args )
18671867
18681868 self .compare_results (result , formatted , 0 )
18691869
18701870 def test_code_option_check (self ) -> None :
18711871 """Test the code option when check is passed."""
18721872 args = ["--check" , "--code" , 'print("Hello world")\n ' ]
1873- result = CliRunner ().invoke (black .main , args )
1873+ result = BlackRunner ().invoke (black .main , args )
18741874 self .compare_results (result , "" , 0 )
18751875
18761876 def test_code_option_check_changed (self ) -> None :
18771877 """Test the code option when changes are required, and check is passed."""
18781878 args = ["--check" , "--code" , "print('hello world')" ]
1879- result = CliRunner ().invoke (black .main , args )
1879+ result = BlackRunner ().invoke (black .main , args )
18801880 self .compare_results (result , "" , 1 )
18811881
18821882 def test_code_option_diff (self ) -> None :
@@ -1886,7 +1886,7 @@ def test_code_option_diff(self) -> None:
18861886 result_diff = diff (code , formatted , "STDIN" , "STDOUT" )
18871887
18881888 args = ["--diff" , "--code" , code ]
1889- result = CliRunner ().invoke (black .main , args )
1889+ result = BlackRunner ().invoke (black .main , args )
18901890
18911891 # Remove time from diff
18921892 output = DIFF_TIME .sub ("" , result .output )
@@ -1903,7 +1903,7 @@ def test_code_option_color_diff(self) -> None:
19031903 result_diff = color_diff (result_diff )
19041904
19051905 args = ["--diff" , "--color" , "--code" , code ]
1906- result = CliRunner ().invoke (black .main , args )
1906+ result = BlackRunner ().invoke (black .main , args )
19071907
19081908 # Remove time from diff
19091909 output = DIFF_TIME .sub ("" , result .output )
@@ -1920,7 +1920,7 @@ def test_code_option_safe(self) -> None:
19201920 error_msg = f"{ code } \n error: cannot format <string>: \n "
19211921
19221922 args = ["--safe" , "--code" , code ]
1923- result = CliRunner ().invoke (black .main , args )
1923+ result = BlackRunner ().invoke (black .main , args )
19241924
19251925 assert error_msg == result .output
19261926 assert result .exit_code == 123
@@ -1933,7 +1933,7 @@ def test_code_option_fast(self) -> None:
19331933 formatted = black .format_str (code , mode = DEFAULT_MODE )
19341934
19351935 args = ["--fast" , "--code" , code ]
1936- result = CliRunner ().invoke (black .main , args )
1936+ result = BlackRunner ().invoke (black .main , args )
19371937
19381938 self .compare_results (result , formatted , 0 )
19391939
@@ -1946,7 +1946,7 @@ def test_code_option_config(self) -> None:
19461946 args = ["--code" , "print" ]
19471947 # This is the only directory known to contain a pyproject.toml
19481948 with change_directory (PROJECT_ROOT ):
1949- CliRunner ().invoke (black .main , args )
1949+ BlackRunner ().invoke (black .main , args )
19501950 pyproject_path = Path (Path .cwd (), "pyproject.toml" ).resolve ()
19511951
19521952 assert (
@@ -1966,7 +1966,7 @@ def test_code_option_parent_config(self) -> None:
19661966 with patch .object (black , "parse_pyproject_toml" , return_value = {}) as parse :
19671967 with change_directory (THIS_DIR ):
19681968 args = ["--code" , "print" ]
1969- CliRunner ().invoke (black .main , args )
1969+ BlackRunner ().invoke (black .main , args )
19701970
19711971 pyproject_path = Path (Path ().cwd ().parent , "pyproject.toml" ).resolve ()
19721972 assert (
@@ -1993,7 +1993,7 @@ def test_line_ranges_with_code_option(self) -> None:
19931993 print ( "OK" )
19941994 """ )
19951995 args = ["--line-ranges=1-1" , "--code" , code ]
1996- result = CliRunner ().invoke (black .main , args )
1996+ result = BlackRunner ().invoke (black .main , args )
19971997
19981998 expected = textwrap .dedent ("""\
19991999 if a == b:
@@ -2028,7 +2028,7 @@ def test_line_ranges_with_source(self) -> None:
20282028 encoding = "utf-8" ,
20292029 )
20302030 args = ["--line-ranges=1-1" , str (test_file )]
2031- result = CliRunner ().invoke (black .main , args )
2031+ result = BlackRunner ().invoke (black .main , args )
20322032 assert not result .exit_code
20332033
20342034 formatted = test_file .read_text (encoding = "utf-8" )
@@ -2045,17 +2045,25 @@ def test_line_ranges_with_multiple_sources(self) -> None:
20452045 test2_file = Path (workspace ) / "test2.py"
20462046 test2_file .write_text ("" , encoding = "utf-8" )
20472047 args = ["--line-ranges=1-1" , str (test1_file ), str (test2_file )]
2048- result = CliRunner ().invoke (black .main , args )
2048+ result = BlackRunner ().invoke (black .main , args )
20492049 assert result .exit_code == 1
2050- assert "Cannot use --line-ranges to format multiple files" in result .output
2050+ assert result .stderr_bytes is not None
2051+ assert (
2052+ "Cannot use --line-ranges to format multiple files"
2053+ in result .stderr_bytes .decode ()
2054+ )
20512055
20522056 def test_line_ranges_with_ipynb (self ) -> None :
20532057 with TemporaryDirectory () as workspace :
20542058 test_file = Path (workspace ) / "test.ipynb"
20552059 test_file .write_text ("{}" , encoding = "utf-8" )
20562060 args = ["--line-ranges=1-1" , "--ipynb" , str (test_file )]
2057- result = CliRunner ().invoke (black .main , args )
2058- assert "Cannot use --line-ranges with ipynb files" in result .output
2061+ result = BlackRunner ().invoke (black .main , args )
2062+ assert result .stderr_bytes is not None
2063+ assert (
2064+ "Cannot use --line-ranges with ipynb files"
2065+ in result .stderr_bytes .decode ()
2066+ )
20592067 assert result .exit_code == 1
20602068
20612069 def test_line_ranges_in_pyproject_toml (self ) -> None :
@@ -2288,7 +2296,7 @@ def test_output_locking_when_writeback_diff(self, color: bool) -> None:
22882296 def test_no_cache_when_stdin (self ) -> None :
22892297 mode = DEFAULT_MODE
22902298 with cache_dir ():
2291- result = CliRunner ().invoke (
2299+ result = BlackRunner ().invoke (
22922300 black .main , ["-" ], input = BytesIO (b"print('hello')" )
22932301 )
22942302 assert not result .exit_code
@@ -3220,7 +3228,7 @@ def test_target_version_exceeds_runtime_warning(self) -> None:
32203228 target_name = f"py3{ sys .version_info [1 ] + 1 } "
32213229 code = "x = 1\n "
32223230 args = ["--target-version" , target_name , "--code" , code ]
3223- result = CliRunner ().invoke (black .main , args )
3231+ result = BlackRunner ().invoke (black .main , args )
32243232 stderr = result .stderr_bytes .decode () if result .stderr_bytes else ""
32253233 assert "Warning:" in stderr
32263234
@@ -3231,7 +3239,7 @@ def test_target_version_exceeds_runtime_no_warning_with_fast(self) -> None:
32313239 target_name = f"py3{ sys .version_info [1 ] + 1 } "
32323240 code = "x = 1\n "
32333241 args = ["--fast" , "--target-version" , target_name , "--code" , code ]
3234- result = CliRunner ().invoke (black .main , args )
3242+ result = BlackRunner ().invoke (black .main , args )
32353243 stderr = result .stderr_bytes .decode () if result .stderr_bytes else ""
32363244 assert "Warning:" not in stderr
32373245
@@ -3240,7 +3248,7 @@ def test_target_version_at_runtime_no_warning(self) -> None:
32403248 target_name = f"py3{ current_minor } "
32413249 code = "x = 1\n "
32423250 args = ["--target-version" , target_name , "--code" , code ]
3243- result = CliRunner ().invoke (black .main , args )
3251+ result = BlackRunner ().invoke (black .main , args )
32443252 stderr = result .stderr_bytes .decode () if result .stderr_bytes else ""
32453253 assert "Warning:" not in stderr
32463254
0 commit comments