From e0e0a7286275b55c3a76c1ef0efffce6f71f327b Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Sat, 5 Feb 2022 18:34:18 +0100 Subject: [PATCH] Add tests attempting to load shipped rulesets It's bad when they don't read, and we may not notice for a while. See #380 (not a real autogames but a good starting point) and #868. --- cmake/FreecivDependencies.cmake | 6 ++++ data/CMakeLists.txt | 11 ++++++ data/tests/CMakeLists.txt | 19 +++++++++++ data/tests/rulesets.cpp.in | 60 +++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 data/tests/CMakeLists.txt create mode 100644 data/tests/rulesets.cpp.in diff --git a/cmake/FreecivDependencies.cmake b/cmake/FreecivDependencies.cmake index bf960eb052..06c2b96f06 100644 --- a/cmake/FreecivDependencies.cmake +++ b/cmake/FreecivDependencies.cmake @@ -164,3 +164,9 @@ endif() if (FREECIV_ENABLE_FCMP_CLI OR FREECIV_ENABLE_FCMP_QT) find_package(SQLite3 REQUIRED) endif() + +# Testing +include(CTest) +if (BUILD_TESTING) + find_package(Qt5Test REQUIRED) +endif() diff --git a/data/CMakeLists.txt b/data/CMakeLists.txt index bd4723f7de..48ceb9ea9f 100644 --- a/data/CMakeLists.txt +++ b/data/CMakeLists.txt @@ -1,3 +1,5 @@ +set(RULESET_LIST) + function(add_ruleset) cmake_parse_arguments(ARG "SERV_FILE" "NAME;README" "" ${ARGN}) if (ARG_SERV_FILE) @@ -12,6 +14,8 @@ function(add_ruleset) DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/${ARG_NAME}" COMPONENT freeciv21) endif() + list(APPEND RULESET_LIST ${ARG_NAME}) + set(RULESET_LIST "${RULESET_LIST}" PARENT_SCOPE) install( FILES ${ARG_NAME}/buildings.ruleset @@ -75,6 +79,13 @@ if (FREECIV_ENABLE_SERVER OR FREECIV_ENABLE_RULEDIT) FILES override/nation.ruleset DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/override" COMPONENT freeciv21) + + # Ruleset tests + if (BUILD_TESTING AND FREECIV_ENABLE_SERVER) + # The stub ruleset isn't installed, but we test it + list(APPEND RULESET_LIST stub) + add_subdirectory(tests) + endif() endif() if (FREECIV_ENABLE_SERVER) diff --git a/data/tests/CMakeLists.txt b/data/tests/CMakeLists.txt new file mode 100644 index 0000000000..5dde27d2b0 --- /dev/null +++ b/data/tests/CMakeLists.txt @@ -0,0 +1,19 @@ +set(CMAKE_AUTOMOC ON) + +enable_testing(true) + +# Turn the list of rulesets into something C++ will understand +list(JOIN RULESET_LIST "\", \"" ruleset_list_cpp) +if (NOT ${ruleset_list_cpp} STREQUAL "") + set(ruleset_list_cpp "\"${ruleset_list_cpp}\"") +endif() + +# Create the test file. This is ugly because we need to replace variables +# and use generator expressions. +configure_file(rulesets.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/rulesets.cpp.in) +file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/rulesets.cpp + INPUT ${CMAKE_CURRENT_BINARY_DIR}/rulesets.cpp.in) + +add_executable(test_rulesets ${CMAKE_CURRENT_BINARY_DIR}/rulesets.cpp) +add_test(NAME test_rulesets COMMAND test_rulesets) +target_link_libraries(test_rulesets PRIVATE Qt5::Test) diff --git a/data/tests/rulesets.cpp.in b/data/tests/rulesets.cpp.in new file mode 100644 index 0000000000..bc597c9875 --- /dev/null +++ b/data/tests/rulesets.cpp.in @@ -0,0 +1,60 @@ +#include + +#include + +// Macro values replaced by cmake +#define RULESETS "$" +#define SERVER_PATH "$" + +/** + * Ruleset-related tests + */ +class test_rulesets : public QObject { + Q_OBJECT + +private slots: + void load_data(); + void load(); +}; + +/** + * Generates test data for load() + */ +void test_rulesets::load_data() +{ + QTest::addColumn("name"); + QTest::addColumn("exists"); + + const char *names[] = {"default", RULESETS}; + for (auto &name : names) { + QTest::newRow(name) << name << true; + } + + // A ruleset that doesn't exist + QTest::newRow("error") << "error" << false; +} + +/** + * Tries to spawn a server with a ruleset. + */ +void test_rulesets::load() +{ + QFETCH(QString, name); + QFETCH(bool, exists); + + QProcess p; + p.start(SERVER_PATH, {QStringLiteral("-r"), name}); + p.waitForStarted(); + p.write("quit\n"); + p.closeWriteChannel(); + p.waitForFinished(5000); + + if (exists) { + QCOMPARE(p.exitCode(), 0); + } else { + QCOMPARE(p.exitCode(), 1); + } +} + +QTEST_MAIN(test_rulesets) +#include "rulesets.moc"