Skip to content

Commit

Permalink
[MRG] check to make sure that .zip files exist before trying to load …
Browse files Browse the repository at this point in the history
…from them. (#1777)

* raise FileNotFoundError if zipfile doesn't exist

* check to see if zipfile exists

* add test for nonexistent zipfile

* fix to handle the right exception in the test
  • Loading branch information
ctb authored Jan 17, 2022
1 parent 91ed12f commit 1c46d7a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/sourmash/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,11 @@ def save(self, path):
def load(cls, location, traverse_yield_all=False, use_manifest=True):
"Class method to load a zipfile."
from .sbt_storage import ZipStorage

# we can only load from existing zipfiles in this method.
if not os.path.exists(location):
raise FileNotFoundError(location)

storage = ZipStorage(location)
return cls(storage, traverse_yield_all=traverse_yield_all,
use_manifest=use_manifest)
Expand Down
10 changes: 8 additions & 2 deletions src/sourmash/sourmash_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,14 @@ def _load_zipfile(filename, **kwargs):
db = None
if filename.endswith('.zip'):
traverse_yield_all = kwargs['traverse_yield_all']
db = ZipFileLinearIndex.load(filename,
traverse_yield_all=traverse_yield_all)
try:
db = ZipFileLinearIndex.load(filename,
traverse_yield_all=traverse_yield_all)
except FileNotFoundError as exc:
# turn this into a ValueError => proper exception handling by
# _load_database.
raise ValueError(exc)

return db


Expand Down
14 changes: 13 additions & 1 deletion tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,18 @@ def test_index_same_md5sum_sbt_zipstorage(c):
assert len([f for f in zout.namelist() if f.startswith(".sbt.zzz/")]) == 5


def test_zipfile_does_not_exist(runtmp):
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.sourmash('sig', 'describe', 'no-exist.zip')

# old behavior, pre PR #1777
assert 'FileNotFoundError: SOURMASH-MANIFEST.csv' not in str(exc)
assert not os.path.exists(runtmp.output('no-exist.zip'))

# correct behavior
assert "ERROR: Error while reading signatures from 'no-exist.zip'." in str(exc)


@utils.in_thisdir
def test_zipfile_protein_command_search(c):
# test command-line search/gather of zipfile with protein sigs
Expand Down Expand Up @@ -2229,7 +2241,7 @@ def test_lazy_loaded_index_1(runtmp):
assert len(db) == 2

# ...but we should get an error when we call signatures.
with pytest.raises(FileNotFoundError):
with pytest.raises(ValueError):
list(db.signatures())

# but put it back, and all is forgiven. yay!
Expand Down

0 comments on commit 1c46d7a

Please sign in to comment.