From 49a2cfa5dcf0b2cebe73a81eb9787022e378f73a Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Wed, 23 Apr 2025 20:05:33 -0700 Subject: [PATCH] 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. --- CMakeLists.txt | 43 +++++++++++++++++++++++++++++++++++++++++++ README.md | 19 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..37205c6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,43 @@ +# This source file is part of the Swift open source project +# +# Copyright (c) 2025 Apple Inc. and the Swift project authors. +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# ============================================================================= +# This file configure testing that each project configures and builds with +# ctest. +# It is not meant for configuring and building each project. + +cmake_minimum_required(VERSION 3.22) +project(SwiftCMakeExamples LANGUAGES NONE) + +include(CTest) + +function(add_cmake_test name source_dir) + cmake_parse_arguments(PARSE_ARGV 2 ARG "" "CMAKE_VERSION" "" ) + if(NOT ARG_CMAKE_VERSION) + set(ARG_CMAKE_VERSION 3.22) + endif() + + if(${CMAKE_VERSION} VERSION_LESS ${ARG_CMAKE_VERSION}) + message(STATUS "Skipping ${name} -- CMake version too old: ${CMAKE_VERSION} < ${ARG_CMAKE_VERSION}") + return() + endif() + + add_test(NAME "${name}-configure" + COMMAND ${CMAKE_COMMAND} + -G ${CMAKE_GENERATOR} + -B "${name}-build" + -S "${CMAKE_CURRENT_SOURCE_DIR}/${source_dir}" + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}) + add_test(NAME "${name}-build" COMMAND + ${CMAKE_COMMAND} --build "${name}-build") + set_tests_properties("${name}-build" PROPERTIES DEPENDS "${name}-configure") +endfunction() + +add_cmake_test(SingleExecutable 1_single_executable) +add_cmake_test(ExecutableLibrary 2_executable_library) +add_cmake_test(BidirectionalCxxInterop 3_bidirectional_cxx_interop + CMAKE_VERSION 3.26) +add_cmake_test(SwiftMacros 4_swift_macros) diff --git a/README.md b/README.md index 1b6d1f1..6b7c934 100644 --- a/README.md +++ b/README.md @@ -57,3 +57,22 @@ using the Swift macro support introduced in Swift 5.9. Requires: - Swift 5.9 (macOS: Swift 5.9.0, Windows and Linux: Swift 5.9.1) + +# Testing + +Tests are run with `ctest`, configured with the CMakeLists at the top-level. + +```sh +cmake -G Ninja -B build -S . +cd build +ctest -j --output-on-failure +``` + +When you add a test, add it to the top-level CMakeLists file and re-run CMake +from the test directory to ensure that the CTest files are updated +appropriately before trying to run the tests. + +```sh +cmake . +ctest -j --output-on-failure +```