Skip to content

Commit 86fb73b

Browse files
jarzecJohelEGP
andauthored
Add a testing script and a github workflow (#897)
* Add a testing script and a github workflow * Fix typos Co-authored-by: Johel Ernesto Guerrero Peña <[email protected]> Signed-off-by: jarzec <[email protected]> * Add workflow_dispatch to regression test workflow * Clean up run-tests.sh logic * Use macos-13 runner for newer clang --------- Signed-off-by: jarzec <[email protected]> Co-authored-by: Johel Ernesto Guerrero Peña <[email protected]>
1 parent d869eed commit 86fb73b

File tree

4 files changed

+329
-0
lines changed

4 files changed

+329
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Regression tests
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
push:
7+
workflow_dispatch:
8+
9+
jobs:
10+
regression-tests-linux-mac:
11+
name: Run on ${{ matrix.os }} using ${{ matrix.compiler }}
12+
runs-on: ${{ matrix.os }}
13+
env:
14+
CXX: ${{ matrix.compiler }}
15+
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
os: [ubuntu-latest]
20+
compiler: [g++-10, g++-13]
21+
include:
22+
- os: macos-13
23+
compiler: clang++
24+
- os: windows-latest
25+
compiler: cl.exe
26+
steps:
27+
- name: Checkout repo
28+
uses: actions/checkout@v3
29+
30+
- name: Run regression tests - Linux and macOS version
31+
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-13'
32+
run: |
33+
cd regression-tests
34+
bash run-tests.sh -c ${{ matrix.compiler }}
35+
36+
- name: Run regression tests - Windows version
37+
if: matrix.os == 'windows-latest'
38+
run: |
39+
"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && ^
40+
git config --local core.autocrlf false && ^
41+
cd regression-tests && ^
42+
bash run-tests.sh -c ${{ matrix.compiler }}
43+
shell: cmd

regression-tests/run-tests.sh

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
#!/bin/bash
2+
3+
################
4+
usage() {
5+
echo "Usage: $0 -c <compiler> [-t <tests to run>]"
6+
echo " -c <compiler> The compiler to use for the test"
7+
echo " -t <tests to run> Runs only the provided, comma-separated tests (filenames including .cpp2)"
8+
echo " If the argument is not used all tests are run"
9+
exit 1
10+
}
11+
12+
################
13+
# Check file existence and compare its state against the version in git
14+
check_file () {
15+
file="$1"
16+
description="$2"
17+
18+
if [[ ! -f "$file" ]]; then
19+
echo " The $description does not exist:"
20+
echo " $file"
21+
failure=1
22+
return
23+
fi
24+
25+
# Check if the file is tracked by git
26+
git ls-files --error-unmatch "$file" > /dev/null 2>&1
27+
untracked=$?
28+
29+
if [[ $untracked -eq 1 ]]; then
30+
echo " The $description is not tracked by git:"
31+
echo " $file"
32+
# Add the file to the index to be able to diff it...
33+
git add "$file"
34+
# ... print the diff ...
35+
git --no-pager diff HEAD -- "$file"
36+
# ... and remove the file from the diff
37+
git rm --cached -- "$file" > /dev/null 2>&1
38+
39+
failure=1
40+
else
41+
# Compare the content with the refernece value checked in git
42+
diff_output=$(git diff --ignore-cr-at-eol -- "$file")
43+
if [[ -n "$diff_output" ]]; then
44+
echo " Non-matching $description:"
45+
printf "\n$diff_output\n\n"
46+
failure=1
47+
fi
48+
fi
49+
}
50+
51+
optstring="c:t:"
52+
while getopts ${optstring} arg; do
53+
case "${arg}" in
54+
c)
55+
cxx_compiler="${OPTARG}"
56+
;;
57+
t)
58+
# Replace commas with spaces
59+
chosen_tests=${OPTARG/,/ }
60+
;;
61+
\?)
62+
echo "Invalid option: -${OPTARG}."
63+
echo
64+
usage
65+
;;
66+
:)
67+
echo "Missing option argument for -$OPTARG"
68+
echo
69+
usage
70+
;;
71+
*)
72+
usage
73+
exit 1
74+
;;
75+
esac
76+
done
77+
78+
if [ -z "$cxx_compiler" ]; then
79+
echo "Compiler not specified"
80+
usage
81+
fi
82+
83+
tests=$(ls | grep ".cpp2$")
84+
if [[ -n "$chosen_tests" ]]; then
85+
for test in $chosen_tests; do
86+
if ! [[ -f "$test" ]]; then
87+
echo "Requested test ($test) not found"
88+
exit 1
89+
fi
90+
done
91+
echo "Performing tests:"
92+
for test in $chosen_tests; do
93+
echo " $test"
94+
done
95+
echo
96+
tests="$chosen_tests"
97+
else
98+
printf "Performing all regression tests\n\n"
99+
fi
100+
101+
expected_results_dir="test-results"
102+
103+
################
104+
# Get the directory with the exec outputs and compilation command
105+
if [[ "$cxx_compiler" == *"cl.exe"* ]]; then
106+
compiler_cmd='cl.exe -nologo -std:c++latest -MD -EHsc -I ..\include -I ..\..\..\include -experimental:module -Fe:'
107+
exec_out_dir="$expected_results_dir/msvc-2022"
108+
compiler_version=$(cl.exe)
109+
else
110+
compiler_cmd="$cxx_compiler -I../include -I../../../include -std=c++20 -pthread -o "
111+
112+
compiler_ver=$("$cxx_compiler" --version)
113+
if [[ "$compiler_ver" == *"Apple clang version 14.0"* ]]; then
114+
exec_out_dir="$expected_results_dir/apple-clang-14"
115+
elif [[ "$compiler_ver" == *"clang version 12.0"* ]]; then
116+
exec_out_dir="$expected_results_dir/clang-12"
117+
elif [[ "$compiler_ver" == *"clang version 15.0"* ]]; then
118+
exec_out_dir="$expected_results_dir/clang-15"
119+
elif [[ "$compiler_ver" == *"g++-10"* ]]; then
120+
exec_out_dir="$expected_results_dir/gcc-10"
121+
elif [[ "$compiler_ver" == *"g++-12"* ||
122+
"$compiler_ver" == *"g++-13"*
123+
]]; then
124+
exec_out_dir="$expected_results_dir/gcc-13"
125+
fi
126+
127+
compiler_version=$("$cxx_compiler" --version)
128+
fi
129+
130+
if [[ -d "$exec_out_dir" ]]; then
131+
printf "Full compiler version for '$cxx_compiler':\n$compiler_version\n\n"
132+
133+
printf "Directory with reference compilation/execution files to use:\n$exec_out_dir\n\n"
134+
else
135+
printf "not found for compiler: '$cxx_compiler'\n\n"
136+
fi
137+
138+
################
139+
cppfront_cmd="cppfront.exe"
140+
echo "Building cppfront"
141+
$compiler_cmd"$cppfront_cmd" ../source/cppfront.cpp
142+
if [[ $? -ne 0 ]]; then
143+
echo "Compilation failed"
144+
exit 2
145+
fi
146+
147+
################
148+
failed_tests=()
149+
failed_compilations=()
150+
skipped_tests=()
151+
echo "Running regression tests"
152+
for test_file in $tests; do
153+
test_name=${test_file%.*}
154+
expeced_output="$expected_results_dir/$test_file.output"
155+
generated_cpp_name=$test_name.cpp
156+
expected_src="$expected_results_dir/$generated_cpp_name"
157+
test_bin="test.exe"
158+
159+
# Choose mode - default to mixed code
160+
descr="mixed Cpp1 and Cpp2 code"
161+
opt=""
162+
# Using naming convention to discriminate pure Cpp2 code
163+
if [[ $test_name == "pure2"* ]]; then
164+
descr="pure Cpp2 code"
165+
opt="-p"
166+
fi
167+
echo " Testing $descr: $test_name.cpp2"
168+
169+
########
170+
# Run the translation test
171+
echo " Generating Cpp1 code"
172+
./"$cppfront_cmd" "$test_file" -o "$expected_src" $opt > "$expeced_output" 2>&1
173+
174+
failure=0
175+
compiler_issue=0
176+
########
177+
# The C++1 generation output has to exist and to be tracked by git
178+
check_file "$expeced_output" "Cpp1 generation output file"
179+
180+
########
181+
# Check the generated code
182+
if [ -f "$expected_src" ]; then
183+
# The file was generated, so it should be tracked by git
184+
check_file "$expected_src" "generated Cpp1 file"
185+
########
186+
# Compile and run the generated code in a sub-shell
187+
expected_src_compil_out="$exec_out_dir/$generated_cpp_name.output"
188+
expected_src_exec_out="$exec_out_dir/$generated_cpp_name.execution"
189+
expected_files="$expected_results_dir/$test_name.files"
190+
191+
echo " Compiling the generated Cpp1 code"
192+
193+
# For some tests the binary needs to be placed in "$exec_out_dir"
194+
# For that reason the compilation is done directly in that dir
195+
# The source is temporarily copied to avoid issues with bash paths in cl.exe
196+
(cd $exec_out_dir; \
197+
cp ../../$expected_src $generated_cpp_name;
198+
$compiler_cmd"$test_bin" \
199+
$generated_cpp_name \
200+
> $generated_cpp_name.output 2>&1)
201+
compilation_result=$?
202+
rm $exec_out_dir/$generated_cpp_name
203+
204+
if [ -f "$expected_src_compil_out" ]; then
205+
# Check for local compiler issues
206+
if [[ $compilation_result -ne 0 ]]; then
207+
# Workaround an issue with MSVC missing std modules
208+
if cat $expected_src_compil_out | grep -q "error C1011"; then
209+
echo " Skipping further checks due to missing std modules support"
210+
compiler_issue=1
211+
fi
212+
fi
213+
########
214+
# Check the Cpp1 compilation message (if there are no local compiler issues)
215+
if [[ $compiler_issue -ne 1 ]]; then
216+
check_file "$expected_src_compil_out" "Cpp1 compilation message file"
217+
fi
218+
# Check the result of a successful compilation
219+
if [[ $compilation_result -eq 0 ]]; then
220+
########
221+
# Execute the compiled code in $exec_out_dir
222+
echo " Executing the compiled test binary"
223+
# Run the binary in a sub-shell in $exec_out_dir so that files are written there
224+
( cd "$exec_out_dir"; ./$test_bin > "$generated_cpp_name.execution" 2>&1 )
225+
226+
check_file "$expected_src_exec_out" "execution output file"
227+
# If the test generates files check their content
228+
if [[ -f "$expected_files" ]]; then
229+
echo " Checking files written by the binary"
230+
files="$(cat "$expected_files")"
231+
for file in ${files/,/ }; do
232+
check_file "$exec_out_dir/$file" "file meant to be written by the binary"
233+
done
234+
fi
235+
fi
236+
fi
237+
elif [[ $(cat "$expeced_output") != *"error"* ]]; then
238+
echo " Missing generated src file treated as failure"
239+
echo " Failing compilation message needs to contain 'error'"
240+
failure=1
241+
fi
242+
243+
if [[ $failure -ne 0 ]]; then
244+
failed_tests+=($test_name)
245+
fi
246+
247+
if [[ $compiler_issue -ne 0 ]]; then
248+
skipped_tests+=($test_name)
249+
elif [[ $compilation_result -ne 0 ]]; then
250+
failed_compilations+=($test_name)
251+
fi
252+
done
253+
254+
################
255+
# Report missing reference data direcotry
256+
if [[ ! -d "$exec_out_dir" ]]; then
257+
echo "Reference data directory not found for compiler: '$cxx_compiler'"
258+
exit 3
259+
fi
260+
261+
################
262+
# Report skipped/failed tests
263+
report_issues() {
264+
local msg=$1
265+
shift 1
266+
local reported_tests=("$@")
267+
local num_reported_tests=$(wc -w <<< ${reported_tests[@]})
268+
if [ $num_reported_tests -ne 0 ]; then
269+
echo "$msg: $num_reported_tests"
270+
for reported_test in ${reported_tests[@]}; do
271+
echo " $reported_test.cpp2"
272+
done
273+
fi
274+
return $num_reported_tests
275+
}
276+
277+
report_issues "Tests skipped due to compiler issues" "${skipped_tests[@]}"
278+
report_issues "Tests with failing compilation step" "${failed_compilations[@]}"
279+
report_issues "Failed tests" "${failed_tests[@]}"
280+
num_failed_tests=$?
281+
if [ $num_failed_tests -eq 0 ]; then
282+
echo "All tests passed"
283+
fi
284+
exit $num_failed_tests
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xyzzy
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xyzzy

0 commit comments

Comments
 (0)