aboutsummaryrefslogtreecommitdiff
path: root/toolchain_utils_githooks
diff options
context:
space:
mode:
authorTobias Bosch <tbosch@google.com>2019-06-20 08:56:58 -0700
committerTobias Bosch <tbosch@google.com>2019-06-20 23:08:36 +0000
commit7f186707b1989eb35c8c42503077edb8d62247a7 (patch)
treeceaec61902f6c3f4411feb9c5a9060d01d8d5e1b /toolchain_utils_githooks
parentef8f969c8ea2498c2a8aa701cb2e83833339f9a8 (diff)
downloadtoolchain-utils-7f186707b1989eb35c8c42503077edb8d62247a7.tar.gz
Call go fmt, go lint, go test and go vet
(via go test) during presubmit and git hooks. Will only warn when the go commands are not available as go is not part of depot_tools. BUG=chromium:976903 TEST=Tried with and without go/gofmt/golint in the path. TEST=Tried on go and non go files. TEST=Tried on go files with and without errors. Change-Id: I8a6aa4227f0d8649f7c390ab59f187f14955293c Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1669745 Reviewed-by: George Burgess <gbiv@chromium.org> Commit-Queue: Tobias Bosch <tbosch@google.com> Tested-by: Tobias Bosch <tbosch@google.com> Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Diffstat (limited to 'toolchain_utils_githooks')
-rwxr-xr-xtoolchain_utils_githooks/check-format38
-rwxr-xr-xtoolchain_utils_githooks/check-lint28
2 files changed, 48 insertions, 18 deletions
diff --git a/toolchain_utils_githooks/check-format b/toolchain_utils_githooks/check-format
index fc256671..372cc483 100755
--- a/toolchain_utils_githooks/check-format
+++ b/toolchain_utils_githooks/check-format
@@ -17,6 +17,7 @@ if [[ $# -eq 0 ]]; then
fi
yapf=yapf
+gofmt=gofmt
if ! type "${yapf}" >/dev/null 2>&1; then
echo "${yapf} isn't on your \$PATH. Please either enter a chroot, or place" \
@@ -24,6 +25,12 @@ if ! type "${yapf}" >/dev/null 2>&1; then
exit 1
fi
+if ! type "${gofmt}" >/dev/null 2>&1; then
+ echo "${gofmt} isn't on your \$PATH. Please either enter a chroot, or add " \
+ "the go binaries to your \$PATH." >&2
+ exit 1
+fi
+
status_to_tf() {
if "$@" >& /dev/null; then
echo true
@@ -55,9 +62,9 @@ check_python_file_header() {
everything_passed=true
python_files=()
+go_files=()
for f in "$@"; do
- [[ "${f}" == *.py ]] || continue
if [[ ! -e "${f}" ]]; then
if "${complain_about_missing}"; then
echo "error: no such file: ${f}" >&2
@@ -66,14 +73,17 @@ for f in "$@"; do
continue
fi
- python_files+=( "${f}" )
+ if [[ "${f}" == *.py ]]; then
+ python_files+=( "${f}" )
- if ! check_python_file_header "${f}"; then
- everything_passed=false
+ if ! check_python_file_header "${f}"; then
+ everything_passed=false
+ fi
+ elif [[ "${f}" == *.go ]]; then
+ go_files+=( "${f}" )
fi
done
-bad_files=()
if [[ "${#python_files[@]}" -ne 0 ]]; then
# yapf will give us a full unified (git-like) diff. We parse out the file
# names, e.g.,
@@ -91,12 +101,22 @@ if [[ "${#python_files[@]}" -ne 0 ]]; then
$("${yapf}" -d "${python_files[@]}" |
sed -n '/^--- /{ s/^--- //; s/ *(original)$//p }')
)
+ if [[ "${#bad_files[@]}" -ne 0 ]]; then
+ echo "One or more python files appear to be incorrectly formatted."
+ echo "Please run \`${yapf} -i ${bad_files[@]}\` to rectify this."
+ everything_passed=false
+ fi
fi
-if [[ "${#bad_files[@]}" -ne 0 ]]; then
- echo "One or more files appear to be incorrectly formatted."
- echo "Please run \`${yapf} -i ${bad_files[@]}\` to rectify this."
- everything_passed=false
+if [[ "${#go_files[@]}" -ne 0 ]]; then
+ bad_files=(
+ $("${gofmt}" -l "${go_files[@]}")
+ )
+ if [[ "${#bad_files[@]}" -ne 0 ]]; then
+ echo "One or more go files appear to be incorrectly formatted."
+ echo "Please run \`${gofmt} -w ${bad_files[@]}\` to rectify this."
+ everything_passed=false
+ fi
fi
"${everything_passed}"
diff --git a/toolchain_utils_githooks/check-lint b/toolchain_utils_githooks/check-lint
index a9345a7e..e4ba934b 100755
--- a/toolchain_utils_githooks/check-lint
+++ b/toolchain_utils_githooks/check-lint
@@ -12,6 +12,7 @@ if [[ $# -eq 0 ]]; then
fi
cros=cros
+golint=golint
if ! type "${cros}" >&/dev/null; then
echo "${cros} isn't on your \$PATH. Please either enter a chroot, or place" \
@@ -60,22 +61,31 @@ fi
# this should get us most of the way there, and is probably the best we can
# reasonably expect to do for users who want to develop without the source
# tree.
-echo "WARNING: No Chrome OS checkout detected, and no viable CrOS tree" >&2
-echo "found; falling back to python-only linting. If you have a Chrome OS" >&2
-echo "checkout, please either develop from inside of the source tree, or" >&2
-echo "set \$CHROMEOS_ROOT_DIRECTORY to the root of it." >&2
+echo "WARNING: No Chrome OS checkout detected, and no viable CrOS tree " >&2
+echo "found; falling back to linting only python and go. If you have a " >&2
+echo "Chrome OS checkout, please either develop from inside of the source ">&2
+echo "tree, or set \$CHROMEOS_ROOT_DIRECTORY to the root of it." >&2
python_files=()
+go_files=()
for file in "$@"; do
if [[ "${file}" == *.py ]]; then
python_files+=( "${file}" )
fi
+ if [[ "${file}" == *.go ]]; then
+ go_files+=( "${file}" )
+ fi
done
-if [[ "${#python_files[@]}" -eq 0 ]]; then
- exit 0
+if [[ "${#python_files[@]}" -ne 0 ]]; then
+ # We saw `cros` above, so assume that `pylint` is in our PATH (depot_tools
+ # packages it, and provides a reasonable default config).
+ pylint "${python_files[@]}"
fi
-# We saw `cros` above, so assume that `pylint` is in our PATH (depot_tools
-# packages it, and provides a reasonable default config).
-pylint "${python_files[@]}"
+if ! type "${golint}" >/dev/null 2>&1; then
+ echo "Warning: go linting disabled. ${golint} isn't on your \$PATH. "\
+ "Please either enter a chroot, or install go locally. Continuing." >&2
+elif [[ "${#go_files[@]}" -ne 0 ]]; then
+ "${golint}" -set_exit_status "${go_files[@]}"
+fi