diff --git a/.gitignore b/.gitignore index 08e5eba3e..50e689128 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ build build_* *.*.sw? .vscode + +__pycache__ diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 000000000..0e26e2c84 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,200 @@ +load( + "@gz//bazel/skylark:build_defs.bzl", + "GZ_FEATURES", + "GZ_ROOT", + "GZ_VISIBILITY", + "add_lint_tests", + "gz_configure_file", + "gz_configure_header", + "gz_export_header", + "gz_include_header", + "gz_py_binary", +) + +package( + default_visibility = GZ_VISIBILITY, + features = GZ_FEATURES, +) + +licenses(["notice"]) + +exports_files(["LICENSE"]) + +gz_configure_header( + name = "config", + src = "include/sdf/config.hh.in", + cmakelists = ["CMakeLists.txt"], + defines = { + "CMAKE_INSTALL_FULL_DATAROOTDIR": "unused", + }, + package = "sdformat", +) + +gz_py_binary( + name = "embed_sdf", + srcs = ["sdf/embedSdf.py"], + main = "sdf/embedSdf.py", +) + +genrule( + name = "embed_sdf_genrule", + srcs = glob([ + "sdf/**/*.sdf", + "sdf/**/*.convert", + ]), + outs = ["EmbeddedSdf.cc"], + cmd = "$(execpath :embed_sdf) --output-file $@ --sdf-root sdformat/sdf/ --input-files $(SRCS)", # noqa + tools = [":embed_sdf"], +) + +public_headers_no_gen = glob([ + "include/sdf/*.h", + "include/sdf/*.hh", +]) + +private_headers = glob(["src/*.hh"]) + +sources = glob( + ["src/*.cc"], + exclude = [ + "src/*_TEST.cc", + "src/gz.cc", + ], +) + +gz_export_header( + name = "include/sdf/Export.hh", + export_base = "GZ_SDFORMAT", + lib_name = "sdf", + visibility = ["//visibility:private"], +) + +gz_include_header( + name = "sdformat_hh_genrule", + out = "include/sdformat.hh", + hdrs = public_headers_no_gen + [ + "include/sdf/config.hh", + "include/sdf/Export.hh", + ], +) + +public_headers = public_headers_no_gen + [ + "include/sdf/Export.hh", + "include/sdf/config.hh", + "include/sdformat.hh", +] + +cc_library( + name = "urdf", + srcs = [ + "src/urdf/urdf_parser/joint.cpp", + "src/urdf/urdf_parser/link.cpp", + "src/urdf/urdf_parser/model.cpp", + "src/urdf/urdf_parser/pose.cpp", + "src/urdf/urdf_parser/twist.cpp", + "src/urdf/urdf_parser/urdf_model_state.cpp", + "src/urdf/urdf_parser/urdf_sensor.cpp", + "src/urdf/urdf_parser/world.cpp", + ], + hdrs = glob( + ["src/urdf/**/*.h"], + ), + copts = ["-Wno-unknown-pragmas"], + includes = ["src/urdf"], + deps = [ + "@tinyxml2", + ], +) + +cc_library( + name = "sdformat", + srcs = sources + private_headers + ["EmbeddedSdf.cc"], + hdrs = public_headers, + defines = [ + 'SDF_SHARE_PATH=\\".\\"', + 'SDF_VERSION_PATH=\\"sdformat\\"', + ], + includes = [ + "include", + "src", + ], + deps = [ + ":urdf", + GZ_ROOT + "math", + GZ_ROOT + "utils", + "@tinyxml2", + ], +) + +cc_library( + name = "sdformat_internal", + srcs = [ + "src/gz.cc", + "src/gz.hh", + ], + visibility = ["//visibility:private"], + deps = [":sdformat"], +) + +test_sources = glob( + ["src/*_TEST.cc"], + exclude = ["src/gz_TEST.cc"], +) + +[cc_test( + name = src.replace("/", "_").replace(".cc", "").replace("src_", ""), + srcs = [src], + data = [ + "sdf", + GZ_ROOT + "sdformat/test:integration", + GZ_ROOT + "sdformat/test:sdf", + ], + env = { + "GZ_BAZEL": "1", + "GZ_BAZEL_PATH": "sdformat", + }, + deps = [ + ":sdformat", + GZ_ROOT + "sdformat/test:test_utils", + "@gtest", + "@gtest//:gtest_main", + ], +) for src in test_sources] + +gz_configure_file( + name = "sdformat.rb", + src = "src/cmd/cmdsdformat.rb.in", + out = "cmdsdformat.rb", + cmakelists = ["CMakeLists.txt"], + defines = [ + "library_location=libgz-sdformat.so", + ], + package = "sdformat", + visibility = [GZ_ROOT + "tools:__pkg__"], +) + +gz_configure_file( + name = "sdformat_yaml", + src = "conf/sdformat.yaml.in", + out = "sdformat.yaml", + cmakelists = ["CMakeLists.txt"], + defines = [ + "gz_library_path=gz/sdformat/cmdsdformat.rb", + ], + package = "sdformat", + visibility = [GZ_ROOT + "tools:__pkg__"], +) + +cc_binary( + name = "libgz-sdformat.so", + srcs = [":sdformat_internal"], + linkshared = True, + visibility = [GZ_ROOT + "tools:__pkg__"], + deps = [ + ":sdformat", + ], +) + +exports_files(["sdf"]) + +add_lint_tests() diff --git a/include/sdf/Param.hh b/include/sdf/Param.hh index 29c5b90e5..dea9aa07a 100644 --- a/include/sdf/Param.hh +++ b/include/sdf/Param.hh @@ -85,6 +85,11 @@ namespace sdf : val(_val), precision(_precision) {} }; + // Template deduction guide for ParamVariant + template + ParamStreamer(const ParamVariant &_val, int _precision) + -> ParamStreamer; + template std::ostream& operator<<(std::ostream &os, ParamStreamer s) { diff --git a/include/sdf/config.hh.in b/include/sdf/config.hh.in index d1b3db532..6a32cb8bc 100644 --- a/include/sdf/config.hh.in +++ b/include/sdf/config.hh.in @@ -47,7 +47,12 @@ #cmakedefine SDFORMAT_DISABLE_CONSOLE_LOGFILE 1 +#ifndef SDF_SHARE_PATH #define SDF_SHARE_PATH "${CMAKE_INSTALL_FULL_DATAROOTDIR}/" -#define SDF_VERSION_PATH "${CMAKE_INSTALL_FULL_DATAROOTDIR}/sdformat${SDF_MAJOR_VERSION}/${SDF_PKG_VERSION}" +#endif + +#ifndef SDF_VERSION_PATH +#define SDF_VERSION_PATH "${CMAKE_INSTALL_FULL_DATAROOTDIR}/sdformat${PROJECT_VERSION_MAJOR}/${PROJECT_VERSION}" +#endif #endif // #ifndef SDF_CONFIG_HH_ diff --git a/sdf/embedSdf.py b/sdf/embedSdf.py index 292958712..8e81054d2 100644 --- a/sdf/embedSdf.py +++ b/sdf/embedSdf.py @@ -160,10 +160,10 @@ def generate_map_content(paths: List[Path], relative_to: Optional[str] = None) - for path in paths: with open(path, "r", encoding="utf8") as input_sdf: file_content = input_sdf.read() - # Strip relative path if requested if relative_to is not None: - path = path.relative_to(relative_to) + _, relative_path = str(path).split(relative_to) + path = relative_path content.append(embed_sdf_content(str(path), file_content)) return ",".join(content) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0757017d2..408add0c5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -61,8 +61,11 @@ if (BUILD_TESTING) XmlUtils.cc) endif() - if(TARGET UNIT_gz_TEST) - target_compile_definitions(UNIT_gz_TEST PUBLIC "-DDETAIL_GZ_CONFIG_PATH=\"${CMAKE_BINARY_DIR}/test/conf/$\"") + if (TARGET UNIT_gz_TEST) + target_compile_definitions(UNIT_gz_TEST PRIVATE + -DGZ_PATH="${GZ_PROGRAM}" + -DDETAIL_GZ_CONFIG_PATH="${CMAKE_BINARY_DIR}/test/conf/$" + -DGZ_TEST_LIBRARY_PATH="${PROJECT_BINARY_DIR}/src") endif() if (TARGET UNIT_FrameSemantics_TEST) diff --git a/src/Capsule_TEST.cc b/src/Capsule_TEST.cc index b24495966..f2a2993f2 100644 --- a/src/Capsule_TEST.cc +++ b/src/Capsule_TEST.cc @@ -154,7 +154,7 @@ TEST(DOMCapsule, Load) // Add a radius element sdf::ElementPtr radiusDesc(new sdf::Element()); radiusDesc->SetName("radius"); - radiusDesc->AddValue("double", "1.0", "1", "radius"); + radiusDesc->AddValue("double", "1.0", true, "radius"); sdf->AddElementDescription(radiusDesc); sdf::ElementPtr radiusElem = sdf->AddElement("radius"); radiusElem->Set(2.0); diff --git a/src/Cylinder_TEST.cc b/src/Cylinder_TEST.cc index efc7f302b..b1edb35a5 100644 --- a/src/Cylinder_TEST.cc +++ b/src/Cylinder_TEST.cc @@ -150,7 +150,7 @@ TEST(DOMCylinder, Load) // Add a radius element sdf::ElementPtr radiusDesc(new sdf::Element()); radiusDesc->SetName("radius"); - radiusDesc->AddValue("double", "1.0", "1", "radius"); + radiusDesc->AddValue("double", "1.0", true, "radius"); sdf->AddElementDescription(radiusDesc); sdf::ElementPtr radiusElem = sdf->AddElement("radius"); radiusElem->Set(2.0); diff --git a/src/Plane_TEST.cc b/src/Plane_TEST.cc index ea93941a0..81439ffed 100644 --- a/src/Plane_TEST.cc +++ b/src/Plane_TEST.cc @@ -145,7 +145,7 @@ TEST(DOMPlane, Load) // Add a normal element sdf::ElementPtr normalDesc(new sdf::Element()); normalDesc->SetName("normal"); - normalDesc->AddValue("vector3", "0 0 1", "1", "normal"); + normalDesc->AddValue("vector3", "0 0 1", true, "normal"); sdf->AddElementDescription(normalDesc); sdf::ElementPtr normalElem = sdf->AddElement("normal"); normalElem->Set({1, 0, 0}); diff --git a/src/SDF.cc b/src/SDF.cc index 07056d792..7a43d0f9a 100644 --- a/src/SDF.cc +++ b/src/SDF.cc @@ -43,6 +43,15 @@ inline namespace SDF_VERSION_NAMESPACE // returns the version string when possible. std::string SDF::version = SDF_VERSION; // NOLINT(runtime/string) +std::string sdfSharePath() +{ +#ifdef SDF_SHARE_PATH + if (std::string(SDF_SHARE_PATH) != "/") + return SDF_SHARE_PATH; +#endif + return ""; +} + ///////////////////////////////////////////////// void setFindCallback(std::function _cb) { @@ -109,16 +118,17 @@ std::string findFile(const std::string &_filename, bool _searchLocalPath, } // Next check the install path. - std::string path = sdf::filesystem::append(SDF_SHARE_PATH, filename); + std::string path = sdf::filesystem::append(sdfSharePath(), filename); if (sdf::filesystem::exists(path)) { return path; } // Next check the versioned install path. - path = sdf::filesystem::append(SDF_SHARE_PATH, - "sdformat" SDF_MAJOR_VERSION_STR, - sdf::SDF::Version(), filename); + path = sdf::filesystem::append(sdfSharePath(), + "sdformat" + std::string(SDF_MAJOR_VERSION_STR), + sdf::SDF::Version(), filename); + if (sdf::filesystem::exists(path)) { return path; diff --git a/src/SDF_TEST.cc b/src/SDF_TEST.cc index 43d2e4732..44bb9f284 100644 --- a/src/SDF_TEST.cc +++ b/src/SDF_TEST.cc @@ -16,7 +16,10 @@ */ #include + #include +#include + #include #include #include @@ -284,7 +287,7 @@ TEST(SDF, SetRoot) sdf::ElementPtr elem; elem.reset(new sdf::Element()); - elem->AddValue("bool", "true", "0", "description"); + elem->AddValue("bool", "true", false, "description"); s.SetRoot(elem); EXPECT_EQ(elem, s.Root()); @@ -306,56 +309,56 @@ TEST(SDF, EmptyValues) elem.reset(new sdf::Element()); EXPECT_FALSE(elem->Get(emptyString)); - elem->AddValue("bool", "true", "0", "description"); + elem->AddValue("bool", "true", false, "description"); EXPECT_TRUE(elem->Get(emptyString)); elem.reset(new sdf::Element()); EXPECT_EQ(elem->Get(emptyString), 0); - elem->AddValue("int", "12", "0", "description"); + elem->AddValue("int", "12", false, "description"); EXPECT_EQ(elem->Get(emptyString), 12); elem.reset(new sdf::Element()); EXPECT_EQ(elem->Get(emptyString), static_cast(0)); - elem->AddValue("unsigned int", "123", "0", "description"); + elem->AddValue("unsigned int", "123", false, "description"); EXPECT_EQ(elem->Get(emptyString), static_cast(123)); elem.reset(new sdf::Element()); EXPECT_EQ(elem->Get(emptyString), '\0'); - elem->AddValue("char", "a", "0", "description"); + elem->AddValue("char", "a", false, "description"); EXPECT_EQ(elem->Get(emptyString), 'a'); elem.reset(new sdf::Element()); EXPECT_EQ(elem->Get(emptyString), ""); - elem->AddValue("string", "hello", "0", "description"); + elem->AddValue("string", "hello", false, "description"); EXPECT_EQ(elem->Get(emptyString), "hello"); elem.reset(new sdf::Element()); EXPECT_EQ(elem->Get(emptyString), gz::math::Vector2d()); - elem->AddValue("vector2d", "1 2", "0", "description"); + elem->AddValue("vector2d", "1 2", false, "description"); EXPECT_EQ(elem->Get(emptyString), gz::math::Vector2d(1, 2)); elem.reset(new sdf::Element()); EXPECT_EQ(elem->Get(emptyString), gz::math::Vector3d()); - elem->AddValue("vector3", "1 2 3", "0", "description"); + elem->AddValue("vector3", "1 2 3", false, "description"); EXPECT_EQ(elem->Get(emptyString), gz::math::Vector3d(1, 2, 3)); elem.reset(new sdf::Element()); EXPECT_EQ(elem->Get(emptyString), gz::math::Quaterniond()); - elem->AddValue("quaternion", "1 2 3", "0", "description"); + elem->AddValue("quaternion", "1 2 3", false, "description"); EXPECT_EQ(elem->Get(emptyString), gz::math::Quaterniond(-2.14159, 1.14159, -0.141593)); elem.reset(new sdf::Element()); EXPECT_EQ(elem->Get(emptyString), gz::math::Pose3d()); - elem->AddValue("pose", "1.0 2.0 3.0 4.0 5.0 6.0", "0", "description"); + elem->AddValue("pose", "1.0 2.0 3.0 4.0 5.0 6.0", false, "description"); EXPECT_EQ(elem->Get(emptyString).Pos(), gz::math::Pose3d(1, 2, 3, 4, 5, 6).Pos()); EXPECT_EQ(elem->Get(emptyString).Rot().Euler(), @@ -364,23 +367,23 @@ TEST(SDF, EmptyValues) elem.reset(new sdf::Element()); EXPECT_EQ(elem->Get(emptyString), gz::math::Color()); - elem->AddValue("color", ".1 .2 .3 1.0", "0", "description"); + elem->AddValue("color", ".1 .2 .3 1.0", false, "description"); EXPECT_EQ(elem->Get(emptyString), gz::math::Color(.1f, .2f, .3f, 1.0f)); elem.reset(new sdf::Element()); EXPECT_EQ(elem->Get(emptyString), sdf::Time()); - elem->AddValue("time", "1 2", "0", "description"); + elem->AddValue("time", "1 2", false, "description"); EXPECT_EQ(elem->Get(emptyString), sdf::Time(1, 2)); elem.reset(new sdf::Element()); EXPECT_NEAR(elem->Get(emptyString), 0.0, 1e-6); - elem->AddValue("float", "12.34", "0", "description"); + elem->AddValue("float", "12.34", false, "description"); EXPECT_NEAR(elem->Get(emptyString), 12.34, 1e-6); elem.reset(new sdf::Element()); EXPECT_NEAR(elem->Get(emptyString), 0.0, 1e-6); - elem->AddValue("double", "12.34", "0", "description"); + elem->AddValue("double", "12.34", false, "description"); EXPECT_NEAR(elem->Get(emptyString), 12.34, 1e-6); } @@ -788,17 +791,17 @@ TEST(SDF, WriteURIPath) ASSERT_EQ(std::remove(tempFile.c_str()), 0); ASSERT_EQ(rmdir(tempDir.c_str()), 0); } - ///////////////////////////////////////////////// TEST(SDF, FindFileModelSDFCurrDir) { - std::string currDir; - - // Get current directory path from $PWD env variable - currDir = sdf::filesystem::current_path(); + // Change to a temporary directory before running test + auto prevPath = std::filesystem::current_path(); + std::string tmpDir; + ASSERT_TRUE(sdf::testing::TestTmpPath(tmpDir)); + std::filesystem::current_path(tmpDir); // A file named model.sdf in current directory - auto tempFile = currDir + "/model.sdf"; + auto tempFile = tmpDir + "/model.sdf"; sdf::SDF tempSDF; tempSDF.Write(tempFile); @@ -815,5 +818,6 @@ TEST(SDF, FindFileModelSDFCurrDir) // Cleanup ASSERT_EQ(std::remove(tempFile.c_str()), 0); + std::filesystem::current_path(prevPath); } #endif // _WIN32 diff --git a/src/gz_TEST.cc b/src/gz_TEST.cc index 72ff7e5cb..3d145b911 100644 --- a/src/gz_TEST.cc +++ b/src/gz_TEST.cc @@ -22,6 +22,7 @@ #include #include +#include #include "sdf/Error.hh" #include "sdf/Filesystem.hh" @@ -74,12 +75,10 @@ std::string custom_exec_str(std::string _cmd) ///////////////////////////////////////////////// TEST(checkUnrecognizedElements, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) { - std::string pathBase = PROJECT_SOURCE_PATH; - pathBase += "/test/sdf"; - // Check an SDFormat file with unrecognized elements { - std::string path = pathBase +"/unrecognized_elements.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "unrecognized_elements.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -104,7 +103,8 @@ TEST(checkUnrecognizedElements, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDFormat file with unrecognized elements with XML namespaces { - std::string path = pathBase +"/unrecognized_elements_with_namespace.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "unrecognized_elements_with_namespace.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -122,12 +122,10 @@ TEST(checkUnrecognizedElements, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) ///////////////////////////////////////////////// TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) { - std::string pathBase = PROJECT_SOURCE_PATH; - pathBase += "/test/sdf"; - // Check a good SDF file { - std::string path = pathBase +"/box_plane_low_friction_test.world"; + const auto path = + sdf::testing::TestFile("sdf", "box_plane_low_friction_test.world"); // Check box_plane_low_friction_test.world std::string output = @@ -137,7 +135,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check a bad SDF file { - std::string path = pathBase +"/box_bad_test.world"; + const auto path = + sdf::testing::TestFile("sdf", "box_bad_test.world"); // Check box_bad_test.world std::string output = @@ -149,7 +148,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // 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"); // Check world_duplicate.sdf std::string output = @@ -161,7 +161,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // 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"); // Check world_sibling_same_names.sdf std::string output = @@ -171,7 +172,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) } // Check an SDF world file is allowed to have duplicate plugin names { - std::string path = pathBase +"/world_duplicate_plugins.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "world_duplicate_plugins.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -181,7 +183,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // 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"); // Check model_duplicate_links.sdf std::string output = @@ -193,7 +196,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // 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"); // Check model_duplicate_joints.sdf std::string output = @@ -205,7 +209,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // 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"); // Check model_link_joint_same_name.sdf std::string output = @@ -216,7 +221,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF model file is allowed to have duplicate plugin names { - std::string path = pathBase +"/model_duplicate_plugins.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "model_duplicate_plugins.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -226,7 +232,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // 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"); // Check link_duplicate_sibling_collisions.sdf std::string output = @@ -239,7 +247,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // 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"); // Check link_duplicate_sibling_visuals.sdf std::string output = @@ -251,7 +261,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // 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"); // Check link_duplicate_cousin_collisions.sdf std::string output = @@ -262,7 +274,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // 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"); // Check link_duplicate_cousin_visuals.sdf std::string output = @@ -272,7 +286,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a joint with an invalid child link. { - std::string path = pathBase +"/joint_invalid_child.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "joint_invalid_child.sdf"); // Check joint_invalid_child.sdf std::string output = @@ -285,7 +300,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a joint with an invalid parent link. { - std::string path = pathBase +"/joint_invalid_parent.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "joint_invalid_parent.sdf"); // Check joint_invalid_parent.sdf std::string output = @@ -298,7 +314,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a joint that names itself as the child frame. { - std::string path = pathBase +"/joint_invalid_self_child.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "joint_invalid_self_child.sdf"); // Check joint_invalid_self_child.sdf std::string output = @@ -310,7 +327,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a joint that names itself as the parent frame. { - std::string path = pathBase +"/joint_invalid_self_parent.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "joint_invalid_self_parent.sdf"); // Check joint_invalid_self_parent.sdf std::string output = @@ -323,7 +341,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a joint with identical parent and child. { - std::string path = pathBase +"/joint_invalid_parent_same_as_child.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "joint_invalid_parent_same_as_child.sdf"); // Check joint_invalid_parent_same_as_child.sdf std::string output = @@ -337,8 +357,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a joint with parent parent frame that resolves // to the same value as the child. { - std::string path = - pathBase + "/joint_invalid_resolved_parent_same_as_child.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "joint_invalid_resolved_parent_same_as_child.sdf"); // Check joint_invalid_resolved_parent_same_as_child.sdf std::string output = @@ -351,7 +372,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with the world specified as a child link. { - std::string path = pathBase +"/joint_child_world.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "joint_child_world.sdf"); // Check joint_child_world.sdf std::string output = @@ -367,7 +389,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with the world specified as a parent link. // This is a valid file. { - std::string path = pathBase +"/joint_parent_world.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "joint_parent_world.sdf"); // Check joint_parent_world.sdf std::string output = @@ -378,7 +401,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a frame specified as the joint child. // This is a valid file. { - std::string path = pathBase +"/joint_child_frame.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "joint_child_frame.sdf"); // Check joint_child_frame.sdf std::string output = @@ -389,7 +413,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a frame specified as the joint parent. // This is a valid file. { - std::string path = pathBase +"/joint_parent_frame.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "joint_parent_frame.sdf"); // Check joint_parent_frame.sdf std::string output = @@ -400,7 +425,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with the infinite values for joint axis limits. // This is a valid file. { - std::string path = pathBase +"/joint_axis_infinite_limits.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "joint_axis_infinite_limits.sdf"); // Check joint_axis_infinite_limits.sdf std::string output = @@ -411,7 +437,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with the second link specified as the canonical link. // This is a valid file. { - std::string path = pathBase +"/model_canonical_link.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "model_canonical_link.sdf"); // Check model_canonical_link.sdf std::string output = @@ -421,7 +448,8 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with an invalid link specified as the canonical link. { - std::string path = pathBase +"/model_invalid_canonical_link.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "model_invalid_canonical_link.sdf"); // Check model_invalid_canonical_link.sdf std::string output = @@ -433,7 +461,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with an invalid model without links. { - std::string path = pathBase +"/model_without_links.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_without_links.sdf"); // Check model_without_links.sdf std::string output = @@ -444,7 +474,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a nested model. { - std::string path = pathBase +"/nested_model.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "nested_model.sdf"); // Check nested_model.sdf std::string output = @@ -454,7 +486,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a model that has a nested canonical link. { - std::string path = pathBase +"/nested_canonical_link.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "nested_canonical_link.sdf"); // Check nested_canonical_link.sdf std::string output = @@ -466,7 +500,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // that is explicitly specified by //model/@canonical_link using :: // syntax. { - std::string path = pathBase +"/nested_explicit_canonical_link.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "nested_explicit_canonical_link.sdf"); // Check nested_explicit_canonical_link.sdf std::string output = @@ -476,7 +512,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a model that a nested model without a link. { - std::string path = pathBase +"/nested_without_links_invalid.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "nested_without_links_invalid.sdf"); // Check nested_without_links_invalid.sdf std::string output = @@ -487,7 +525,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an invalid SDF file that uses reserved names. { - std::string path = pathBase +"/model_invalid_reserved_names.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_invalid_reserved_names.sdf"); // Check model_invalid_reserved_names.sdf std::string output = @@ -513,7 +553,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check that validity checks are disabled inside elements { - std::string path = pathBase +"/ignore_sdf_in_plugin.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "ignore_sdf_in_plugin.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -522,7 +564,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check that validity checks are disabled inside namespaced elements { - std::string path = pathBase +"/ignore_sdf_in_namespaced_elements.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "ignore_sdf_in_namespaced_elements.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -532,7 +576,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model frames using the attached_to attribute. // This is a valid file. { - std::string path = pathBase +"/model_frame_attached_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_frame_attached_to.sdf"); // Check model_frame_attached_to.sdf std::string output = @@ -543,7 +589,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model frames attached_to joints. // This is a valid file. { - std::string path = pathBase +"/model_frame_attached_to_joint.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_frame_attached_to_joint.sdf"); // Check model_frame_attached_to_joint.sdf std::string output = @@ -554,7 +602,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model frames attached_to a nested model. // This is a valid file. { - std::string path = pathBase +"/model_frame_attached_to_nested_model.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_frame_attached_to_nested_model.sdf"); // Check model_frame_attached_to_nested_model.sdf std::string output = @@ -564,7 +614,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model frames with invalid attached_to attributes. { - std::string path = pathBase +"/model_frame_invalid_attached_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_frame_invalid_attached_to.sdf"); // Check model_frame_invalid_attached_to.sdf std::string output = @@ -582,7 +634,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a cycle in its FrameAttachedTo graph { - std::string path = pathBase +"/model_frame_invalid_attached_to_cycle.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_frame_invalid_attached_to_cycle.sdf"); // Check model_frame_invalid_attached_to_cycle.sdf std::string output = @@ -600,7 +654,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model frames using the attached_to attribute. // This is a valid file. { - std::string path = pathBase +"/world_frame_attached_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "world_frame_attached_to.sdf"); // Check world_frame_attached_to.sdf std::string output = @@ -610,7 +666,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with world frames with invalid attached_to attributes. { - std::string path = pathBase +"/world_frame_invalid_attached_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "world_frame_invalid_attached_to.sdf"); // Check world_frame_invalid_attached_to.sdf std::string output = @@ -630,7 +688,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with links using the relative_to attribute. // This is a valid file. { - std::string path = pathBase +"/model_link_relative_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_link_relative_to.sdf"); // Check model_link_relative_to.sdf std::string output = @@ -640,7 +700,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model links with invalid relative_to attributes. { - std::string path = pathBase +"/model_invalid_link_relative_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_invalid_link_relative_to.sdf"); // Check model_invalid_link_relative_to.sdf std::string output = @@ -659,7 +721,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with nested_models using the relative_to attribute. // This is a valid file. { - std::string path = pathBase +"/model_nested_model_relative_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_nested_model_relative_to.sdf"); // Check model_nested_model_relative_to.sdf std::string output = @@ -671,7 +735,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // parent or child frames. // This is a valid file. { - std::string path = pathBase +"/joint_nested_parent_child.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "joint_nested_parent_child.sdf"); // Check model_nested_model_relative_to.sdf std::string output = @@ -682,7 +748,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with joints using the relative_to attribute. // This is a valid file. { - std::string path = pathBase +"/model_joint_relative_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_joint_relative_to.sdf"); // Check model_joint_relative_to.sdf std::string output = @@ -692,7 +760,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model joints with invalid relative_to attributes. { - std::string path = pathBase +"/model_invalid_joint_relative_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_invalid_joint_relative_to.sdf"); // Check model_invalid_joint_relative_to.sdf std::string output = @@ -711,7 +781,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model frames using the relative_to attribute. // This is a valid file. { - std::string path = pathBase +"/model_frame_relative_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_frame_relative_to.sdf"); // Check model_frame_relative_to.sdf std::string output = @@ -722,7 +794,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model frames relative_to joints. // This is a valid file. { - std::string path = pathBase +"/model_frame_relative_to_joint.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_frame_relative_to_joint.sdf"); // Check model_frame_relative_to_joint.sdf std::string output = @@ -732,7 +806,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model frames with invalid relative_to attributes. { - std::string path = pathBase +"/model_invalid_frame_relative_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_invalid_frame_relative_to.sdf"); // Check model_invalid_frame_relative_to.sdf std::string output = @@ -750,7 +826,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with a cycle in its PoseRelativeTo graph { - std::string path = pathBase +"/model_invalid_frame_relative_to_cycle.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_invalid_frame_relative_to_cycle.sdf"); // Check model_invalid_frame_relative_to_cycle.sdf std::string output = @@ -768,7 +846,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with model frames using the attached_to attribute. // This is a valid file. { - std::string path = pathBase +"/world_frame_relative_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "world_frame_relative_to.sdf"); // Check world_frame_relative_to.sdf std::string output = @@ -778,7 +858,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with world frames with invalid relative_to attributes. { - std::string path = pathBase +"/world_frame_invalid_relative_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "world_frame_invalid_relative_to.sdf"); // Check world_frame_invalid_relative_to.sdf std::string output = @@ -807,7 +889,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with an invalid frame specified as the placement_frame // attribute. { - std::string path = pathBase + "/model_invalid_placement_frame.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_invalid_placement_frame.sdf"); // Check model_invalid_placement_frame.sdf std::string output = @@ -820,7 +904,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF file with an valid nested model cross references { - std::string path = pathBase + "/nested_model_cross_references.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "nested_model_cross_references.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -829,7 +915,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an SDF model file with an invalid usage of __root__ { - std::string path = pathBase + "/model_invalid_root_reference.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_invalid_root_reference.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -848,8 +936,11 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) { // Set SDF_PATH so that included models can be found gz::utils::setenv( - "SDF_PATH", sdf::testing::SourceFile("test", "integration", "model")); - std::string path = pathBase + "/world_invalid_root_reference.sdf"; + "SDF_PATH", sdf::testing::TestFile("integration", "model")); + + const auto path = + sdf::testing::TestFile("sdf", + "world_invalid_root_reference.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -896,7 +987,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) } // Check an SDF world file with an valid usage of __root__ { - std::string path = pathBase + "/world_valid_root_reference.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "world_valid_root_reference.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -904,7 +997,9 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) } // Check an SDF with an invalid relative frame at the top level model { - std::string path = pathBase + "/model_invalid_top_level_frame.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "model_invalid_top_level_frame.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -918,11 +1013,10 @@ TEST(check, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) ///////////////////////////////////////////////// TEST(check_shapes_sdf, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) { - std::string pathBase = PROJECT_SOURCE_PATH; - pathBase += "/test/sdf"; - { - std::string path = pathBase +"/shapes.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "shapes.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -930,7 +1024,9 @@ TEST(check_shapes_sdf, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) } { - std::string path = pathBase +"/shapes_world.sdf"; + const auto path = + sdf::testing::TestFile("sdf", + "shapes_world.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -941,12 +1037,11 @@ TEST(check_shapes_sdf, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) ///////////////////////////////////////////////// TEST(check_model_sdf, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) { - std::string pathBase = PROJECT_SOURCE_PATH; - pathBase += "/test/integration/model/box"; - // Check a good SDF file by passing the absolute path { - std::string path = pathBase +"/model.sdf"; + const auto path = + sdf::testing::TestFile("integration", + "model", "box", "model.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf -k " + path + SdfVersion()); @@ -955,7 +1050,9 @@ TEST(check_model_sdf, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check a good SDF file from the same folder by passing a relative path { - std::string path = "model.sdf"; + const auto pathBase = sdf::testing::TestFile("integration", + "model", "box"); + const auto path = "model.sdf"; std::string output = custom_exec_str("cd " + pathBase + " && " + @@ -979,12 +1076,10 @@ TEST(describe, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) ///////////////////////////////////////////////// TEST(print, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) { - std::string pathBase = PROJECT_SOURCE_PATH; - pathBase += "/test/sdf"; - // Check a good SDF file { - std::string path = pathBase +"/box_plane_low_friction_test.world"; + const auto path = + sdf::testing::TestFile("sdf", "box_plane_low_friction_test.world"); sdf::SDFPtr sdf(new sdf::SDF()); EXPECT_TRUE(sdf::init(sdf)); EXPECT_TRUE(sdf::readFile(path, sdf)); @@ -997,7 +1092,8 @@ TEST(print, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check a bad SDF file { - std::string path = pathBase +"/box_bad_test.world"; + const auto path = + sdf::testing::TestFile("sdf", "box_bad_test.world"); // Check box_bad_test.world std::string output = @@ -1359,7 +1455,7 @@ TEST(print_includes_rotations_in_quaternions, { // Set SDF_PATH so that included models can be found gz::utils::setenv( - "SDF_PATH", sdf::testing::SourceFile("test", "integration", "model")); + "SDF_PATH", sdf::testing::TestFile("integration", "model")); const auto path = sdf::testing::TestFile( "sdf", "includes_rotations_in_quaternions.sdf"); @@ -1637,11 +1733,9 @@ TEST(print_snap_to_degrees_tolerance_too_high, ///////////////////////////////////////////////// TEST(GraphCmd, GZ_UTILS_TEST_DISABLED_ON_WIN32(WorldPoseRelativeTo)) { - const std::string pathBase = std::string(PROJECT_SOURCE_PATH) + "/test/sdf"; - // world pose relative_to graph const std::string path = - pathBase + "/world_relative_to_nested_reference.sdf"; + sdf::testing::TestFile("sdf", "world_relative_to_nested_reference.sdf"); const std::string output = custom_exec_str(GzCommand() + " sdf -g pose " + path + SdfVersion()); @@ -1688,8 +1782,9 @@ TEST(GraphCmd, GZ_UTILS_TEST_DISABLED_ON_WIN32(WorldPoseRelativeTo)) ///////////////////////////////////////////////// TEST(GraphCmd, GZ_UTILS_TEST_DISABLED_ON_WIN32(ModelPoseRelativeTo)) { - const std::string pathBase = std::string(PROJECT_SOURCE_PATH) + "/test/sdf"; - const std::string path = pathBase + "/model_relative_to_nested_reference.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "model_relative_to_nested_reference.sdf"); + const std::string output = custom_exec_str(GzCommand() + " sdf -g pose " + path + SdfVersion()); @@ -1764,8 +1859,8 @@ TEST(GraphCmd, GZ_UTILS_TEST_DISABLED_ON_WIN32(ModelPoseRelativeTo)) ///////////////////////////////////////////////// TEST(GraphCmd, GZ_UTILS_TEST_DISABLED_ON_WIN32(WorldFrameAttachedTo)) { - const std::string pathBase = std::string(PROJECT_SOURCE_PATH) + "/test/sdf"; - const std::string path = pathBase + "/world_nested_frame_attached_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "world_nested_frame_attached_to.sdf"); const std::string output = custom_exec_str(GzCommand() + " sdf -g frame " + path + SdfVersion()); @@ -1810,8 +1905,9 @@ TEST(GraphCmd, GZ_UTILS_TEST_DISABLED_ON_WIN32(WorldFrameAttachedTo)) ///////////////////////////////////////////////// TEST(GraphCmd, GZ_UTILS_TEST_DISABLED_ON_WIN32(ModelFrameAttachedTo)) { - const std::string pathBase = std::string(PROJECT_SOURCE_PATH) + "/test/sdf"; - const std::string path = pathBase + "/model_nested_frame_attached_to.sdf"; + const auto path = + sdf::testing::TestFile("sdf", "model_nested_frame_attached_to.sdf"); + const std::string output = custom_exec_str(GzCommand() + " sdf -g frame " + path + SdfVersion()); @@ -1861,9 +1957,6 @@ TEST(GraphCmd, GZ_UTILS_TEST_DISABLED_ON_WIN32(ModelFrameAttachedTo)) ///////////////////////////////////////////////// TEST(inertial_stats, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) { - std::string pathBase = PROJECT_SOURCE_PATH; - pathBase += "/test/sdf"; - std::string expectedOutput = "Inertial statistics for model: test_model\n" "---\n" @@ -1882,7 +1975,7 @@ TEST(inertial_stats, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check a good SDF file by passing the absolute path { - std::string path = pathBase +"/inertial_stats.sdf"; + const auto path = sdf::testing::TestFile("sdf", "inertial_stats.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf --inertial-stats " + @@ -1892,7 +1985,8 @@ TEST(inertial_stats, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check a good SDF file from the same folder by passing a relative path { - std::string path = "inertial_stats.sdf"; + const auto path = "inertial_stats.sdf"; + const auto pathBase = sdf::testing::TestFile("sdf"); std::string output = custom_exec_str("cd " + pathBase + " && " + @@ -1923,7 +2017,7 @@ TEST(inertial_stats, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) // Check an invalid SDF file by passing the absolute path { - std::string path = pathBase +"/inertial_invalid.sdf"; + const auto path = sdf::testing::TestFile("sdf", "inertial_invalid.sdf"); std::string output = custom_exec_str(GzCommand() + " sdf --inertial-stats " + @@ -1935,7 +2029,8 @@ TEST(inertial_stats, GZ_UTILS_TEST_DISABLED_ON_WIN32(SDF)) "Error: Expected a model file but received a world file.\n"; // Check a valid world file. { - std::string path = pathBase +"/box_plane_low_friction_test.world"; + const auto path = + sdf::testing::TestFile("sdf", "box_plane_low_friction_test.world"); std::string output = custom_exec_str(GzCommand() + " sdf --inertial-stats " + @@ -1954,9 +2049,8 @@ TEST(HelpVsCompletionFlags, SDF) std::string helpOutput = custom_exec_str(GzCommand() + " sdf --help"); // Call the output function in the bash completion script - std::string scriptPath = PROJECT_SOURCE_PATH; - scriptPath = sdf::filesystem::append(scriptPath, "src", "cmd", - "sdf.bash_completion.sh"); + const auto scriptPath = + sdf::testing::SourceFile("src", "cmd", "sdf.bash_completion.sh"); // Equivalent to: // sh -c "bash -c \". /path/to/sdf.bash_completion.sh; _gz_sdf_flags\"" diff --git a/src/parser_TEST.cc b/src/parser_TEST.cc index 7eb54d5e4..f8a614e34 100644 --- a/src/parser_TEST.cc +++ b/src/parser_TEST.cc @@ -23,11 +23,12 @@ #include "sdf/Element.hh" #include "sdf/Console.hh" #include "sdf/Filesystem.hh" -#include "test_config.hh" -#include "test_utils.hh" #include +#include "test_config.hh" +#include "test_utils.hh" + ///////////////////////////////////////////////// TEST(Parser, initStringTrim) { @@ -418,10 +419,18 @@ TEST(Parser, IncludesErrors) const auto path = sdf::testing::TestFile("sdf", "includes_missing_model.sdf"); + sdf::setFindCallback([&](const std::string &/*_file*/) + { + return ""; + }); + sdf::SDFPtr sdf(new sdf::SDF()); sdf::init(sdf); sdf::readFile(path, sdf); + + std::cout << buffer.str() << std::endl; + EXPECT_PRED2(sdf::testing::contains, buffer.str(), "Error Code " + std::to_string( @@ -937,7 +946,14 @@ int main(int argc, char **argv) { // temporarily set HOME std::string homeDir; - sdf::testing::TestSetHomePath(homeDir); + sdf::testing::TestTmpPath(homeDir); + +#ifdef _WIN32 + gz::utils::setenv("HOMEPATH", homeDir); +#else + gz::utils::setenv("HOME", homeDir); +#endif + sdf::Console::Clear(); ::testing::InitGoogleTest(&argc, argv); diff --git a/test/BUILD.bazel b/test/BUILD.bazel new file mode 100644 index 000000000..bb1baa410 --- /dev/null +++ b/test/BUILD.bazel @@ -0,0 +1,72 @@ +load( + "@gz//bazel/skylark:build_defs.bzl", + "GZ_FEATURES", + "GZ_ROOT", + "GZ_VISIBILITY", + "cmake_configure_file", +) + +package( + default_visibility = GZ_VISIBILITY, + features = GZ_FEATURES, +) + +licenses(["notice"]) + +cmake_configure_file( + name = "config", + src = "test_config.hh.in", + out = "test_config.hh", + cmakelists = ["CMakeLists.txt"], + defines = [], +) + +cc_library( + name = "test_utils", + hdrs = [ + "test_config.hh", + "test_utils.hh", + ], + includes = ["."], + deps = [ + GZ_ROOT + "utils:utils", + GZ_ROOT + "sdformat:sdformat", + ], +) + +integration_test_sources = glob( + ["integration/*.cc"], + exclude = [ + "integration/schema_test.cc", + "integration/element_memory_leak.cc", + ], +) + +[cc_test( + name = src.replace("/", "_").replace(".cc", "").replace("integration_", "INTEGRATION_"), + srcs = [ + src, + "integration/toml_parser.hh", + ], + data = [ + GZ_ROOT + "sdformat:sdf", + "integration", + "sdf", + ], + env = { + "GZ_BAZEL": "1", + "GZ_BAZEL_PATH": "sdformat", + }, + includes = ["integration"], + deps = [ + GZ_ROOT + "sdformat:sdformat", + ":test_utils", + "@gtest", + "@gtest//:gtest_main", + ], +) for src in integration_test_sources] + +exports_files([ + "sdf", + "integration", +]) diff --git a/test/integration/CMakeLists.txt b/test/integration/CMakeLists.txt index 9bad38ea4..b7d846e95 100644 --- a/test/integration/CMakeLists.txt +++ b/test/integration/CMakeLists.txt @@ -74,11 +74,22 @@ else() gz_build_warning("xmllint not found. schema_test won't be run") endif() -gz_build_tests(TYPE ${TEST_TYPE} SOURCES ${tests} INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/test) +gz_build_tests(TYPE ${TEST_TYPE} + SOURCES ${tests} + INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/test +) -if (EXISTS ${XMLLINT_EXE}) - # Need to run schema target (build .xsd files) before running schema_test - add_dependencies(${TEST_TYPE}_schema_test schema) + +if (TARGET ${TEST_TYPE}_schema_test) + target_compile_definitions(${TEST_TYPE}_schema_test + PRIVATE + -DSDF_ROOT_SCHEMA="${PROJECT_BINARY_DIR}/sdf/${SDF_PROTOCOL_VERSION}/root.xsd" + ) + + if (EXISTS ${XMLLINT_EXE}) + # Need to run schema target (build .xsd files) before running schema_test + add_dependencies(${TEST_TYPE}_schema_test schema) + endif() endif() # Test symbols having the right name on linux only diff --git a/test/integration/converter.cc b/test/integration/converter.cc index ec062d5ad..1509de722 100644 --- a/test/integration/converter.cc +++ b/test/integration/converter.cc @@ -156,7 +156,7 @@ void ParserStringConverter(const std::string &_version) TEST(ConverterIntegration, UnflattenConversion) { const std::string filename = - sdf::testing::SourceFile("test", "sdf", + sdf::testing::TestFile("sdf", "flattened_test_nested_model_with_frames.sdf"); sdf::SDFPtr sdf(new sdf::SDF()); diff --git a/test/integration/default_elements.cc b/test/integration/default_elements.cc index efad8d8ad..f07cee0c8 100644 --- a/test/integration/default_elements.cc +++ b/test/integration/default_elements.cc @@ -26,14 +26,14 @@ #include "sdf/Root.hh" #include "sdf/World.hh" #include "sdf/Filesystem.hh" + #include "test_config.hh" ////////////////////////////////////////////////// TEST(ExplicitlySetInFile, EmptyRoadSphCoords) { - const std::string testFile = - sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf", - "empty_road_sph_coords.sdf"); + const auto testFile = + sdf::testing::TestFile("sdf", "empty_road_sph_coords.sdf"); sdf::Root root; sdf::Errors errors = root.Load(testFile); @@ -109,9 +109,8 @@ TEST(ExplicitlySetInFile, EmptyRoadSphCoords) ////////////////////////////////////////////////// TEST(ExplicitlySetInFile, EmptyAxis) { - const std::string testFile = - sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf", - "empty_axis.sdf"); + const auto testFile = + sdf::testing::TestFile("sdf", "empty_axis.sdf"); sdf::Root root; sdf::Errors errors = root.Load(testFile); @@ -157,9 +156,8 @@ TEST(ExplicitlySetInFile, EmptyAxis) ////////////////////////////////////////////////// TEST(ExplicitlySetInFile, ToString) { - const std::string testFile = - sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf", - "empty_road_sph_coords.sdf"); + const auto testFile = + sdf::testing::TestFile("sdf", "empty_road_sph_coords.sdf"); sdf::Root root; sdf::Errors errors = root.Load(testFile); diff --git a/test/integration/frame.cc b/test/integration/frame.cc index 9e0438273..1ed67cd3a 100644 --- a/test/integration/frame.cc +++ b/test/integration/frame.cc @@ -1299,8 +1299,7 @@ TEST(DOMFrame, WorldIncludeModel) TEST(Frame, IncludeFrameWithSubmodel) { const std::string MODEL_PATH = - sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "integration", - "model", "box_with_submodel"); + sdf::testing::TestFile("integration", "model", "box_with_submodel"); std::ostringstream stream; std::string version = SDF_VERSION; diff --git a/test/integration/nested_model.cc b/test/integration/nested_model.cc index ec9181c4e..0c63cbc43 100644 --- a/test/integration/nested_model.cc +++ b/test/integration/nested_model.cc @@ -623,7 +623,7 @@ TEST(NestedModel, NestedModelWithFramesDirectComparison) const std::string expectedSdfPath = sdf::testing::TestFile( "integration", "nested_model_with_frames_expected.sdf"); - std::fstream fs; + std::ifstream fs; fs.open(expectedSdfPath); EXPECT_TRUE(fs.is_open()); std::stringstream expected; @@ -779,7 +779,7 @@ TEST(NestedModel, TwoLevelNestedModelWithFramesDirectComparison) const std::string expectedSdfPath = sdf::testing::TestFile( "integration", "two_level_nested_model_with_frames_expected.sdf"); - std::fstream fs; + std::ifstream fs; fs.open(expectedSdfPath); EXPECT_TRUE(fs.is_open()); std::stringstream expected; diff --git a/test/integration/nested_multiple_elements_error.cc b/test/integration/nested_multiple_elements_error.cc index 46c0e975a..7831d2271 100644 --- a/test/integration/nested_multiple_elements_error.cc +++ b/test/integration/nested_multiple_elements_error.cc @@ -26,17 +26,17 @@ #include "sdf/Model.hh" #include "sdf/Root.hh" #include "sdf/World.hh" + #include "test_config.hh" const auto g_testPath = sdf::testing::TestFile(); -const auto g_modelsPath = - sdf::filesystem::append(g_testPath, "integration", "model"); -const auto g_sdfPath = sdf::filesystem::append(g_testPath, "sdf"); +const auto g_modelsPath = sdf::testing::TestFile("integration", "model"); +const auto g_sdfPath = sdf::testing::TestFile("sdf"); ///////////////////////////////////////////////// std::string findFileCb(const std::string &_input) { - return sdf::filesystem::append(g_testPath, "integration", "model", _input); + return sdf::filesystem::append(g_modelsPath, _input); } ////////////////////////////////////////////////// diff --git a/test/integration/schema_test.cc b/test/integration/schema_test.cc index 1fa5e5a8f..99a309fdb 100644 --- a/test/integration/schema_test.cc +++ b/test/integration/schema_test.cc @@ -31,9 +31,7 @@ class SDFSchemaGenerator : public testing::Test public: void runXMLlint(const std::string & model) { - const std::string sdfRootSchema = sdf::filesystem::append( - PROJECT_BINARY_DIR, "sdf", SDF_PROTOCOL_VERSION, "root.xsd"); - + const auto sdfRootSchema = sdf::filesystem::append(SDF_ROOT_SCHEMA); std::string xmllintCmd = "xmllint --noout --schema " + sdfRootSchema + " " + model; std::cout << "CMD[" << xmllintCmd << "]\n"; diff --git a/test/integration/toml_parser.hh b/test/integration/toml_parser.hh index ee3f68ba1..528563cab 100644 --- a/test/integration/toml_parser.hh +++ b/test/integration/toml_parser.hh @@ -135,8 +135,8 @@ struct Document: public Value Document parseToml(const std::string &_filePath, sdf::Errors &_errors) { - std::fstream fs; - fs.open(_filePath, std::fstream::in); + std::ifstream fs; + fs.open(_filePath); if (!fs.good()) { _errors.emplace_back(sdf::ErrorCode::FILE_READ, diff --git a/test/test_config.hh.in b/test/test_config.hh.in index 8e025d922..303eab375 100644 --- a/test/test_config.hh.in +++ b/test/test_config.hh.in @@ -18,17 +18,25 @@ #ifndef SDF_TEST_CONFIG_HH_ #define SDF_TEST_CONFIG_HH_ -#define GZ_PATH "@GZ_PROGRAM@" -#define GZ_TEST_LIBRARY_PATH "${PROJECT_BINARY_DIR}/src:"\ - "@GZ-MSGS_LIBRARY_DIRS@:" -#define PROJECT_SOURCE_PATH "${PROJECT_SOURCE_DIR}" -#define PROJECT_BINARY_DIR "${CMAKE_BINARY_DIR}" -#define SDF_PROTOCOL_VERSION "${SDF_PROTOCOL_VERSION}" -#define SDF_TMP_DIR "tmp-sdf/" - #include #include + +#cmakedefine PROJECT_SOURCE_DIR "@PROJECT_SOURCE_DIR@" +#cmakedefine PROJECT_BINARY_DIR "@PROJECT_BINARY_DIR@" + +#ifdef PROJECT_SOURCE_DIR +constexpr const char* kProjectSourceDir = PROJECT_SOURCE_DIR; +#else +constexpr const char* kProjectSourceDir = ""; +#endif + +#ifdef PROJECT_BINARY_DIR +constexpr const char* kProjectBinaryDir = PROJECT_BINARY_DIR; +#else +constexpr const char* kProjectBinaryDir= ""; +#endif + namespace sdf { namespace testing @@ -40,17 +48,18 @@ namespace sdf /// \return True if directory is set correctly, false otherwise bool ProjectSourcePath(std::string &_sourceDir) { + std::string bazel_path; // Bazel builds set TEST_SRCDIR - if(gz::utils::env("TEST_SRCDIR", _sourceDir)) + if(gz::utils::env("TEST_SRCDIR", _sourceDir) && + gz::utils::env("GZ_BAZEL_PATH", bazel_path)) { _sourceDir = sdf::filesystem::append( - _sourceDir, "__main__", "sdformat"); + _sourceDir, "gz", bazel_path); return true; } else { - // CMake builds set PROJECT_SOURCE_PATH - _sourceDir = PROJECT_SOURCE_PATH; + _sourceDir = kProjectSourceDir; return true; } @@ -70,7 +79,8 @@ namespace sdf } else { - _tmpDir = sdf::filesystem::append(PROJECT_BINARY_DIR, SDF_TMP_DIR); + _tmpDir = sdf::filesystem::append(kProjectBinaryDir, "sdf-tmp"); + sdf::filesystem::create_directory(_tmpDir); return true; } } @@ -92,7 +102,7 @@ namespace sdf } else { - _homeDir = PROJECT_BINARY_DIR; + _homeDir = kProjectBinaryDir; // Set both for linux and windows return gz::utils::setenv("HOME", _homeDir) && gz::utils::setenv("HOMEPATH", _homeDir);