Skip to content

Commit

Permalink
build: Add tests via ctest in cmake.
Browse files Browse the repository at this point in the history
  • Loading branch information
moschmdt committed Dec 12, 2024
1 parent 43a2da4 commit 72d3ab1
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 31 deletions.
90 changes: 76 additions & 14 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@ name: CMake on multiple platforms

on:
push:
branches: ["main"]
branches: ["development"]
pull_request:
branches: ["main"]
branches: ["development"]
workflow_dispatch:

jobs:
build:
build-nix:
runs-on: ${{ matrix.os }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

# Set up a matrix to run the following 3 configurations:
# 1. <Windows, Release, latest MSVC compiler toolchain on the default runner image, default generator>
# 2. <Linux, Release, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <Linux, Release, latest Clang compiler toolchain on the default runner image, default generator>
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
build_type: [Release]
os: [ubuntu-latest, macos-latest]
build_type: [Release, Debug]

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -55,7 +49,7 @@ jobs:
# Install dependencies with Conan
- name: Install dependencies
run: |
conan install . --build=missing
conan install . --build=missing --settings=build_type=${{ matrix.build_type }}
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
Expand All @@ -70,8 +64,76 @@ jobs:
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

# Install is required to add the SoarTestAgents directory to the build directory
- name: Install
run: sudo cmake --install ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory:
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: sudo ctest --test-dir ${{ steps.strings.outputs.build-output-dir }}/UnitTests/ --output-on-failure --build-config ${{ matrix.build_type }}

build-windows:
runs-on: ${{ matrix.os }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

matrix:
os: [windows-latest]
build_type: [Release, Debug]

steps:
- uses: actions/checkout@v4

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}\build" >> "$GITHUB_OUTPUT"
# Set up Python (required for Conan)
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.13

# Install Conan
- name: Install Conan
run: pip install conan

# Create Conan profile
- name: Configure Conan
run: |
conan profile detect
# Install dependencies with Conan
- name: Install dependencies
run: |
conan install . --build=missing --settings=build_type=${{ matrix.build_type }}
- name: Configure CMake Windows
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-DCMAKE_TOOLCHAIN_FILE=${{ steps.strings.outputs.build-output-dir }}\generators\conan_toolchain.cmake
-S ${{ github.workspace }}
- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

# Install is required to add the SoarTestAgents directory to the build directory
- name: Install
run: cmake --install ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
working-directory:
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
run: ctest --test-dir ${{ steps.strings.outputs.build-output-dir }}\UnitTests\ --output-on-failure --build-config ${{ matrix.build_type }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,12 @@ build/
/.pydevproject
user-env*.*
build_*.*
!build_time_date.h
testCommandToFile-output.soar
soarversion
build_time_date
backup.sqlite
temp_chunks.soar

CMakeUserPresets.json
Testing/*
34 changes: 29 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ file(GLOB_RECURSE HEADERS
"Core/shared/*.h"
)

add_compile_definitions(NO_SVS)
# ASAN only works in debug mode.
option(ASAN "Enable AddressSanitizer" OFF)
option(SVS "SVS Enabled" OFF)

if(NOT SVS)
message(STATUS "Soar: Disabling SVS")
add_compile_definitions(NO_SVS)
endif()

# Add executable/library target
add_library(${PROJECT_NAME}_static STATIC ${SOURCES})
Expand Down Expand Up @@ -79,11 +86,26 @@ target_compile_features(${PROJECT_NAME}_static PUBLIC
cxx_std_17
)

set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,readability-*)

if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(${PROJECT_NAME}_static PRIVATE -Wall -Wextra -Wpedantic -fsanitize=address -fno-omit-frame-pointer)
target_link_options(${PROJECT_NAME}_static PRIVATE -fsanitize=address)
target_compile_options(${PROJECT_NAME}_static PRIVATE -Wall -Wpedantic)

if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
if(ASAN)
message(STATUS "Soar: Enabling AddressSanitizer")
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
add_link_options(-fsanitize=address)
endif()

find_program(CLANG_TIDY_EXE NAMES clang-tidy)

if(CLANG_TIDY_EXE)
set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_EXE} -checks=-*,readability-*)
else()
message(WARNING "clang-tidy not found!")
endif()
else()
message(WARNING "Debug flags for windows not set up.")
endif()
endif()

# This only sets up the install structure, but no installation happens yet.
Expand Down Expand Up @@ -123,4 +145,6 @@ export(EXPORT SoarTargets

add_library(soar::soar_static ALIAS soar_static)

include(CTest)
add_subdirectory(SoarCLI)
add_subdirectory(UnitTests)
2 changes: 2 additions & 0 deletions Core/shared/build_time_date.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const char* kTimestamp = __TIME__;
const char* kDatestamp = __DATE__;
60 changes: 60 additions & 0 deletions UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
cmake_minimum_required(VERSION 3.22)
project(test_soar
LANGUAGES CXX)

option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)

file(GLOB TEST_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/TestHarness/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/SoarHelpers/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/SoarUnitTests/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/SoarUnitTests/wma/*.cpp
)

file(GLOB TEST_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/TestHarness/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/SoarHelpers/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/SoarUnitTests/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/SoarUnitTests/wma/*.hpp
)

add_executable(${PROJECT_NAME} TestHarness/testMain.cpp ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME} soar_static)
target_compile_features(${PROJECT_NAME} PUBLIC
cxx_std_17
)

add_executable(test_external_lib TestExternalLibraryLib.cpp)
target_link_libraries(test_external_lib soar_static)
target_compile_features(test_external_lib PUBLIC
cxx_std_17
)

target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/SoarHelpers
${CMAKE_CURRENT_SOURCE_DIR}/TestHarness
${CMAKE_CURRENT_SOURCE_DIR}/SoarUnitTests
${CMAKE_CURRENT_SOURCE_DIR}/SoarUnitTests/wma
)

install(TARGETS ${PROJECT_NAME} test_external_lib
DESTINATION bin)

install(DIRECTORY SoarTestAgents DESTINATION ${CMAKE_INSTALL_PREFIX}/SoarTestAgents)
install(DIRECTORY SoarTestAgents DESTINATION ${CMAKE_BINARY_DIR}/UnitTests)

enable_testing()

# TESTS FROM CI:
if(BUILD_TESTING)
add_test(NAME test_ci COMMAND test_soar -e PRIMS_Sanity1 -e PRIMS_Sanity2 -f testLoadLibrary -f testSmemArithmetic -f testHamilton -E SvsTests -e testCommandToFile)

if(SVS)
add_test(NAME test_svs COMMAND test_soar -c SvsTests)
endif()
endif()

if(ENABLE_TEST_COVERAGE)
target_compile_options(soar_static PUBLIC -O0 -g -fprofile-arcs -ftest-coverage)
target_link_options(soar_static PUBLIC -fprofile-arcs -ftest-coverage)
endif()
10 changes: 0 additions & 10 deletions UnitTests/TestHarness/TestHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@
#include <exception>
#include <sstream>

bool isfile(const char* path)
{
#ifdef _WIN32
DWORD a = GetFileAttributes(path);
return a != INVALID_FILE_ATTRIBUTES && !(a & FILE_ATTRIBUTE_DIRECTORY);
#else
struct stat st;
return (stat(path, &st) == 0 && !S_ISDIR(st.st_mode));
#endif
}

void setCWDToEnv()
{
Expand Down
1 change: 0 additions & 1 deletion UnitTests/TestHarness/TestHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
//#define TEST(X, Y) Test test_##X = Test(#X, std::bind(&test_type::X, this), Y, m_TestCategory_tests);
#define TEST(X, Y) Test test_##X = Test(#X, [this](){ this->X(); }/*std::bind(&test_type::X, this)*/, Y, m_TestCategory_tests);

bool isfile(const char* path);
void setCWDToEnv();
void printDebugInformation(std::stringstream& output, sml::Agent* agent);

Expand Down
2 changes: 1 addition & 1 deletion conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[requires]
sqlite3/3.47.0
sqlite3/3.40.0

[generators]
CMakeDeps
Expand Down

0 comments on commit 72d3ab1

Please sign in to comment.