diff --git a/tools/test_dist.py b/tools/test_dist.py index 6b35f6e9b..e539fda51 100755 --- a/tools/test_dist.py +++ b/tools/test_dist.py @@ -18,6 +18,51 @@ EXPECTED_SCRIPTS = set({'falcon-bench', 'falcon-inspect-app', 'falcon-print-routes'}) EXPECTED_PACKAGES = set({'falcon'}) +# NOTE(danielvjs): These are installed explicitly before the package under test, so that +# we can verify that Falcon itself does not pull in any other distribution. +CHECK_REQUIREMENTS = ('packaging', 'pytest') +# NOTE(danielvjs): pip is bootstrapped by venv itself. +VENV_TOOLING = frozenset({'pip'}) + + +def requirement_closure(names): + """Return the given distributions together with their runtime requirements.""" + import importlib.metadata + + from packaging.requirements import Requirement + from packaging.utils import canonicalize_name + + pending = [canonicalize_name(name) for name in names] + closure = set() + + while pending: + name = pending.pop() + if name in closure: + continue + closure.add(name) + + for spec in importlib.metadata.requires(name) or (): + req = Requirement(spec) + if req.marker is None or req.marker.evaluate({'extra': ''}): + pending.append(canonicalize_name(req.name)) + + return closure + + +def check_installed(allowed): + """Exit with an error if any distributions beyond the allowed ones are found.""" + import importlib.metadata + + from packaging.utils import canonicalize_name + + installed = { + canonicalize_name(dist.metadata['Name']) + for dist in importlib.metadata.distributions() + } + unexpected = sorted(installed - requirement_closure(allowed) - VENV_TOOLING) + if unexpected: + sys.exit(f'Unexpected distributions installed: {", ".join(unexpected)}') + def test_package(package): with tempfile.TemporaryDirectory() as tmpdir: @@ -28,8 +73,8 @@ def test_package(package): logging.info(f'Created a temporary venv in {venv}.') subprocess.check_call((venv_pip, 'install', '--upgrade', 'pip')) - subprocess.check_call((venv_pip, 'install', '-r', REQUIREMENTS)) - logging.info(f'Installed test requirements in {venv}.') + subprocess.check_call((venv_pip, 'install', *CHECK_REQUIREMENTS)) + logging.info(f'Installed {", ".join(CHECK_REQUIREMENTS)} in {venv}.') (venv_site_pkg,) = venv.glob('lib/python*/site-packages') bin_before = {path.name for path in venv_bin.iterdir()} @@ -51,6 +96,15 @@ def test_package(package): f'{pkg_after - pkg_before - EXPECTED_PACKAGES}' ) + subprocess.check_call( + (venv_bin / 'python', __file__, '--check-installed', 'falcon') + + CHECK_REQUIREMENTS + ) + logging.info(f'{package} does not install any third-party distributions.') + + subprocess.check_call((venv_pip, 'install', '-r', REQUIREMENTS)) + logging.info(f'Installed test requirements in {venv}.') + subprocess.check_call((venv_bin / 'pytest', TESTS), cwd=venv) logging.info(f'{package} passes tests.') @@ -59,10 +113,22 @@ def main(): description = 'Test Falcon packages (sdist or generic wheel).' parser = argparse.ArgumentParser(description=description) parser.add_argument( - 'package', metavar='PACKAGE', nargs='+', help='sdist/wheel(s) to test' + 'package', metavar='PACKAGE', nargs='*', help='sdist/wheel(s) to test' + ) + # NOTE(danielvjs): Used internally by test_package() to run the check inside the + # temporary venv, where packaging is available. + parser.add_argument( + '--check-installed', metavar='DIST', nargs='+', help=argparse.SUPPRESS ) args = parser.parse_args() + if args.check_installed: + check_installed(args.check_installed) + return + + if not args.package: + parser.error('the following arguments are required: PACKAGE') + for package in args.package: test_package(package)