-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathrun-tests.sh
executable file
·219 lines (197 loc) · 6.32 KB
/
run-tests.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env bash
#
# This file is part of CERN Open Data Portal.
# Copyright (C) 2015, 2016, 2017, 2018, 2019, 2020, 2024, 2025 CERN.
#
# CERN Open Data Portal is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# CERN Open Data Portal is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CERN Open Data Portal; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
set -o errexit
set -o nounset
data_dois() {
dupes=$(jq '.[].doi' data/{records,skeletons}/*.json | sort | grep -v null | uniq -d)
if [ "x${dupes}" != "x" ]; then
echo "[ERROR] Found duplicate record DOIs:"
echo "${dupes}"
exit 1
fi
echo "Record DOIs are unique."
}
data_json() {
find . -name "*.json" -exec ./scripts/clean_json_file.py --check {} \;
}
data_licenses() {
scripts/check_licenses.py
}
data_recids() {
dupes=$(jq '.[].recid' data/{records,skeletons}/*.json | sort | uniq -d)
if [ "x${dupes}" != "x" ]; then
echo "[ERROR] Found duplicate record IDs:"
echo "${dupes}"
exit 1
fi
echo "Record IDs are unique."
}
data_slugs() {
# shellcheck disable=SC2044
dupes=$(for file in $(find data/docs -name "*.json"); do jq '.[].slug' "$file"; done | sort | grep -v null | uniq -d)
if [ "x${dupes}" != "x" ]; then
echo "[ERROR] Found duplicate docs slugs:"
echo "${dupes}"
exit 1
fi
echo "Docs slugs are unique."
}
data_types() {
# shellcheck disable=SC2044
for file in $(find data/{records,docs}/ -name "*.json"); do
secondaries=$(jq '.[].type.secondary' "$file" -c | sort | uniq)
if echo "$secondaries" | grep -q -e '\[\]' -e "null"; then
echo "[Warning] empty type.secondary field in $file"
fi
done
}
format_black() {
black --check .
}
format_isort() {
isort -rc -c -df --profile black -- **/*.py
}
format_shfmt() {
shfmt -d .
}
format_whitespace() {
whitespace_found_p=0
for file in $(git ls-files | grep -E '.(py|html|css|json|md|sh|txt|yml)$'); do
if grep -q ' $' "$file"; then
whitespace_found_p=1
echo "[ERROR] Found trailing whitespace in ${file}."
fi
done
if [ "${whitespace_found_p}" != "0" ]; then
exit 1
fi
}
lint_commitlint() {
from=${2:-master}
to=${3:-HEAD}
npx commitlint --from="$from" --to="$to"
found=0
while IFS= read -r line; do
commit_hash=$(echo "$line" | cut -d ' ' -f 1)
commit_title=$(echo "$line" | cut -d ' ' -f 2-)
commit_number_of_parents=$(git rev-list --parents "$commit_hash" -n1 | awk '{print NF-1}')
# (i) skip checking release commits generated by Release Please
if [ "$commit_number_of_parents" -le 1 ] && echo "$commit_title" | grep -qP "^chore\(.*\): release"; then
continue
fi
# (ii) check absence of merge commits in feature branches
if [ "$commit_number_of_parents" -gt 1 ]; then
if echo "$commit_title" | grep -qP "^chore\(.*\): merge "; then
break # skip checking maint-to-master merge commits
else
echo "✖ Merge commits are not allowed in feature branches: $commit_title"
found=1
fi
fi
done < <(git log "$from..$to" --format="%H %s")
if [ $found -gt 0 ]; then
exit 1
fi
}
lint_flake8() {
flake8 .
}
lint_jsonlint() {
find . -name "*.json" -exec jsonlint -q {} \+
}
lint_pydocstyle() {
pydocstyle scripts
}
lint_shellcheck() {
find . -name "*.sh" -exec shellcheck {} \+
}
lint_yamllint() {
yamllint .
}
all() {
data_dois
data_json
data_licenses
data_recids
data_slugs
data_types
format_black
format_isort
format_shfmt
format_whitespace
lint_commitlint
lint_flake8
lint_jsonlint
lint_pydocstyle
lint_shellcheck
lint_yamllint
}
help() {
echo "Usage: $0 [options]"
echo "Options:"
echo " --all Perform all checks [default]"
echo " --data-dois Check data DOIs"
echo " --data-json Check data JSON"
echo " --data-licenses Check data licenses"
echo " --data-recids Check data record IDs "
echo " --data-slugs Check data slugs"
echo " --data-types Check data types"
echo " --help Display this help message"
echo " --format-black Check formatting of Python code"
echo " --format-isort Check formatting of Python imports"
echo " --format-shfmt Check formatting of shell scripts"
echo " --format-whitespace Check formatting of whitespace"
echo " --lint-commitlint Check linting of commit messages"
echo " --lint-flake8 Check linting of Python code"
echo " --lint-jsonlint Check linting of JSON files"
echo " --lint-pydocstyle Check linting of Python docstrings"
echo " --lint-shellcheck Check linting of shell scripts"
echo " --lint-yamllint Check linting of YAML files"
}
if [ $# -eq 0 ]; then
all
exit 0
fi
arg="$1"
case $arg in
--all) all ;;
--help) help ;;
--data-dois) data_dois ;;
--data-json) data_json ;;
--data-licenses) data_licenses ;;
--data-recids) data_recids ;;
--data-slugs) data_slugs ;;
--data-types) data_types ;;
--format-black) format_black ;;
--format-isort) format_isort ;;
--format-shfmt) format_shfmt ;;
--format-whitespace) format_whitespace ;;
--lint-commitlint) lint_commitlint "$@" ;;
--lint-flake8) lint_flake8 ;;
--lint-jsonlint) lint_jsonlint ;;
--lint-pydocstyle) lint_pydocstyle ;;
--lint-shellcheck) lint_shellcheck ;;
--lint-yamllint) lint_yamllint ;;
*) echo "[ERROR] Invalid argument '$arg'. Exiting." && help && exit 1 ;;
esac