changeset 283:99b78fa04bc1

FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 23 Feb 2025 14:45:26 +0100
parents d507ae4943d5
children b65d25882e44
files cutils/util/walk.py tests/test_walk.py
diffstat 2 files changed, 40 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/util/walk.py	Sun Feb 23 12:40:28 2025 +0100
+++ b/cutils/util/walk.py	Sun Feb 23 14:45:26 2025 +0100
@@ -143,23 +143,26 @@
 
     @staticmethod
     def alt_fs(what):
+        #
+        # Prevent double encoding ...
+        # ... and hope that the current FS encoding is compatible
+        #     with it
+        #
+        if isinstance(what, bytes):
+            s = (what.replace(b'\\', b"\\x5c")
+                 .replace(b'\n', b"\\x0a")
+                 .replace(b'\r', b"\\x0d"))
+        else:
+            s = (what.replace(u'\\', u"\\x5c")
+                 .replace(u'\n', u"\\x0a")
+                 .replace(u'\r', u"\\x0d"))
         if PY2:
-            if isinstance(what, bytes):
-                s = what
+            if isinstance(s, bytes):
+                return s
             else:
-                #
-                # Prevent double encoding ...
-                # ... and hope that the current FS encoding is compatible
-                #     with it
-                #
-                s = what.replace(u'\\', u"\\x5c")
-                s = s.encode(_FSENCODING, "backslashreplace")
-                return s.replace(b'\n', b"\\x0a").replace(b'\r', b"\\x0d")
+                return s.encode(_FSENCODING, "backslashreplace")
         else:
-            s = os.fsencode(what)
-        return (s.replace(b'\\', b"\\x5c")
-                .replace(b'\n', b"\\x0a")
-                .replace(b'\r', b"\\x0d"))
+            return os.fsencode(s)
 
     @property
     def uname(self):
@@ -247,21 +250,31 @@
 
     @staticmethod
     def alt_u8(what):
+        #
+        # Prevent double encoding ...
+        # ... and hope that the current UTF-8 is compatible
+        #     with it
+        #
+        if isinstance(what, bytes):
+            s = (what.replace(b'\\', b"\\x5c")
+                 .replace(b'\n', b"\\x0a")
+                 .replace(b'\r', b"\\x0d"))
+        else:
+            s = (what.replace(u'\\', u"\\x5c")
+                 .replace(u'\n', u"\\x0a")
+                 .replace(u'\r', u"\\x0d"))
         if PY2:
-            if isinstance(what, bytes):
+            if isinstance(s, bytes):
                 try:
-                    s = (what.decode(_FSENCODING, "strict")
-                         .encode("utf-8", "strict"))
+                    return (s.decode(_FSENCODING, "strict")
+                            .encode("utf-8", "strict"))
                 except UnicodeError:
-                    s = (WalkDirEntry.surrogate_decode(what)
-                         .encode("ascii", "backslashreplace"))
+                    return (WalkDirEntry.surrogate_decode(s)
+                            .encode("ascii", "backslashreplace"))
             else:
-                s = what.encode("ascii", "backslashreplace")
+                return s.encode("ascii", "backslashreplace")
         else:
-            s = what.encode("utf-8", "backslashreplace")
-        return (s.replace(b'\\', b"\\x5c")
-                .replace(b'\n', b"\\x0a")
-                .replace(b'\r', b"\\x0d"))
+            return s.encode("utf-8", "backslashreplace")
 
     @property
     def is_symlink(self):
--- a/tests/test_walk.py	Sun Feb 23 12:40:28 2025 +0100
+++ b/tests/test_walk.py	Sun Feb 23 14:45:26 2025 +0100
@@ -127,8 +127,8 @@
             b"tests", b"test-\xc4", _do_stat=False)
         self.assertEqual(b"test-\xc4", entry.name)
         self.assertEqual(b"tests/test-\xc4", entry.path)
-        self.assertEqual(b"test-\xc4", entry.fsname)
-        self.assertEqual(b"tests/test-\xc4", entry.fspath)
+        self.assertIsNone(entry.fsname)
+        self.assertIsNone(entry.fspath)
         self.assertEqual(b"test-\xc4", entry.alt_fsname)
         self.assertEqual(b"tests/test-\xc4", entry.alt_fspath)
 
@@ -147,7 +147,7 @@
         self.assertEqual(b"test", entry.name)
         self.assertEqual(b"tests\xc5/test", entry.path)
         self.assertEqual(b"test", entry.fsname)
-        self.assertEqual(b"tests\xc5/test", entry.fspath)
+        self.assertIsNone(entry.fspath)
         self.assertEqual(b"test", entry.alt_fsname)
         self.assertEqual(b"tests\xc5/test", entry.alt_fspath)