Skip to content
Myr

test_imports.py

This file

Path

tests/test_imports.py

56 lines · 1.4 KiB · Python

Last changed by

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption702acf7

History of this file

Take it

1import sys2import json3import subprocess45from click._compat import WIN678IMPORT_TEST = b'''\9try:10    import __builtin__ as builtins11except ImportError:12    import builtins1314found_imports = set()15real_import = builtins.__import__16import sys1718def tracking_import(module, locals=None, globals=None, fromlist=None,19                    level=0):20    rv = real_import(module, locals, globals, fromlist, level)21    if globals and globals['__name__'].startswith('click') and level == 0:22        found_imports.add(module)23    return rv24builtins.__import__ = tracking_import2526import click27rv = list(found_imports)28import json29click.echo(json.dumps(rv))30'''3132ALLOWED_IMPORTS = set([33    'weakref', 'os', 'struct', 'collections', 'sys', 'contextlib',34    'functools', 'stat', 're', 'codecs', 'inspect', 'itertools', 'io',35    'threading', 'colorama', 'errno'36])3738if WIN:39    ALLOWED_IMPORTS.update(['ctypes', 'ctypes.wintypes', 'msvcrt', 'time',40                            'zlib'])414243def test_light_imports():44    c = subprocess.Popen([sys.executable, '-'], stdin=subprocess.PIPE,45                         stdout=subprocess.PIPE)46    rv = c.communicate(IMPORT_TEST)[0]4748    if sys.version_info[0] != 2:49        rv = rv.decode('utf-8')50    imported = json.loads(rv)51    print(imported)5253    for module in imported:54        if module == 'click' or module.startswith('click.'):55            continue56        assert module in ALLOWED_IMPORTS