Commit de28d11f7c9959aa59617b9ce2315b35434658ae

Authored by Henry Schreiner
Committed by GitHub
1 parent a55a7fc0

tests: support external Catch2 (#653)

tests/CMakeLists.txt
@@ -63,23 +63,33 @@ endif() @@ -63,23 +63,33 @@ endif()
63 63
64 set(CLI11_MULTIONLY_TESTS TimerTest) 64 set(CLI11_MULTIONLY_TESTS TimerTest)
65 65
66 -add_library(catch_main main.cpp) 66 +add_library(catch_main main.cpp catch.hpp)
67 target_include_directories(catch_main PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") 67 target_include_directories(catch_main PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
68 68
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}")
81 endif() 92 endif()
82 -target_include_directories(catch_main PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")  
83 93
84 # Target must already exist 94 # Target must already exist
85 macro(add_catch_test TESTNAME) 95 macro(add_catch_test TESTNAME)
@@ -174,8 +184,6 @@ file(WRITE "${PROJECT_BINARY_DIR}/CTestCustom.cmake" @@ -174,8 +184,6 @@ file(WRITE "${PROJECT_BINARY_DIR}/CTestCustom.cmake"
174 target_compile_definitions(informational PRIVATE ${boost-optional-def}) 184 target_compile_definitions(informational PRIVATE ${boost-optional-def})
175 target_compile_definitions(OptionalTest PRIVATE ${boost-optional-def}) 185 target_compile_definitions(OptionalTest PRIVATE ${boost-optional-def})
176 186
177 -message(STATUS "Boost libs=${Boost_INCLUDE_DIRS}")  
178 -  
179 if(TARGET Boost::boost) 187 if(TARGET Boost::boost)
180 message(STATUS "including boost target") 188 message(STATUS "including boost target")
181 target_link_libraries(informational PRIVATE Boost::boost) 189 target_link_libraries(informational PRIVATE Boost::boost)
@@ -185,6 +193,7 @@ if(TARGET Boost::boost) @@ -185,6 +193,7 @@ if(TARGET Boost::boost)
185 target_link_libraries(OptionalTest_Single PRIVATE Boost::boost) 193 target_link_libraries(OptionalTest_Single PRIVATE Boost::boost)
186 target_link_libraries(BoostOptionTypeTest_Single PRIVATE Boost::boost) 194 target_link_libraries(BoostOptionTypeTest_Single PRIVATE Boost::boost)
187 endif() 195 endif()
  196 + message(STATUS "Boost libs=${Boost_INCLUDE_DIRS}")
188 elseif(BOOST_FOUND) 197 elseif(BOOST_FOUND)
189 message(STATUS "no boost target") 198 message(STATUS "no boost target")
190 target_include_directories(informational PRIVATE ${Boost_INCLUDE_DIRS}) 199 target_include_directories(informational PRIVATE ${Boost_INCLUDE_DIRS})
@@ -194,6 +203,9 @@ elseif(BOOST_FOUND) @@ -194,6 +203,9 @@ elseif(BOOST_FOUND)
194 target_include_directories(OptionalTest_Single PRIVATE ${Boost_INCLUDE_DIRS}) 203 target_include_directories(OptionalTest_Single PRIVATE ${Boost_INCLUDE_DIRS})
195 target_include_directories(BoostOptionTypeTest_Single PRIVATE ${Boost_INCLUDE_DIRS}) 204 target_include_directories(BoostOptionTypeTest_Single PRIVATE ${Boost_INCLUDE_DIRS})
196 endif() 205 endif()
  206 + message(STATUS "Boost libs=${Boost_INCLUDE_DIRS}")
  207 +else()
  208 + message(STATUS "Boost not found, not adding boost tests")
197 endif() 209 endif()
198 210
199 if(CMAKE_BUILD_TYPE STREQUAL Coverage) 211 if(CMAKE_BUILD_TYPE STREQUAL Coverage)
tests/catch.hpp 0 → 100644
  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>