Skip to content

Commit

Permalink
Fix Rust panic error in signature creation (#1172)
Browse files Browse the repository at this point in the history
* add test for rust panic error
* raise proper ValueError in rust code for add_protein
  • Loading branch information
ctb authored Aug 17, 2020
1 parent d921fc3 commit eb35860
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/core/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,15 @@ impl Signature {
if #[cfg(feature = "parallel")] {
self.signatures
.par_iter_mut()
.for_each(|sketch| {
sketch.add_protein(&seq).unwrap(); }
);
.try_for_each(|sketch| {
sketch.add_protein(&seq) }
)?;
} else {
self.signatures
.iter_mut()
.for_each(|sketch| {
sketch.add_protein(&seq).unwrap(); }
);
.try_for_each(|sketch| {
sketch.add_protein(&seq) }
)?;
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/test_sourmash_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import csv
import pytest
import screed

from . import sourmash_tst_utils as utils
import sourmash
Expand Down Expand Up @@ -98,6 +99,28 @@ def test_protein_override_bad_2():
factory = _signatures_for_sketch_factory(['k=21,dna'],
'protein', False)

def test_protein_override_bad_rust_foo():
# mimic 'sourmash sketch protein -p dna'
factory = _signatures_for_sketch_factory([], 'protein', False)

# reach in and avoid error checking to construct a bad params_list.
factory.params_list = [('dna', {})]

# now, get sigs...
siglist = factory()
assert len(siglist) == 1
sig = siglist[0]

# try adding something
testdata1 = utils.get_test_data('ecoli.faa')
record = next(iter(screed.open(testdata1)))

with pytest.raises(ValueError) as exc:
sig.add_protein(record.sequence)

assert 'Invalid hash function: "dna"' in str(exc)


def test_dayhoff_defaults():
factory = _signatures_for_sketch_factory([], 'dayhoff', True)
params_list = list(factory.get_compute_params())
Expand Down

0 comments on commit eb35860

Please sign in to comment.