-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrun-tests.sh
executable file
·176 lines (155 loc) · 4.75 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
#!/usr/bin/env bash
#
# This file is part of cernopendata-client.
#
# Copyright (C) 2019, 2020, 2021, 2023, 2024, 2025 CERN.
#
# cernopendata-client is free software; you can redistribute it and/or modify
# it under the terms of the GPLv3 license; see LICENSE file for more details.
set -o errexit
set -o nounset
lint_commitlint() {
from=${2:-master}
to=${3:-HEAD}
pr=${4:-[0-9]+}
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 presence of PR number
if ! echo "$commit_title" | grep -qP "\(\#$pr\)$"; then
echo "✖ Headline does not end by '(#$pr)' PR number: $commit_title"
found=1
fi
# (iii) 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_shellcheck() {
find . -name "*.sh" -exec shellcheck {} \+
}
format_black() {
black --check .
}
lint_pydocstyle() {
pydocstyle cernopendata_client
}
lint_flake8() {
flake8 .
}
lint_manifest() {
check-manifest
}
lint_hadolint() {
docker run -i --rm docker.io/hadolint/hadolint:v2.12.0 <Dockerfile
}
docker_build() {
docker build -t docker.io/cernopendata/cernopendata-client .
}
docker_tests() {
docker run --rm -v "$PWD"/tests:/code/tests --entrypoint /bin/bash docker.io/cernopendata/cernopendata-client -c 'pytest tests'
}
docs_sphinx() {
sphinx-build -qnNW docs docs/_build/html
sphinx-build -qnNW -b doctest docs docs/_build/doctest
}
lint_markdownlint() {
markdownlint-cli2 "**/*.md"
}
lint_yamllint() {
yamllint .
}
format_prettier() {
prettier -c .
}
python_tests() {
pytest
}
format_shfmt() {
shfmt -d .
}
lint_jsonlint() {
find . -name "*.json" -exec jsonlint -q {} \+
}
all() {
docker_build
docker_tests
docs_sphinx
format_black
format_prettier
format_shfmt
lint_commitlint
lint_flake8
lint_hadolint
lint_jsonlint
lint_manifest
lint_markdownlint
lint_pydocstyle
lint_shellcheck
lint_yamllint
python_tests
}
help() {
echo "Usage: $0 [options]"
echo "Options:"
echo " --all Perform all checks [default]"
echo " --docker-build Check Docker build"
echo " --docker-tests Check Docker tests"
echo " --docs-sphinx Check Sphinx docs build"
echo " --format-black Check formatting of Python code"
echo " --format-prettier Check formatting of Markdown etc files"
echo " --format-shfmt Check formatting of shell scripts"
echo " --help Display this help message"
echo " --lint-commitlint Check linting of commit messages"
echo " --lint-flake8 Check linting of Python code"
echo " --lint-hadolint Check linting of Dockerfiles"
echo " --lint-jsonlint Check linting of JSON files"
echo " --lint-manifest Check linting of Python manifest"
echo " --lint-markdownlint Check linting of Markdown 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"
echo " --python-tests Check Python test suite"
}
if [ $# -eq 0 ]; then
all
exit 0
fi
arg="$1"
case $arg in
--all) all ;;
--help) help ;;
--docker-build) docker_build ;;
--docker-tests) docker_tests ;;
--docs-sphinx) docs_sphinx ;;
--format-black) format_black ;;
--format-prettier) format_prettier ;;
--format-shfmt) format_shfmt ;;
--lint-commitlint) lint_commitlint "$@" ;;
--lint-flake8) lint_flake8 ;;
--lint-hadolint) lint_hadolint ;;
--lint-jsonlint) lint_jsonlint ;;
--lint-manifest) lint_manifest ;;
--lint-markdownlint) lint_markdownlint ;;
--lint-pydocstyle) lint_pydocstyle ;;
--lint-shellcheck) lint_shellcheck ;;
--lint-yamllint) lint_yamllint ;;
--python-tests) python_tests ;;
*) echo "[ERROR] Invalid argument '$arg'. Exiting." && help && exit 1 ;;
esac