From bd9189df871aad9a9a9333fa52155d205628d874 Mon Sep 17 00:00:00 2001 From: Wilson Yan Date: Mon, 5 Jan 2026 19:51:43 +0000 Subject: [PATCH] Add support for with_suffix in Path --- elements/path.py | 4 ++++ tests/test_path.py | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/elements/path.py b/elements/path.py index 20f7335..011cb7d 100644 --- a/elements/path.py +++ b/elements/path.py @@ -106,6 +106,10 @@ def write_text(self, content): def write_bytes(self, content): self.write(content, mode='wb') + def with_suffix(self, suffix): + path = str(self.parent / self.stem) + suffix + return type(self)(path) + def open(self, mode='r'): raise NotImplementedError diff --git a/tests/test_path.py b/tests/test_path.py index cb8be4d..25d420a 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -39,3 +39,14 @@ def test_parent(self): assert (empty / 'foo' / 'bar.txt').parent.parent == empty assert root.parent == root assert empty.parent == empty + + def test_with_suffix(self): + examples = [ + ('foo.a', '.b', 'foo.b'), + ('foo/bar.a.b', '.c', 'foo/bar.c'), + ('foo', '.abc', 'foo.abc'), + ('foo.xyz', '', 'foo'), + ('foo', '', 'foo'), + ] + for path, suffix, output in examples: + assert str(elements.Path(path).with_suffix(suffix)) == output