-
Notifications
You must be signed in to change notification settings - Fork 3
252 lines (242 loc) · 9.44 KB
/
tests.yaml
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
name: Tests
on:
pull_request:
branches-ignore:
- '**test-branch/**'
jobs:
inclusive-naming-check:
name: Inclusive naming
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4
with:
repository: canonical/Inclusive-naming
path: "./"
- run: mv * /tmp
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4
- name: Merge configuration files
run: |
# Combine all entries and replace matching elements by
# .name in .rules for the ones in .woke.yaml
woke_file=""
if [ -f .woke.yaml ]; then
woke_file=".woke.yaml"
elif [ -f .woke.yml ]; then
woke_file=".woke.yml"
fi
if [ ! -z "$woke_file" ]; then
yq eval-all '
(
. as $item ireduce ({}; . *+ $item) | .rules | unique_by(.name)
) as $mergedArray | . as $item ireduce ({}; . *+ $item) | .rules = $mergedArray
' $woke_file /tmp/config.yml | tee /tmp/merged.yml
mv /tmp/merged.yml /tmp/config.yml
fi
- name: Run inclusive naming check
uses: canonical/inclusive-naming@main
with:
fail-on-error: "true"
github-token: ${{ secrets.GITHUB_TOKEN }}
reporter: github-pr-review
woke-args: '. -c /tmp/config.yml'
filter-mode: nofilter
workdir: "./"
woke-version: latest
shellcheck-lint:
name: Shell scripts lint
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4
- name: Gather files to scan
shell: bash
id: gather
run: |
declare -a filepaths
shebangregex="^#! */[^ ]*/(env *)?[abk]*sh"
set -f # temporarily disable globbing so that globs in inputs aren't expanded
while IFS= read -r -d '' file; do
filepaths+=("$file")
done < <(find . \
-type f \
'(' \
-name '*.bash' \
-o -name '.bashrc' \
-o -name 'bashrc' \
-o -name '.bash_aliases' \
-o -name '.bash_completion' \
-o -name '.bash_login' \
-o -name '.bash_logout' \
-o -name '.bash_profile' \
-o -name 'bash_profile' \
-o -name '*.ksh' \
-o -name 'suid_profile' \
-o -name '*.zsh' \
-o -name '.zlogin' \
-o -name 'zlogin' \
-o -name '.zlogout' \
-o -name 'zlogout' \
-o -name '.zprofile' \
-o -name 'zprofile' \
-o -name '.zsenv' \
-o -name 'zsenv' \
-o -name '.zshrc' \
-o -name 'zshrc' \
-o -name '*.sh' \
-o -path '*/.profile' \
-o -path '*/profile' \
-o -name '*.shlib' \
')' \
-print0)
while IFS= read -r -d '' file; do
head -n1 "$file" | grep -Eqs "$shebangregex" || continue
filepaths+=("$file")
done < <(find . \
-type f ! -name '*.*' -perm /111 \
-print0)
echo "filepaths=${filepaths[@]}" >> $GITHUB_OUTPUT
set +f # re-enable globbing
- if: ${{ steps.gather.outputs.filepaths != '' }}
name: Shellcheck Problem Matchers
uses: lumaxis/[email protected]
- if: ${{ steps.gather.outputs.filepaths != '' }}
run: shellcheck -f gcc ${{steps.gather.outputs.filepaths}}
code-lint:
name: Code lint
runs-on: ubuntu-22.04
outputs:
outcome: ${{ steps.report.outputs.outcome }}
steps:
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4
- name: Install tox
run: python3 -m pip install tox
- name: Run tests
id: run-tests
run: |
# Ensure that stdout appears as normal and redirect to file and exit depends on exit code of first command
STDOUT_LOG=$(mktemp --suffix=stdout.log)
echo STDOUT_LOG=$STDOUT_LOG >> $GITHUB_ENV
tox --result-json=test-result.json | tee $STDOUT_LOG ; test ${PIPESTATUS[0]} -eq 0
- name: Check lint and test stdout
run: |
# Check dependencies
EXPECTED_LINT_DEPS="\
mypy \
isort \
black \
flake8-docstrings \
flake8-docstrings-complete \
flake8-builtins \
flake8-test-docs \
pep8-naming \
codespell \
pylint \
pydocstyle \
"
for EXPECTED_LINT_DEP in $EXPECTED_LINT_DEPS; do
# Check that there is a `lint...<dependency>` line for each of the expected dependencies
DEP_REGEX="lint.*$EXPECTED_LINT_DEP"
if ! grep -q "$DEP_REGEX" $STDOUT_LOG ; then
# Write to stderr
>&2 echo "$EXPECTED_LINT_DEP should be in deps of [testenv:lint] environment in tox.ini"
exit 1
fi
done
# Check commands
EXPECTED_LINT_CMDS="\
pydocstyle \
codespell \
flake8 \
isort \
black \
mypy \
pylint \
"
for EXPECTED_LINT_CMD in $EXPECTED_LINT_CMDS; do
# Check that there is a `lint...commands...<command>` line for each of the expected commands
CMD_REGEX="lint.*commands.*$EXPECTED_LINT_CMD"
if ! grep -q "$CMD_REGEX" $STDOUT_LOG ; then
# Write to stderr
>&2 echo "$EXPECTED_LINT_CMD should be in commands of [testenv:lint] environment in tox.ini"
exit 1
fi
done
- name: Export test report
if: always()
uses: actions/github-script@v6
with:
script: |
const no_color = (text) => {
return text.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
}
const sha = '${{ github.event.pull_request.head.sha }}';
const fs = require('fs');
const result = JSON.parse(fs.readFileSync('./test-result.json')).testenvs;
let lint_result = result.lint.test;
let lint_success = true;
let lint_output = '';
for (let lint_test_result of lint_result) {
if (lint_test_result.retcode != 0) {
lint_success = false;
}
if (lint_test_result.output) {
lint_output += lint_test_result.output;
}
}
let static_result = result.static.test;
let static_output = static_result[0].output;
let reports = [];
if (!lint_success) {
reports.push(
`Lint checks failed for ${sha}\n
\`\`\`\n${no_color(lint_output).trim()}\n\`\`\``
);
}
reports.push(
`Static code analysis report\n
\`\`\`\n${no_color(static_output).trim()}\n\`\`\``
);
let json = JSON.stringify(reports);
fs.writeFileSync('report.json', json);
- name: Upload Lint report
uses: actions/upload-artifact@v3
if: always() && github.event_name == 'pull_request'
with:
name: report
path: report.json
- name: Report
if: always()
id: report
run: echo "outcome=${{ steps.run-tests.conclusion }}" >> $GITHUB_OUTPUT
license-headers-check:
name: Check license headers
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4
- name: Check .licenserc.yaml exists
id: licenserc-yaml
run: echo "exists=$([ -f .licenserc.yaml ] && echo "true" || echo "false")" >> $GITHUB_OUTPUT
- name: Get default license configuration
if: steps.licenserc-yaml.outputs.exists == 'false'
run: |
curl -fLsSo .licenserc.yaml https://raw.githubusercontent.com/canonical/operator-workflows/main/.github/files/.licenserc.yaml
- name: Check license headers
uses: apache/skywalking-eyes/header@main
with:
config: .licenserc.yaml
required_status_checks:
if: "always()"
name: Required Test Status Checks
runs-on: ubuntu-22.04
needs:
- inclusive-naming-check
- code-lint
- shellcheck-lint
- license-headers-check
steps:
- run: |
[ '${{ needs.inclusive-naming-check.result }}' = 'success' ] || (echo inclusive-naming-check failed && false)
[ '${{ needs.code-lint.result }}' = 'success' ] || (echo lint-and-unit-test failed && false)
[ '${{ needs.shellcheck-lint.result }}' = 'success' ] || (echo shellcheck-lint failed && false)
[ '${{ needs.license-headers-check.result }}' = 'success' ] || (echo license-headers-check failed && false)