From 676c57dba4ad3de011a4b7d8b9ecbd54a24533c0 Mon Sep 17 00:00:00 2001 From: Roberto Alanis Date: Mon, 4 Oct 2021 23:27:21 +0000 Subject: [PATCH] patch-check: shfmt shfmt is expected to be installed along with go. GO111MODULE=on go install mvdan.cc/sh/v3/cmd/shfmt@latest Change-Id: I27004e72f79f71df9405158f77df485f43b028bb Bug: b:185520494 --- PRESUBMIT.py | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/PRESUBMIT.py b/PRESUBMIT.py index de751589..71f026af 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -36,6 +36,7 @@ details on the presubmit API built into depot_tools. import subprocess2 USE_PYTHON3 = True +_BASH_INDENTATION = "2" _INCLUDE_BASH_FILES_ONLY = [r".*\.sh$"] _INCLUDE_MAN_FILES_ONLY = [r"man/.+\.1$"] _LIBWEBP_MAX_LINE_LENGTH = 80 @@ -75,7 +76,24 @@ def _RunShellCheckCmd(input_api, output_api, bash_file): cmd = ["shellcheck", "-x", "-oall", "-sbash", bash_file] name = "Check %s file." % bash_file start = input_api.time.time() - subprocess2.communicate(["shellcheck", "--version"]) + output, rc = subprocess2.communicate( + cmd, stdout=None, stderr=subprocess2.PIPE, universal_newlines=True) + duration = input_api.time.time() - start + if rc == 0: + return output_api.PresubmitResult("%s\n%s (%4.2fs)\n" % + (name, " ".join(cmd), duration)) + return output_api.PresubmitError("%s\n%s (%4.2fs) failed\n%s" % + (name, " ".join(cmd), duration, output[1])) + + +def _RunShfmtCheckCmd(input_api, output_api, bash_file): + """shfmt command wrapper.""" + cmd = [ + "shfmt", "-i", _BASH_INDENTATION, "-bn", "-ci", "-sr", "-kp", "-d", + bash_file + ] + name = "Check %s file." % bash_file + start = input_api.time.time() output, rc = subprocess2.communicate( cmd, stdout=None, stderr=subprocess2.PIPE, universal_newlines=True) duration = input_api.time.time() - start @@ -122,28 +140,36 @@ def _CommonChecks(input_api, output_api): check_clang_format=False, check_python=True, result_factory=output_api.PresubmitError)) + results.extend( + _RunCmdOnCheckedFiles(input_api, output_api, _RunManCmd, + _INCLUDE_MAN_FILES_ONLY)) + + # Binaries shellcheck and shfmt are not installed in depot_tools. + # Installation is needed + try: + subprocess2.communicate(["shellcheck", "--version"]) + results.extend( + _RunCmdOnCheckedFiles(input_api, output_api, _RunShellCheckCmd, + _INCLUDE_BASH_FILES_ONLY)) + print("shfmt") + subprocess2.communicate(["shfmt", "-version"]) + results.extend( + _RunCmdOnCheckedFiles(input_api, output_api, _RunShfmtCheckCmd, + _INCLUDE_BASH_FILES_ONLY)) + except OSError as os_error: + results.append( + output_api.PresubmitPromptWarning( + '%s\nPlease install missing binaries locally.' % os_error.args[0])) return results def CheckChangeOnUpload(input_api, output_api): results = [] results.extend(_CommonChecks(input_api, output_api)) - results.extend( - _RunCmdOnCheckedFiles(input_api, output_api, _RunManCmd, - _INCLUDE_MAN_FILES_ONLY)) - results.extend( - _RunCmdOnCheckedFiles(input_api, output_api, _RunShellCheckCmd, - _INCLUDE_BASH_FILES_ONLY)) return results def CheckChangeOnCommit(input_api, output_api): results = [] results.extend(_CommonChecks(input_api, output_api)) - results.extend( - _RunCmdOnCheckedFiles(input_api, output_api, _RunManCmd, - _INCLUDE_MAN_FILES_ONLY)) - results.extend( - _RunCmdOnCheckedFiles(input_api, output_api, _RunShellCheckCmd, - _INCLUDE_BASH_FILES_ONLY)) return results