I have an SConstruct file to drive the compilation of a C++ library and compile a number of test programs to check its validation. For building the test programs, the following SConstruct snippet works just fine:
test_sources = glob.glob(r"test/unit_tests/*.cpp")
env.Append(RPATH=root_dir)
for tsource in test_sources:
ttarget = tsource.replace('.cpp', '.out')
env.Program(target=ttarget, source=tsource, CPPPATH='src/', LIBS=vlib, LIBPATH='.')
Some of the tests however, should not compile,i.e. they are deliberately designed to produce compilation errors. How would i go about testing for compilation errors? I tried placing the compilation command in a try/except block, i.e.
test_sources = glob.glob(r"test/unit_tests/*.cpp")
env.Append(RPATH=root_dir)
for tsource in test_sources:
ttarget = tsource.replace('_', '-').replace('.cpp', '.out')
if 'mock' in os.path.basename(tsource):
# filenames including 'mock' should not compile
try:
env.Program(target=ttarget, source=tsource, CPPPATH='src/', LIBS=vlib, LIBPATH='.')
sys.exit(1)
except:
pass
else:
env.Program(target=ttarget, source=tsource, CPPPATH='src/', LIBS=vlib, LIBPATH='.')
but it does not work (scons exits with error)
scons: Reading SConscript files ...
running with -j 2
scons: done reading SConscript files.
scons: Building targets ...
g++ -o test/unit-tests/month.out -Wl,-rpath=/home/xanthos/Software/ggdatetime test/unit_tests/month.o -L. libdatetime.so.0.1.0
g++ -o test/unit_tests/year_mock_1.o -c -std=c++17 -Wall -Wextra -Werror -pedantic -W -Wshadow -Winline -O2 -march=native --std=c++17 -Isrc test/unit_tests/year_mock_1.cpp
g++ -o test/unit_tests/year_mock_2.o -c -std=c++17 -Wall -Wextra -Werror -pedantic -W -Wshadow -Winline -O2 -march=native --std=c++17 -Isrc test/unit_tests/year_mock_2.cpp
test/unit_tests/year_mock_1.cpp: In function 'int main()':
test/unit_tests/year_mock_1.cpp:9:6: error: no match for 'operator==' (operand types are 'dso::year' and 'int')
9 | y1 == 2024;
| ~~ ^~ ~~~~
| | |
| | int
| dso::year
In file included from test/unit_tests/year_mock_1.cpp:1:
src/dtfund.hpp:1616:16: note: candidate: 'template<class DType, class, class> constexpr bool dso::operator==(DType, DType)'
1616 | constexpr bool operator==(DType a, DType b) noexcept {
| ^~~~~~~~
src/dtfund.hpp:1616:16: note: template argument deduction/substitution failed:
test/unit_tests/year_mock_1.cpp:9:9: note: deduced conflicting types for parameter 'DType' ('dso::year' and 'int')
9 | y1 == 2024;
| ^~~~
scons: *** [test/unit_tests/year_mock_1.o] Error 1
test/unit_tests/year_mock_2.cpp: In function 'int main()':
test/unit_tests/year_mock_2.cpp:9:6: error: no match for 'operator+' (operand types are 'dso::year' and 'int')
9 | y1 + 2024;
| ~~ ^ ~~~~
| | |
| | int
| dso::year
In file included from test/unit_tests/year_mock_2.cpp:1:
src/dtfund.hpp:1751:17: note: candidate: 'template<class DType, class, class> constexpr DType dso::operator+(DType, DType)'
1751 | constexpr DType operator+(DType _a, DType _b) noexcept {
| ^~~~~~~~
src/dtfund.hpp:1751:17: note: template argument deduction/substitution failed:
test/unit_tests/year_mock_2.cpp:9:8: note: deduced conflicting types for parameter 'DType' ('dso::year' and 'int')
9 | y1 + 2024;
| ^~~~
scons: *** [test/unit_tests/year_mock_2.o] Error 1
scons: building terminated because of errors.
Try this:
Prepending any action with
-will ignore the return value. So we're cloning your original Environment, and only modifying the C++ compile and linking commands (and also for shared objects, you could probably narrow it down to just the SH* ones, but I think this should work.See: https://scons.org/doc/production/HTML/scons-man.html#action_objects for more information on action objects.