Skip to content

Commit 4c75ece

Browse files
committed
tests: support external Catch2
1 parent 3ce61a1 commit 4c75ece

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

tests/CMakeLists.txt

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,33 @@ endif()
6363

6464
set(CLI11_MULTIONLY_TESTS TimerTest)
6565

66-
add_library(catch_main main.cpp)
66+
add_library(catch_main main.cpp catch.hpp)
6767
target_include_directories(catch_main PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
6868

69-
# Currently a required download; could be make to look for existing Catch2, but
70-
# that would require changing the includes. FetchContent would be better, but
71-
# requires newer CMake.
72-
73-
set(url https://github.com/philsquared/Catch/releases/download/v2.13.6/catch.hpp)
74-
file(
75-
DOWNLOAD ${url} "${CMAKE_CURRENT_BINARY_DIR}/catch.hpp"
76-
STATUS status
77-
EXPECTED_HASH SHA256=681e7505a50887c9085539e5135794fc8f66d8e5de28eadf13a30978627b0f47)
78-
list(GET status 0 error)
79-
if(error)
80-
message(FATAL_ERROR "Could not download ${url}")
69+
find_package(Catch2 CONFIG)
70+
71+
if(Catch2_FOUND)
72+
if(NOT TARGET Catch2::Catch2)
73+
message(FATAL_ERROR "Found Catch2 at ${Catch2_DIR} but targets are missing.")
74+
endif()
75+
message(STATUS "Found Catch2")
76+
target_link_libraries(catch_main PUBLIC Catch2::Catch2)
77+
else()
78+
message(STATUS "Downloading Catch2")
79+
80+
# FetchContent would be better, but requires newer CMake.
81+
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/catch2")
82+
set(url https://github.com/philsquared/Catch/releases/download/v2.13.7/catch.hpp)
83+
file(
84+
DOWNLOAD ${url} "${CMAKE_CURRENT_BINARY_DIR}/catch2/catch.hpp"
85+
STATUS status
86+
EXPECTED_HASH SHA256=ea379c4a3cb5799027b1eb451163dff065a3d641aaba23bf4e24ee6b536bd9bc)
87+
list(GET status 0 error)
88+
if(error)
89+
message(FATAL_ERROR "Could not download ${url}, and Catch2 not found on your system.")
90+
endif()
91+
target_include_directories(catch_main PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")
8192
endif()
82-
target_include_directories(catch_main PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")
8393

8494
# Target must already exist
8595
macro(add_catch_test TESTNAME)
@@ -174,8 +184,6 @@ file(WRITE "${PROJECT_BINARY_DIR}/CTestCustom.cmake"
174184
target_compile_definitions(informational PRIVATE ${boost-optional-def})
175185
target_compile_definitions(OptionalTest PRIVATE ${boost-optional-def})
176186

177-
message(STATUS "Boost libs=${Boost_INCLUDE_DIRS}")
178-
179187
if(TARGET Boost::boost)
180188
message(STATUS "including boost target")
181189
target_link_libraries(informational PRIVATE Boost::boost)
@@ -185,6 +193,7 @@ if(TARGET Boost::boost)
185193
target_link_libraries(OptionalTest_Single PRIVATE Boost::boost)
186194
target_link_libraries(BoostOptionTypeTest_Single PRIVATE Boost::boost)
187195
endif()
196+
message(STATUS "Boost libs=${Boost_INCLUDE_DIRS}")
188197
elseif(BOOST_FOUND)
189198
message(STATUS "no boost target")
190199
target_include_directories(informational PRIVATE ${Boost_INCLUDE_DIRS})
@@ -194,6 +203,9 @@ elseif(BOOST_FOUND)
194203
target_include_directories(OptionalTest_Single PRIVATE ${Boost_INCLUDE_DIRS})
195204
target_include_directories(BoostOptionTypeTest_Single PRIVATE ${Boost_INCLUDE_DIRS})
196205
endif()
206+
message(STATUS "Boost libs=${Boost_INCLUDE_DIRS}")
207+
else()
208+
message(STATUS "Boost not found, not adding boost tests")
197209
endif()
198210

199211
if(CMAKE_BUILD_TYPE STREQUAL Coverage)

tests/catch.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2017-2021, University of Cincinnati, developed by Henry Schreiner
2+
// under NSF AWARD 1414736 and by the respective contributors.
3+
// All rights reserved.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
#pragma once
8+
9+
#include <catch2/catch.hpp>

0 commit comments

Comments
 (0)