forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint.sh
executable file
·175 lines (153 loc) · 4.77 KB
/
lint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -e
if ! [[ "$0" =~ scripts/lint.sh ]]; then
echo "must be run from repository root"
exit 255
fi
# The -P option is not supported by the grep version installed by
# default on macos. Since `-o errexit` is ignored in an if
# conditional, triggering the problem here ensures script failure when
# using an unsupported version of grep.
grep -P 'lint.sh' scripts/lint.sh &> /dev/null || (\
>&2 echo "error: This script requires a recent version of gnu grep.";\
>&2 echo " On macos, gnu grep can be installed with 'brew install grep'.";\
>&2 echo " It will also be necessary to ensure that gnu grep is available in the path.";\
exit 255 )
if [ "$#" -eq 0 ]; then
# by default, check all source code
# to test only "snow" package
# ./scripts/lint.sh ./snow/...
TARGET="./..."
else
TARGET="${1}"
fi
# by default, "./scripts/lint.sh" runs all lint tests
# to run only "license_header" test
# TESTS='license_header' ./scripts/lint.sh
TESTS=${TESTS:-"golangci_lint license_header require_error_is_no_funcs_as_params single_import interface_compliance_nil require_equal_zero require_len_zero require_equal_len require_nil require_no_error_inline_func"}
function test_golangci_lint {
go install -v github.com/golangci/golangci-lint/cmd/[email protected]
golangci-lint run --config .golangci.yml
}
# automatically checks license headers
# to modify the file headers (if missing), remove "--verify" flag
# TESTS='license_header' ADDLICENSE_FLAGS="--debug" ./scripts/lint.sh
_addlicense_flags=${ADDLICENSE_FLAGS:-"--verify --debug"}
function test_license_header {
go install -v github.com/palantir/[email protected]
local files=()
while IFS= read -r line; do files+=("$line"); done < <(find . -type f -name '*.go' ! -name '*.pb.go' ! -name 'mock_*.go')
go-license \
--config=./header.yml \
${_addlicense_flags} \
"${files[@]}"
}
function test_single_import {
if grep -R -zo -P 'import \(\n\t".*"\n\)' .; then
echo ""
return 1
fi
}
function test_require_error_is_no_funcs_as_params {
if grep -R -zo -P 'require.ErrorIs\(.+?\)[^\n]*\)\n' .; then
echo ""
return 1
fi
}
function test_require_equal_zero {
# check if the first arg, other than t, is 0
if grep -R -o -P 'require\.Equal\((t, )?(u?int\d*\(0\)|0)' .; then
echo ""
echo "Use require.Zero instead of require.Equal when testing for 0."
echo ""
return 1
fi
# check if the last arg is 0
if grep -R -zo -P 'require\.Equal\(.+?, (u?int\d*\(0\)|0)\)\n' .; then
echo ""
echo "Use require.Zero instead of require.Equal when testing for 0."
echo ""
return 1
fi
}
function test_require_len_zero {
if grep -R -o -P 'require\.Len\((t, )?.+, 0(,|\))' .; then
echo ""
echo "Use require.Empty instead of require.Len when testing for 0 length."
echo ""
return 1
fi
}
function test_require_equal_len {
# This should only flag if len(foo) is the *actual* val, not the expected val.
#
# These should *not* match:
# - require.Equal(len(foo), 2)
# - require.Equal(t, len(foo), 2)
#
# These should match:
# - require.Equal(2, len(foo))
# - require.Equal(t, 2, len(foo))
if grep -R -o -P --exclude-dir='scripts' 'require\.Equal\((t, )?.*, len\([^,]*$' .; then
echo ""
echo "Use require.Len instead of require.Equal when testing for length."
echo ""
return 1
fi
}
function test_require_nil {
if grep -R -o -P 'require\..+?!= nil' .; then
echo ""
echo "Use require.NotNil when testing for nil inequality."
echo ""
return 1
fi
if grep -R -o -P 'require\..+?== nil' .; then
echo ""
echo "Use require.Nil when testing for nil equality."
echo ""
return 1
fi
if grep -R -o -P 'require\.ErrorIs.+?nil\)' .; then
echo ""
echo "Use require.NoError instead of require.ErrorIs when testing for nil error."
echo ""
return 1
fi
}
function test_require_no_error_inline_func {
if grep -R -zo -P '\t+err :?= ((?!require|if).|\n)*require\.NoError\((t, )?err\)' .; then
echo ""
echo "Checking that a function with a single error return doesn't error should be done in-line."
echo ""
return 1
fi
}
# Ref: https://go.dev/doc/effective_go#blank_implements
function test_interface_compliance_nil {
if grep -R -o -P '_ .+? = &.+?\{\}' .; then
echo ""
echo "Interface compliance checks need to be of the form:"
echo " var _ json.Marshaler = (*RawMessage)(nil)"
echo ""
return 1
fi
}
function run {
local test="${1}"
shift 1
echo "START: '${test}' at $(date)"
if "test_${test}" "$@" ; then
echo "SUCCESS: '${test}' completed at $(date)"
else
echo "FAIL: '${test}' failed at $(date)"
exit 255
fi
}
echo "Running '$TESTS' at: $(date)"
for test in $TESTS; do
run "${test}" "${TARGET}"
done
echo "ALL SUCCESS!"