Skip to content

Commit

Permalink
Add tests attempting to load shipped rulesets
Browse files Browse the repository at this point in the history
It's bad when they don't read, and we may not notice for a while.
See longturn#380 (not a real autogames but a good starting point) and longturn#868.
  • Loading branch information
lmoureaux committed Dec 27, 2023
1 parent 434d7ef commit a5807a7
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmake/FreecivDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,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()
11 changes: 11 additions & 0 deletions data/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
set(RULESET_LIST)

function(add_ruleset)
cmake_parse_arguments(ARG "SERV_FILE" "NAME;README" "" ${ARGN})
if (ARG_SERV_FILE)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions data/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
60 changes: 60 additions & 0 deletions data/tests/rulesets.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <QProcess>

#include <QtTest>

// Macro values replaced by cmake
#define RULESETS "$<JOIN:${RULESET_LIST},", ">"
#define SERVER_PATH "$<TARGET_FILE:freeciv21-server>"

/**
* 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<QString>("name");
QTest::addColumn<bool>("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"

0 comments on commit a5807a7

Please sign in to comment.