Skip to content

Commit

Permalink
Run all C++ tests from a single executable (#3582)
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche authored Aug 12, 2024
1 parent 4ff63a7 commit c719cce
Show file tree
Hide file tree
Showing 44 changed files with 138 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
run: cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS=--coverage

- name: Build project
run: cmake --build build --target build_tests
run: cmake --build build --target test_runner --target task_executable

- name: Test project
run: ctest --test-dir build -j 8 --output-on-failure
Expand Down
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions doc/devel/contrib/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ cmake --build build-clang
## Run the Test Suite:
For running the test suite [ctest](https://cmake.org/cmake/help/latest/manual/ctest.1.html) is used.
Before one can run the test suite the `task_executable` must be built.
After that also the `build_tests` target must be build, which can be done over:
After that also the `test_runner` target must be build, which can be done over:
```sh
cmake --build build --target build_tests
cmake --build build --target test_runner
```
Again you may also use the `-j <number-of-jobs>` option for parallel builds.

Expand Down
102 changes: 56 additions & 46 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cmake_minimum_required (VERSION 3.22)

# -- C++ tests

include_directories (${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/src/commands
Expand All @@ -8,56 +10,64 @@ include_directories (${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/test
${TASK_INCLUDE_DIRS})

set (test_SRCS
col.test.cpp
dom.test.cpp
eval.test.cpp
lexer.test.cpp
t.test.cpp
tw-2689.test.cpp
tdb2.test.cpp
tc.test.cpp
util.test.cpp
variant_add.test.cpp
variant_and.test.cpp
variant_cast.test.cpp
variant_divide.test.cpp
variant_equal.test.cpp
variant_exp.test.cpp
variant_gt.test.cpp
variant_gte.test.cpp
variant_inequal.test.cpp
variant_lt.test.cpp
variant_lte.test.cpp
variant_match.test.cpp
variant_math.test.cpp
variant_modulo.test.cpp
variant_multiply.test.cpp
variant_nomatch.test.cpp
variant_not.test.cpp
variant_or.test.cpp
variant_partial.test.cpp
variant_subtract.test.cpp
variant_xor.test.cpp
view.test.cpp
# All C++ test files. Note that the portion before `.cpp` must be a valid,
# unique C++ identifier.
set(test_SRCS
col_test.cpp
dom_test.cpp
eval_test.cpp
lexer_test.cpp
t_test.cpp
tw_2689_test.cpp
tdb2_test.cpp
tc_cpp_test.cpp
util_test.cpp
variant_add_test.cpp
variant_and_test.cpp
variant_cast_test.cpp
variant_divide_test.cpp
variant_equal_test.cpp
variant_exp_test.cpp
variant_gt_test.cpp
variant_gte_test.cpp
variant_inequal_test.cpp
variant_lt_test.cpp
variant_lte_test.cpp
variant_match_test.cpp
variant_math_test.cpp
variant_modulo_test.cpp
variant_multiply_test.cpp
variant_nomatch_test.cpp
variant_not_test.cpp
variant_or_test.cpp
variant_partial_test.cpp
variant_subtract_test.cpp
variant_xor_test.cpp
view_test.cpp
)

add_custom_target (build_tests DEPENDS ${test_SRCS}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test)

foreach (src_FILE ${test_SRCS})
add_executable (${src_FILE} ${src_FILE} test.cpp)
target_link_libraries (${src_FILE} task commands columns libshared task commands columns libshared task commands columns libshared ${TASK_LIBRARIES})
add_dependencies (${src_FILE} task_executable)
if (DARWIN)
target_link_libraries (${src_FILE} "-framework CoreFoundation -framework Security -framework SystemConfiguration")
endif (DARWIN)
# Build `test_runner` containing all CPP tests, linked once.
create_test_sourcelist (cpp_test_SRCS cpp_tests.cpp ${test_SRCS})
add_executable(test_runner
test.cpp
${cpp_test_SRCS}
)
target_link_libraries (test_runner task commands columns libshared task commands columns libshared task commands columns libshared ${TASK_LIBRARIES})
if (DARWIN)
target_link_libraries (test_runner "-framework CoreFoundation -framework Security -framework SystemConfiguration")
endif (DARWIN)

add_test(NAME ${src_FILE}
COMMAND ${src_FILE}
foreach (test_FILE ${test_SRCS})
get_filename_component (test_NAME ${test_FILE} NAME_WE)
# Tell the source file what its own name is
set_source_files_properties(${test_FILE} PROPERTIES COMPILE_FLAGS -DTEST_NAME=${test_NAME})
add_test(NAME ${test_FILE}
COMMAND test_runner ${test_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
endforeach (src_FILE)
)
endforeach (test_FILE)

# -- Python tests

add_subdirectory(basetest)
add_subdirectory(simpletap)
Expand Down
2 changes: 1 addition & 1 deletion test/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Running Tests
Do this to run all tests:
```shell
cmake --build build --target build_tests
cmake --build build --target test_runner --target task_executable
ctest --test-dir build
```

Expand Down
2 changes: 1 addition & 1 deletion test/col.test.cpp → test/col_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <test.h>

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest test(12);

// Ensure environment has no influence.
Expand Down
2 changes: 1 addition & 1 deletion test/docker/arch
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ RUN cmake --install build
RUN task --version

# Setup tests
RUN cmake --build build --target build_tests -j 8
RUN cmake --build build --target test_runner -j 8
CMD ctest --test-dir build -j 8 --output-on-failure --rerun-failed
2 changes: 1 addition & 1 deletion test/docker/debianstable
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ RUN cmake --install build
RUN task --version

# Setup tests
RUN cmake --build build --target build_tests -j 8
RUN cmake --build build --target test_runner -j 8
CMD ctest --test-dir build -j 8 --output-on-failure --rerun-failed
2 changes: 1 addition & 1 deletion test/docker/debiantesting
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ RUN cmake --install build
RUN task --version

# Setup tests
RUN cmake --build build --target build_tests -j 8
RUN cmake --build build --target test_runner -j 8
CMD ctest --test-dir build -j 8 --output-on-failure --rerun-failed
2 changes: 1 addition & 1 deletion test/docker/fedora39
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ RUN cmake --install build
RUN task --version

# Setup tests
RUN cmake --build build --target build_tests -j 8
RUN cmake --build build --target test_runner -j 8
CMD ctest --test-dir build -j 8 --output-on-failure --rerun-failed
2 changes: 1 addition & 1 deletion test/docker/fedora40
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ RUN cmake --install build
RUN task --version

# Setup tests
RUN cmake --build build --target build_tests -j 8
RUN cmake --build build --target test_runner -j 8
CMD ctest --test-dir build -j 8 --output-on-failure --rerun-failed
2 changes: 1 addition & 1 deletion test/docker/opensuse
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ RUN cmake --install build
RUN task --version

# Setup tests
RUN cmake --build build --target build_tests -j 8
RUN cmake --build build --target test_runner -j 8
CMD ctest --test-dir build -j 8 --output-on-failure --rerun-failed
2 changes: 1 addition & 1 deletion test/docker/ubuntu2004
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ RUN cmake --install build
RUN task --version

# Setup tests
RUN cmake --build build --target build_tests -j 8
RUN cmake --build build --target test_runner -j 8
CMD ctest --test-dir build -j 8 --output-on-failure --rerun-failed
2 changes: 1 addition & 1 deletion test/docker/ubuntu2204
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ RUN cmake --install build
RUN task --version

# Setup tests
RUN cmake --build build --target build_tests -j 8
RUN cmake --build build --target test_runner -j 8
CMD ctest --test-dir build -j 8 --output-on-failure --rerun-failed
6 changes: 5 additions & 1 deletion test/dom.test.cpp → test/dom_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <Variant.h>
#include <test.h>

namespace {

////////////////////////////////////////////////////////////////////////////////
bool providerString(const std::string& path, Variant& var) {
if (path == "name") {
Expand All @@ -50,8 +52,10 @@ bool providerString(const std::string& path, Variant& var) {
return false;
}

} // namespace

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest t(12);

DOM dom;
Expand Down
6 changes: 5 additions & 1 deletion test/eval.test.cpp → test/eval_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <Eval.h>
#include <test.h>

namespace {

////////////////////////////////////////////////////////////////////////////////
// A few hard-coded symbols.
bool get(const std::string& name, Variant& value) {
Expand All @@ -42,8 +44,10 @@ bool get(const std::string& name, Variant& value) {
return true;
}

} // namespace

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest t(52);
Context context;
Context::setContext(&context);
Expand Down
2 changes: 1 addition & 1 deletion test/lexer.test.cpp → test/lexer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <vector>

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
#ifdef PRODUCT_TASKWARRIOR
UnitTest t(1255);
#else
Expand Down
2 changes: 1 addition & 1 deletion test/t.test.cpp → test/t_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <test.h>

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest test(48);
Context context;
Context::setContext(&context);
Expand Down
2 changes: 1 addition & 1 deletion test/tc.test.cpp → test/tc_cpp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::string uuid2str(tc::Uuid uuid) { return static_cast<std::string>(uuid.to_st
// Tests for the basic cxxbridge functionality. This focuses on the methods with
// complex cxxbridge implementations, rather than those with complex Rust
// implementations but simple APIs, like sync.
int main(int, char **) {
int TEST_NAME(int, char **) {
UnitTest t;
std::string str;

Expand Down
6 changes: 4 additions & 2 deletions test/tdb2.test.cpp → test/tdb2_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@

#include <iostream>

Context context;
namespace {

void cleardb() {
// Remove any residual test files.
rmdir("./extensions");
unlink("./taskchampion.sqlite3");
}

} // namespace

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest t(12);
Context context;
Context::setContext(&context);
Expand Down
2 changes: 1 addition & 1 deletion test/tw-2689.test.cpp → test/tw_2689_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <test.h>

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest test(12);

// Ensure environment has no influence.
Expand Down
2 changes: 1 addition & 1 deletion test/util.test.cpp → test/util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <iostream>

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest t(19);
Context context;
Context::setContext(&context);
Expand Down
2 changes: 1 addition & 1 deletion test/variant_add.test.cpp → test/variant_add_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#define EPSILON 0.001

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest t(80);

Variant v0(true);
Expand Down
2 changes: 1 addition & 1 deletion test/variant_and.test.cpp → test/variant_and_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <iostream>

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest t(76);

Variant v0(true);
Expand Down
2 changes: 1 addition & 1 deletion test/variant_cast.test.cpp → test/variant_cast_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#define EPSILON 0.001

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest t(81);

time_t now = time(nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#define EPSILON 0.0001

////////////////////////////////////////////////////////////////////////////////
int main(int, char**) {
int TEST_NAME(int, char**) {
UnitTest t(44);

Variant v0(true);
Expand Down
Loading

0 comments on commit c719cce

Please sign in to comment.