Skip to content

Commit

Permalink
Move check_type_error to tests.utils
Browse files Browse the repository at this point in the history
  • Loading branch information
DMRobertson committed Dec 22, 2021
1 parent 098de32 commit 0edfb49
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
8 changes: 1 addition & 7 deletions tests/test_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from nacl.utils import random

from .test_bindings import _box_from_seed_vectors

from .test_utils import check_type_error

VECTORS = [
# privalice, pubalice, privbob, pubbob, nonce, plaintext, ciphertext
Expand Down Expand Up @@ -342,12 +342,6 @@ def test_box_wrong_length():
b.decrypt(b"", b"")


def check_type_error(expected, f, *args):
with pytest.raises(TypeError) as e:
f(*args)
assert expected in str(e.value)


def test_wrong_types():
priv = PrivateKey.generate()

Expand Down
7 changes: 1 addition & 6 deletions tests/test_sealed_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from nacl.exceptions import CryptoError
from nacl.public import PrivateKey, PublicKey, SealedBox

from .test_utils import check_type_error
from .utils import read_crypto_test_vectors


Expand Down Expand Up @@ -97,12 +98,6 @@ def test_sealed_box_decryption(privalice, pubalice, plaintext, encrypted):
assert binascii.hexlify(decrypted) == plaintext


def check_type_error(expected, f, *args):
with pytest.raises(TypeError) as e:
f(*args)
assert expected in str(e.value)


def test_wrong_types():
priv = PrivateKey.generate()

Expand Down
13 changes: 2 additions & 11 deletions tests/test_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import binascii
from typing import Callable, List, Tuple, Union
from typing import List, Tuple, Union

import pytest

Expand All @@ -21,6 +21,7 @@
from nacl.exceptions import BadSignatureError
from nacl.signing import SignedMessage, SigningKey, VerifyKey

from .test_utils import check_type_error
from .utils import assert_equal, assert_not_equal, read_crypto_test_vectors


Expand Down Expand Up @@ -269,16 +270,6 @@ def test_key_conversion(self):
)


# Type safety: it's fine to use `...` here, but mypy config doesn't like it because it's
# an explict `Any`.
def check_type_error( # type: ignore[misc]
expected: str, f: Callable[..., object], *args: object
) -> None:
with pytest.raises(TypeError) as e:
f(*args)
assert expected in str(e.value)


def test_wrong_types():
sk = SigningKey.generate()

Expand Down
12 changes: 11 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Callable

import pytest

Expand Down Expand Up @@ -57,3 +57,13 @@ def test_deterministic_random_bytes_invalid_seed_length():
with pytest.raises(TypeError) as e:
nacl.utils.randombytes_deterministic(100, seed)
assert expected in str(e.value)


# Type safety: it's fine to use `...` here, but mypy config doesn't like it because it's
# an explict `Any`.
def check_type_error( # type: ignore[misc]
expected: str, f: Callable[..., object], *args: object
) -> None:
with pytest.raises(TypeError) as e:
f(*args)
assert expected in str(e.value)

0 comments on commit 0edfb49

Please sign in to comment.