Skip to content

Commit

Permalink
Replace PROJECT_SOURCE_DIR in tests (#460)
Browse files Browse the repository at this point in the history
This allows the test files to be loaded in a more abstracted way,
so that it is less dependant on the way that CMake works.

Signed-off-by: Michael Carroll <michael@openrobotics.org>
  • Loading branch information
mjcarroll committed Jan 21, 2021
1 parent 1be14a2 commit 97a2be6
Show file tree
Hide file tree
Showing 39 changed files with 402 additions and 286 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ if (BUILD_SDF_TEST)
)
endif()

include_directories(${PROJECT_SOURCE_DIR}/test)
sdf_build_tests(${gtest_sources})

if (TARGET UNIT_ign_TEST)
Expand Down
4 changes: 2 additions & 2 deletions src/Converter_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1899,9 +1899,9 @@ TEST(Converter, MuchNewerVersion)
}

const std::string CONVERT_DOC_15_16 =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "sdf", "1.6", "1_5.convert");
sdf::testing::SourceFile("sdf", "1.6", "1_5.convert");
const std::string CONVERT_DOC_16_17 =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "sdf", "1.7", "1_6.convert");
sdf::testing::SourceFile("sdf", "1.7", "1_6.convert");

/////////////////////////////////////////////////
/// Test conversion of imu in 1.5 to 1.6
Expand Down
9 changes: 3 additions & 6 deletions src/FrameSemantics_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
TEST(FrameSemantics, buildFrameAttachedToGraph_Model)
{
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"model_frame_attached_to.sdf");
sdf::testing::TestFile("sdf", "model_frame_attached_to.sdf");

// Load the SDF file
sdf::Root root;
Expand Down Expand Up @@ -103,8 +102,7 @@ TEST(FrameSemantics, buildFrameAttachedToGraph_Model)
TEST(FrameSemantics, buildFrameAttachedToGraph_World)
{
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"world_frame_attached_to.sdf");
sdf::testing::TestFile("sdf", "world_frame_attached_to.sdf");

// Load the SDF file
sdf::Root root;
Expand Down Expand Up @@ -198,8 +196,7 @@ TEST(FrameSemantics, buildFrameAttachedToGraph_World)
TEST(FrameSemantics, buildPoseRelativeToGraph)
{
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"model_frame_relative_to_joint.sdf");
sdf::testing::TestFile("sdf", "model_frame_relative_to_joint.sdf");

// Load the SDF file
sdf::Root root;
Expand Down
8 changes: 4 additions & 4 deletions src/ign_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ int main(int argc, char **argv)
{
// Set IGN_CONFIG_PATH to the directory where the .yaml configuration file
// is located.
setenv("IGN_CONFIG_PATH", IGN_CONFIG_PATH, 1);
sdf::testing::setenv("IGN_CONFIG_PATH", IGN_CONFIG_PATH);

// Make sure that we load the library recently built and not the one installed
// in your system. This is done by placing the the current build directory
Expand All @@ -728,13 +728,13 @@ int main(int argc, char **argv)
#ifndef _WIN32
std::string testLibraryPath = IGN_TEST_LIBRARY_PATH;

char *currentLibraryPath = std::getenv("LD_LIBRARY_PATH");
if (currentLibraryPath)
std::string currentLibraryPath;
if (sdf::testing::env("LD_LIBRARY_PATH", currentLibraryPath))
{
testLibraryPath = testLibraryPath + ":" + currentLibraryPath;
}

setenv("LD_LIBRARY_PATH", testLibraryPath.c_str(), 1);
sdf::testing::setenv("LD_LIBRARY_PATH", testLibraryPath);
#endif

::testing::InitGoogleTest(&argc, argv);
Expand Down
74 changes: 31 additions & 43 deletions src/parser_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ sdf::SDFPtr InitSDF()
/// Checks emitted warnings for custom/unknown elements in log file
TEST(Parser, CustomUnknownElements)
{
std::string pathBase = PROJECT_SOURCE_PATH;
pathBase += "/test/sdf";
const std::string path = pathBase +"/custom_and_unknown_elements.sdf";
const auto path = sdf::testing::TestFile(
"sdf", "custom_and_unknown_elements.sdf");

sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
Expand Down Expand Up @@ -100,10 +99,9 @@ TEST(Parser, CustomUnknownElements)
/////////////////////////////////////////////////
TEST(Parser, ReusedSDFVersion)
{
std::string pathBase = PROJECT_SOURCE_PATH;
pathBase += "/test/sdf";
const std::string path17 = pathBase +"/model_link_relative_to.sdf";
const std::string path16 = pathBase +"/joint_complete.sdf";
const auto path17 = sdf::testing::TestFile(
"sdf", "model_link_relative_to.sdf");
const auto path16 = sdf::testing::TestFile("sdf", "joint_complete.sdf");

// Call readFile API that always converts
sdf::SDFPtr sdf = InitSDF();
Expand All @@ -123,9 +121,7 @@ TEST(Parser, ReusedSDFVersion)
/////////////////////////////////////////////////
TEST(Parser, readFileConversions)
{
std::string pathBase = PROJECT_SOURCE_PATH;
pathBase += "/test/sdf";
const std::string path = pathBase +"/joint_complete.sdf";
const auto path = sdf::testing::TestFile("sdf", "joint_complete.sdf");

// Call readFile API that always converts
{
Expand Down Expand Up @@ -384,16 +380,13 @@ TEST(Parser, addNestedModel)
/////////////////////////////////////////////////
TEST(Parser, NameUniqueness)
{
std::string pathBase = PROJECT_SOURCE_PATH;
pathBase += "/test/sdf";

// These tests are copies of the ones in ign_TEST.cc but use direct calls to
// name uniqueness validator functions instead of going through ign.

// Check an SDF file with sibling elements of the same type (world)
// that have duplicate names.
{
std::string path = pathBase +"/world_duplicate.sdf";
const auto path = sdf::testing::TestFile("sdf", "world_duplicate.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -406,7 +399,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of different types (model, light)
// that have duplicate names.
{
std::string path = pathBase +"/world_sibling_same_names.sdf";
const auto path = sdf::testing::TestFile("sdf",
"world_sibling_same_names.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSiblingUniqueNames(sdf->Root()));
Expand All @@ -419,7 +413,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of the same type (link)
// that have duplicate names.
{
std::string path = pathBase +"/model_duplicate_links.sdf";
const auto path = sdf::testing::TestFile("sdf",
"model_duplicate_links.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -432,7 +427,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of the same type (joint)
// that have duplicate names.
{
std::string path = pathBase +"/model_duplicate_joints.sdf";
const auto path = sdf::testing::TestFile("sdf",
"model_duplicate_joints.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -445,7 +441,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of different types (link, joint)
// that have duplicate names.
{
std::string path = pathBase +"/model_link_joint_same_name.sdf";
const auto path = sdf::testing::TestFile("sdf",
"model_link_joint_same_name.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSiblingUniqueNames(sdf->Root()));
Expand All @@ -458,7 +455,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of the same type (collision)
// that have duplicate names.
{
std::string path = pathBase +"/link_duplicate_sibling_collisions.sdf";
const auto path = sdf::testing::TestFile("sdf",
"link_duplicate_sibling_collisions.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -471,7 +469,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of the same type (visual)
// that have duplicate names.
{
std::string path = pathBase +"/link_duplicate_sibling_visuals.sdf";
const auto path = sdf::testing::TestFile("sdf",
"link_duplicate_sibling_visuals.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSiblingUniqueNames(sdf->Root()));
Expand All @@ -484,7 +483,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with cousin elements of the same type (collision)
// that have duplicate names. This is a valid file.
{
std::string path = pathBase +"/link_duplicate_cousin_collisions.sdf";
const auto path = sdf::testing::TestFile("sdf",
"link_duplicate_cousin_collisions.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_TRUE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -498,7 +498,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with cousin elements of the same type (visual)
// that have duplicate names. This is a valid file.
{
std::string path = pathBase +"/link_duplicate_cousin_visuals.sdf";
const auto path = sdf::testing::TestFile("sdf",
"link_duplicate_cousin_visuals.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_TRUE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -520,9 +521,6 @@ static bool contains(const std::string &_a, const std::string &_b)
/////////////////////////////////////////////////
TEST(Parser, SyntaxErrorInValues)
{
std::string pathBase = PROJECT_SOURCE_PATH;
pathBase += "/test/sdf";

// Capture sdferr output
std::stringstream buffer;
auto old = std::cerr.rdbuf(buffer.rdbuf());
Expand All @@ -532,7 +530,7 @@ TEST(Parser, SyntaxErrorInValues)
#endif

{
std::string path = pathBase +"/bad_syntax_pose.sdf";
const auto path = sdf::testing::TestFile("sdf", "bad_syntax_pose.sdf");
sdf::SDFPtr sdf(new sdf::SDF());
sdf::init(sdf);

Expand All @@ -543,7 +541,7 @@ TEST(Parser, SyntaxErrorInValues)
{
// clear the contents of the buffer
buffer.str("");
std::string path = pathBase +"/bad_syntax_double.sdf";
const auto path = sdf::testing::TestFile("sdf", "bad_syntax_double.sdf");
sdf::SDFPtr sdf(new sdf::SDF());
sdf::init(sdf);

Expand All @@ -554,7 +552,7 @@ TEST(Parser, SyntaxErrorInValues)
{
// clear the contents of the buffer
buffer.str("");
std::string path = pathBase +"/bad_syntax_vector.sdf";
const auto path = sdf::testing::TestFile("sdf", "bad_syntax_vector.sdf");
sdf::SDFPtr sdf(new sdf::SDF());
sdf::init(sdf);

Expand Down Expand Up @@ -605,8 +603,8 @@ class ValueConstraintsFixture : public ::testing::Test
/// Check if minimum/maximum values are valided
TEST_F(ValueConstraintsFixture, ElementMinMaxValues)
{
std::string sdfDescPath = std::string(PROJECT_SOURCE_PATH) +
"/test/sdf/stricter_semantics_desc.sdf";
const auto sdfDescPath =
sdf::testing::TestFile("sdf", "stricter_semantics_desc.sdf");

auto sdfTest = std::make_shared<sdf::SDF>();
sdf::initFile(sdfDescPath, sdfTest);
Expand Down Expand Up @@ -677,19 +675,9 @@ TEST_F(ValueConstraintsFixture, ElementMinMaxValues)
/// Main
int main(int argc, char **argv)
{
// temporarily set HOME to build directory
#ifndef _WIN32
setenv("HOME", PROJECT_BINARY_DIR, 1);
#else
std::string buildDir = PROJECT_BINARY_DIR;
for (int i = 0; i < buildDir.size(); ++i)
{
if (buildDir[i] == '/')
buildDir[i] = '\\';
}
std::string homePath = "HOMEPATH=" + buildDir;
_putenv(homePath.c_str());
#endif
// temporarily set HOME
std::string homeDir;
sdf::testing::TestSetHomePath(homeDir);
sdf::Console::Clear();

::testing::InitGoogleTest(&argc, argv);
Expand Down
Loading

0 comments on commit 97a2be6

Please sign in to comment.