From 9d0b0683030b96b7042d46de491aa1171693a9ed Mon Sep 17 00:00:00 2001 From: Arjo Chakravarty Date: Wed, 16 Feb 2022 22:56:11 +0800 Subject: [PATCH 1/2] Adds an `isRelativePath` to determine if path is relative This PR adds an `isRelativePath` function to ign-common Filesystem.hh. It is a simple wrapper around `std::filesystem`. The rationale for this is so we can remove `std::Filesystem` from ign-gazebo as this is causing other issues such as https://github.com/ignition-tooling/release-tools/issues/639. Signed-off-by: Arjo Chakravarty --- include/ignition/common/Filesystem.hh | 5 +++++ src/Filesystem.cc | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/include/ignition/common/Filesystem.hh b/include/ignition/common/Filesystem.hh index fb765d523..af835d52f 100644 --- a/include/ignition/common/Filesystem.hh +++ b/include/ignition/common/Filesystem.hh @@ -57,6 +57,11 @@ namespace ignition /// \return True if _path is a file. bool IGNITION_COMMON_VISIBLE isFile(const std::string &_path); + /// \brief Check if the given path is relative. + /// \param[in] _path Path. + /// \return True if _path is relative. + bool IGNITION_COMMON_VISIBLE isRelativePath(const std::string &_path); + /// \brief Create a new directory on the filesystem. Intermediate /// directories must already exist. /// \param[in] _path The new directory path to create diff --git a/src/Filesystem.cc b/src/Filesystem.cc index ce66d6d6f..99cc9094b 100644 --- a/src/Filesystem.cc +++ b/src/Filesystem.cc @@ -71,6 +71,12 @@ bool ignition::common::isFile(const std::string &_path) return fs::is_regular_file(_path); } +///////////////////////////////////////////////// +bool ignition::common::isRelativePath(const std::string &_path) +{ + return fs::path(_path).is_relative(); +} + ///////////////////////////////////////////////// bool ignition::common::createDirectory(const std::string &_path) { From b9dad99dfb4456b915455f6a81ba5ff3d3361443 Mon Sep 17 00:00:00 2001 From: Arjo Chakravarty Date: Thu, 17 Feb 2022 00:06:19 +0800 Subject: [PATCH 2/2] Add unit tests Signed-off-by: Arjo Chakravarty --- src/Filesystem_TEST.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Filesystem_TEST.cc b/src/Filesystem_TEST.cc index 0f77e3ee5..ebe39d698 100644 --- a/src/Filesystem_TEST.cc +++ b/src/Filesystem_TEST.cc @@ -245,6 +245,20 @@ TEST_F(FilesystemTest, exists) EXPECT_FALSE(isDirectory(absoluteSubdir)); } +///////////////////////////////////////////////// +TEST_F(FilesystemTest, relative) +{ + #ifndef _WIN32 + std::string absPath {"/tmp/fstest"}; + std::string relPath {"../fstest"}; + #else + std::string absPath {"C:\\Users\\user\\Desktop\\test.txt"}; + std::string relPath {"user\\Desktop\\test.txt"}; + #endif + EXPECT_FALSE(isRelativePath(absPath)); + EXPECT_TRUE(isRelativePath(relPath)); +} + // The symlink tests require special permissions to work on Windows, // so they will be disabled by default. For more information, see: // https://github.com/ignitionrobotics/ign-common/issues/21