1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
import pytest
import sys
from unittest import (
mock,
)
def clean_module(name):
"""Clean a module import so all variables in it will be created again"""
try:
del sys.modules[name]
except KeyError:
pass
return
def test_import_auto():
clean_module("eth_hash.auto")
from eth_hash.auto import keccak # noqa: F401
def test_import_auto_empty_crash(monkeypatch):
clean_module("eth_hash.auto")
from eth_hash.auto import (
keccak,
)
clean_module("eth_hash.backends.pycryptodome")
clean_module("eth_hash.backends.pysha3")
with mock.patch.dict("sys.modules", {
"sha3": None,
"Crypto": None,
"Cryptodome": None,
"eth_hash.backends": mock.MagicMock()
}):
with pytest.raises(
ImportError, match="None of these hashing backends are installed"
):
keccak(b"eh")
def test_import_and_version():
clean_module("eth_hash")
import eth_hash
assert isinstance(eth_hash.__version__, str)
@pytest.mark.parametrize(
"backend",
[
"pycryptodome",
"pysha3",
],
)
def test_load_by_env(monkeypatch, backend):
clean_module("eth_hash.auto")
from eth_hash.auto import (
keccak,
)
monkeypatch.setenv("ETH_HASH_BACKEND", backend)
clean_module("eth_hash.backends.pycryptodome")
clean_module("eth_hash.backends.pysha3")
with mock.patch.dict("sys.modules", {
"sha3": None,
"Crypto": None,
"Cryptodome": None,
"eth_hash.backends": mock.MagicMock()
}):
with pytest.raises(ImportError) as excinfo:
keccak(b"triggered")
expected_msg = (
f"The backend specified in ETH_HASH_BACKEND, '{backend}', is not installed. "
f'Install with `python -m pip install "eth-hash[{backend}]"`.'
)
assert expected_msg in str(excinfo.value)
def test_load_unavailable_backend_by_env(monkeypatch):
clean_module("eth_hash.auto")
from eth_hash.auto import (
keccak,
)
backend = "this-backend-will-never-exist"
monkeypatch.setenv("ETH_HASH_BACKEND", backend)
with pytest.raises(ValueError) as excinfo:
keccak(b"triggered")
expected_msg = (
f"The backend specified in ETH_HASH_BACKEND, '{backend}', is not supported. "
"Choose one of"
)
assert expected_msg in str(excinfo.value)
|