lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 0e3d61a 21/33: Rework modify_directory() docu


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 0e3d61a 21/33: Rework modify_directory() documentation; augment unit test
Date: Mon, 3 May 2021 08:15:54 -0400 (EDT)

branch: master
commit 0e3d61ad8d6f95979f516d76ff8cb7ca3a39275e
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Rework modify_directory() documentation; augment unit test
---
 path_utility.cpp      |  8 +++-----
 path_utility_test.cpp | 14 ++++++++++++--
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/path_utility.cpp b/path_utility.cpp
index 151fa35..86fa92b 100644
--- a/path_utility.cpp
+++ b/path_utility.cpp
@@ -77,11 +77,9 @@
 /// returns 'true' iff the OS reports that such a directory exists;
 /// but the same function call would return 'false' after
 ///   rm -rf /usr/lib ; touch /usr/lib
-/// Notably, path("/bin/sh/") fails because it hasn't the filename.
-/// [original comment for boost:
-/// Notably, path("/bin/sh/") succeeds, silently discarding the
-/// trailing '/'.
-/// ...end boost comment]
+/// It allows both fs::path("/bin/") and fs::path("/bin"): posix
+/// would say the first must be a dirname, while the second could be
+/// a basename, but fs::path includes both those concepts.
 
 fs::path modify_directory
     (fs::path const& original_filepath
diff --git a/path_utility_test.cpp b/path_utility_test.cpp
index beb1d80..787299a 100644
--- a/path_utility_test.cpp
+++ b/path_utility_test.cpp
@@ -89,10 +89,20 @@ void test_modify_directory()
     LMI_TEST_EQUAL("sh"           , modify_directory("sh"     , ""        
).string());
     LMI_TEST_EQUAL("sh"           , modify_directory("/bin/sh", ""        
).string());
 
-    // This is forbidden, consistently with the observed behaviour:
+    // A dirname can have a trailing slash, but a basename cannot:
     //   $ls /bin/sh/
     //   ls: cannot access '/bin/sh/': Not a directory
-    // because "sh/" doesn't have the filename.
+    //
+    // The original boost implementation nevertheless allowed this:
+    //   modify_directory("sh/", "/bin/")
+    // even though 'has_leaf()' was asserted for its first argument,
+    // because its path ctor silently discarded the trailing slash.
+    //
+    // Appropriately, std::filesystem doesn't discard a trailing '/':
+    LMI_TEST_EQUAL('/', fs::path("sh/").string().back());
+    // so 'has_filename()' returns false:
+    LMI_TEST(!fs::path("sh/").has_filename());
+    // and the assertion fires as intended:
     LMI_TEST_THROW
         (modify_directory("sh/", "/bin/")
         ,std::runtime_error



reply via email to

[Prev in Thread] Current Thread [Next in Thread]