From 4420563a5dfe5cdb72470e0ce5a7b8330d45819d Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Tue, 26 Aug 2025 01:27:05 -0400 Subject: [PATCH 1/6] tests: Fix execution of testrunner for out-of-source builds This changes the testrunner configuration to correctly handle out-of-source builds by setting the SIMPLECPP_SOURCE_DIR definition. This ensures that the source directory is available to the test runner, allowing it to find include paths correctly. --- CMakeLists.txt | 4 ++++ test.cpp | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index f9e3eb6d..01122e20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,10 @@ add_library(simplecpp_obj OBJECT simplecpp.cpp) add_executable(simplecpp $ main.cpp) add_executable(testrunner $ test.cpp) +target_compile_definitions(testrunner + PRIVATE + SIMPLECPP_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}" +) enable_testing() add_test(NAME testrunner COMMAND testrunner) diff --git a/test.cpp b/test.cpp index 7b108638..978ebabb 100644 --- a/test.cpp +++ b/test.cpp @@ -1556,6 +1556,9 @@ static void has_include_1() " #endif\n" "#endif"; simplecpp::DUI dui; +#ifdef SIMPLECPP_SOURCE_DIR + dui.includePaths.push_back(SIMPLECPP_SOURCE_DIR); +#endif dui.std = "c++17"; ASSERT_EQUALS("\n\nA", preprocess(code, dui)); dui.std = "c++14"; @@ -1573,6 +1576,9 @@ static void has_include_2() " #endif\n" "#endif"; simplecpp::DUI dui; +#ifdef SIMPLECPP_SOURCE_DIR + dui.includePaths.push_back(SIMPLECPP_SOURCE_DIR); +#endif dui.std = "c++17"; ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); @@ -1592,7 +1598,11 @@ static void has_include_3() // Test file not found... ASSERT_EQUALS("\n\n\n\nB", preprocess(code, dui)); // Unless -I is set (preferably, we should differentiate -I and -isystem...) +#ifdef SIMPLECPP_SOURCE_DIR + dui.includePaths.push_back(std::string(SIMPLECPP_SOURCE_DIR) + "/testsuite"); +#else dui.includePaths.push_back("./testsuite"); +#endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } @@ -1608,6 +1618,9 @@ static void has_include_4() "#endif"; simplecpp::DUI dui; dui.std = "c++17"; +#ifdef SIMPLECPP_SOURCE_DIR + dui.includePaths.push_back(SIMPLECPP_SOURCE_DIR); +#endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } @@ -1623,6 +1636,9 @@ static void has_include_5() "#endif"; simplecpp::DUI dui; dui.std = "c++17"; +#ifdef SIMPLECPP_SOURCE_DIR + dui.includePaths.push_back(SIMPLECPP_SOURCE_DIR); +#endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } @@ -1638,6 +1654,9 @@ static void has_include_6() "#endif"; simplecpp::DUI dui; dui.std = "gnu99"; +#ifdef SIMPLECPP_SOURCE_DIR + dui.includePaths.push_back(SIMPLECPP_SOURCE_DIR); +#endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } From a2afe26606e7d1f02cc1df73b29ab03b512c15d0 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 27 Aug 2025 09:52:19 -0400 Subject: [PATCH 2/6] tests: Rename define to more explicitly indicate the intent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `SIMPLECPP_SOURCE_DIR` define was changed to `SIMPLECPP_TEST_SOURCE_DIR` to remove ambiguity about its purpose. This change clarifies that the variable is intended to be used in the context of testing. Suggested-by: Oliver Stöneberg --- CMakeLists.txt | 2 +- test.cpp | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 01122e20..8799b7a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,7 +78,7 @@ add_executable(simplecpp $ main.cpp) add_executable(testrunner $ test.cpp) target_compile_definitions(testrunner PRIVATE - SIMPLECPP_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}" + SIMPLECPP_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}" ) enable_testing() diff --git a/test.cpp b/test.cpp index 978ebabb..19d9f3e2 100644 --- a/test.cpp +++ b/test.cpp @@ -1556,8 +1556,8 @@ static void has_include_1() " #endif\n" "#endif"; simplecpp::DUI dui; -#ifdef SIMPLECPP_SOURCE_DIR - dui.includePaths.push_back(SIMPLECPP_SOURCE_DIR); +#ifdef SIMPLECPP_TEST_SOURCE_DIR + dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); #endif dui.std = "c++17"; ASSERT_EQUALS("\n\nA", preprocess(code, dui)); @@ -1576,8 +1576,8 @@ static void has_include_2() " #endif\n" "#endif"; simplecpp::DUI dui; -#ifdef SIMPLECPP_SOURCE_DIR - dui.includePaths.push_back(SIMPLECPP_SOURCE_DIR); +#ifdef SIMPLECPP_TEST_SOURCE_DIR + dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); #endif dui.std = "c++17"; ASSERT_EQUALS("\n\nA", preprocess(code, dui)); @@ -1598,8 +1598,8 @@ static void has_include_3() // Test file not found... ASSERT_EQUALS("\n\n\n\nB", preprocess(code, dui)); // Unless -I is set (preferably, we should differentiate -I and -isystem...) -#ifdef SIMPLECPP_SOURCE_DIR - dui.includePaths.push_back(std::string(SIMPLECPP_SOURCE_DIR) + "/testsuite"); +#ifdef SIMPLECPP_TEST_SOURCE_DIR + dui.includePaths.push_back(std::string(SIMPLECPP_TEST_SOURCE_DIR) + "/testsuite"); #else dui.includePaths.push_back("./testsuite"); #endif @@ -1618,8 +1618,8 @@ static void has_include_4() "#endif"; simplecpp::DUI dui; dui.std = "c++17"; -#ifdef SIMPLECPP_SOURCE_DIR - dui.includePaths.push_back(SIMPLECPP_SOURCE_DIR); +#ifdef SIMPLECPP_TEST_SOURCE_DIR + dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); #endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); @@ -1636,8 +1636,8 @@ static void has_include_5() "#endif"; simplecpp::DUI dui; dui.std = "c++17"; -#ifdef SIMPLECPP_SOURCE_DIR - dui.includePaths.push_back(SIMPLECPP_SOURCE_DIR); +#ifdef SIMPLECPP_TEST_SOURCE_DIR + dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); #endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); @@ -1654,8 +1654,8 @@ static void has_include_6() "#endif"; simplecpp::DUI dui; dui.std = "gnu99"; -#ifdef SIMPLECPP_SOURCE_DIR - dui.includePaths.push_back(SIMPLECPP_SOURCE_DIR); +#ifdef SIMPLECPP_TEST_SOURCE_DIR + dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); #endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); From acd1e999c2ff7ea9450b547667c62d2a99486b5b Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 27 Aug 2025 10:02:52 -0400 Subject: [PATCH 3/6] ci: Run CMake testrunner from both repo root and build dir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This updates the GitHub Actions workflow to execute `testrunner` twice, but only for the CMake build case: - Once from the repository root (default case). - Once from within the `cmake.output` build directory to ensure the `SIMPLECPP_TEST_SOURCE_DIR` definition is correctly set and resolved. Note that this is not yet supported for the Makefile-based build. Suggested-by: Oliver Stöneberg --- .github/workflows/CI-unixish.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/CI-unixish.yml b/.github/workflows/CI-unixish.yml index 60361389..f76fdd15 100644 --- a/.github/workflows/CI-unixish.yml +++ b/.github/workflows/CI-unixish.yml @@ -73,6 +73,9 @@ jobs: run: | cmake --build cmake.output --target testrunner -- -j $(nproc) ./cmake.output/testrunner + # Re-run tests from within the build directory to validate that + # SIMPLECPP_TEST_SOURCE_DIR is correctly defined and resolved + (cd cmake.output && ./testrunner) - name: Run valgrind if: matrix.os == 'ubuntu-24.04' From aa00d92966c300b3b6e63868a9cc9300c9897953 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 27 Aug 2025 10:30:21 -0400 Subject: [PATCH 4/6] tests: Define SIMPLECPP_TEST_SOURCE_DIR for Makefile build This updates the Makefile so that `test.o` is compiled with `-DSIMPLECPP_TEST_SOURCE_DIR="$(CURDIR)"`, ensuring the test suite can locate headers and sources consistently with the CMake build. --- Makefile | 7 ++++++- test.cpp | 18 ++++-------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index d899d2cd..618d8d85 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,15 @@ all: testrunner simplecpp CXXFLAGS = -Wall -Wextra -pedantic -Wcast-qual -Wfloat-equal -Wmissing-declarations -Wmissing-format-attribute -Wredundant-decls -Wundef -Wno-multichar -Wold-style-cast -std=c++11 -g $(CXXOPTS) LDFLAGS = -g $(LDOPTS) +# Define test source dir macro for compilation +TEST_DEFINES = -DSIMPLECPP_TEST_SOURCE_DIR=\"$(CURDIR)\" + +# Only test.o gets the define +test.o: CXXFLAGS += $(TEST_DEFINES) + %.o: %.cpp simplecpp.h $(CXX) $(CXXFLAGS) -c $< - testrunner: test.o simplecpp.o $(CXX) $(LDFLAGS) simplecpp.o test.o -o testrunner diff --git a/test.cpp b/test.cpp index 19d9f3e2..becd5ef8 100644 --- a/test.cpp +++ b/test.cpp @@ -16,6 +16,10 @@ #include #include +#ifndef SIMPLECPP_TEST_SOURCE_DIR +#error "SIMPLECPP_TEST_SOURCE_DIR is not defined." +#endif + #define STRINGIZE_(x) #x #define STRINGIZE(x) STRINGIZE_(x) @@ -1556,9 +1560,7 @@ static void has_include_1() " #endif\n" "#endif"; simplecpp::DUI dui; -#ifdef SIMPLECPP_TEST_SOURCE_DIR dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); -#endif dui.std = "c++17"; ASSERT_EQUALS("\n\nA", preprocess(code, dui)); dui.std = "c++14"; @@ -1576,9 +1578,7 @@ static void has_include_2() " #endif\n" "#endif"; simplecpp::DUI dui; -#ifdef SIMPLECPP_TEST_SOURCE_DIR dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); -#endif dui.std = "c++17"; ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); @@ -1598,11 +1598,7 @@ static void has_include_3() // Test file not found... ASSERT_EQUALS("\n\n\n\nB", preprocess(code, dui)); // Unless -I is set (preferably, we should differentiate -I and -isystem...) -#ifdef SIMPLECPP_TEST_SOURCE_DIR dui.includePaths.push_back(std::string(SIMPLECPP_TEST_SOURCE_DIR) + "/testsuite"); -#else - dui.includePaths.push_back("./testsuite"); -#endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } @@ -1618,9 +1614,7 @@ static void has_include_4() "#endif"; simplecpp::DUI dui; dui.std = "c++17"; -#ifdef SIMPLECPP_TEST_SOURCE_DIR dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); -#endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } @@ -1636,9 +1630,7 @@ static void has_include_5() "#endif"; simplecpp::DUI dui; dui.std = "c++17"; -#ifdef SIMPLECPP_TEST_SOURCE_DIR dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); -#endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } @@ -1654,9 +1646,7 @@ static void has_include_6() "#endif"; simplecpp::DUI dui; dui.std = "gnu99"; -#ifdef SIMPLECPP_TEST_SOURCE_DIR dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); -#endif ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } From d3b0ba1e6d18483b9ef3b4c83cc6f83629e09639 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 27 Aug 2025 10:33:52 -0400 Subject: [PATCH 5/6] chore: Simplify test runner introducing testSourceDir static variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Oliver Stöneberg --- test.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test.cpp b/test.cpp index becd5ef8..c44524a3 100644 --- a/test.cpp +++ b/test.cpp @@ -23,6 +23,7 @@ #define STRINGIZE_(x) #x #define STRINGIZE(x) STRINGIZE_(x) +static const std::string testSourceDir = SIMPLECPP_TEST_SOURCE_DIR; static int numberOfFailedAssertions = 0; #define ASSERT_EQUALS(expected, actual) (assertEquals((expected), (actual), __LINE__)) @@ -1560,7 +1561,7 @@ static void has_include_1() " #endif\n" "#endif"; simplecpp::DUI dui; - dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); + dui.includePaths.push_back(testSourceDir); dui.std = "c++17"; ASSERT_EQUALS("\n\nA", preprocess(code, dui)); dui.std = "c++14"; @@ -1578,7 +1579,7 @@ static void has_include_2() " #endif\n" "#endif"; simplecpp::DUI dui; - dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); + dui.includePaths.push_back(testSourceDir); dui.std = "c++17"; ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); @@ -1598,7 +1599,7 @@ static void has_include_3() // Test file not found... ASSERT_EQUALS("\n\n\n\nB", preprocess(code, dui)); // Unless -I is set (preferably, we should differentiate -I and -isystem...) - dui.includePaths.push_back(std::string(SIMPLECPP_TEST_SOURCE_DIR) + "/testsuite"); + dui.includePaths.push_back(testSourceDir + "/testsuite"); ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } @@ -1614,7 +1615,7 @@ static void has_include_4() "#endif"; simplecpp::DUI dui; dui.std = "c++17"; - dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); + dui.includePaths.push_back(testSourceDir); ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } @@ -1630,7 +1631,7 @@ static void has_include_5() "#endif"; simplecpp::DUI dui; dui.std = "c++17"; - dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); + dui.includePaths.push_back(testSourceDir); ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } @@ -1646,7 +1647,7 @@ static void has_include_6() "#endif"; simplecpp::DUI dui; dui.std = "gnu99"; - dui.includePaths.push_back(SIMPLECPP_TEST_SOURCE_DIR); + dui.includePaths.push_back(testSourceDir); ASSERT_EQUALS("\n\nA", preprocess(code, dui)); ASSERT_EQUALS("", preprocess(code)); } From 82c495bb7a595bb3fbd28f9fe4e88a668f6faaf7 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Thu, 28 Aug 2025 16:33:09 -0400 Subject: [PATCH 6/6] chore: move test-specific defines from CXXFLAGS to CPPFLAGS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This ensures that preprocessor flags are managed separately from compiler flags, avoiding confusion with user-provided CXXOPTS. Co-authored-by: Oliver Stöneberg --- Makefile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 618d8d85..7489ec83 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,17 @@ all: testrunner simplecpp +CPPFLAGS ?= CXXFLAGS = -Wall -Wextra -pedantic -Wcast-qual -Wfloat-equal -Wmissing-declarations -Wmissing-format-attribute -Wredundant-decls -Wundef -Wno-multichar -Wold-style-cast -std=c++11 -g $(CXXOPTS) LDFLAGS = -g $(LDOPTS) -# Define test source dir macro for compilation -TEST_DEFINES = -DSIMPLECPP_TEST_SOURCE_DIR=\"$(CURDIR)\" +# Define test source dir macro for compilation (preprocessor flags) +TEST_CPPFLAGS = -DSIMPLECPP_TEST_SOURCE_DIR=\"$(CURDIR)\" # Only test.o gets the define -test.o: CXXFLAGS += $(TEST_DEFINES) +test.o: CPPFLAGS += $(TEST_CPPFLAGS) %.o: %.cpp simplecpp.h - $(CXX) $(CXXFLAGS) -c $< + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< testrunner: test.o simplecpp.o $(CXX) $(LDFLAGS) simplecpp.o test.o -o testrunner