-
Notifications
You must be signed in to change notification settings - Fork 0
204 lines (178 loc) · 7.69 KB
/
codeql-qlt-unit-tests.yml
File metadata and controls
204 lines (178 loc) · 7.69 KB
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
name: "CodeQL Development Toolkit (QLT) - Run Unit Tests (Multi-Language)"
# # `qlt` is the (Code)QL (Development) Toolkit, which wraps the `codeql` CLI.
# # Ref: https://github.com/advanced-security/codeql-development-toolkit/blob/main/README.md
on:
push:
branches:
- "main"
paths:
- ".github/workflows/codeql-qlt-unit-tests.yml"
- "languages/*/tools/{src,test}/**"
pull_request:
branches:
- "main"
paths:
- ".github/workflows/codeql-qlt-unit-tests.yml"
- "languages/*/tools/{src,test}/**"
workflow_dispatch:
permissions:
contents: read
jobs:
discover-test-languages:
name: Discover Languages with CodeQL Unit Tests
runs-on: ubuntu-latest
outputs:
languages: ${{ steps.find-languages.outputs.languages }}
has_languages: ${{ steps.find-languages.outputs.has_languages }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Setup CodeQL environment for discovering unit tests
uses: ./.github/actions/setup-codeql-environment
with:
install-codeql: true
install-language-runtimes: false
install-ql-packs: false
- name: Find languages with CodeQL unit tests
id: find-languages
shell: bash
run: |
# Find all languages with unit tests
languages_array="["
first=true
for lang_dir in languages/*/; do
if [[ -d "$lang_dir" ]]; then
lang=$(basename "$lang_dir")
# Skip directories for unsupported languages and/or languages without tests.
# The `ql` language (i.e. ql for ql) is always a special case.
# The `actions` language is currently not supported by `qlt` but is supported by `codeql`.
# TODO: Implement support for `actions` language in `qlt`
if [[ "$lang" == "ql" || "$lang" == "actions" ]]; then
echo "⚠️ qlt unit testing is not currently supported for language: $lang"
continue
fi
echo "Checking for unit tests in language: $lang"
# Use codeql resolve tests to check if this language has any unit tests
if codeql resolve tests --format=text --strict-test-discovery -- "languages/$lang" 2>/dev/null | grep -q ".qlref"; then
echo "✅ Found unit tests for language: $lang"
if [[ "$first" == "true" ]]; then
languages_array="$languages_array\"$lang\""
first=false
else
languages_array="$languages_array,\"$lang\""
fi
else
echo "⚠️ No unit tests found for language: $lang"
fi
fi
done
languages_array="$languages_array]"
if [[ "$languages_array" == "[]" ]]; then
echo "languages=[]" >> $GITHUB_OUTPUT
echo "has_languages=false" >> $GITHUB_OUTPUT
echo "❌ No languages with unit tests found"
else
echo "languages=$languages_array" >> $GITHUB_OUTPUT
echo "has_languages=true" >> $GITHUB_OUTPUT
echo "📊 Languages with tests: $languages_array"
fi
run-test-suites:
name: Use `qlt` to run unit tests for language=${{ matrix.language }}
needs: [discover-test-languages]
permissions:
contents: write
if: ${{ needs.discover-test-languages.outputs.has_languages == 'true' }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
language: ${{ fromJSON(needs.discover-test-languages.outputs.languages) }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Setup CodeQL environment for executing `qlt` unit tests
uses: ./.github/actions/setup-codeql-environment
- name: "Run CodeQL unit tests for language=${{ matrix.language }}"
# Only run when the language is set to a known non-empty value
if: ${{ matrix.language && matrix.language != 'unknown' }}
id: run-test-suites
env:
RUNNER_OS: ${{ runner.os }}
RUNNER_TMP: ${{ runner.temp }}
# Additional environment variables for specific languages
LGTM_INDEX_XML_MODE: all
shell: bash
run: |
echo "Running CodeQL unit tests for language: ${{ matrix.language }}"
# Create work directory for test results
mkdir -p ./test-results
# Run tests for the specific language using qlt
set +e # Don't exit on error so we can still upload partial results
qlt test run execute-unit-tests \
--base languages/ \
--runner-os ${{ runner.os }} \
--language ${{ matrix.language }} \
--work-dir ./test-results
TEST_EXIT_CODE=$?
set -e # Re-enable exit on error
# Check if any results were generated
if [ -d "./test-results" ] && [ "$(find ./test-results -type f | wc -l)" -gt 0 ]; then
echo "✅ Test results generated for language: ${{ matrix.language }}"
echo "Generated files:"
find ./test-results -type f | head -10
else
echo "⚠️ No test result files found for language: ${{ matrix.language }}"
fi
if [ $TEST_EXIT_CODE -ne 0 ]; then
echo "❌ Tests failed for language: ${{ matrix.language }} with exit code: $TEST_EXIT_CODE"
exit $TEST_EXIT_CODE
fi
echo "✅ Tests completed successfully for language: ${{ matrix.language }}"
- name: Upload test results
# Upload results even if tests failed, but only if the language is valid
if: ${{ always() && matrix.language && matrix.language != 'unknown' }}
uses: actions/upload-artifact@v7
with:
name: qlt-test-results-${{ matrix.language }}-${{ runner.os }}
path: |
test-results/
if-no-files-found: warn
validate-test-results:
name: Validate test results
needs: [discover-test-languages, run-test-suites]
if: ${{ needs.discover-test-languages.outputs.has_languages == 'true' && !cancelled() }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Setup CodeQL environment for validating `qlt` unit tests
uses: ./.github/actions/setup-codeql-environment
with:
install-codeql: false
install-language-runtimes: false
install-ql-packs: false
- name: Download CodeQL unit test results
uses: actions/download-artifact@v8
with:
pattern: qlt-test-results-*
path: test-results/
merge-multiple: true
- name: Validate and display unit test results
shell: bash
run: |
echo "Running qlt test run validate-unit-tests to validate all test results..."
# Debug: List downloaded artifacts
echo "Downloaded artifacts structure:"
find test-results/ -type f -name "*.json" -o -name "*.xml" -o -name "*.txt" | head -20
# Ensure the test-results directory exists and has content
if [ ! -d "test-results/" ] || [ -z "$(find test-results/ -type f)" ]; then
echo "❌ No test results found in test-results/ directory"
echo "Contents of current directory:"
ls -la
echo "Contents of test-results/ (if it exists):"
ls -la test-results/ || echo "test-results/ directory does not exist"
exit 1
fi
# Use qlt to validate the test results
qlt test run validate-unit-tests --base languages/ --results-directory test-results/
echo "✅ Test validation completed"