Skip to content

Commit d54db4d

Browse files
committed
feat(test): check expected build results
1 parent 857240f commit d54db4d

File tree

3 files changed

+88
-39
lines changed

3 files changed

+88
-39
lines changed

regression-tests/CMakeLists.txt

+52-29
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ find_package(cppfront REQUIRED)
1111
set(REGRESSION_TESTS_DIR "${CMAKE_CURRENT_LIST_DIR}/../cppfront/regression-tests")
1212
set(TEST_RESULTS_DIR "${REGRESSION_TESTS_DIR}/test-results")
1313

14-
if (CPPFRONT_DEVELOPING)
15-
# Set `compiler_id` and `compiler_major_version`.
16-
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
17-
set(compiler_id "gcc")
18-
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
19-
set(compiler_id "apple-clang")
20-
else ()
21-
string(TOLOWER "${CMAKE_CXX_COMPILER_ID}" compiler_id)
22-
endif ()
23-
string(REGEX MATCH "[0-9]+" compiler_major_version "${CMAKE_CXX_COMPILER_VERSION}")
14+
# Set `COMPILER_ITEM_NAME`.
15+
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
16+
set(compiler_id "gcc")
17+
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
18+
set(compiler_id "apple-clang")
19+
else ()
20+
string(TOLOWER "${CMAKE_CXX_COMPILER_ID}" compiler_id)
21+
endif ()
22+
string(REGEX MATCH "[0-9]+" compiler_major_version "${CMAKE_CXX_COMPILER_VERSION}")
23+
set(COMPILER_ITEM_NAME "${compiler_id}-${compiler_major_version}")
2424

25-
# Setup `BUILD_RESULTS_DIR`.
26-
set(compiler_item_name "${compiler_id}-${compiler_major_version}")
27-
set(BUILD_RESULTS_DIR "${TEST_RESULTS_DIR}/${compiler_item_name}")
25+
# Setup `BUILD_RESULTS_DIR`.
26+
set(BUILD_RESULTS_DIR "${TEST_RESULTS_DIR}/${COMPILER_ITEM_NAME}")
27+
28+
if (CPPFRONT_DEVELOPING)
2829
file(MAKE_DIRECTORY "${BUILD_RESULTS_DIR}")
2930

3031
# Write compiler version output.
@@ -36,23 +37,26 @@ if (CPPFRONT_DEVELOPING)
3637
COMMAND ${compiler_version_command}
3738
OUTPUT_FILE "${BUILD_RESULTS_DIR}/${compiler_id}-version.output"
3839
)
39-
40-
# Set `OLD_BUILD_RESULTS_DIRS`.
41-
file(
42-
GLOB build_result_dirs
43-
RELATIVE "${TEST_RESULTS_DIR}"
44-
"${TEST_RESULTS_DIR}/${compiler_id}-*"
45-
)
46-
list(SORT build_result_dirs)
47-
list(FIND build_result_dirs "${compiler_item_name}" i)
48-
list(SUBLIST build_result_dirs 0 ${i} OLD_BUILD_RESULTS_DIRS)
49-
list(REVERSE OLD_BUILD_RESULTS_DIRS)
5040
endif ()
5141

5242
configure_file("cmake/ExecuteWithRedirection.cmake" "ExecuteWithRedirection.cmake" COPYONLY)
5343
configure_file("cmake/ExecuteTestCase.cmake.in" "ExecuteTestCase.cmake" @ONLY)
44+
configure_file("cmake/FindBuildResultFile.cmake.in" "FindBuildResultFile.cmake" @ONLY)
5445
configure_file("cmake/UpdateBuildOutput.cmake.in" "UpdateBuildOutput.cmake" @ONLY)
5546

47+
include("${CMAKE_CURRENT_BINARY_DIR}/FindBuildResultFile.cmake")
48+
49+
function(cppfront_add_check_test)
50+
cmake_parse_arguments(PARSE_ARGV 0 ARG "" "NAME;NEW_FILE;OLD_FILE;FIXTURES_REQUIRED" "")
51+
52+
add_test(
53+
NAME "${ARG_NAME}"
54+
COMMAND "${CMAKE_COMMAND}" -E compare_files "${ARG_NEW_FILE}" "${ARG_OLD_FILE}"
55+
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
56+
)
57+
set_tests_properties("${ARG_NAME}" PROPERTIES FIXTURES_REQUIRED "${ARG_FIXTURES_REQUIRED}")
58+
endfunction()
59+
5660
function(cppfront_command_tests)
5761
cmake_parse_arguments(PARSE_ARGV 0 ARG "" "SOURCE;EXPECTED_FILE" "EXTRA_FLAGS")
5862

@@ -113,14 +117,13 @@ function(cppfront_command_tests)
113117

114118
if (ARG_EXPECTED_FILE)
115119
configure_file("${ARG_EXPECTED_FILE}" "${gen_cpp_src}.original" COPYONLY)
116-
add_test(
120+
cppfront_add_check_test(
117121
NAME "codegen/check/${test_name}"
118-
COMMAND "${CMAKE_COMMAND}" -E compare_files "${gen_cpp_src}" "${gen_cpp_src}.original"
119-
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
122+
NEW_FILE "${gen_cpp_src}"
123+
OLD_FILE "${gen_cpp_src}.original"
124+
FIXTURES_REQUIRED "codegen/${test_name}"
120125
)
121126

122-
set_tests_properties("codegen/check/${test_name}" PROPERTIES FIXTURES_REQUIRED "codegen/${test_name}")
123-
124127
cppfront_build_tests(
125128
SOURCE ${ARG_SOURCE}
126129
EXTRA_FLAGS ${ARG_EXTRA_FLAGS}
@@ -161,6 +164,16 @@ function(cppfront_build_tests)
161164
FIXTURES_SETUP "build/${test_name}"
162165
)
163166

167+
cppfront_find_build_result_file(expected_output_file RESULT_FILE "${gen_cpp_src}.output")
168+
if (expected_output_file)
169+
cppfront_add_check_test(
170+
NAME "build/check/${test_name}"
171+
NEW_FILE "${test_dir}/build/${gen_cpp_src}.output"
172+
OLD_FILE "${expected_output_file}"
173+
FIXTURES_REQUIRED "build/${test_name}"
174+
)
175+
endif()
176+
164177
add_test(
165178
NAME "build/execute/${test_name}"
166179
COMMAND
@@ -176,6 +189,16 @@ function(cppfront_build_tests)
176189
FIXTURES_SETUP "build/execute/${test_name}"
177190
RESOURCE_LOCK "test.exe")
178191

192+
cppfront_find_build_result_file(expected_execution_file RESULT_FILE "${gen_cpp_src}.execution")
193+
if (expected_execution_file)
194+
cppfront_add_check_test(
195+
NAME "build/execute/check/${test_name}"
196+
NEW_FILE "${test_dir}/${gen_cpp_src}.execution"
197+
OLD_FILE "${expected_execution_file}"
198+
FIXTURES_REQUIRED "build/execute/${test_name}"
199+
)
200+
endif()
201+
179202
if (CPPFRONT_DEVELOPING)
180203
add_test(
181204
NAME "build/update/${test_name}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Set `OLD_BUILD_RESULTS_DIRS`.
2+
file(
3+
GLOB build_result_dirs
4+
RELATIVE "@TEST_RESULTS_DIR@"
5+
"@TEST_RESULTS_DIR@/@compiler_id@-*"
6+
)
7+
list(SORT build_result_dirs)
8+
list(FIND build_result_dirs "@COMPILER_ITEM_NAME@" i)
9+
list(SUBLIST build_result_dirs 0 ${i} OLD_BUILD_RESULTS_DIRS)
10+
list(REVERSE OLD_BUILD_RESULTS_DIRS)
11+
12+
function(cppfront_find_build_result_file out_var)
13+
cmake_parse_arguments(PARSE_ARGV 0 ARG "OLD_ONLY" "RESULT_FILE" "")
14+
15+
if (NOT ARG_OLD_ONLY)
16+
set(extra_item "@COMPILER_ITEM_NAME@")
17+
endif ()
18+
19+
foreach (build_results_dir IN ITEMS "${extra_item}" LISTS OLD_BUILD_RESULTS_DIRS)
20+
set(result_file "@TEST_RESULTS_DIR@/${build_results_dir}/${ARG_RESULT_FILE}")
21+
if (EXISTS "${result_file}")
22+
set("${out_var}" "${result_file}" PARENT_SCOPE)
23+
return()
24+
endif ()
25+
endforeach ()
26+
27+
unset("${out_var}" PARENT_SCOPE)
28+
endfunction()

regression-tests/cmake/UpdateBuildOutput.cmake.in

+8-10
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
# - For "absence of output" to mean that "it's the same as the previous version".
66
# - To commit only new and changed outputs in PRs for easier reviewing.
77

8+
include("@CMAKE_CURRENT_BINARY_DIR@/FindBuildResultFile.cmake")
9+
810
file(READ "${OUTPUT_FILE}" new_output)
911
if (EXISTS "${EXECUTION_FILE}")
1012
file(READ "${EXECUTION_FILE}" new_execution)
1113
endif ()
1214
function(write path_var ext)
13-
foreach (build_results_dir IN ITEMS @OLD_BUILD_RESULTS_DIRS@)
14-
set(result_file "@TEST_RESULTS_DIR@/${build_results_dir}/${GEN_CPP_SRC}.${ext}")
15-
if (EXISTS "${result_file}")
16-
file(READ "${result_file}" "old_${ext}")
17-
if (new_${ext} STREQUAL old_${ext})
18-
return()
19-
else ()
20-
break ()
21-
endif ()
15+
cppfront_find_build_result_file(result_file OLD_ONLY RESULT_FILE "${GEN_CPP_SRC}.${ext}")
16+
if (DEFINED result_file)
17+
file(READ "${result_file}" "old_${ext}")
18+
if (new_${ext} STREQUAL old_${ext})
19+
return()
2220
endif ()
23-
endforeach ()
21+
endif ()
2422
file(COPY "${${path_var}}" DESTINATION "@BUILD_RESULTS_DIR@")
2523
endfunction()
2624
write(OUTPUT_FILE "output")

0 commit comments

Comments
 (0)