Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_cache_broken_file(self) -> None:
mode = black.FileMode()
with cache_dir() as workspace:
cache_file = black.get_cache_file(mode)
with cache_file.open("w") as fobj:
fobj.write("this is not a pickle")
self.assertEqual(black.read_cache(mode), {})
src = (workspace / "test.py").resolve()
with src.open("w") as fobj:
fobj.write("print('hello')")
self.invokeBlack([str(src)])
cache = black.read_cache(mode)
self.assertIn(src, cache)
def test_single_file_force_pyi(self) -> None:
reg_mode = black.FileMode()
pyi_mode = black.FileMode(is_pyi=True)
contents, expected = read_data("force_pyi")
with cache_dir() as workspace:
path = (workspace / "file.py").resolve()
with open(path, "w") as fh:
fh.write(contents)
self.invokeBlack([str(path), "--pyi"])
with open(path, "r") as fh:
actual = fh.read()
# verify cache with --pyi is separate
pyi_cache = black.read_cache(pyi_mode)
self.assertIn(path, pyi_cache)
normal_cache = black.read_cache(reg_mode)
self.assertNotIn(path, normal_cache)
self.assertEqual(actual, expected)
source, expected = read_data("force_py36")
with cache_dir() as workspace:
paths = [
(workspace / "file1.py").resolve(),
(workspace / "file2.py").resolve(),
]
for path in paths:
with open(path, "w") as fh:
fh.write(source)
self.invokeBlack([str(p) for p in paths] + PY36_ARGS)
for path in paths:
with open(path, "r") as fh:
actual = fh.read()
self.assertEqual(actual, expected)
# verify cache with --target-version is separate
pyi_cache = black.read_cache(py36_mode)
normal_cache = black.read_cache(reg_mode)
for path in paths:
self.assertIn(path, pyi_cache)
self.assertNotIn(path, normal_cache)
def test_failed_formatting_does_not_get_cached(self) -> None:
mode = black.FileMode()
with cache_dir() as workspace, patch(
"black.ProcessPoolExecutor", new=ThreadPoolExecutor
):
failing = (workspace / "failing.py").resolve()
with failing.open("w") as fobj:
fobj.write("not actually python")
clean = (workspace / "clean.py").resolve()
with clean.open("w") as fobj:
fobj.write('print("hello")\n')
self.invokeBlack([str(workspace)], exit_code=123)
cache = black.read_cache(mode)
self.assertNotIn(failing, cache)
self.assertIn(clean, cache)
def test_read_cache_line_lengths(self) -> None:
mode = black.FileMode()
short_mode = black.FileMode(line_length=1)
with cache_dir() as workspace:
path = (workspace / "file.py").resolve()
path.touch()
black.write_cache({}, [path], mode)
one = black.read_cache(mode)
self.assertIn(path, one)
two = black.read_cache(short_mode)
self.assertNotIn(path, two)
def test_read_cache_line_lengths(self) -> None:
mode = black.FileMode()
short_mode = black.FileMode(line_length=1)
with cache_dir() as workspace:
path = (workspace / "file.py").resolve()
path.touch()
black.write_cache({}, [path], mode)
one = black.read_cache(mode)
self.assertIn(path, one)
two = black.read_cache(short_mode)
self.assertNotIn(path, two)
def test_write_cache_read_cache(self) -> None:
mode = black.FileMode()
with cache_dir() as workspace:
src = (workspace / "test.py").resolve()
src.touch()
black.write_cache({}, [src], mode)
cache = black.read_cache(mode)
self.assertIn(src, cache)
self.assertEqual(cache[src], black.get_cache_info(src))
src: Path,
line_length: int,
write_back: black.WriteBack,
mode: black.FileMode,
clear_output: bool,
report: black.Report,
) -> None:
"""Reformat a single file under `src`."""
try:
sub_report = SubReport(write_back=write_back)
changed = black.Changed.NO
cache: black.Cache = {}
if write_back is not black.WriteBack.DIFF:
cache = black.read_cache(line_length, mode)
res_src = src.resolve()
if res_src in cache and cache[res_src] == black.get_cache_info(
res_src
):
changed = black.Changed.CACHED
if changed is not black.Changed.CACHED:
sub_report = format_file_in_place(
src,
line_length=line_length,
write_back=write_back,
mode=mode,
clear_output=clear_output,
sub_report=sub_report,
)
if sub_report.change_count:
changed = black.Changed.YES