-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added presubmit script distilled from that in trillian (#29)
Removed some of the flags and branches that weren't needed, such as code generation. This isn't hooked into CI _yet_, but at least developers can run `./scripts/presubmit.sh` before submitting in the meantime.
- Loading branch information
1 parent
ff6e921
commit ded06ce
Showing
2 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/bin/bash | ||
# | ||
# Checks that source files (.go and .proto) have the Apache License header. | ||
# Automatically skips generated files. | ||
set -eu | ||
|
||
check_license() { | ||
local path="$1" | ||
|
||
if head -1 "$path" | grep -iq 'generated by'; then | ||
return 0 | ||
fi | ||
|
||
# Look for "Apache License" on the file header | ||
if ! head -10 "$path" | grep -q 'Apache License'; then | ||
# Format: $path:$line:$message | ||
echo "$path:10:license header not found" | ||
return 1 | ||
fi | ||
} | ||
|
||
main() { | ||
if [[ $# -lt 1 ]]; then | ||
echo "Usage: $0 <path>" | ||
exit 1 | ||
fi | ||
|
||
local code=0 | ||
while [[ $# -gt 0 ]]; do | ||
local path="$1" | ||
if [[ -d "$path" ]]; then | ||
for f in "$path"/*.{go,proto}; do | ||
if [[ ! -f "$f" ]]; then | ||
continue # Empty glob | ||
fi | ||
check_license "$f" || code=1 | ||
done | ||
else | ||
check_license "$path" || code=1 | ||
fi | ||
shift | ||
done | ||
exit $code | ||
} | ||
|
||
main "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#!/bin/bash | ||
# | ||
# Presubmit checks for this repository. | ||
# | ||
# Checks for lint errors, spelling, licensing, correct builds / tests and so on. | ||
# Flags may be specified to allow suppressing of checks or automatic fixes, try | ||
# `scripts/presubmit.sh --help` for details. | ||
# | ||
# Globals: | ||
# GO_TEST_TIMEOUT: timeout for 'go test'. Optional (defaults to 5m). | ||
set -eu | ||
|
||
check_pkg() { | ||
local cmd="$1" | ||
local pkg="$2" | ||
check_cmd "$cmd" "try running 'go get -u $pkg'" | ||
} | ||
|
||
check_cmd() { | ||
local cmd="$1" | ||
local msg="$2" | ||
if ! type -p "${cmd}" > /dev/null; then | ||
echo "${cmd} not found, ${msg}" | ||
return 1 | ||
fi | ||
} | ||
|
||
usage() { | ||
echo "$0 [--coverage] [--fix] [--no-mod-tidy] [--no-build] [--no-linters]" | ||
} | ||
|
||
main() { | ||
local coverage=0 | ||
local fix=0 | ||
local run_mod_tidy=1 | ||
local run_build=1 | ||
local run_lint=1 | ||
while [[ $# -gt 0 ]]; do | ||
case "$1" in | ||
--coverage) | ||
coverage=1 | ||
;; | ||
--fix) | ||
fix=1 | ||
;; | ||
--no-mod-tidy) | ||
run_mod_tidy=0 | ||
;; | ||
--help) | ||
usage | ||
exit 0 | ||
;; | ||
--no-build) | ||
run_build=0 | ||
;; | ||
--no-linters) | ||
run_lint=0 | ||
;; | ||
*) | ||
usage | ||
exit 1 | ||
;; | ||
esac | ||
shift 1 | ||
done | ||
|
||
cd "$(dirname "$0")" # at scripts/ | ||
cd .. # at top level | ||
|
||
go_srcs="$(find . -name '*.go' | \ | ||
grep -v mock_ | \ | ||
grep -v .pb.go | \ | ||
grep -v _string.go | \ | ||
grep -v .shims.go | \ | ||
tr '\n' ' ')" | ||
|
||
if [[ "$fix" -eq 1 ]]; then | ||
check_pkg goimports golang.org/x/tools/cmd/goimports || exit 1 | ||
|
||
echo 'running gofmt' | ||
gofmt -s -w ${go_srcs} | ||
echo 'running goimports' | ||
goimports -w ${go_srcs} | ||
if [[ "$run_mod_tidy" -eq 1 ]]; then | ||
echo 'running go mod tidy' | ||
go mod tidy | ||
fi | ||
fi | ||
|
||
if [[ "${run_build}" -eq 1 ]]; then | ||
echo 'running go build' | ||
go build ./... | ||
|
||
export TEST_FLAGS="-timeout=${GO_TEST_TIMEOUT:-5m}" | ||
|
||
if [[ ${coverage} -eq 1 ]]; then | ||
TEST_FLAGS+=" -covermode=atomic -coverprofile=coverage.txt" | ||
fi | ||
|
||
echo "running go test ${TEST_FLAGS} ./..." | ||
go test ${TEST_FLAGS} ./... | ||
fi | ||
|
||
if [[ "${run_lint}" -eq 1 ]]; then | ||
check_cmd golangci-lint \ | ||
'have you installed github.com/golangci/golangci-lint?' || exit 1 | ||
|
||
echo 'running golangci-lint' | ||
golangci-lint run --deadline=8m | ||
echo 'checking license headers' | ||
./scripts/check_license.sh ${go_srcs} | ||
fi | ||
} | ||
|
||
main "$@" |