Skip to content

Commit

Permalink
Uses compile definitions to avoid having to source install when runni…
Browse files Browse the repository at this point in the history
…ng the tests. (#219)

Signed-off-by: Franco Cipollone <franco.c@ekumenlabs.com>
  • Loading branch information
francocipollone committed Jun 13, 2022
1 parent 65e42b0 commit 15a209c
Show file tree
Hide file tree
Showing 17 changed files with 379 additions and 140 deletions.

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions src/utility/resources.cc
Expand Up @@ -65,10 +65,17 @@ std::string AppendPath(const std::string& base_path, const std::string& relative
} // namespace

std::string FindResourceInPath(const std::string& resource_name, const std::string& path_to_resources) {
const std::string file_path = AppendPath(path_to_resources, resource_name);
if (std::ifstream(file_path)) {
return file_path;
}
throw std::runtime_error(std::string("Resource: ") + resource_name + std::string(" couldn't be found."));
}

std::string FindResourceInEnvPath(const std::string& resource_name, const std::string& path_to_resources) {
const std::vector<std::string> env_paths = GetAllPathDirectories(path_to_resources);
const std::string resources_folder{"resources"};
for (const std::string& env_path : env_paths) {
const std::string file_path = AppendPath(env_path, AppendPath(resources_folder, resource_name));
const std::string file_path = AppendPath(env_path, resource_name);
if (std::ifstream(file_path)) {
return file_path;
}
Expand All @@ -77,7 +84,8 @@ std::string FindResourceInPath(const std::string& resource_name, const std::stri
}

std::string FindResource(const std::string& resource_name) {
return FindResourceInPath(resource_name, kMalidriveEnvVariable);
const std::string resources_folder{"resources"};
return FindResourceInEnvPath(AppendPath(resources_folder, resource_name), kMalidriveEnvVariable);
}

} // namespace utility
21 changes: 15 additions & 6 deletions src/utility/resources.h
Expand Up @@ -48,14 +48,23 @@ std::string FindResource(const std::string& resource_name);

// Finds a resource file by its `resource_name` name.
//
// The first valid file within path_directory/`resource_name` is returned.
// Where path_directory is indicated by the path that the env variable `path_to_resources` holds.
// Path delimeter is assumed to be `/`.
// The first valid `resource_name` file within the paths pointed by the environment variable `path_to_resources` is
// returned. Path delimeter is assumed to be `/`.
//
// @param resource_name The name of the file.
// @param path_to_resources Env variable holding a directory path.
// @param path_to_resources Env variable holding one or many directory path.
// @return The complete file path to the resource.
// @throws std::runtime_error When the resource cannot be found under any
// of the possible combinations of 'path_directory/resource_name'.
// @throws std::runtime_error When the resource cannot be found for any path.
std::string FindResourceInEnvPath(const std::string& resource_name, const std::string& path_to_resources);

// Finds a resource file by its `resource_name` name.
//
// The first valid `resource_name` file within `path_to_resources`.
//
// @param resource_name The name of the file.
// @param path_to_resources Env variable holding one or many directory path.
// @return The complete file path to the resource.
// @throws std::runtime_error When the resource cannot be found for any path.
std::string FindResourceInPath(const std::string& resource_name, const std::string& path_to_resources);

} // namespace utility
61 changes: 53 additions & 8 deletions test/CMakeLists.txt
Expand Up @@ -17,6 +17,8 @@ macro(maliput_malidrive_plugin_tests)
foreach(GTEST_SOURCE_file ${ARGN})
string(REGEX REPLACE ".cc" "" BINARY_NAME ${GTEST_SOURCE_file})
set(BINARY_NAME ${TEST_TYPE}_${BINARY_NAME})
set(RESOURCE_FILE_PREFIX ${PROJECT_SOURCE_DIR}/resources/)
set(ROAD_NETWORK_PLUGIN ${CMAKE_INSTALL_PREFIX}/lib/plugins/)

ament_add_gtest(${BINARY_NAME} ${GTEST_SOURCE_file} APPEND_LIBRARY_DIRS ${MALIPUT_MALIDRIVE_PLUGIN_LIBRARY_DIRS}
TIMEOUT 240)
Expand Down Expand Up @@ -46,6 +48,12 @@ macro(maliput_malidrive_plugin_tests)
# Remove a warning in GTest.
target_compile_options(${BINARY_NAME} PRIVATE "-Wno-sign-compare")

target_compile_definitions(${BINARY_NAME}
PRIVATE
DEF_MALIDRIVE_RESOURCES="${RESOURCE_FILE_PREFIX}"
DEF_ROAD_NETWORK_PLUGIN="${ROAD_NETWORK_PLUGIN}"
)

endforeach()
endmacro()

Expand All @@ -54,14 +62,7 @@ macro(maliput_malidrive_build_tests)
foreach(GTEST_SOURCE_file ${ARGN})
string(REGEX REPLACE ".cc" "" BINARY_NAME ${GTEST_SOURCE_file})
set(BINARY_NAME ${TEST_TYPE}_${BINARY_NAME})

# When integration tests are disabled, we just skip adding them.
if(NOT MALIDRIVE_INTEGRATION_TESTS)
if(${TEST_TYPE} MATCHES "INTEGRATION")
message(STATUS "Integration tests are disarmed, skipping: " ${BINARY_NAME})
continue()
endif()
endif()
set(RESOURCE_FILE_PREFIX ${PROJECT_SOURCE_DIR}/resources/)

ament_add_gtest(${BINARY_NAME} ${GTEST_SOURCE_file} TIMEOUT 240)

Expand Down Expand Up @@ -94,6 +95,50 @@ macro(maliput_malidrive_build_tests)
# Remove a warning in GTest.
target_compile_options(${BINARY_NAME} PRIVATE "-Wno-sign-compare")

target_compile_definitions(${BINARY_NAME}
PRIVATE
DEF_MALIDRIVE_RESOURCES="${RESOURCE_FILE_PREFIX}"
)

endforeach()
endmacro()

macro(maliput_malidrive_utility_tests)
# Build all the tests
foreach(GTEST_SOURCE_file ${ARGN})
string(REGEX REPLACE ".cc" "" BINARY_NAME ${GTEST_SOURCE_file})
set(BINARY_NAME ${TEST_TYPE}_${BINARY_NAME})

ament_add_gtest(${BINARY_NAME} ${GTEST_SOURCE_file} APPEND_LIBRARY_DIRS ${MALIPUT_MALIDRIVE_PLUGIN_LIBRARY_DIRS}
TIMEOUT 240)

target_include_directories(${BINARY_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/include
${PYTHON_INCLUDE_DIRS}
)

# Kind of an ugly catch-all bucket
target_link_libraries(${BINARY_NAME}
utility
)

# To avoid a false positive when running ubsan the symbols must be exported
# See https://stackoverflow.com/questions/57361776/use-ubsan-with-dynamically-loaded-shared-libraries
set_target_properties(${BINARY_NAME}
PROPERTIES
ENABLE_EXPORTS ON
)

# Remove a warning in GTest.
target_compile_options(${BINARY_NAME} PRIVATE "-Wno-sign-compare")

target_compile_definitions(${BINARY_NAME}
PRIVATE
DEF_PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}"
)

endforeach()
endmacro()

Expand Down
1 change: 1 addition & 0 deletions test/regression/CMakeLists.txt
Expand Up @@ -8,6 +8,7 @@ add_subdirectory(common)
add_subdirectory(loader)
add_subdirectory(plugin)
add_subdirectory(road_curve)
add_subdirectory(utility)
add_subdirectory(xodr)

##############################################################################
Expand Down
9 changes: 6 additions & 3 deletions test/regression/base/road_geometry_test.cc
Expand Up @@ -53,6 +53,9 @@ namespace malidrive {
namespace tests {
namespace {

// Resource folder path defined via compile definition.
static constexpr char kMalidriveResourceFolder[] = DEF_MALIDRIVE_RESOURCES;

std::unique_ptr<road_curve::Function> MakeZeroCubicPolynomial(double p0, double p1, double linear_tolerance) {
return std::make_unique<road_curve::CubicPolynomial>(0., 0., 0., 0., p0, p1, linear_tolerance);
}
Expand All @@ -77,8 +80,8 @@ class RoadGeometryTest : public ::testing::Test {
MakeZeroCubicPolynomial(kP0, kP1, kLinearTolerance), MakeZeroCubicPolynomial(kP0, kP1, kLinearTolerance),
kAssertContiguity);
std::unique_ptr<road_curve::Function> reference_line_offset = MakeZeroCubicPolynomial(kP0, kP1, kLinearTolerance);
std::unique_ptr<xodr::DBManager> manager =
xodr::LoadDataBaseFromFile(utility::FindResource("odr/SingleLane.xodr"), kParserConfiguration);
std::unique_ptr<xodr::DBManager> manager = xodr::LoadDataBaseFromFile(
utility::FindResourceInPath("SingleLane.xodr", kMalidriveResourceFolder), kParserConfiguration);
};

// Tests getters and the constructor of an empty RoadGeometry.
Expand Down Expand Up @@ -145,7 +148,7 @@ class RoadGeometryFigure8Trafficlights : public ::testing::Test {
void SetUp() override {
road_geometry_configuration_.id = maliput::api::RoadGeometryId("figure8_trafficlights");
road_geometry_configuration_.opendrive_file =
utility::FindResource("odr/figure8_trafficlights/figure8_trafficlights.xodr");
utility::FindResourceInPath("figure8_trafficlights/figure8_trafficlights.xodr", kMalidriveResourceFolder);
road_network_ =
::malidrive::loader::Load<::malidrive::builder::RoadNetworkBuilder>(road_geometry_configuration_.ToStringMap());
}
Expand Down
6 changes: 5 additions & 1 deletion test/regression/builder/builder_tools_test.cc
Expand Up @@ -45,6 +45,9 @@ namespace builder {
namespace test {
namespace {

// Resource folder path defined via compile definition.
static constexpr char kMalidriveResourceFolder[] = DEF_MALIDRIVE_RESOURCES;

// TODO(maliput#341): Remove this operator once it is implemented in maliput.
bool operator==(const maliput::api::LaneEnd& end_a, const maliput::api::LaneEnd& end_b) {
return end_a.end == end_b.end && end_a.lane == end_b.lane;
Expand Down Expand Up @@ -246,7 +249,8 @@ class LanePropertiesTest : public ::testing::Test {

protected:
void SetUp() override {
manager_ = xodr::LoadDataBaseFromFile(utility::FindResource("odr/BikingLineLane.xodr"), {std::nullopt});
manager_ = xodr::LoadDataBaseFromFile(utility::FindResourceInPath("BikingLineLane.xodr", kMalidriveResourceFolder),
{std::nullopt});
road_header_ = &manager_->GetRoadHeaders().at(xodr::RoadHeader::Id(kRoadId));
}
std::unique_ptr<xodr::DBManager> manager_;
Expand Down
19 changes: 13 additions & 6 deletions test/regression/builder/phase_provider_builder_test.cc
Expand Up @@ -58,6 +58,9 @@ using maliput::api::rules::Phase;
using maliput::api::rules::PhaseProvider;
using maliput::api::rules::PhaseRing;

// Resource folder path defined via compile definition.
static constexpr char kMalidriveResourceFolder[] = DEF_MALIDRIVE_RESOURCES;

// Creates the stack of entities that are necessary to populate the ManualPhaseProvider through the PhaseProviderBuilder
// functor.
class PhaseProviderBuilderTest : public ::testing::Test {
Expand All @@ -74,12 +77,16 @@ class PhaseProviderBuilderTest : public ::testing::Test {
}

protected:
const std::string map_id{"odr/figure8_trafficlights/figure8_trafficlights"};
const std::string xodr_file_path{utility::FindResource(map_id + ".xodr")};
const std::string rule_registry_path{utility::FindResource(map_id + "_new_rules.yaml")};
const std::string road_rulebook_path{utility::FindResource(map_id + "_new_rules.yaml")};
const std::string traffic_light_path{utility::FindResource(map_id + "_new_rules.yaml")};
const std::string phase_ring_book_path{utility::FindResource(map_id + "_new_rules.yaml")};
const std::string map_id{"figure8_trafficlights/figure8_trafficlights"};
const std::string xodr_file_path{utility::FindResourceInPath(map_id + ".xodr", kMalidriveResourceFolder)};
const std::string rule_registry_path{
utility::FindResourceInPath(map_id + "_new_rules.yaml", kMalidriveResourceFolder)};
const std::string road_rulebook_path{
utility::FindResourceInPath(map_id + "_new_rules.yaml", kMalidriveResourceFolder)};
const std::string traffic_light_path{
utility::FindResourceInPath(map_id + "_new_rules.yaml", kMalidriveResourceFolder)};
const std::string phase_ring_book_path{
utility::FindResourceInPath(map_id + "_new_rules.yaml", kMalidriveResourceFolder)};
const RoadGeometryConfiguration road_geometry_configuration_{RoadGeometryConfiguration::FromMap({
{"opendrive_file", xodr_file_path},
{"omit_nondrivable_lanes", "false"},
Expand Down
67 changes: 40 additions & 27 deletions test/regression/builder/road_geometry_builder_test.cc
Expand Up @@ -57,6 +57,9 @@ using maliput::api::test::IsRBoundsClose;

using malidrive::test::GetRoadGeometryConfigurationFor;

// Resource folder path defined via compile definition.
static constexpr char kMalidriveResourceFolder[] = DEF_MALIDRIVE_RESOURCES;

/*
Loaded map has the following structure:
Expand All @@ -77,8 +80,9 @@ class BuilderTestSingleLane : public ::testing::Test {
road_geometry_configuration_.tolerances.linear_tolerance = kLinearTolerance;
road_geometry_configuration_.tolerances.max_linear_tolerance = std::nullopt;
road_geometry_configuration_.tolerances.angular_tolerance = kAngularTolerance;
manager_ = xodr::LoadDataBaseFromFile(utility::FindResource(road_geometry_configuration_.opendrive_file),
{kLinearTolerance});
manager_ = xodr::LoadDataBaseFromFile(
utility::FindResourceInPath(road_geometry_configuration_.opendrive_file, kMalidriveResourceFolder),
{kLinearTolerance});
}

RoadGeometryConfiguration road_geometry_configuration_{GetRoadGeometryConfigurationFor("SingleLane.xodr").value()};
Expand Down Expand Up @@ -372,8 +376,9 @@ class RoadGeometryBuilderBaseTest : public ::testing::TestWithParam<RoadGeometry
road_geometry_configuration_.tolerances.linear_tolerance = kLinearTolerance;
road_geometry_configuration_.tolerances.max_linear_tolerance = std::nullopt;
road_geometry_configuration_.tolerances.angular_tolerance = kAngularTolerance;
manager_ = xodr::LoadDataBaseFromFile(utility::FindResource(road_geometry_configuration_.opendrive_file),
{kLinearTolerance});
manager_ = xodr::LoadDataBaseFromFile(
utility::FindResourceInPath(road_geometry_configuration_.opendrive_file, kMalidriveResourceFolder),
{kLinearTolerance});
}

// Tests Junction, Segments and Lanes properties.
Expand Down Expand Up @@ -883,8 +888,9 @@ class BuilderBranchPointTest : public ::testing::TestWithParam<BuilderBranchPoin
road_geometry_configuration_.tolerances.linear_tolerance = kLinearTolerance;
road_geometry_configuration_.tolerances.max_linear_tolerance = std::nullopt;
road_geometry_configuration_.tolerances.angular_tolerance = kAngularTolerance;
auto manager = xodr::LoadDataBaseFromFile(utility::FindResource(road_geometry_configuration_.opendrive_file),
{kLinearTolerance});
auto manager = xodr::LoadDataBaseFromFile(
utility::FindResourceInPath(road_geometry_configuration_.opendrive_file, kMalidriveResourceFolder),
{kLinearTolerance});
rg_ = builder::RoadGeometryBuilder(std::move(manager), road_geometry_configuration_)();
expected_connections = GetParam().expected_connections;
}
Expand Down Expand Up @@ -996,8 +1002,9 @@ class RoadGeometryBuilderSurfaceBoundariesTest : public ::testing::TestWithParam
road_geometry_configuration_.tolerances.max_linear_tolerance = std::nullopt;
road_geometry_configuration_.tolerances.angular_tolerance = kAngularTolerance;
dut_ = builder::RoadGeometryBuilder(
xodr::LoadDataBaseFromFile(utility::FindResource(road_geometry_configuration_.opendrive_file),
{kLinearTolerance}),
xodr::LoadDataBaseFromFile(
utility::FindResourceInPath(road_geometry_configuration_.opendrive_file, kMalidriveResourceFolder),
{kLinearTolerance}),
road_geometry_configuration_)();
}

Expand Down Expand Up @@ -1108,8 +1115,9 @@ class RoadGeometryOmittingNonDrivableLanesTest

// Load RoadGeometry.
dut_ = builder::RoadGeometryBuilder(
xodr::LoadDataBaseFromFile(utility::FindResource(road_geometry_configuration_.opendrive_file),
{road_geometry_configuration_.tolerances.linear_tolerance.value()}),
xodr::LoadDataBaseFromFile(
utility::FindResourceInPath(road_geometry_configuration_.opendrive_file, kMalidriveResourceFolder),
{road_geometry_configuration_.tolerances.linear_tolerance.value()}),
road_geometry_configuration_)();

// Fill useful pointers;
Expand Down Expand Up @@ -1179,10 +1187,12 @@ TEST_F(RoadGeometryNegativeLaneWidthTest, Strict) {
// Disables the tolerance range.
rg_config.tolerances.max_linear_tolerance = std::nullopt;

EXPECT_THROW(builder::RoadGeometryBuilder(xodr::LoadDataBaseFromFile(utility::FindResource(rg_config.opendrive_file),
{rg_config.tolerances.linear_tolerance.value()}),
rg_config)(),
maliput::common::assertion_error);
EXPECT_THROW(
builder::RoadGeometryBuilder(
xodr::LoadDataBaseFromFile(utility::FindResourceInPath(rg_config.opendrive_file, kMalidriveResourceFolder),
{rg_config.tolerances.linear_tolerance.value()}),
rg_config)(),
maliput::common::assertion_error);
}

// Allow having negative width descriptions.
Expand All @@ -1192,9 +1202,10 @@ TEST_F(RoadGeometryNegativeLaneWidthTest, AllowNegativeWidthDescriptions) {
builder::RoadGeometryConfiguration::StandardStrictnessPolicy::kAllowSemanticErrors;

ASSERT_NO_THROW(
dut_ = builder::RoadGeometryBuilder(xodr::LoadDataBaseFromFile(utility::FindResource(rg_config.opendrive_file),
{rg_config.tolerances.linear_tolerance.value()}),
rg_config)());
dut_ = builder::RoadGeometryBuilder(
xodr::LoadDataBaseFromFile(utility::FindResourceInPath(rg_config.opendrive_file, kMalidriveResourceFolder),
{rg_config.tolerances.linear_tolerance.value()}),
rg_config)());
ASSERT_NE(dut_, nullptr);

// Let's make a `lane_bounds` query to a lane whose lane-width function has negative values.
Expand Down Expand Up @@ -1224,19 +1235,21 @@ TEST_P(RoadGeometryNonContiguousInNonDrivableLanesTest, CheckG1ContiguityEnforce
// Gap in non drivable road/lane + strict.
rg_config.standard_strictness_policy = builder::RoadGeometryConfiguration::StandardStrictnessPolicy::kStrict;

EXPECT_THROW(builder::RoadGeometryBuilder(xodr::LoadDataBaseFromFile(utility::FindResource(rg_config.opendrive_file),
{rg_config.tolerances.linear_tolerance.value()}),
rg_config)(),
maliput::common::assertion_error);
EXPECT_THROW(
builder::RoadGeometryBuilder(
xodr::LoadDataBaseFromFile(utility::FindResourceInPath(rg_config.opendrive_file, kMalidriveResourceFolder),
{rg_config.tolerances.linear_tolerance.value()}),
rg_config)(),
maliput::common::assertion_error);

// Gap in non drivable road/lane + allow semantic errors.
rg_config.standard_strictness_policy =
builder::RoadGeometryConfiguration::StandardStrictnessPolicy::kAllowSemanticErrors;

EXPECT_NO_THROW(
builder::RoadGeometryBuilder(xodr::LoadDataBaseFromFile(utility::FindResource(rg_config.opendrive_file),
{rg_config.tolerances.linear_tolerance.value()}),
rg_config)());
EXPECT_NO_THROW(builder::RoadGeometryBuilder(
xodr::LoadDataBaseFromFile(utility::FindResourceInPath(rg_config.opendrive_file, kMalidriveResourceFolder),
{rg_config.tolerances.linear_tolerance.value()}),
rg_config)());
}

INSTANTIATE_TEST_CASE_P(CheckG1ContiguityEnforcementGroup, RoadGeometryNonContiguousInNonDrivableLanesTest,
Expand All @@ -1251,14 +1264,14 @@ INSTANTIATE_TEST_CASE_P(CheckG1ContiguityEnforcementGroup, RoadGeometryNonContig
class ToleranceSelectionPolicyTest : public ::testing::Test {
protected:
void SetUp() override {
rg_config.opendrive_file = utility::FindResource(kXodrFile);
rg_config.opendrive_file = utility::FindResourceInPath(kXodrFile, kMalidriveResourceFolder);
rg_config.omit_nondrivable_lanes = false;
rg_config.standard_strictness_policy = RoadGeometryConfiguration::StandardStrictnessPolicy::kStrict;
rg_config.tolerances.linear_tolerance = std::nullopt;
rg_config.tolerances.max_linear_tolerance = std::nullopt;
}

const std::string kXodrFile{"odr/GapInElevationNonDrivableRoad.xodr"};
const std::string kXodrFile{"GapInElevationNonDrivableRoad.xodr"};
builder::RoadGeometryConfiguration rg_config{};
};

Expand Down

0 comments on commit 15a209c

Please sign in to comment.