Skip to content

Commit 49a2cfa

Browse files
committed
Testing: Add test suite to project
Adding a simple test suite for configuring and building each project. Also adding info to the readme on how to run the tests.
1 parent e673cd1 commit 49a2cfa

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This source file is part of the Swift open source project
2+
#
3+
# Copyright (c) 2025 Apple Inc. and the Swift project authors.
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See https://swift.org/LICENSE.txt for license information
7+
# =============================================================================
8+
# This file configure testing that each project configures and builds with
9+
# ctest.
10+
# It is not meant for configuring and building each project.
11+
12+
cmake_minimum_required(VERSION 3.22)
13+
project(SwiftCMakeExamples LANGUAGES NONE)
14+
15+
include(CTest)
16+
17+
function(add_cmake_test name source_dir)
18+
cmake_parse_arguments(PARSE_ARGV 2 ARG "" "CMAKE_VERSION" "" )
19+
if(NOT ARG_CMAKE_VERSION)
20+
set(ARG_CMAKE_VERSION 3.22)
21+
endif()
22+
23+
if(${CMAKE_VERSION} VERSION_LESS ${ARG_CMAKE_VERSION})
24+
message(STATUS "Skipping ${name} -- CMake version too old: ${CMAKE_VERSION} < ${ARG_CMAKE_VERSION}")
25+
return()
26+
endif()
27+
28+
add_test(NAME "${name}-configure"
29+
COMMAND ${CMAKE_COMMAND}
30+
-G ${CMAKE_GENERATOR}
31+
-B "${name}-build"
32+
-S "${CMAKE_CURRENT_SOURCE_DIR}/${source_dir}"
33+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
34+
add_test(NAME "${name}-build" COMMAND
35+
${CMAKE_COMMAND} --build "${name}-build")
36+
set_tests_properties("${name}-build" PROPERTIES DEPENDS "${name}-configure")
37+
endfunction()
38+
39+
add_cmake_test(SingleExecutable 1_single_executable)
40+
add_cmake_test(ExecutableLibrary 2_executable_library)
41+
add_cmake_test(BidirectionalCxxInterop 3_bidirectional_cxx_interop
42+
CMAKE_VERSION 3.26)
43+
add_cmake_test(SwiftMacros 4_swift_macros)

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,22 @@ using the Swift macro support introduced in Swift 5.9.
5757
Requires:
5858

5959
- Swift 5.9 (macOS: Swift 5.9.0, Windows and Linux: Swift 5.9.1)
60+
61+
# Testing
62+
63+
Tests are run with `ctest`, configured with the CMakeLists at the top-level.
64+
65+
```sh
66+
cmake -G Ninja -B build -S .
67+
cd build
68+
ctest -j --output-on-failure
69+
```
70+
71+
When you add a test, add it to the top-level CMakeLists file and re-run CMake
72+
from the test directory to ensure that the CTest files are updated
73+
appropriately before trying to run the tests.
74+
75+
```sh
76+
cmake .
77+
ctest -j --output-on-failure
78+
```

0 commit comments

Comments
 (0)