diff --git a/flask_annex/file.py b/flask_annex/file.py index 45e1944..6ed8cf0 100644 --- a/flask_annex/file.py +++ b/flask_annex/file.py @@ -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): @@ -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) diff --git a/tests/helpers.py b/tests/helpers.py index c583507..9c90042 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -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('')