@@ -149,8 +149,7 @@ def test_empty_ff(self) -> None:
149
149
tmp_file = Path (black .dump_to_file ())
150
150
try :
151
151
self .assertFalse (ff (tmp_file , write_back = black .WriteBack .YES ))
152
- with open (tmp_file , encoding = "utf8" ) as f :
153
- actual = f .read ()
152
+ actual = tmp_file .read_text (encoding = "utf8" )
154
153
finally :
155
154
os .unlink (tmp_file )
156
155
self .assertFormatEqual (expected , actual )
@@ -286,8 +285,7 @@ def test_expression_ff(self) -> None:
286
285
tmp_file = Path (black .dump_to_file (source ))
287
286
try :
288
287
self .assertTrue (ff (tmp_file , write_back = black .WriteBack .YES ))
289
- with open (tmp_file , encoding = "utf8" ) as f :
290
- actual = f .read ()
288
+ actual = tmp_file .read_text (encoding = "utf8" )
291
289
finally :
292
290
os .unlink (tmp_file )
293
291
self .assertFormatEqual (expected , actual )
@@ -390,8 +388,7 @@ def test_skip_source_first_line(self) -> None:
390
388
black .main , [str (tmp_file ), "-x" , f"--config={ EMPTY_CONFIG } " ]
391
389
)
392
390
self .assertEqual (result .exit_code , 0 )
393
- with open (tmp_file , encoding = "utf8" ) as f :
394
- actual = f .read ()
391
+ actual = tmp_file .read_text (encoding = "utf8" )
395
392
self .assertFormatEqual (source , actual )
396
393
397
394
def test_skip_source_first_line_when_mixing_newlines (self ) -> None :
@@ -1081,7 +1078,7 @@ def test_works_in_mono_process_only_environment(self) -> None:
1081
1078
(workspace / "one.py" ).resolve (),
1082
1079
(workspace / "two.py" ).resolve (),
1083
1080
]:
1084
- f .write_text ('print("hello")\n ' )
1081
+ f .write_text ('print("hello")\n ' , encoding = "utf-8" )
1085
1082
self .invokeBlack ([str (workspace )])
1086
1083
1087
1084
@event_loop ()
@@ -1118,11 +1115,9 @@ def test_single_file_force_pyi(self) -> None:
1118
1115
contents , expected = read_data ("miscellaneous" , "force_pyi" )
1119
1116
with cache_dir () as workspace :
1120
1117
path = (workspace / "file.py" ).resolve ()
1121
- with open (path , "w" ) as fh :
1122
- fh .write (contents )
1118
+ path .write_text (contents , encoding = "utf-8" )
1123
1119
self .invokeBlack ([str (path ), "--pyi" ])
1124
- with open (path , "r" ) as fh :
1125
- actual = fh .read ()
1120
+ actual = path .read_text (encoding = "utf8" )
1126
1121
# verify cache with --pyi is separate
1127
1122
pyi_cache = black .read_cache (pyi_mode )
1128
1123
self .assertIn (str (path ), pyi_cache )
@@ -1143,12 +1138,10 @@ def test_multi_file_force_pyi(self) -> None:
1143
1138
(workspace / "file2.py" ).resolve (),
1144
1139
]
1145
1140
for path in paths :
1146
- with open (path , "w" ) as fh :
1147
- fh .write (contents )
1141
+ path .write_text (contents , encoding = "utf-8" )
1148
1142
self .invokeBlack ([str (p ) for p in paths ] + ["--pyi" ])
1149
1143
for path in paths :
1150
- with open (path , "r" ) as fh :
1151
- actual = fh .read ()
1144
+ actual = path .read_text (encoding = "utf8" )
1152
1145
self .assertEqual (actual , expected )
1153
1146
# verify cache with --pyi is separate
1154
1147
pyi_cache = black .read_cache (pyi_mode )
@@ -1172,11 +1165,9 @@ def test_single_file_force_py36(self) -> None:
1172
1165
source , expected = read_data ("miscellaneous" , "force_py36" )
1173
1166
with cache_dir () as workspace :
1174
1167
path = (workspace / "file.py" ).resolve ()
1175
- with open (path , "w" ) as fh :
1176
- fh .write (source )
1168
+ path .write_text (source , encoding = "utf-8" )
1177
1169
self .invokeBlack ([str (path ), * PY36_ARGS ])
1178
- with open (path , "r" ) as fh :
1179
- actual = fh .read ()
1170
+ actual = path .read_text (encoding = "utf8" )
1180
1171
# verify cache with --target-version is separate
1181
1172
py36_cache = black .read_cache (py36_mode )
1182
1173
self .assertIn (str (path ), py36_cache )
@@ -1195,12 +1186,10 @@ def test_multi_file_force_py36(self) -> None:
1195
1186
(workspace / "file2.py" ).resolve (),
1196
1187
]
1197
1188
for path in paths :
1198
- with open (path , "w" ) as fh :
1199
- fh .write (source )
1189
+ path .write_text (source , encoding = "utf-8" )
1200
1190
self .invokeBlack ([str (p ) for p in paths ] + PY36_ARGS )
1201
1191
for path in paths :
1202
- with open (path , "r" ) as fh :
1203
- actual = fh .read ()
1192
+ actual = path .read_text (encoding = "utf8" )
1204
1193
self .assertEqual (actual , expected )
1205
1194
# verify cache with --target-version is separate
1206
1195
pyi_cache = black .read_cache (py36_mode )
@@ -1631,8 +1620,8 @@ def test_read_pyproject_toml_from_stdin(self) -> None:
1631
1620
src_pyproject = src_dir / "pyproject.toml"
1632
1621
src_pyproject .touch ()
1633
1622
1634
- test_toml_file = THIS_DIR / "test.toml"
1635
- src_pyproject .write_text (test_toml_file . read_text () )
1623
+ test_toml_content = ( THIS_DIR / "test.toml" ). read_text ( encoding = "utf-8" )
1624
+ src_pyproject .write_text (test_toml_content , encoding = "utf-8" )
1636
1625
1637
1626
src_python = src_dir / "foo.py"
1638
1627
src_python .touch ()
@@ -1985,10 +1974,10 @@ def test_cache_broken_file(self) -> None:
1985
1974
mode = DEFAULT_MODE
1986
1975
with cache_dir () as workspace :
1987
1976
cache_file = get_cache_file (mode )
1988
- cache_file .write_text ("this is not a pickle" )
1977
+ cache_file .write_text ("this is not a pickle" , encoding = "utf-8" )
1989
1978
assert black .read_cache (mode ) == {}
1990
1979
src = (workspace / "test.py" ).resolve ()
1991
- src .write_text ("print('hello')" )
1980
+ src .write_text ("print('hello')" , encoding = "utf-8" )
1992
1981
invokeBlack ([str (src )])
1993
1982
cache = black .read_cache (mode )
1994
1983
assert str (src ) in cache
@@ -1997,10 +1986,10 @@ def test_cache_single_file_already_cached(self) -> None:
1997
1986
mode = DEFAULT_MODE
1998
1987
with cache_dir () as workspace :
1999
1988
src = (workspace / "test.py" ).resolve ()
2000
- src .write_text ("print('hello')" )
1989
+ src .write_text ("print('hello')" , encoding = "utf-8" )
2001
1990
black .write_cache ({}, [src ], mode )
2002
1991
invokeBlack ([str (src )])
2003
- assert src .read_text () == "print('hello')"
1992
+ assert src .read_text (encoding = "utf-8" ) == "print('hello')"
2004
1993
2005
1994
@event_loop ()
2006
1995
def test_cache_multiple_files (self ) -> None :
@@ -2009,17 +1998,13 @@ def test_cache_multiple_files(self) -> None:
2009
1998
"concurrent.futures.ProcessPoolExecutor" , new = ThreadPoolExecutor
2010
1999
):
2011
2000
one = (workspace / "one.py" ).resolve ()
2012
- with one .open ("w" ) as fobj :
2013
- fobj .write ("print('hello')" )
2001
+ one .write_text ("print('hello')" , encoding = "utf-8" )
2014
2002
two = (workspace / "two.py" ).resolve ()
2015
- with two .open ("w" ) as fobj :
2016
- fobj .write ("print('hello')" )
2003
+ two .write_text ("print('hello')" , encoding = "utf-8" )
2017
2004
black .write_cache ({}, [one ], mode )
2018
2005
invokeBlack ([str (workspace )])
2019
- with one .open ("r" ) as fobj :
2020
- assert fobj .read () == "print('hello')"
2021
- with two .open ("r" ) as fobj :
2022
- assert fobj .read () == 'print("hello")\n '
2006
+ assert one .read_text (encoding = "utf-8" ) == "print('hello')"
2007
+ assert two .read_text (encoding = "utf-8" ) == 'print("hello")\n '
2023
2008
cache = black .read_cache (mode )
2024
2009
assert str (one ) in cache
2025
2010
assert str (two ) in cache
@@ -2029,8 +2014,7 @@ def test_no_cache_when_writeback_diff(self, color: bool) -> None:
2029
2014
mode = DEFAULT_MODE
2030
2015
with cache_dir () as workspace :
2031
2016
src = (workspace / "test.py" ).resolve ()
2032
- with src .open ("w" ) as fobj :
2033
- fobj .write ("print('hello')" )
2017
+ src .write_text ("print('hello')" , encoding = "utf-8" )
2034
2018
with patch ("black.read_cache" ) as read_cache , patch (
2035
2019
"black.write_cache"
2036
2020
) as write_cache :
@@ -2049,8 +2033,7 @@ def test_output_locking_when_writeback_diff(self, color: bool) -> None:
2049
2033
with cache_dir () as workspace :
2050
2034
for tag in range (0 , 4 ):
2051
2035
src = (workspace / f"test{ tag } .py" ).resolve ()
2052
- with src .open ("w" ) as fobj :
2053
- fobj .write ("print('hello')" )
2036
+ src .write_text ("print('hello')" , encoding = "utf-8" )
2054
2037
with patch (
2055
2038
"black.concurrency.Manager" , wraps = multiprocessing .Manager
2056
2039
) as mgr :
@@ -2120,11 +2103,9 @@ def test_failed_formatting_does_not_get_cached(self) -> None:
2120
2103
"concurrent.futures.ProcessPoolExecutor" , new = ThreadPoolExecutor
2121
2104
):
2122
2105
failing = (workspace / "failing.py" ).resolve ()
2123
- with failing .open ("w" ) as fobj :
2124
- fobj .write ("not actually python" )
2106
+ failing .write_text ("not actually python" , encoding = "utf-8" )
2125
2107
clean = (workspace / "clean.py" ).resolve ()
2126
- with clean .open ("w" ) as fobj :
2127
- fobj .write ('print("hello")\n ' )
2108
+ clean .write_text ('print("hello")\n ' , encoding = "utf-8" )
2128
2109
invokeBlack ([str (workspace )], exit_code = 123 )
2129
2110
cache = black .read_cache (mode )
2130
2111
assert str (failing ) not in cache
0 commit comments