Skip to content

Commit

Permalink
Reorganize the CMake configuration for the tests (#428)
Browse files Browse the repository at this point in the history
* Fix repeated download of test inputs if they are alrady present

* Split tests into subdirectories to declutter things a bit

* Move test utility functionality into dedicated file

- Introduce PODIO_SET_TEST_ENV function to set consistent test environment

* Move dumpmodel roundtrip tests into separate subfolder

* Move commonly used declaration to separate header (Fixes clang-tidy not being able to find an included header otherwise)
  • Loading branch information
tmadlener authored Jun 15, 2023
1 parent adbd1ea commit 0f5acd6
Show file tree
Hide file tree
Showing 40 changed files with 372 additions and 358 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/NOTICE
DESTINATION ${CMAKE_INSTALL_DOCDIR})

#--- project specific subdirectories -------------------------------------------
add_subdirectory(python)
add_subdirectory(src)
if(BUILD_TESTING)
include(cmake/podioTest.cmake)
add_subdirectory(tests)
endif()
add_subdirectory(tools)
add_subdirectory(python)

#--- add CMake infrastructure --------------------------------------------------
include(cmake/podioCreateConfig.cmake)
52 changes: 52 additions & 0 deletions cmake/podioTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#--- small utility helper function to set a consistent test environment for the passed test

function(PODIO_SET_TEST_ENV test)
# We need to convert this into a list of arguments that can be used as environment variable
list(JOIN PODIO_IO_HANDLERS " " IO_HANDLERS)
set_property(TEST ${test}
PROPERTY ENVIRONMENT
LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/tests:${CMAKE_BINARY_DIR}/src:$<TARGET_FILE_DIR:ROOT::Tree>:$<$<TARGET_EXISTS:SIO::sio>:$<TARGET_FILE_DIR:SIO::sio>>:$ENV{LD_LIBRARY_PATH}
PYTHONPATH=${CMAKE_SOURCE_DIR}/python:$ENV{PYTHONPATH}
PODIO_SIOBLOCK_PATH=${CMAKE_BINARY_DIR}/tests
ROOT_INCLUDE_PATH=${CMAKE_BINARY_DIR}/tests/datamodel:${CMAKE_SOURCE_DIR}/include
SKIP_SIO_TESTS=$<NOT:$<BOOL:${ENABLE_SIO}>>
IO_HANDLERS=${IO_HANDLERS}
PODIO_USE_CLANG_FORMAT=${PODIO_USE_CLANG_FORMAT}
PODIO_BASE=${CMAKE_SOURCE_DIR}
ENABLE_SIO=${ENABLE_SIO}
)
endfunction()

#--- small utility helper function to allow for a more terse definition of tests below
function(CREATE_PODIO_TEST sourcefile additional_libs)
string( REPLACE ".cpp" "" name ${sourcefile} )
add_executable( ${name} ${sourcefile} )
add_test(NAME ${name} COMMAND ${name})

target_link_libraries(${name} PRIVATE TestDataModel ExtensionDataModel ${additional_libs})
PODIO_SET_TEST_ENV(${name})
endfunction()

#--- utility macro to facilitate the downloading of legacy input data
macro(PODIO_DOWNLOAD_LEGACY_INPUTS)
# Avoid fetching these everytime cmake is run by caching the directory the first
# time the inputs are fetched or if the expected file does not exist in the
# expected directory
if (NOT DEFINED CACHE{PODIO_TEST_INPUT_DATA_DIR} OR NOT EXISTS ${PODIO_TEST_INPUT_DATA_DIR}/v00-16-05/example_frame.root)
message(STATUS "Getting test input files")
execute_process(
COMMAND bash ${CMAKE_SOURCE_DIR}/tests/scripts/get_test_inputs.sh
OUTPUT_VARIABLE podio_test_input_data_dir
RESULT_VARIABLE test_inputs_available
)
if (NOT "${test_inputs_available}" STREQUAL "0")
message(WARNING "Could not get test input files. Will skip some tests that depend on these")
# Catch cases where the variable is cached but the file no longer exists
unset(PODIO_TEST_INPUT_DATA_DIR CACHE)
else()
message(STATUS "Test inputs stored in: " ${podio_test_input_data_dir})
set(PODIO_TEST_INPUT_DATA_DIR ${podio_test_input_data_dir} CACHE INTERNAL "input dir for test inputs fetched from remote sources")
mark_as_advanced(PODIO_TEST_INPUT_DATA_DIR)
endif()
endif()
endmacro()
12 changes: 11 additions & 1 deletion python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
SET(podio_PYTHON_INSTALLDIR python)
SET(podio_PYTHON_INSTALLDIR ${podio_PYTHON_INSTALLDIR} PARENT_SCOPE)
SET(podio_PYTHON_DIR ${CMAKE_CURRENT_LIST_DIR} PARENT_SCOPE)

set(to_install
podio_class_generator.py
Expand All @@ -27,3 +26,14 @@ endif()
#--- install templates ---------------------------------------------------------
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/templates
DESTINATION ${podio_PYTHON_INSTALLDIR})

IF (BUILD_TESTING)
add_test( NAME pyunittest COMMAND python3 -m unittest discover -s ${CMAKE_SOURCE_DIR}/python/podio)
PODIO_SET_TEST_ENV(pyunittest)

set_property(TEST pyunittest PROPERTY DEPENDS write write_frame_root)
if (TARGET write_sio)
set_property(TEST pyunittest PROPERTY DEPENDS write_sio write_frame_sio)
endif()
set_property(TEST pyunittest PROPERTY WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/tests)
ENDIF()
4 changes: 2 additions & 2 deletions python/podio/test_EventStoreRoot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class EventStoreRootTestCase(EventStoreBaseTestCaseMixin, unittest.TestCase):
"""Test cases for root input files"""
def setUp(self):
"""Setup an EventStore reading from a ROOT file"""
self.filename = 'example.root'
self.filename = 'root_io/example.root'
self.assertTrue(os.path.isfile(self.filename))
self.store = EventStore(['example.root'])
self.store = EventStore([self.filename])

def test_chain(self):
self.store = EventStore([self.filename,
Expand Down
2 changes: 1 addition & 1 deletion python/podio/test_EventStoreSio.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class EventStoreSioTestCase(EventStoreBaseTestCaseMixin, unittest.TestCase):
"""Test cases for root input files"""
def setUp(self):
"""setup an EventStore reading an SIO file"""
self.filename = 'example.sio'
self.filename = 'sio_io/example.sio'
self.assertTrue(os.path.isfile(self.filename))
self.store = EventStore([self.filename])

Expand Down
2 changes: 1 addition & 1 deletion python/podio/test_Frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def setUp(self):
Reading only one event/Frame of each category here as looping and other
basic checks are already handled by the Reader tests
"""
reader = Reader('example_frame.root')
reader = Reader('root_io/example_frame.root')
self.event = reader.get('events')[0]
self.other_event = reader.get('other_events')[7]

Expand Down
4 changes: 2 additions & 2 deletions python/podio/test_ReaderRoot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class RootReaderTestCase(ReaderTestCaseMixin, unittest.TestCase):
"""Test cases for root input files"""
def setUp(self):
"""Setup the corresponding reader"""
self.reader = Reader('example_frame.root')
self.reader = Reader('root_io/example_frame.root')


class RootLegacyReaderTestCase(LegacyReaderTestCaseMixin, unittest.TestCase):
"""Test cases for the legacy root input files and reader."""
def setUp(self):
"""Setup a reader, reading from the example files"""
self.reader = LegacyReader('example.root')
self.reader = LegacyReader('root_io/example.root')
4 changes: 2 additions & 2 deletions python/podio/test_ReaderSio.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SioReaderTestCase(ReaderTestCaseMixin, unittest.TestCase):
def setUp(self):
"""Setup the corresponding reader"""
from podio.sio_io import Reader # pylint: disable=import-outside-toplevel
self.reader = Reader('example_frame.sio')
self.reader = Reader('sio_io/example_frame.sio')


@unittest.skipIf(SKIP_SIO_TESTS, "no SIO support")
Expand All @@ -22,4 +22,4 @@ class SIOLegacyReaderTestCase(LegacyReaderTestCaseMixin, unittest.TestCase):
def setUp(self):
"""Setup a reader, reading from the example files"""
from podio.sio_io import LegacyReader # pylint: disable=import-outside-toplevel
self.reader = LegacyReader('example.sio')
self.reader = LegacyReader('sio_io/example.sio')
Loading

0 comments on commit 0f5acd6

Please sign in to comment.