-
Notifications
You must be signed in to change notification settings - Fork 58
201 lines (169 loc) · 7.57 KB
/
standard_library_upgrade_tests.yml
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
name: CodeQL Standard Library Upgrade tests
# Run this workflow every time the "supported_codeql_configs.json" file is changed
on:
pull_request:
branches:
- main
- "rc/**"
- next
paths:
- "supported_codeql_configs.json"
workflow_dispatch:
jobs:
prepare-unit-test-matrix:
name: Prepare CodeQL unit test matrix
runs-on: ubuntu-22.04
outputs:
matrix: ${{ steps.export-unit-test-matrix.outputs.matrix }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Export unit test matrix
id: export-unit-test-matrix
run: |
echo "::set-output name=matrix::$(
jq --compact-output \
'.supported_environment | map([.+{os: "ubuntu-20.04-xl", codeql_standard_library_ident : .codeql_standard_library | sub("\/"; "_")}]) | flatten | {include: .}' \
supported_codeql_configs.json
)"
run-test-suites:
name: Run unit tests
needs: prepare-unit-test-matrix
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix: ${{fromJSON(needs.prepare-unit-test-matrix.outputs.matrix)}}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Python 3
uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Cache CodeQL
id: cache-codeql
uses: actions/cache@v4
with:
# A list of files, directories, and wildcard patterns to cache and restore
path: ${{github.workspace}}/codeql_home
# An explicit key for restoring and saving the cache
key: codeql-home-${{matrix.os}}-${{matrix.codeql_cli}}-${{matrix.codeql_standard_library}}
- name: Install CodeQL
if: steps.cache-codeql.outputs.cache-hit != 'true'
uses: ./.github/actions/install-codeql
with:
codeql-cli-version: ${{matrix.codeql_cli}}
codeql-stdlib-version: ${{matrix.codeql_standard_library}}
codeql-home: ${{ github.workspace }}/codeql_home
- name: Run test suites
id: run-test-suites
env:
RUNNER_OS: ${{runner.os}}
RUNNER_TEMP: ${{runner.temp}}
CODEQL_CLI: ${{matrix.codeql_cli}}
CODEQL_STDLIB: ${{matrix.codeql_standard_library}}
CODEQL_STDLIB_IDENT: ${{matrix.codeql_standard_library_ident}}
shell: python
run: |
import os
import sys
import subprocess
import re
import json
from pathlib import Path
def print_error(fmt, *args):
print(f"::error::{fmt}", *args)
def print_error_and_fail(fmt, *args):
print_error(fmt, args)
sys.exit(1)
def print_debug(fmt, *args):
print(f"::debug::{fmt}", *args)
def print_warning(fmt, *args):
print(f"::warning::{fmt}", *args)
def set_output(key, value):
print(f"::set-output name={key}::{value}")
cli_version = os.environ['CODEQL_CLI']
stdlib_ref = os.environ['CODEQL_STDLIB']
stdlib_ref_ident = os.environ['CODEQL_STDLIB_IDENT']
workspace = os.environ['GITHUB_WORKSPACE']
runner_os = os.environ['RUNNER_OS']
runner_temp = os.environ['RUNNER_TEMP']
codeql_home = os.path.join(workspace, 'codeql_home')
codeql_bin = os.path.join(codeql_home, 'codeql', 'codeql')
if runner_os == "Windows":
codeql_bin += ".exe"
test_report_path = os.path.join(runner_temp, f"test_report_{runner_os}_{cli_version}_{stdlib_ref_ident}.json")
os.makedirs(os.path.dirname(test_report_path), exist_ok=True)
with open(test_report_path, 'w') as test_report_file:
codeql_home = os.path.join(workspace, 'codeql_home')
stdlib_path = os.path.join(codeql_home, 'codeql-stdlib')
cpp_test_root = Path(stdlib_path, 'cpp/ql/test')
print(f"Executing tests found (recursively) in the directory '{cpp_test_root}'")
cp = subprocess.run([codeql_bin, "test", "run", "--format=json", cpp_test_root], stdout=test_report_file, stderr=subprocess.PIPE)
if cp.returncode != 0:
print_error_and_fail(f"Failed to run tests with return code {cp.returncode} and error {cp.stderr}")
test_summary = cp.stderr.decode('utf-8')
m = re.match(r"Executing (?P<tests>\d+) tests in (?P<dirs>\d+) directories\.(?P<progress>.*?)\r?\nAll (?P<passed>\d+) tests passed\.", test_summary, re.DOTALL)
if m == None:
print_error_and_fail(f"Unable to parse test summary: {test_summary}")
tests = int(m.group('tests'))
dirs = int(m.group('dirs'))
passed = int(m.group('passed'))
print(f"Executed {tests} tests in {dirs} dirs and {passed} passed.")
test_summary_path = os.path.join(runner_temp, f"test_summary_{runner_os}_{cli_version}_{stdlib_ref_ident}.json")
with open(test_summary_path, 'w') as test_summary_file:
json.dump({
"os": runner_os,
"cli_version": cli_version,
"stdlib_ref": stdlib_ref,
"tests": tests,
"dirs": dirs,
"passed": passed
}, test_summary_file)
- name: Upload test results
uses: actions/upload-artifact@v4
with:
name: test-results-${{runner.os}}-${{matrix.codeql_cli}}-${{matrix.codeql_standard_library_ident}}
path: |
${{runner.temp}}/test_report_${{runner.os}}_${{matrix.codeql_cli}}_${{matrix.codeql_standard_library_ident}}.json
${{runner.temp}}/test_summary_${{runner.os}}_${{matrix.codeql_cli}}_${{matrix.codeql_standard_library_ident}}.json
if-no-files-found: error
validate-test-results:
name: Validate test results
needs: [run-test-suites]
runs-on: ubuntu-22.04
steps:
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.9"
- name: Collect test results
uses: actions/download-artifact@v4
- name: Validate test results
shell: python
run: |
import os
import sys
import json
from pathlib import Path
def print_error(fmt, *args):
print(f"::error::{fmt}", *args)
def print_error_and_fail(fmt, *args):
print_error(fmt, args)
sys.exit(1)
workspace = Path(os.environ['GITHUB_WORKSPACE'])
test_summary_files = workspace.glob("*/test_summary_*.json")
total_tests = 0
total_dirs = 0
total_passed = 0
for test_summary_file_path in test_summary_files:
with open(test_summary_file_path, 'r') as test_summary_file:
test_summary = json.load(test_summary_file)
print(f"os: {test_summary['os']} cli: {test_summary['cli_version']} stdlib: {test_summary['stdlib_ref']} tests: {test_summary['tests']} dirs: {test_summary['dirs']} passed: {test_summary['passed']}")
total_tests += test_summary['tests']
total_dirs += test_summary['dirs']
total_passed += test_summary['passed']
print(f"Total tests: {total_tests} total dirs: {total_dirs} total passed: {total_passed}")
if total_passed != total_tests:
print_error_and_fail(f"{total_tests - total_passed} out of ${total_tests} failed!")
print("All tests passed!")