Skip to content

Commit

Permalink
Merge pull request #34 from 4Catalyzer/delete-nonexistent
Browse files Browse the repository at this point in the history
Don't fail when deleting nonexistent files
  • Loading branch information
taion authored Nov 23, 2016
2 parents 25e4ff2 + 30bdf9f commit ac3b13d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 10 additions & 3 deletions flask_annex/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ def _get_filename(self, key):
return flask.safe_join(self._root_path, key)

def delete(self, key):
os.unlink(self._get_filename(key))
try:
os.unlink(self._get_filename(key))
except OSError as e:
if e.errno != errno.ENOENT:
# It's fine if the file doesn't exist.
raise # pragma: no cover

self._clean_empty_dirs(key)

def _clean_empty_dirs(self, key):
Expand All @@ -30,9 +36,10 @@ def _clean_empty_dirs(self, key):
try:
os.rmdir(dir_name)
except OSError as e:
if e.errno != errno.ENOTEMPTY:
if e.errno == errno.ENOTEMPTY:
break
if e.errno != errno.ENOENT:
raise # pragma: no cover
break

key_dir_name = os.path.dirname(key_dir_name)

Expand Down
5 changes: 4 additions & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def test_delete(self, annex):
annex.delete('foo/bar.txt')
assert not annex.list_keys('foo/bar.txt')

def test_delete_nonexistent(self, annex):
annex.delete('@@nonexistent')

def test_delete_many(self, annex):
assert annex.list_keys('')
annex.delete_many(('foo/bar.txt', 'foo/baz.json'))
annex.delete_many(('foo/bar.txt', 'foo/baz.json', 'foo/@@nonexistent'))
assert not annex.list_keys('')

0 comments on commit ac3b13d

Please sign in to comment.