Skip to content

Commit cd2dd49

Browse files
jcfrfirewave
andauthored
tests: Fix execution of testrunner for out-of-source builds (danmar#510)
Co-authored-by: Oliver Stöneberg <[email protected]>
1 parent 871dc30 commit cd2dd49

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

.github/workflows/CI-unixish.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ jobs:
7373
run: |
7474
cmake --build cmake.output --target testrunner -- -j $(nproc)
7575
./cmake.output/testrunner
76+
# Re-run tests from within the build directory to validate that
77+
# SIMPLECPP_TEST_SOURCE_DIR is correctly defined and resolved
78+
(cd cmake.output && ./testrunner)
7679
7780
- name: Run valgrind
7881
if: matrix.os == 'ubuntu-24.04'

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ add_library(simplecpp_obj OBJECT simplecpp.cpp)
7676

7777
add_executable(simplecpp $<TARGET_OBJECTS:simplecpp_obj> main.cpp)
7878
add_executable(testrunner $<TARGET_OBJECTS:simplecpp_obj> test.cpp)
79+
target_compile_definitions(testrunner
80+
PRIVATE
81+
SIMPLECPP_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
82+
)
7983

8084
enable_testing()
8185
add_test(NAME testrunner COMMAND testrunner)

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
all: testrunner simplecpp
22

3+
CPPFLAGS ?=
34
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)
45
LDFLAGS = -g $(LDOPTS)
56

6-
%.o: %.cpp simplecpp.h
7-
$(CXX) $(CXXFLAGS) -c $<
7+
# Define test source dir macro for compilation (preprocessor flags)
8+
TEST_CPPFLAGS = -DSIMPLECPP_TEST_SOURCE_DIR=\"$(CURDIR)\"
9+
10+
# Only test.o gets the define
11+
test.o: CPPFLAGS += $(TEST_CPPFLAGS)
812

13+
%.o: %.cpp simplecpp.h
14+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $<
915

1016
testrunner: test.o simplecpp.o
1117
$(CXX) $(LDFLAGS) simplecpp.o test.o -o testrunner

test.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
#include <utility>
1717
#include <vector>
1818

19+
#ifndef SIMPLECPP_TEST_SOURCE_DIR
20+
#error "SIMPLECPP_TEST_SOURCE_DIR is not defined."
21+
#endif
22+
1923
#define STRINGIZE_(x) #x
2024
#define STRINGIZE(x) STRINGIZE_(x)
2125

26+
static const std::string testSourceDir = SIMPLECPP_TEST_SOURCE_DIR;
2227
static int numberOfFailedAssertions = 0;
2328

2429
#define ASSERT_EQUALS(expected, actual) (assertEquals((expected), (actual), __LINE__))
@@ -1556,6 +1561,7 @@ static void has_include_1()
15561561
" #endif\n"
15571562
"#endif";
15581563
simplecpp::DUI dui;
1564+
dui.includePaths.push_back(testSourceDir);
15591565
dui.std = "c++17";
15601566
ASSERT_EQUALS("\n\nA", preprocess(code, dui));
15611567
dui.std = "c++14";
@@ -1573,6 +1579,7 @@ static void has_include_2()
15731579
" #endif\n"
15741580
"#endif";
15751581
simplecpp::DUI dui;
1582+
dui.includePaths.push_back(testSourceDir);
15761583
dui.std = "c++17";
15771584
ASSERT_EQUALS("\n\nA", preprocess(code, dui));
15781585
ASSERT_EQUALS("", preprocess(code));
@@ -1592,7 +1599,7 @@ static void has_include_3()
15921599
// Test file not found...
15931600
ASSERT_EQUALS("\n\n\n\nB", preprocess(code, dui));
15941601
// Unless -I is set (preferably, we should differentiate -I and -isystem...)
1595-
dui.includePaths.push_back("./testsuite");
1602+
dui.includePaths.push_back(testSourceDir + "/testsuite");
15961603
ASSERT_EQUALS("\n\nA", preprocess(code, dui));
15971604
ASSERT_EQUALS("", preprocess(code));
15981605
}
@@ -1608,6 +1615,7 @@ static void has_include_4()
16081615
"#endif";
16091616
simplecpp::DUI dui;
16101617
dui.std = "c++17";
1618+
dui.includePaths.push_back(testSourceDir);
16111619
ASSERT_EQUALS("\n\nA", preprocess(code, dui));
16121620
ASSERT_EQUALS("", preprocess(code));
16131621
}
@@ -1623,6 +1631,7 @@ static void has_include_5()
16231631
"#endif";
16241632
simplecpp::DUI dui;
16251633
dui.std = "c++17";
1634+
dui.includePaths.push_back(testSourceDir);
16261635
ASSERT_EQUALS("\n\nA", preprocess(code, dui));
16271636
ASSERT_EQUALS("", preprocess(code));
16281637
}
@@ -1638,6 +1647,7 @@ static void has_include_6()
16381647
"#endif";
16391648
simplecpp::DUI dui;
16401649
dui.std = "gnu99";
1650+
dui.includePaths.push_back(testSourceDir);
16411651
ASSERT_EQUALS("\n\nA", preprocess(code, dui));
16421652
ASSERT_EQUALS("", preprocess(code));
16431653
}

0 commit comments

Comments
 (0)