python-click/tests/test_imports.py

69 lines
1.3 KiB
Python
Raw Normal View History

2014-10-16 20:40:34 +02:00
import json
import subprocess
2020-07-21 08:23:42 +02:00
import sys
2014-10-16 20:40:34 +02:00
2015-12-04 16:51:02 +01:00
from click._compat import WIN
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
IMPORT_TEST = b"""\
2021-10-10 03:31:57 +02:00
import builtins
2014-10-16 20:40:34 +02:00
found_imports = set()
real_import = builtins.__import__
import sys
def tracking_import(module, locals=None, globals=None, fromlist=None,
level=0):
rv = real_import(module, locals, globals, fromlist, level)
if globals and globals['__name__'].startswith('click') and level == 0:
found_imports.add(module)
return rv
builtins.__import__ = tracking_import
import click
rv = list(found_imports)
import json
click.echo(json.dumps(rv))
2020-07-21 08:23:42 +02:00
"""
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
ALLOWED_IMPORTS = {
"weakref",
"os",
"struct",
"collections",
"sys",
"contextlib",
"functools",
"stat",
"re",
"codecs",
"inspect",
"itertools",
"io",
"threading",
"errno",
"fcntl",
"datetime",
2021-10-10 03:31:57 +02:00
"enum",
"typing",
"types",
"gettext",
2020-07-21 08:23:42 +02:00
}
2014-10-16 20:40:34 +02:00
2015-12-04 16:51:02 +01:00
if WIN:
2021-10-10 03:31:57 +02:00
ALLOWED_IMPORTS.update(["ctypes", "ctypes.wintypes", "msvcrt", "time"])
2015-12-04 16:51:02 +01:00
2014-10-16 20:40:34 +02:00
def test_light_imports():
2020-07-21 08:23:42 +02:00
c = subprocess.Popen(
[sys.executable, "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
2014-10-16 20:40:34 +02:00
rv = c.communicate(IMPORT_TEST)[0]
2021-10-10 03:31:57 +02:00
rv = rv.decode("utf-8")
2014-10-16 20:40:34 +02:00
imported = json.loads(rv)
for module in imported:
2020-07-21 08:23:42 +02:00
if module == "click" or module.startswith("click."):
2014-10-16 20:40:34 +02:00
continue
assert module in ALLOWED_IMPORTS