pre-upload: switch black (Python) format checks to chromite The underlying tools are the same, so there shouldn't be any changes, but this helps unify the logic a bit so improvements to `cros format` automatically improve pre-upload. BUG=None TEST=CQ passes Change-Id: Id45222de7c711bc6d99b297a9d53107f16d69c3c Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/repohooks/+/4189457 Commit-Queue: Mike Frysinger <vapier@chromium.org> Tested-by: Mike Frysinger <vapier@chromium.org> Reviewed-by: Sergey Frolov <sfrolov@google.com>
diff --git a/pre-upload.py b/pre-upload.py index 6f7d19c..e5cbc7a 100755 --- a/pre-upload.py +++ b/pre-upload.py
@@ -2378,51 +2378,18 @@ _get_affected_files(commit, relative=True), included, excluded ) - # Detect if the repo has their own black config. If not, we want to - # use the one here. - config_path = REPOHOOKS_DIR / "pyproject.toml" - their_config = None - if project.dir: - their_config = Path(project.dir) / "pyproject.toml" - if their_config.is_file() and "tool.black" in their_config.read_text(): - config_path = their_config - - black_tool = Path(constants.DEPOT_TOOLS_DIR) / "black" - files_need_formatting = [] + errors = [] + project_path = Path(project.dir) for file in files: contents = _get_file_content(file, commit) - result = cros_build_lib.run( - [ - black_tool, - f"--config={config_path}", - "--quiet", - "--check", - "--diff", - f"--stdin-filename={file}", - "-", - ], - print_cmd=False, - input=contents, - check=False, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - encoding="utf-8", - ) - if result.returncode: - files_need_formatting.append((file, result.stdout)) + if contents != formatters.python.Data( + contents, path=project_path / file + ): + errors.append(file) - if files_need_formatting: - black_cmd = [black_tool] - if config_path != their_config: - black_cmd.extend(["--config", config_path]) - for file, _ in files_need_formatting: - black_cmd.append(file) - black_cmd_repr = cros_build_lib.CmdToStr(black_cmd) + if errors: return HookFailure( - f"Files need formatted with black.\n" - f"Run this command to fix automatically:\n" - f" {black_cmd_repr}", - [f"{file}\n{diff}" for file, diff in files_need_formatting], + "Files not formatted (run `cros format` to fix):", errors ) return None
diff --git a/pre-upload_unittest.py b/pre-upload_unittest.py index 7dd082c..512b076 100755 --- a/pre-upload_unittest.py +++ b/pre-upload_unittest.py
@@ -2866,17 +2866,7 @@ ) result = pre_upload._check_black(ProjectNamed("PROJECT"), "COMMIT") self.assertIsNotNone(result) - self.assertEqual( - f"Files need formatted with black.\n" - f"Run this command to fix automatically:\n" - f" {constants.DEPOT_TOOLS_DIR}/black --config " - f"{pre_upload.REPOHOOKS_DIR}/pyproject.toml " - f"needs_reformatted.py", - result.msg, - ) - self.assertEqual( - ["needs_reformatted.py"], [x.splitlines()[0] for x in result.items] - ) + self.assertEqual(["needs_reformatted.py"], result.items) def testLocalConfig(self): """When a pyproject.toml exists in the project, it should be used.""" @@ -2888,13 +2878,8 @@ "[tool.black]\n" "line-length = 6\n" ) result = pre_upload._check_black(project, "COMMIT") - self.assertEqual( - f"Files need formatted with black.\n" - f"Run this command to fix automatically:\n" - f" {constants.DEPOT_TOOLS_DIR}/black ok.py", - result.msg, - ) - self.assertEqual(["ok.py"], [x.splitlines()[0] for x in result.items]) + self.assertIsNotNone(result) + self.assertEqual(["ok.py"], result.items) class CheckRustfmtTest(PreUploadTestCase):