File: test_imports.py

package info (click to toggle)
python-ase 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,936 kB
  • sloc: python: 122,428; xml: 946; makefile: 111; javascript: 47
file content (74 lines) | stat: -rw-r--r-- 2,054 bytes parent folder | download | duplicates (2)
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
from pathlib import Path
from importlib import import_module
from numpy import VisibleDeprecationWarning
import pytest
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', 'kimpy', 'pymysql', 'IPython',
    'docutils',
    'gpaw',  # ase.vibrations.placzek
    'gpaw.lrtddft',  # ase.vibrations.placzek
}

newpy_only_modules = {
    'ase.utils.build_web_page'
}


@pytest.mark.filterwarnings('ignore:Moved to')
def test_imports():
    for module in all_modules:
        if module in deprecated_modules:
            warning = (DeprecationWarning, VisibleDeprecationWarning)
        else:
            warning = None

        try:
            with pytest.warns(warning):
                import_module(module)
        except SyntaxError:
            if module not in newpy_only_modules:
                raise
        except ImportError as err:
            if err.name not in ignore_imports and 'deprecated' not in str(err):
                raise