Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 22 additions & 2 deletions bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <catch2\/catch.hpp>/#include <catch2\/catch_all.hpp>/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}"
Expand Down
53 changes: 48 additions & 5 deletions src/xml2json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <boost/json/serialize.hpp>

#include <exception>
#include <fstream>
#include <iostream>
#include <string_view>

Expand All @@ -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<int>("Catch2TestRun.OverallResultsCases.<xmlattr>.failures");
if (failures == 0)
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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<std::string>("<xmlattr>.filename") + ":" + element.get<std::string>("<xmlattr>.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<std::string>("Expression.<xmlattr>.type")};
std::string original{trim_copy(tree.get<std::string>("Expression.Original"))};
std::string expanded{trim_copy(tree.get<std::string>("Expression.Expanded"))};
Expand Down
64 changes: 64 additions & 0 deletions tests/example-exit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<TARGET_OBJECTS:catchlib>)
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})
17 changes: 17 additions & 0 deletions tests/example-exit/example_exit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "example_exit.h"

#include <cstdlib>

namespace leap
{

bool is_leap_year(int year)
{
if (year == 2000)
{
std::exit(0);
}
return (year % 100) ? (year % 4 == 0) : (year % 400 == 0);
}

}
11 changes: 11 additions & 0 deletions tests/example-exit/example_exit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if !defined(LEAP_H)
#define LEAP_H

namespace leap
{

bool is_leap_year(int year);

}

#endif
38 changes: 38 additions & 0 deletions tests/example-exit/example_exit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "example_exit.h"
#ifdef EXERCISM_TEST_SUITE
#include <catch2/catch_all.hpp>
#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
1 change: 1 addition & 0 deletions tests/example-exit/expected_results.json
Original file line number Diff line number Diff line change
@@ -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()."}