test_imports.py
This file
Path
tests/test_imports.py
80 lines · 1.5 KiB · Python
Last changed by
`ParamType` and other typing improvements (#3371)Rowlando13before adoption8a2b489
History of this fileTake it
1import json2import subprocess3import sys45from click._compat import WIN67IMPORT_TEST = b"""\8import builtins910found_imports = set()11real_import = builtins.__import__12import sys1314def tracking_import(module, locals=None, globals=None, fromlist=None,15 level=0):16 rv = real_import(module, locals, globals, fromlist, level)17 if globals and globals['__name__'].startswith('click') and level == 0:18 found_imports.add(module)19 return rv20builtins.__import__ = tracking_import2122import click23rv = list(found_imports)24import json25click.echo(json.dumps(rv))26"""2728ALLOWED_IMPORTS = {29 "__future__",30 "abc",31 "codecs",32 "collections",33 "collections.abc",34 "configparser",35 "contextlib",36 "datetime",37 "enum",38 "errno",39 "fcntl",40 "functools",41 "gettext",42 "inspect",43 "io",44 "itertools",45 "os",46 "re",47 "stat",48 "struct",49 "sys",50 "threading",51 "types",52 "typing",53 "uuid",54 "weakref",55}5657if WIN:58 ALLOWED_IMPORTS.update(59 {60 "ctypes",61 "ctypes._layout",62 "ctypes.wintypes",63 "msvcrt",64 "time",65 }66 )676869def test_light_imports():70 c = subprocess.Popen(71 [sys.executable, "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE72 )73 rv = c.communicate(IMPORT_TEST)[0]74 rv = rv.decode("utf-8")75 imported = json.loads(rv)7677 for module in imported:78 if module == "click" or module.startswith("click."):79 continue80 assert module in ALLOWED_IMPORTS