From 32ce47128e4f5ae6476d572a92ed2d3e87a7e76c Mon Sep 17 00:00:00 2001 From: iHiD <286476+iHiD@users.noreply.github.com> Date: Fri, 27 Nov 2026 14:19:50 +0900 Subject: [PATCH 1/4] Build with Ninja, link with mold, and run the tests once Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- Dockerfile | 2 +- bin/run.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 401a488..41cae61 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM alpine:3.24.3@sha256:be2656061b536c1883c4034790e21d0a09c5aaaf51968b89368a3c0371ea2c9a -RUN apk add --no-cache coreutils g++ libc-dev cmake make git boost-dev +RUN apk add --no-cache coreutils g++ libc-dev cmake make ninja-is-really-ninja mold git boost-dev # Build Catch as a library directly inside the Docker image # since it takes a bit of time to compile it,this way we speed diff --git a/bin/run.sh b/bin/run.sh index b420940..fa17639 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -43,12 +43,64 @@ cp -R "${input_dir}/" "${build_dir}" && cd "${build_dir}" # Replace the old Catch2 v2 include line with Catch2 v3 sed -i -e 's/#include /#include /g' "${test_file_path}" -cmake -DEXERCISM_TEST_SUITE=1 -DEXERCISM_RUN_ALL_TESTS=1 . -make 2> "${compilation_errors_file_name}" +cmake_options="-DEXERCISM_TEST_SUITE=1 -DEXERCISM_RUN_ALL_TESTS=1" + +# The Docker image has Ninja and mold. The browser runner has neither, so it +# builds with make and links with the default linker as before. +# Ninja compiles the solution and the test file in parallel, where make +# compiles them one after the other. +if command -v ninja > /dev/null; then + cmake_options="${cmake_options} -G Ninja" +fi +# mold links the test executable faster than GNU ld, because it uses all cores. +if command -v mold > /dev/null; then + cmake_options="${cmake_options} -DCMAKE_LINKER_TYPE=MOLD" +fi + +cmake ${cmake_options} . + +# Build only the test executable. The default target also runs the tests, so +# building it ran every test twice: once in the build and once below to write +# the XML. +if [ -f build.ninja ]; then + # Ninja prints the compiler's output on stdout. Before each failed step it + # prints a "FAILED:" line and the step's command line, and at the end it + # prints "ninja: build stopped". The sed script removes those lines and + # stops at a second failed step, because make stopped at the first one. + ninja --quiet "${slug}" 2>&1 | sed -n \ + -e '/^FAILED: /{x;/^seen$/q;s/.*/seen/;x;n;d;}' \ + -e '/^ninja: build stopped: /d' \ + -e p > "${compilation_errors_file_name}" +else + make "${slug}" 2> "${compilation_errors_file_name}" +fi + +parse_results() { + /opt/test-runner/bin/exercism_parser "${build_dir}/${test_output_file_name}" "${results_file}" "${build_dir}/${compilation_errors_file_name}" "${test_file_path}" +} # In case of compilation errors the executable will not be created [ -f "./${slug}" ] && chmod +x "./${slug}" && "./${slug}" -r xml -o "${test_output_file_name}" -/opt/test-runner/bin/exercism_parser "${build_dir}/${test_output_file_name}" "${results_file}" "${build_dir}/${compilation_errors_file_name}" "${test_file_path}" +parse_results + +# If the tests crashed or exited early, the parser reports an error and shows +# the compilation errors file as the message. While Catch2 writes the XML, it +# holds back the tests' own output. So this runs the tests again with the +# console reporter, which adds their error output and the shell's note of any +# crash to that file, as the test run in the build did before. If the tests +# called exit, the shell adds no note, so this adds one with the exit status. +if [ -f "./${slug}" ]; then + case "$(cat "${results_file}")" in + *'"status": "error"'*) + "./${slug}" > /dev/null 2>> "${compilation_errors_file_name}" + exit_status=$? + if [ "${exit_status}" -lt 128 ]; then + echo "The tests exited early with status ${exit_status}." >> "${compilation_errors_file_name}" + fi + parse_results + ;; + esac +fi cd - From fb7ad69c033f8c7af5a4a73823e7a293c32cd284 Mon Sep 17 00:00:00 2001 From: iHiD <286476+iHiD@users.noreply.github.com> Date: Sun, 29 Nov 2026 17:28:36 +0900 Subject: [PATCH 2/4] Say how the tests ended when they crash or exit early On the make path in the browser, bash writes its crash note to the script's stderr, so an aborted test run left the message empty. The runner now writes its own note in both shells. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- bin/run.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bin/run.sh b/bin/run.sh index fa17639..553cbb8 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -86,15 +86,18 @@ parse_results # If the tests crashed or exited early, the parser reports an error and shows # the compilation errors file as the message. While Catch2 writes the XML, it # holds back the tests' own output. So this runs the tests again with the -# console reporter, which adds their error output and the shell's note of any -# crash to that file, as the test run in the build did before. If the tests -# called exit, the shell adds no note, so this adds one with the exit status. +# console reporter, which adds their error output to that file, as the test +# run in the build did before. Then it adds a note of how the tests ended. +# The subshell keeps the shell's own crash note out of the file, because +# busybox sh would write it there and bash would not. if [ -f "./${slug}" ]; then case "$(cat "${results_file}")" in *'"status": "error"'*) - "./${slug}" > /dev/null 2>> "${compilation_errors_file_name}" + (exec "./${slug}" > /dev/null 2>> "${compilation_errors_file_name}") exit_status=$? - if [ "${exit_status}" -lt 128 ]; then + if [ "${exit_status}" -ge 128 ]; then + echo "The tests crashed with signal SIG$(kill -l "${exit_status}")." >> "${compilation_errors_file_name}" + else echo "The tests exited early with status ${exit_status}." >> "${compilation_errors_file_name}" fi parse_results From ed57280154be53b0729203b805ca7dfc210be8af Mon Sep 17 00:00:00 2001 From: iHiD <286476+iHiD@users.noreply.github.com> Date: Mon, 30 Nov 2026 07:55:27 +0900 Subject: [PATCH 3/4] Shorten the comment above the Ninja and mold options Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- bin/run.sh | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/bin/run.sh b/bin/run.sh index 553cbb8..9812858 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -45,14 +45,10 @@ sed -i -e 's/#include /#include /g' "$ cmake_options="-DEXERCISM_TEST_SUITE=1 -DEXERCISM_RUN_ALL_TESTS=1" -# The Docker image has Ninja and mold. The browser runner has neither, so it -# builds with make and links with the default linker as before. -# Ninja compiles the solution and the test file in parallel, where make -# compiles them one after the other. +# Use ninja and mold for performance when available (not present in browser runner). if command -v ninja > /dev/null; then cmake_options="${cmake_options} -G Ninja" fi -# mold links the test executable faster than GNU ld, because it uses all cores. if command -v mold > /dev/null; then cmake_options="${cmake_options} -DCMAKE_LINKER_TYPE=MOLD" fi From fa510f29e620dc019debb2f0f6df77a6bf2f9c1b Mon Sep 17 00:00:00 2001 From: iHiD <286476+iHiD@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:26:42 +0900 Subject: [PATCH 4/4] Report crashing tests from the XML and drop the second test run The parser now reads Catch2's FatalErrorCondition and Exception elements, so a test that crashes or throws outside an assertion fails with Catch2's message, and the tests before it keep their results. If the XML stops part way (exit() in Docker, or any crash in the browser), the parser says so. run.sh no longer runs the tests a second time, and it keeps the raw output of Ninja or make with -j. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- bin/run.sh | 45 +++-------------- src/xml2json.cpp | 53 ++++++++++++++++++-- tests/example-exit/CMakeLists.txt | 64 ++++++++++++++++++++++++ tests/example-exit/example_exit.cpp | 17 +++++++ tests/example-exit/example_exit.h | 11 ++++ tests/example-exit/example_exit_test.cpp | 38 ++++++++++++++ tests/example-exit/expected_results.json | 1 + 7 files changed, 186 insertions(+), 43 deletions(-) create mode 100644 tests/example-exit/CMakeLists.txt create mode 100644 tests/example-exit/example_exit.cpp create mode 100644 tests/example-exit/example_exit.h create mode 100644 tests/example-exit/example_exit_test.cpp create mode 100644 tests/example-exit/expected_results.json diff --git a/bin/run.sh b/bin/run.sh index 9812858..0330d50 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -57,49 +57,18 @@ cmake ${cmake_options} . # Build only the test executable. The default target also runs the tests, so # building it ran every test twice: once in the build and once below to write -# the XML. +# the XML. The browser runner's cmake has no --build, so this calls the build +# tool directly. Ninja prints the compiler's messages on stdout, so both +# outputs go to the file. if [ -f build.ninja ]; then - # Ninja prints the compiler's output on stdout. Before each failed step it - # prints a "FAILED:" line and the step's command line, and at the end it - # prints "ninja: build stopped". The sed script removes those lines and - # stops at a second failed step, because make stopped at the first one. - ninja --quiet "${slug}" 2>&1 | sed -n \ - -e '/^FAILED: /{x;/^seen$/q;s/.*/seen/;x;n;d;}' \ - -e '/^ninja: build stopped: /d' \ - -e p > "${compilation_errors_file_name}" + ninja "${slug}" else - make "${slug}" 2> "${compilation_errors_file_name}" -fi - -parse_results() { - /opt/test-runner/bin/exercism_parser "${build_dir}/${test_output_file_name}" "${results_file}" "${build_dir}/${compilation_errors_file_name}" "${test_file_path}" -} + make -j"$(nproc)" "${slug}" +fi > "${compilation_errors_file_name}" 2>&1 # In case of compilation errors the executable will not be created [ -f "./${slug}" ] && chmod +x "./${slug}" && "./${slug}" -r xml -o "${test_output_file_name}" -parse_results - -# If the tests crashed or exited early, the parser reports an error and shows -# the compilation errors file as the message. While Catch2 writes the XML, it -# holds back the tests' own output. So this runs the tests again with the -# console reporter, which adds their error output to that file, as the test -# run in the build did before. Then it adds a note of how the tests ended. -# The subshell keeps the shell's own crash note out of the file, because -# busybox sh would write it there and bash would not. -if [ -f "./${slug}" ]; then - case "$(cat "${results_file}")" in - *'"status": "error"'*) - (exec "./${slug}" > /dev/null 2>> "${compilation_errors_file_name}") - exit_status=$? - if [ "${exit_status}" -ge 128 ]; then - echo "The tests crashed with signal SIG$(kill -l "${exit_status}")." >> "${compilation_errors_file_name}" - else - echo "The tests exited early with status ${exit_status}." >> "${compilation_errors_file_name}" - fi - parse_results - ;; - esac -fi +/opt/test-runner/bin/exercism_parser "${build_dir}/${test_output_file_name}" "${results_file}" "${build_dir}/${compilation_errors_file_name}" "${test_file_path}" cd - diff --git a/src/xml2json.cpp b/src/xml2json.cpp index 5a25ecb..40eac46 100644 --- a/src/xml2json.cpp +++ b/src/xml2json.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -27,14 +28,37 @@ std::string restrict_msg_length(std::string uncut_msg) { void Output_message::load_from_catch2_xml(const std::string &xml_test_output_file, const std::string &compilation_error_file) { + // The test executable writes the XML file. If there is no file, the + // executable wasn't built, so the compiler's output is the message. + std::ifstream xml_file(xml_test_output_file); + if (!xml_file) + { + status = "error"; + message = restrict_msg_length(read_file(compilation_error_file)); + return; + } + // Create an empty property tree object pt::ptree tree; - // Parse the XML into the property tree. + // Catch2 finishes the XML file even when a test crashes, because it catches + // the signal. If the file stops part way, the tests ended without Catch2, + // for example because the code called exit(), or because it crashed in the + // browser runner, where Catch2 can't catch signals. try { - pt::read_xml(xml_test_output_file, tree); + pt::read_xml(xml_file, tree); + } + catch (const pt::xml_parser_error &) + { + status = "error"; + message = "The tests stopped before they finished, so there are no test results. " + "This can happen if the code calls exit() or crashes."; + return; + } + try + { // Check if all test have succeded int failures = tree.get("Catch2TestRun.OverallResultsCases..failures"); if (failures == 0) @@ -109,10 +133,9 @@ void Output_message::load_from_catch2_xml(const std::string &xml_test_output_fil } catch (std::exception &e) { - // Checks if xml output file exists, in case it doesn't - // compilation must have failed and tests can't be run. + // The XML has a shape this parser doesn't know. status = "error"; - message = restrict_msg_length(read_file(compilation_error_file)); + message = std::string{"The test runner could not read the test results: "} + e.what(); } } @@ -169,10 +192,30 @@ void Output_message::save_as_exercism_json(const std::string &filename) } } +// The "file:line" of a Catch2 XML element, followed by a newline. +static std::string location(const pt::ptree &element) +{ + return element.get(".filename") + ":" + element.get(".line") + "\n"; +} + std::string Output_message::build_test_message(const pt::ptree &tree) { // V2 template: // \nFAILED:\n REQUIRE( leap::is_leap_year(2000) )\nwith expansion:\n false\nat /tmp/example-partial-fail/example_partial_fail_test.cpp:31\n + // A crash (such as a segmentation fault) ends the test, so report it first. + if (const auto fatal = tree.get_child_optional("FatalErrorCondition")) + { + return "\nFAILED:\n due to a fatal error condition:\n " + trim_copy(fatal->data()) + + "\nat " + location(*fatal); + } + // An exception thrown outside an assertion. + if (!tree.get_child_optional("Expression")) + { + const auto &exception = tree.get_child("Exception"); + return "\nFAILED:\n due to unexpected exception with message:\n " + trim_copy(exception.data()) + + "\nat " + location(exception); + } + std::string testing_type{tree.get("Expression..type")}; std::string original{trim_copy(tree.get("Expression.Original"))}; std::string expanded{trim_copy(tree.get("Expression.Expanded"))}; diff --git a/tests/example-exit/CMakeLists.txt b/tests/example-exit/CMakeLists.txt new file mode 100644 index 0000000..3b0589f --- /dev/null +++ b/tests/example-exit/CMakeLists.txt @@ -0,0 +1,64 @@ +# Get the exercise name from the current directory +get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME) + +# Basic CMake project +cmake_minimum_required(VERSION 3.5.1) + +# Name the project after the exercise +project(${exercise} CXX) + +# Get a source filename from the exercise name by replacing -'s with _'s +string(REPLACE "-" "_" file ${exercise}) + +# Implementation could be only a header +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp) + set(exercise_cpp ${file}.cpp) +else() + set(exercise_cpp "") +endif() + +# Use the common Catch library? +if(EXERCISM_COMMON_CATCH) + # For Exercism track development only + add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $) +elseif(EXERCISM_TEST_SUITE) + # The Exercism test suite is being run, the Docker image already + # includes a pre-built version of Catch. + find_package(Catch2 REQUIRED) + add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h) + target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain) + # When Catch is installed system wide we need to include a different + # header, we need this define to use the correct one. + target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE) +else() + # Build executable from sources and headers + add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp) +endif() + +set_target_properties(${exercise} PROPERTIES + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED OFF + CXX_EXTENSIONS OFF +) + +set(CMAKE_BUILD_TYPE Debug) + +if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)") + set_target_properties(${exercise} PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror" + ) +endif() + +# Configure to run all the tests? +if(${EXERCISM_RUN_ALL_TESTS}) + target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS) +endif() + +# Tell MSVC not to warn us about unchecked iterators in debug builds +if(${MSVC}) + set_target_properties(${exercise} PROPERTIES + COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS) +endif() + +# Run the tests on every build +add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise}) diff --git a/tests/example-exit/example_exit.cpp b/tests/example-exit/example_exit.cpp new file mode 100644 index 0000000..fcf1200 --- /dev/null +++ b/tests/example-exit/example_exit.cpp @@ -0,0 +1,17 @@ +#include "example_exit.h" + +#include + +namespace leap +{ + + bool is_leap_year(int year) + { + if (year == 2000) + { + std::exit(0); + } + return (year % 100) ? (year % 4 == 0) : (year % 400 == 0); + } + +} diff --git a/tests/example-exit/example_exit.h b/tests/example-exit/example_exit.h new file mode 100644 index 0000000..05070c6 --- /dev/null +++ b/tests/example-exit/example_exit.h @@ -0,0 +1,11 @@ +#if !defined(LEAP_H) +#define LEAP_H + +namespace leap +{ + + bool is_leap_year(int year); + +} + +#endif diff --git a/tests/example-exit/example_exit_test.cpp b/tests/example-exit/example_exit_test.cpp new file mode 100644 index 0000000..1717205 --- /dev/null +++ b/tests/example-exit/example_exit_test.cpp @@ -0,0 +1,38 @@ +#include "example_exit.h" +#ifdef EXERCISM_TEST_SUITE +#include +#else +#include "test/catch.hpp" +#endif + +TEST_CASE("not_divisible_by_4") +{ + REQUIRE(!leap::is_leap_year(2015)); +} + +#if defined(EXERCISM_RUN_ALL_TESTS) +TEST_CASE("divisible_by_2_not_divisible_by_4") +{ + REQUIRE(!leap::is_leap_year(1970)); +} + +TEST_CASE("divisible_by_4_not_divisible_by_100") +{ + REQUIRE(leap::is_leap_year(1996)); +} + +TEST_CASE("divisible_by_100_not_divisible_by_400") +{ + REQUIRE(!leap::is_leap_year(2100)); +} + +TEST_CASE("divisible_by_400") +{ + REQUIRE(leap::is_leap_year(2000)); +} + +TEST_CASE("divisible_by_200_not_divisible_by_400") +{ + REQUIRE(!leap::is_leap_year(1800)); +} +#endif diff --git a/tests/example-exit/expected_results.json b/tests/example-exit/expected_results.json new file mode 100644 index 0000000..807ca02 --- /dev/null +++ b/tests/example-exit/expected_results.json @@ -0,0 +1 @@ +{"version": 3, "status": "error", "message": "The tests stopped before they finished, so there are no test results. This can happen if the code calls exit() or std::exit()."} \ No newline at end of file