Skip to content

Commit 0d36fe9

Browse files
committed
Added presubmit script distilled from that in trillian
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.
1 parent ff6e921 commit 0d36fe9

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

scripts/check_license.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
#
3+
# Checks that source files (.go and .proto) have the Apache License header.
4+
# Automatically skips generated files.
5+
set -eu
6+
7+
check_license() {
8+
local path="$1"
9+
10+
if head -1 "$path" | grep -iq 'generated by'; then
11+
return 0
12+
fi
13+
14+
# Look for "Apache License" on the file header
15+
if ! head -10 "$path" | grep -q 'Apache License'; then
16+
# Format: $path:$line:$message
17+
echo "$path:10:license header not found"
18+
return 1
19+
fi
20+
}
21+
22+
main() {
23+
if [[ $# -lt 1 ]]; then
24+
echo "Usage: $0 <path>"
25+
exit 1
26+
fi
27+
28+
local code=0
29+
while [[ $# -gt 0 ]]; do
30+
local path="$1"
31+
if [[ -d "$path" ]]; then
32+
for f in "$path"/*.{go,proto}; do
33+
if [[ ! -f "$f" ]]; then
34+
continue # Empty glob
35+
fi
36+
check_license "$f" || code=1
37+
done
38+
else
39+
check_license "$path" || code=1
40+
fi
41+
shift
42+
done
43+
exit $code
44+
}
45+
46+
main "$@"

scripts/presubmit.sh

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/bin/bash
2+
#
3+
# Presubmit checks for this repository.
4+
#
5+
# Checks for lint errors, spelling, licensing, correct builds / tests and so on.
6+
# Flags may be specified to allow suppressing of checks or automatic fixes, try
7+
# `scripts/presubmit.sh --help` for details.
8+
#
9+
# Globals:
10+
# GO_TEST_TIMEOUT: timeout for 'go test'. Optional (defaults to 5m).
11+
set -eu
12+
13+
check_pkg() {
14+
local cmd="$1"
15+
local pkg="$2"
16+
check_cmd "$cmd" "try running 'go get -u $pkg'"
17+
}
18+
19+
check_cmd() {
20+
local cmd="$1"
21+
local msg="$2"
22+
if ! type -p "${cmd}" > /dev/null; then
23+
echo "${cmd} not found, ${msg}"
24+
return 1
25+
fi
26+
}
27+
28+
usage() {
29+
echo "$0 [--coverage] [--fix] [--no-mod-tidy] [--no-build] [--no-linters]"
30+
}
31+
32+
main() {
33+
local coverage=0
34+
local fix=0
35+
local run_mod_tidy=1
36+
local run_build=1
37+
local run_lint=1
38+
while [[ $# -gt 0 ]]; do
39+
case "$1" in
40+
--coverage)
41+
coverage=1
42+
;;
43+
--fix)
44+
fix=1
45+
;;
46+
--no-mod-tidy)
47+
run_mod_tidy=0
48+
;;
49+
--help)
50+
usage
51+
exit 0
52+
;;
53+
--no-build)
54+
run_build=0
55+
;;
56+
--no-linters)
57+
run_lint=0
58+
;;
59+
*)
60+
usage
61+
exit 1
62+
;;
63+
esac
64+
shift 1
65+
done
66+
67+
cd "$(dirname "$0")" # at scripts/
68+
cd .. # at top level
69+
70+
go_srcs="$(find . -name '*.go' | \
71+
grep -v mock_ | \
72+
grep -v .pb.go | \
73+
grep -v _string.go | \
74+
grep -v .shims.go | \
75+
tr '\n' ' ')"
76+
77+
if [[ "$fix" -eq 1 ]]; then
78+
check_pkg goimports golang.org/x/tools/cmd/goimports || exit 1
79+
80+
echo 'running gofmt'
81+
gofmt -s -w ${go_srcs}
82+
echo 'running goimports'
83+
goimports -w ${go_srcs}
84+
if [[ "$run_mod_tidy" -eq 1 ]]; then
85+
echo 'running go mod tidy'
86+
go mod tidy
87+
fi
88+
fi
89+
90+
if [[ "${run_build}" -eq 1 ]]; then
91+
echo 'running go build'
92+
go build ./...
93+
94+
export TEST_FLAGS="-timeout=${GO_TEST_TIMEOUT:-5m}"
95+
96+
if [[ ${coverage} -eq 1 ]]; then
97+
TEST_FLAGS+=" -covermode=atomic -coverprofile=coverage.txt"
98+
fi
99+
100+
echo "running go test ${TEST_FLAGS} ./..."
101+
go test ${TEST_FLAGS} ./...
102+
fi
103+
104+
if [[ "${run_lint}" -eq 1 ]]; then
105+
check_cmd golangci-lint \
106+
'have you installed github.com/golangci/golangci-lint?' || exit 1
107+
108+
echo 'running golangci-lint'
109+
golangci-lint run --deadline=8m
110+
echo 'checking license headers'
111+
./scripts/check_license.sh ${go_srcs}
112+
fi
113+
}
114+
115+
main "$@"

0 commit comments

Comments
 (0)