Skip to content

Commit

Permalink
Add .symlink_to and .hardlink_to.
Browse files Browse the repository at this point in the history
Ref #214
  • Loading branch information
jaraco committed Apr 8, 2024
1 parent e0ff184 commit d702e7d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions newsfragments/214.feature.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add .symlink_to and .hardlink_to.
16 changes: 16 additions & 0 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,14 @@ def remove_p(self):

# --- Links

def hardlink_to(self, target: str) -> None:
"""
Create a hard link at self, pointing to target.
.. seealso:: :func:`os.link`
"""
os.link(target, self)

def link(self, newpath):
"""Create a hard link at `newpath`, pointing to this file.
Expand All @@ -1489,6 +1497,14 @@ def link(self, newpath):
os.link(self, newpath)
return self._next_class(newpath)

def symlink_to(self, target: str, target_is_directory: bool = True) -> None:
"""
Create a symbolic link at self, pointing to target.
.. seealso:: :func:`os.symlink`
"""
os.symlink(target, self, target_is_directory)

def symlink(self, newlink=None):
"""Create a symbolic link at `newlink`, pointing here.
Expand Down
14 changes: 14 additions & 0 deletions test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,26 @@ def test_get_owner(self):


class TestLinks:
def test_hardlink_to(self, tmpdir):
target = Path(tmpdir) / 'target'
target.write_text('hello', encoding='utf-8')
link = Path(tmpdir).joinpath('link')
link.hardlink_to(target)
assert link.read_text(encoding='utf-8') == 'hello'

def test_link(self, tmpdir):
target = Path(tmpdir) / 'target'
target.write_text('hello', encoding='utf-8')
link = target.link(Path(tmpdir) / 'link')
assert link.read_text(encoding='utf-8') == 'hello'

def test_symlink_to(self, tmpdir):
target = Path(tmpdir) / 'target'
target.write_text('hello', encoding='utf-8')
link = Path(tmpdir).joinpath('link')
link.symlink_to(target)
assert link.read_text(encoding='utf-8') == 'hello'

def test_symlink_none(self, tmpdir):
root = Path(tmpdir)
with root:
Expand Down

0 comments on commit d702e7d

Please sign in to comment.