def test_write_cache_read_cache(self) -> None: with cache_dir() as workspace: src = (workspace / "test.py").resolve() src.touch() black.write_cache({}, [src]) cache = black.read_cache() self.assertIn(src, cache) self.assertEqual(cache[src], black.get_cache_info(src))
def test_read_cache_line_lengths(self) -> None: with cache_dir() as workspace: path = (workspace / "file.py").resolve() path.touch() black.write_cache({}, [path], 1) one = black.read_cache(1) self.assertIn(path, one) two = black.read_cache(2) self.assertNotIn(path, two)
def test_write_cache_read_cache(self) -> None: mode = black.FileMode.AUTO_DETECT with cache_dir() as workspace: src = (workspace / "test.py").resolve() src.touch() black.write_cache({}, [src], black.DEFAULT_LINE_LENGTH, mode) cache = black.read_cache(black.DEFAULT_LINE_LENGTH, mode) self.assertIn(src, cache) self.assertEqual(cache[src], black.get_cache_info(src))
def test_read_cache_line_lengths(self) -> None: mode = black.FileMode.AUTO_DETECT with cache_dir() as workspace: path = (workspace / "file.py").resolve() path.touch() black.write_cache({}, [path], 1, mode) one = black.read_cache(1, mode) self.assertIn(path, one) two = black.read_cache(2, mode) self.assertNotIn(path, two)
def test_cache_single_file_already_cached(self) -> None: with cache_dir() as workspace: src = (workspace / "test.py").resolve() with src.open("w") as fobj: fobj.write("print('hello')") black.write_cache({}, [src]) result = CliRunner().invoke(black.main, [str(src)]) self.assertEqual(result.exit_code, 0) with src.open("r") as fobj: self.assertEqual(fobj.read(), "print('hello')")
def test_cache_single_file_already_cached(self) -> None: mode = black.FileMode.AUTO_DETECT with cache_dir() as workspace: src = (workspace / "test.py").resolve() with src.open("w") as fobj: fobj.write("print('hello')") black.write_cache({}, [src], black.DEFAULT_LINE_LENGTH, mode) result = CliRunner().invoke(black.main, [str(src)]) self.assertEqual(result.exit_code, 0) with src.open("r") as fobj: self.assertEqual(fobj.read(), "print('hello')")
def reformat_one( src: Path, line_length: int, write_back: black.WriteBack, mode: black.FileMode, clear_output: bool, report: black.Report, quiet: bool, verbose: bool, ) -> 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 or sub_report.output_change_count: changed = black.Changed.YES if ( write_back is black.WriteBack.YES and changed is not black.Changed.CACHED ) or ( write_back is black.WriteBack.CHECK and changed is black.Changed.NO ): black.write_cache(cache, [src], line_length, mode) report.done(src, changed) if changed is not black.Changed.CACHED and (verbose or not quiet): click.secho(f" {sub_report}", err=True) except Exception as exc: report.failed(src, str(exc))
def test_cache_multiple_files(self) -> None: with cache_dir() as workspace: one = (workspace / "one.py").resolve() with one.open("w") as fobj: fobj.write("print('hello')") two = (workspace / "two.py").resolve() with two.open("w") as fobj: fobj.write("print('hello')") black.write_cache({}, [one]) result = CliRunner().invoke(black.main, [str(workspace)]) self.assertEqual(result.exit_code, 0) with one.open("r") as fobj: self.assertEqual(fobj.read(), "print('hello')") with two.open("r") as fobj: self.assertEqual(fobj.read(), 'print("hello")\n') cache = black.read_cache() self.assertIn(one, cache) self.assertIn(two, cache)
def test_cache_multiple_files(self) -> None: with cache_dir() as workspace, patch("black.ProcessPoolExecutor", new=ThreadPoolExecutor): one = (workspace / "one.py").resolve() with one.open("w") as fobj: fobj.write("print('hello')") two = (workspace / "two.py").resolve() with two.open("w") as fobj: fobj.write("print('hello')") black.write_cache({}, [one], black.DEFAULT_LINE_LENGTH) result = CliRunner().invoke(black.main, [str(workspace)]) self.assertEqual(result.exit_code, 0) with one.open("r") as fobj: self.assertEqual(fobj.read(), "print('hello')") with two.open("r") as fobj: self.assertEqual(fobj.read(), 'print("hello")\n') cache = black.read_cache(black.DEFAULT_LINE_LENGTH) self.assertIn(one, cache) self.assertIn(two, cache)
def test_cache_multiple_files(self) -> None: mode = black.FileMode.AUTO_DETECT with cache_dir() as workspace, patch( "black.ProcessPoolExecutor", new=ThreadPoolExecutor ): one = (workspace / "one.py").resolve() with one.open("w") as fobj: fobj.write("print('hello')") two = (workspace / "two.py").resolve() with two.open("w") as fobj: fobj.write("print('hello')") black.write_cache({}, [one], black.DEFAULT_LINE_LENGTH, mode) result = CliRunner().invoke(black.main, [str(workspace)]) self.assertEqual(result.exit_code, 0) with one.open("r") as fobj: self.assertEqual(fobj.read(), "print('hello')") with two.open("r") as fobj: self.assertEqual(fobj.read(), 'print("hello")\n') cache = black.read_cache(black.DEFAULT_LINE_LENGTH, mode) self.assertIn(one, cache) self.assertIn(two, cache)
def test_write_cache_creates_directory_if_needed(self) -> None: mode = black.FileMode.AUTO_DETECT with cache_dir(exists=False) as workspace: self.assertFalse(workspace.exists()) black.write_cache({}, [], black.DEFAULT_LINE_LENGTH, mode) self.assertTrue(workspace.exists())
def test_write_cache_write_fail(self) -> None: with cache_dir(), patch.object(Path, "open") as mock: mock.side_effect = OSError black.write_cache({}, [])
def test_write_cache_creates_directory_if_needed(self) -> None: with cache_dir(exists=False) as workspace: self.assertFalse(workspace.exists()) black.write_cache({}, []) self.assertTrue(workspace.exists())
def test_write_cache_write_fail(self) -> None: mode = black.FileMode.AUTO_DETECT with cache_dir(), patch.object(Path, "open") as mock: mock.side_effect = OSError black.write_cache({}, [], black.DEFAULT_LINE_LENGTH, mode)