Skip to content

Commit

Permalink
GHI #13 Support install tests from multiple directories
Browse files Browse the repository at this point in the history
Signed-off-by: doodspav <[email protected]>
  • Loading branch information
doodspav committed Mar 19, 2023
1 parent ae67297 commit 124a5f8
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 58 deletions.
61 changes: 21 additions & 40 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,6 @@ find_package(GTest REQUIRED)
include(GoogleTest)


# ---- Declare Tests ----

add_executable(
patomic_test
example_add.cpp
)
add_executable(patomic::test ALIAS patomic_test)

target_link_libraries(
patomic_test
PRIVATE
patomic::patomic
GTest::gtest_main
)

target_compile_features(patomic_test PRIVATE cxx_std_14)


# ---- Discover Tests ----

gtest_add_tests(
TARGET patomic_test
TEST_LIST patomic_test_TESTS
)


# ---- Windows Path Issues ----

include(cmake/WindowsDependenciesPath.cmake)
Expand All @@ -63,27 +37,34 @@ windows_deps_path(
)

# check we're on Windows, and we have SHARED_LIBRARY dependencies
if(NOT deps_path STREQUAL "" AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")

# set environment variable for each test so that ctest works automatically
if(patomic_test_SET_CTEST_PATH_ENV_WINDOWS)
foreach(test IN LISTS patomic_test_TESTS)
set_property(
TEST "${test}"
PROPERTY ENVIRONMENT "PATH=${deps_path}"
)
endforeach()
endif()

# create file with PATH contents for when not running tests through CMake
set(windows_and_shared FALSE)
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows" AND NOT deps_path STREQUAL "")
set(windows_and_shared TRUE)

# create file with PATH contents for when running tests independent of CTest
file(GENERATE
OUTPUT "${PROJECT_BINARY_DIR}/windows_dependencies_path.txt"
CONTENT "${deps_path}"
)
endif()


# ---- Install Rules ----
# ---- Declare Tests ----

add_custom_target(patomic_test)
set_target_properties(
patomic_test
PROPERTIES
WIN_DEPS_PATH ${deps_path}
WIN_AND_SHARED ${windows_and_shared}
)

# add all test subdirectories
add_subdirectory(bt)
add_subdirectory(ut)


# ---- Further Installation and Packaging ----

if(NOT CMAKE_SKIP_INSTALL_RULES)
include(cmake/InstallRules.cmake)
Expand Down
4 changes: 4 additions & 0 deletions test/bt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include(../cmake/CreateTest.cmake)

create_test(BT example_add SOURCE example_add.cpp)
create_test(BT example_sub SOURCE example_sub.cpp)
8 changes: 8 additions & 0 deletions test/bt/example_add.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <gtest/gtest.h>
#include <patomic/patomic.h>


TEST(suite, name)
{
ASSERT_EQ(5, patomic_example_add(2, 3));
}
8 changes: 8 additions & 0 deletions test/bt/example_sub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <gtest/gtest.h>
#include <patomic/patomic.h>


TEST(suite2, name2)
{
ASSERT_EQ(5, patomic_example_add(2, 3));
}
115 changes: 115 additions & 0 deletions test/cmake/CreateTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# ---- Create Test ----

# Creates a test executable and registers it with CTest.
#
# create_test(
# BT|UT <name>
# [INCLUDE <item>...]
# [SOURCE <item>...]
# )
function(create_test)
cmake_parse_arguments(
"ARG"
""
"BT;UT"
"INCLUDE;SOURCE"
${ARGN}
)


# validate arguments

set(args_valid TRUE)
set(func_name "create_test")

if(NOT ARG_BT AND NOT ARG_UT)
message(WARNING "<BT|UT> option needs to be specified when invoking '${func_name}'")
set(args_valid FALSE)
elseif(ARG_BT AND ARG_UT)
message(WARNING "Only one of <BT|UT> options may be specified when invoking '${func_name}'")
set(args_valid FALSE)
endif()

if(DEFINED ARG_UNPARSED_ARGUMENTS)
message(WARNING "The following arguments were not recognised when invoking '${func_name}': ${ARG_UNPARSED_ARGUMENTS}")
set(args_valid FALSE)
endif()

if(NOT ${args_valid})
message(FATAL_ERROR "Aborting '${func_name}' due to invalid arguments")
endif()


# coalesce arguments

set(name "${ARG_BT}")
set(kind "bt")
if(ARG_UT)
set(name "${ARG_UT}")
set(kind "ut")
endif()


# setup target

set(target patomic_${kind}_${name})

add_executable(
${target}
${ARG_INCLUDE}
${ARG_SOURCE}
)

target_link_libraries(
${target}
PRIVATE
patomic::patomic
GTest::gtest_main
)

target_compile_features(${target} PRIVATE cxx_std_14)


# setup tests with CTest

# must be run in same directory scope as target
gtest_add_tests(
TARGET ${target}
TEST_LIST added_tests
)

# get these values from root target so we don't recompute every time
get_target_property(deps_path patomic_test WIN_DEPS_PATH)
get_target_property(win_and_shared patomic_test WIN_AND_SHARED)

# set environment variable for each test so that CTest works automatically
if(win_and_shared AND patomic_test_SET_CTEST_PATH_ENV_WINDOWS)
foreach(test IN LISTS added_tests)
set_property(
TEST "${test}"
PROPERTY ENVIRONMENT "PATH=${deps_path}"
)
endforeach()
endif()


# setup install of target

set(component patomic_${kind})

if(NOT CMAKE_SKIP_INSTALL_RULES)
install(
TARGETS ${target}
RUNTIME #
COMPONENT ${component}
DESTINATION "${CMAKE_INSTALL_TESTDIR}/patomic/${kind}"
EXCLUDE_FROM_ALL
)
endif()


# attach to root target

add_dependencies(patomic_test ${target})

endfunction()
20 changes: 2 additions & 18 deletions test/cmake/InstallRules.cmake
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
# we don't want the default install location to be in bin
# note: not currently an official CMake variable
set(
CMAKE_INSTALL_TESTDIR "share/test"
CACHE PATH "(unofficial) Default test install location"
)

# copy target build output artifacts to OS dependent locations
install(
TARGETS patomic_test
RUNTIME #
COMPONENT patomic_test_Runtime
DESTINATION "${CMAKE_INSTALL_TESTDIR}"
EXCLUDE_FROM_ALL
)

# copy file with windows dependencies path
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
install(
FILES "${PROJECT_BINARY_DIR}/windows_dependencies_path.txt"
COMPONENT patomic_test_Runtime
FILES "${PROJECT_BINARY_DIR}/patomic/windows_dependencies_path.txt"
COMPONENT patomic_test
DESTINATION "${CMAKE_INSTALL_TESTDIR}"
EXCLUDE_FROM_ALL
)
Expand Down
15 changes: 15 additions & 0 deletions test/cmake/OptionVariables.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# ---- Test Install Directory ----

# Normally tests would be install in CMAKE_INSTALL_BINDIR by default since
# they're executables.
# This is undesirable, so this variable exists to override the install location
# of test binaries separately.
# It's not prefixed with patomic_test because it's ok for it to be shared and
# overridden by parent projects.
# Note: this is not an official CMake variable
set(
CMAKE_INSTALL_TESTDIR "share/test"
CACHE PATH "(unofficial) Default test install location"
)


# ---- Windows Tests Path ----

# By default we set PATH for tests run with CTest on Windows in order to prevent
Expand Down
4 changes: 4 additions & 0 deletions test/ut/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include(../cmake/CreateTest.cmake)

create_test(UT example_add SOURCE example_add.cpp)
create_test(UT example_sub SOURCE example_sub.cpp)
8 changes: 8 additions & 0 deletions test/ut/example_add.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <gtest/gtest.h>
#include <patomic/patomic.h>


TEST(suite, name)
{
ASSERT_EQ(5, patomic_example_add(2, 3));
}
8 changes: 8 additions & 0 deletions test/ut/example_sub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <gtest/gtest.h>
#include <patomic/patomic.h>


TEST(suite2, name2)
{
ASSERT_EQ(5, patomic_example_add(2, 3));
}

0 comments on commit 124a5f8

Please sign in to comment.