Skip to content

Commit

Permalink
Merge pull request #333 from eranpeer/fix-more-warning-and-add-werror-ci
Browse files Browse the repository at this point in the history
Treat warnings as errors when building tests in the CI.
  • Loading branch information
FranckRJ authored Apr 27, 2024
2 parents a3c7448 + ccd2589 commit bec66cb
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci_linux_clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ jobs:
cmake -S . -B build \
-DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} \
-DENABLE_TESTING=ON \
-DWARNINGS_AS_ERRORS_FOR_TESTS=ON \
-DOVERRIDE_CXX_STANDARD_FOR_TESTS=${{ matrix.config.cxx_standard }} \
-DENABLE_SANITIZERS_IN_TESTS=${{ matrix.config.enable_sanitizers_in_tests }}
cmake --build build -j
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci_linux_gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
cmake -S . -B build \
-DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} \
-DENABLE_TESTING=ON \
-DWARNINGS_AS_ERRORS_FOR_TESTS=ON \
-DOVERRIDE_CXX_STANDARD_FOR_TESTS=${{ matrix.config.cxx_standard }} \
-DENABLE_SANITIZERS_IN_TESTS=${{ matrix.config.enable_sanitizers_in_tests }}
cmake --build build -j
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci_macos_appleclang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
cmake -S . -B build \
-DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} \
-DENABLE_TESTING=ON \
-DWARNINGS_AS_ERRORS_FOR_TESTS=ON \
-DOVERRIDE_CXX_STANDARD_FOR_TESTS=${{ matrix.config.cxx_standard }} \
-DENABLE_SANITIZERS_IN_TESTS=${{ matrix.config.enable_sanitizers_in_tests }}
cmake --build build -j
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci_windows_msvc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
-G "${{ matrix.config.generator }}" \
-A ${{ matrix.config.architecture }} \
-DENABLE_TESTING=ON \
-DWARNINGS_AS_ERRORS_FOR_TESTS=ON \
-DOVERRIDE_CXX_STANDARD_FOR_TESTS=${{ matrix.config.cxx_standard }}
cmake --build build --config ${{ matrix.config.build_type }} -j
- name: Run tests
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ project(FakeIt VERSION 2.4.0 LANGUAGES CXX)

option(ENABLE_TESTING "Enable build of tests." OFF)
set(OVERRIDE_CXX_STANDARD_FOR_TESTS "" CACHE STRING "Override the C++ standard used for building tests.")
option(WARNINGS_AS_ERRORS_FOR_TESTS "Treat warnings as errors when building tests." OFF)
option(ENABLE_SANITIZERS_IN_TESTS "Enable address / undefined sanitizers in tests." OFF)
option(ENABLE_COVERAGE "Enable coverage reporting for gcc/clang." OFF)

Expand Down
10 changes: 9 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ target_link_libraries(FakeIt_tests PRIVATE
)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
target_compile_options(FakeIt_tests PRIVATE -Wall -Wextra -Wno-ignored-qualifiers -Wno-uninitialized -O0 -g3)
target_compile_options(FakeIt_tests PRIVATE -Wall -Wextra -Wno-ignored-qualifiers -O0 -g3)
elseif(MSVC)
target_compile_options(FakeIt_tests PRIVATE /W3 /sdl /Od)
endif()
Expand All @@ -45,6 +45,14 @@ if(OVERRIDE_CXX_STANDARD_FOR_TESTS)
message(STATUS "Building tests in C++${OVERRIDE_CXX_STANDARD_FOR_TESTS}.")
endif()

if(WARNINGS_AS_ERRORS_FOR_TESTS)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
target_compile_options(FakeIt_tests PRIVATE -Werror)
elseif(MSVC)
target_compile_options(FakeIt_tests PRIVATE /WX)
endif()
endif()

if(ENABLE_SANITIZERS_IN_TESTS)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
target_compile_options(FakeIt_tests PRIVATE -fsanitize=address,undefined -fno-sanitize-recover=address,undefined)
Expand Down
16 changes: 9 additions & 7 deletions tests/tpunit++.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@
* used by all test functions.
* TEST(function); registers a function to run as a test within a test fixture.
*/
#define AFTER(M) tpunit_detail_method(&M, #M, method::AFTER_METHOD)
#define AFTER_CLASS(M) tpunit_detail_method(&M, #M, method::AFTER_CLASS_METHOD)
#define BEFORE(M) tpunit_detail_method(&M, #M, method::BEFORE_METHOD)
#define BEFORE_CLASS(M) tpunit_detail_method(&M, #M, method::BEFORE_CLASS_METHOD)
#define TEST(M) tpunit_detail_method(&M, #M, method::TEST_METHOD)
#define AFTER(M) tpunit_detail_method(this, &M, #M, method::AFTER_METHOD)
#define AFTER_CLASS(M) tpunit_detail_method(this, &M, #M, method::AFTER_CLASS_METHOD)
#define BEFORE(M) tpunit_detail_method(this, &M, #M, method::BEFORE_METHOD)
#define BEFORE_CLASS(M) tpunit_detail_method(this, &M, #M, method::BEFORE_CLASS_METHOD)
#define TEST(M) tpunit_detail_method(this, &M, #M, method::TEST_METHOD)

/**
* Try our best to detect compiler support for exception handling so
Expand Down Expand Up @@ -297,12 +297,14 @@ namespace tpunit {
/**
* Create a new method to register with the test fixture.
*
* @param[in] obj The test fixture for which the new method will be registered.
* @param[in] _method A method to register with the test fixture.
* @param[in] _name The internal name of the method used when status messages are displayed.
* @param[in] _type The type of method to register.
*/
template <typename C>
method* tpunit_detail_method(void (C::*_method)(), const char* _name, unsigned char _type) {
return new method(this, static_cast<void (TestFixture::*)()>(_method), _name, _type);
static method* tpunit_detail_method(TestFixture* obj, void (C::*_method)(), const char* _name, unsigned char _type) {
return new method(obj, static_cast<void (TestFixture::*)()>(_method), _name, _type);
}

/**
Expand Down

0 comments on commit bec66cb

Please sign in to comment.