Skip to content

Commit 8be2617

Browse files
authored
Merge pull request #75 from GitHubSecurityLab/improveci
Always run tests.
2 parents 1014f8d + 4353a05 commit 8be2617

File tree

2 files changed

+87
-60
lines changed

2 files changed

+87
-60
lines changed

.github/scripts/pr-tests.sh

Lines changed: 0 additions & 58 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,61 @@ jobs:
5555
- name: Test Queries
5656
if: steps.changes.outputs.src == 'true'
5757
env:
58-
GITHUB_TOKEN: ${{ github.token }}
58+
RUNNER_TEMP: ${{ runner.temp }}
59+
shell: python
5960
run: |
60-
./.github/scripts/pr-tests.sh ${{ github.event.number }} ${{ matrix.language }}
61+
import os
62+
import sys
63+
import subprocess
64+
from pathlib import Path
65+
66+
def print_error(fmt, *args):
67+
print(f"::error::{fmt}", *args)
68+
69+
def print_error_and_fail(fmt, *args):
70+
print_error(fmt, args)
71+
sys.exit(1)
72+
73+
runner_temp = os.environ['RUNNER_TEMP']
74+
75+
test_root = Path('${{ github.workspace }}', '${{ matrix.language }}', 'test')
76+
print(f"Executing tests found (recursively) in the directory '{test_root}'")
77+
files_to_close = []
78+
try:
79+
# Runners have 4 cores, so split the tests into 4 "slices", and run one per thread
80+
num_slices = 4
81+
procs = []
82+
83+
for slice in range(1, num_slices+1):
84+
test_report_path = os.path.join(runner_temp, "${{ matrix.language }}", f"test_report_slice_{slice}_of_{num_slices}.json")
85+
os.makedirs(os.path.dirname(test_report_path), exist_ok=True)
86+
test_report_file = open(test_report_path, 'w')
87+
files_to_close.append(test_report_file)
88+
procs.append(subprocess.Popen(["codeql", "test", "run", "--failing-exitcode=122", f"--slice={slice}/{num_slices}", "--ram=2048", "--format=json", test_root], stdout=test_report_file, stderr=subprocess.PIPE))
89+
90+
for p in procs:
91+
_, err = p.communicate()
92+
if p.returncode != 0:
93+
if p.returncode == 122:
94+
# Failed because a test case failed, so just print the regular output.
95+
# This will allow us to proceed to validate-test-results, which will fail if
96+
# any test cases failed
97+
print(f"{err.decode()}")
98+
else:
99+
# Some more serious problem occurred, so print and fail fast
100+
print_error_and_fail(f"Failed to run tests with return code {p.returncode}\n{err.decode()}")
101+
finally:
102+
for file in files_to_close:
103+
file.close()
104+
105+
- name: Upload test results
106+
if: steps.changes.outputs.src == 'true'
107+
uses: actions/upload-artifact@v4
108+
with:
109+
name: ${{ matrix.language }}-test-results
110+
path: |
111+
${{ runner.temp }}/${{ matrix.language }}/test_report_slice_*.json
112+
if-no-files-found: error
61113

62114
- name: Compile / Check Suites & Packs
63115
if: steps.changes.outputs.src == 'true'
@@ -66,6 +118,39 @@ jobs:
66118
run: |
67119
./.github/scripts/pr-suites-packs.sh ${{ github.event.number }} ${{ matrix.language }}
68120
121+
validate-test-results:
122+
name: Validate test results
123+
needs: compile-and-test
124+
runs-on: ubuntu-latest
125+
steps:
126+
- name: Check if compile-and-test job failed to complete, if so fail
127+
if: ${{ needs.compile-and-test.result == 'failure' }}
128+
uses: actions/github-script@v3
129+
with:
130+
script: |
131+
core.setFailed('Test run job failed')
132+
133+
- name: Collect test results
134+
uses: actions/download-artifact@v4
135+
136+
- name: Validate test results
137+
run: |
138+
if [[ ! -n "$(find . -name 'test_report_*' -print -quit)" ]]; then
139+
echo "No test results found"
140+
exit 0
141+
fi
142+
143+
for json_report in *-test-results/test_report_*
144+
do
145+
jq --raw-output '"PASS \(map(select(.pass == true)) | length)/\(length)'" $json_report\"" "$json_report"
146+
done
147+
FAILING_TESTS=$(jq --raw-output '.[] | select(.pass == false)' *-test-results/test_report_*.json)
148+
if [[ ! -z "$FAILING_TESTS" ]]; then
149+
echo "ERROR: The following tests failed:"
150+
echo $FAILING_TESTS | jq .
151+
exit 1
152+
fi
153+
69154
extensions:
70155
runs-on: ubuntu-latest
71156

0 commit comments

Comments
 (0)