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
|
import contextlib
from importlib import import_module
from pathlib import Path
import pytest
try:
from numpy.exceptions import VisibleDeprecationWarning # NumPy 2.0.0
except ImportError:
from numpy import ( # type: ignore[attr-defined,no-redef]
VisibleDeprecationWarning,
)
import ase
# This test imports modules.
#
# This test exists because we don't want those modules at the bottom
# of the coverage ranking distracting us from real issues, and because
# importing them is at least slightly better than just ignoring them
# in the coverage stats.
#
# Some modules get 100% coverage because of this, while others
# will still have low coverage. That's okay.
def filenames2modules(filenames):
modules = []
for filename in filenames:
filename = Path(filename).as_posix()
module = filename.rsplit('.', 1)[0]
module = module.replace('/', '.')
if module == 'ase.data.tmxr200x':
continue
modules.append(module)
print(module)
return modules
def glob_modules():
topdir = Path(ase.__file__).parent
for path in topdir.rglob('*.py'):
path = 'ase' / path.relative_to(topdir)
if path.name.startswith('__'):
continue
if path.parts[1] == 'test':
continue
yield path
all_modules = filenames2modules(glob_modules())
deprecated_modules = {'ase.dft.band_structure'}
ignore_imports = {
'flask', 'psycopg2', 'pymysql', 'docutils',
}
newpy_only_modules = {
'ase.utils.build_web_page'
}
@pytest.mark.filterwarnings('ignore:Moved to')
def test_imports():
for module in all_modules:
with contextlib.ExitStack() as ignored_warnings:
if module in deprecated_modules:
ignored_warnings.enter_context(
pytest.warns((DeprecationWarning,
VisibleDeprecationWarning)))
try:
import_module(module)
except SyntaxError:
if module not in newpy_only_modules:
raise
except ImportError as err:
ok = err.name in ignore_imports or 'deprecated' in str(err)
if not ok:
raise
|