From d4c87c6462d7975d00374ce5995ba00a8c67fcb1 Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 4 Dec 2023 02:02:32 +0000 Subject: [PATCH] Add tests for new methods/properties --- Lib/pathlib.py | 12 +++++------ Lib/test/test_pathlib.py | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 921c46098b7b5e..826556c98a0ac1 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -443,12 +443,6 @@ def with_suffix(self, suffix): else: raise ValueError(f"Invalid suffix {suffix!r}") - def without_trailing_sep(self): - """Return a new path without a trailing slash after its name.""" - if not self.has_trailing_sep: - return self - return self._from_parsed_parts(self.drive, self.root, self._tail, False) - def with_trailing_sep(self): """Return a new path with a trailing slash after its name. If the path has no name, ValueError is raised.""" @@ -459,6 +453,12 @@ def with_trailing_sep(self): raise ValueError(f"{self!r} has an empty name") return self._from_parsed_parts(self.drive, self.root, tail, True) + def without_trailing_sep(self): + """Return a new path without a trailing slash after its name.""" + if not self.has_trailing_sep: + return self + return self._from_parsed_parts(self.drive, self.root, self._tail, False) + def relative_to(self, other, /, *_deprecated, walk_up=False): """Return the relative path to another path identified by the passed arguments. If the operation is not possible (because this is not diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 1d337f7abea6b2..c3c8a808577250 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -653,6 +653,51 @@ def test_with_suffix_common(self): self.assertRaises(ValueError, P('a/b').with_suffix, './.d') self.assertRaises(ValueError, P('a/b').with_suffix, '.d/.') + def test_has_trailing_sep(self): + P = self.cls + self.assertFalse(P().has_trailing_sep) + self.assertFalse(P('').has_trailing_sep) + self.assertFalse(P('.').has_trailing_sep) + self.assertFalse(P('a').has_trailing_sep) + self.assertTrue(P('a/').has_trailing_sep) + self.assertFalse(P('a/b').has_trailing_sep) + self.assertTrue(P('a/b/').has_trailing_sep) + self.assertFalse(P('/').has_trailing_sep) + self.assertFalse(P('/a').has_trailing_sep) + self.assertTrue(P('/a/').has_trailing_sep) + self.assertFalse(P('/a/b').has_trailing_sep) + self.assertTrue(P('/a/b/').has_trailing_sep) + + def test_with_trailing_sep(self): + P = self.cls + self.assertRaises(ValueError, P().with_trailing_sep) + self.assertRaises(ValueError, P('').with_trailing_sep) + self.assertRaises(ValueError, P('.').with_trailing_sep) + self.assertEqual(P('a/'), P('a').with_trailing_sep()) + self.assertEqual(P('a/'), P('a/').with_trailing_sep()) + self.assertEqual(P('a/b/'), P('a/b').with_trailing_sep()) + self.assertEqual(P('a/b/'), P('a/b/').with_trailing_sep()) + self.assertRaises(ValueError, P('/').with_trailing_sep) + self.assertEqual(P('/a/'), P('/a').with_trailing_sep()) + self.assertEqual(P('/a/'), P('/a/').with_trailing_sep()) + self.assertEqual(P('/a/b/'), P('/a/b').with_trailing_sep()) + self.assertEqual(P('/a/b/'), P('/a/b/').with_trailing_sep()) + + def test_without_trailing_sep(self): + P = self.cls + self.assertEqual(P(), P().without_trailing_sep()) + self.assertEqual(P(), P('').without_trailing_sep()) + self.assertEqual(P(), P('.').without_trailing_sep()) + self.assertEqual(P('a'), P('a').without_trailing_sep()) + self.assertEqual(P('a'), P('a/').without_trailing_sep()) + self.assertEqual(P('a/b'), P('a/b').without_trailing_sep()) + self.assertEqual(P('a/b'), P('a/b/').without_trailing_sep()) + self.assertEqual(P('/'), P('/').without_trailing_sep()) + self.assertEqual(P('/a'), P('/a').without_trailing_sep()) + self.assertEqual(P('/a'), P('/a/').without_trailing_sep()) + self.assertEqual(P('/a/b'), P('/a/b').without_trailing_sep()) + self.assertEqual(P('/a/b'), P('/a/b/').without_trailing_sep()) + def test_relative_to_common(self): P = self.cls p = P('a/b')