Skip to content

Commit

Permalink
Merge pull request #22 from rconradharris/issue21
Browse files Browse the repository at this point in the history
Make link idempotent
  • Loading branch information
rconradharris committed Nov 18, 2013
2 parents d5fcb3e + 78651e7 commit 64294ed
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
15 changes: 13 additions & 2 deletions plypatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,21 @@ def abort(self):

def link(self, patch_repo_path):
"""Link a working-repo to a patch-repo."""
patch_repo_path = os.path.abspath(os.path.expanduser(patch_repo_path))

if not os.path.exists(patch_repo_path):
raise exc.PathNotFound

if self.patch_repo_path:
raise exc.AlreadyLinkedToPatchRepo
existing_patch_repo_path = os.path.abspath(
os.path.expanduser(self.patch_repo_path))

patch_repo_path = os.path.abspath(os.path.expanduser(patch_repo_path))
if os.path.samefile(existing_patch_repo_path, patch_repo_path):
raise exc.AlreadyLinkedToSamePatchRepo(
patch_repo_path=self.patch_repo_path)
else:
raise exc.AlreadyLinkedToDifferentPatchRepo(
patch_repo_path=self.patch_repo_path)

self.config('add', config_key='ply.patchrepo',
config_value=patch_repo_path)
Expand Down
7 changes: 5 additions & 2 deletions plypatch/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ def do(self, args):
"""Link a working-repo to a patch-repo"""
try:
self.working_repo.link(args.path)
except plypatch.exc.AlreadyLinkedToPatchRepo:
die('Already linked to a patch-repo')
except plypatch.exc.AlreadyLinkedToSamePatchRepo:
print 'Already linked to this patch-repo'
except plypatch.exc.AlreadyLinkedToDifferentPatchRepo as e:
die('Already linked to a different patch-repo: %s'
% e.patch_repo_path)


class ResolveCommand(CLICommand):
Expand Down
10 changes: 10 additions & 0 deletions plypatch/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ class PlyException(Exception):


class AlreadyLinkedToPatchRepo(PlyException):
def __init__(self, patch_repo_path=None):
super(AlreadyLinkedToPatchRepo, self).__init__()
self.patch_repo_path = patch_repo_path


class AlreadyLinkedToSamePatchRepo(AlreadyLinkedToPatchRepo):
pass


class AlreadyLinkedToDifferentPatchRepo(AlreadyLinkedToPatchRepo):
pass


Expand Down
8 changes: 6 additions & 2 deletions tests/functional/test_everything.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ def assert_based_on(self, expected):
based_on = re.search('Ply-Based-On: (.*)', commit_msg).group(1)
self.assertEqual(expected, based_on)

def test_cant_link_twice(self):
with self.assertRaises(plypatch.exc.AlreadyLinkedToPatchRepo):
def test_already_linked_same_repo(self):
with self.assertRaises(plypatch.exc.AlreadyLinkedToSamePatchRepo):
self.working_repo.link(self.patch_repo_path)

def test_already_linked_different_repo(self):
with self.assertRaises(plypatch.exc.AlreadyLinkedToDifferentPatchRepo):
self.working_repo.link('/bin')

def test_cant_unlink_if_not_linked(self):
self.working_repo.unlink()
self.assertIs(None, self.working_repo.patch_repo_path)
Expand Down

0 comments on commit 64294ed

Please sign in to comment.