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..0330d50 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -43,8 +43,28 @@ 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" + +# 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 +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. 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 "${slug}" +else + 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}" 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