From e140d29c725c9fe419a54198f0a3a81c05c3975f Mon Sep 17 00:00:00 2001 From: Huanyi Xie <74809554+xiehuanyi@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:39:27 +0300 Subject: [PATCH] Preserve filename whitespace when combining an empty base --- fs/path.py | 2 +- tests/test_path.py | 6 ++++++ tests/test_walk.py | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/fs/path.py b/fs/path.py index 0bfa5149..ee81635f 100644 --- a/fs/path.py +++ b/fs/path.py @@ -261,7 +261,7 @@ def combine(path1, path2): """ if not path1: - return path2.lstrip() + return path2 return "{}/{}".format(path1.rstrip("/"), path2.lstrip("/")) diff --git a/tests/test_path.py b/tests/test_path.py index 3ab778ed..c92cb1e1 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -123,6 +123,12 @@ def test_combine(self): self.assertEqual(combine("", "bar"), "bar") self.assertEqual(combine("foo", "bar"), "foo/bar") + def test_combine_preserves_leading_whitespace(self): + for path in (" report.txt", "\tfile", " directory/file"): + self.assertEqual(combine("", path), path) + self.assertEqual(combine("foo", path), "foo/" + path) + self.assertEqual(combine("", "/file"), "/file") + def test_parts(self): self.assertEqual(parts("/"), ["/"]) self.assertEqual(parts(""), ["./"]) diff --git a/tests/test_walk.py b/tests/test_walk.py index e0146f2d..c5ad5884 100644 --- a/tests/test_walk.py +++ b/tests/test_walk.py @@ -25,6 +25,13 @@ def test_on_error_invalid(self): with self.assertRaises(TypeError): walk.Walker(on_error="nope") + def test_files_preserve_leading_whitespace_at_relative_root(self): + with MemoryFS() as fs: + fs.writetext(" report.txt", "content") + paths = list(self.walker.files(fs, path="")) + self.assertEqual(paths, [" report.txt"]) + self.assertEqual(fs.readtext(paths[0]), "content") + class TestBoundWalkerBase(unittest.TestCase): def setUp(self):