Skip to content

Commit d4edd74

Browse files
authored
Add Testing with ctest and remove some examples files
* Add simple testing for examples * Create ctest.yml * Move ctest.yml to .github/workflows * Update ctest.yml * Update ctest.yml and remove stack overflow example
1 parent ae29ec4 commit d4edd74

6 files changed

Lines changed: 91 additions & 38 deletions

File tree

.github/workflows/ctest.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Cross-Platform Build & Test
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
8+
permissions:
9+
contents: read
10+
checks: write
11+
pull-requests: write
12+
13+
jobs:
14+
build-and-test:
15+
name: ${{ matrix.name }}
16+
runs-on: ${{ matrix.os }}
17+
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
include:
22+
- name: Linux GCC
23+
os: ubuntu-latest
24+
cc: gcc
25+
26+
- name: Linux Clang
27+
os: ubuntu-latest
28+
cc: clang
29+
30+
- name: Windows MSVC
31+
os: windows-latest
32+
33+
steps:
34+
- uses: actions/checkout@v6
35+
36+
- name: Configure CMake (Unix)
37+
if: runner.os != 'Windows'
38+
run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=ON
39+
env:
40+
CC: ${{ matrix.cc }}
41+
42+
- name: Configure CMake (Windows)
43+
if: runner.os == 'Windows'
44+
run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=ON -G "Visual Studio 17 2022"
45+
46+
- name: Build
47+
run: cmake --build build --config Release
48+
49+
- name: Run Tests
50+
working-directory: build
51+
run: ctest -C Release --output-on-failure --output-junit test-results.xml
52+
53+
- name: Publish Test Results
54+
uses: dorny/test-reporter@v2
55+
if: success() || failure()
56+
with:
57+
name: Tests (${{ matrix.name }})
58+
path: build/test-results.xml
59+
reporter: java-junit

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ if(PROJECT_IS_TOP_LEVEL)
112112

113113
# --- Build Examples ---
114114
if(BUILD_EXAMPLES)
115+
enable_testing()
116+
115117
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/examples/CMakeLists.txt")
116118
add_subdirectory(examples)
117119
endif()

examples/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@ file(GLOB EXAMPLE_SOURCES CONFIGURE_DEPENDS "example*.c")
33
foreach(SOURCE_FILE ${EXAMPLE_SOURCES})
44

55
get_filename_component(TARGET_NAME ${SOURCE_FILE} NAME_WE)
6+
67
add_executable(${TARGET_NAME} ${SOURCE_FILE})
78
target_link_libraries(${TARGET_NAME} PRIVATE c_traceback::c_traceback)
9+
810
if(ENABLE_SANITIZERS AND NOT MSVC)
911
target_compile_options(${TARGET_NAME} PRIVATE -fsanitize=address,undefined)
1012
target_link_options(${TARGET_NAME} PRIVATE -fsanitize=address,undefined)
1113
endif()
1214

15+
# Test
16+
add_test(
17+
NAME ${TARGET_NAME}
18+
COMMAND ${CMAKE_COMMAND}
19+
-DTEST_PROG=$<TARGET_FILE:${TARGET_NAME}>
20+
-P ${CMAKE_CURRENT_SOURCE_DIR}/check_test.cmake
21+
)
22+
1323
endforeach()

examples/check_test.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
execute_process(
2+
COMMAND ${TEST_PROG}
3+
OUTPUT_VARIABLE TEST_OUTPUT
4+
ERROR_VARIABLE TEST_OUTPUT
5+
RESULT_VARIABLE TEST_RESULT
6+
)
7+
8+
message("${TEST_OUTPUT}")
9+
10+
# Determine Success
11+
# The output contains "Traceback" (Even if it crashed)
12+
if(TEST_OUTPUT MATCHES "Traceback")
13+
message(STATUS "Traceback detected. Test Passed.")
14+
# The program ran successfully (Exit code 0)
15+
elseif(TEST_RESULT EQUAL 0)
16+
message(STATUS "Clean exit. Test Passed.")
17+
else()
18+
# Fail: It crashed AND didn't print a Traceback
19+
message(FATAL_ERROR "Test Failed: Process crashed (Code: ${TEST_RESULT}) and no 'Traceback' found.")
20+
endif()

examples/example_keyboard_interrupt.c

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

examples/example_stack_overflow.c

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

0 commit comments

Comments
 (0)