From a4d9f63a2eece6c9c7f7a9343693400feddc6032 Mon Sep 17 00:00:00 2001 From: danielvjs Date: Mon, 26 Oct 2026 11:14:02 -0300 Subject: [PATCH 1/3] test(mintest): fail if unexpected third-party packages are installed The mintest env already runs the test suite with only the packages from requirements/mintest, which covers most of what #1790 asks for. However, nothing actually verified that the virtualenv was free of other distributions, which could accidentally mask missing-dependency issues. Add tools/check_no_third_party_deps.py, which computes the requirement closure of the allowed distributions via importlib.metadata, and run it as the first mintest command so that the env fails early if anything else (besides installer tooling) is present. Refs #1790 --- tools/check_no_third_party_deps.py | 52 ++++++++++++++++++++++++++++++ tox.toml | 3 ++ 2 files changed, 55 insertions(+) create mode 100644 tools/check_no_third_party_deps.py diff --git a/tools/check_no_third_party_deps.py b/tools/check_no_third_party_deps.py new file mode 100644 index 000000000..450bfc749 --- /dev/null +++ b/tools/check_no_third_party_deps.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +import argparse +import importlib.metadata +import sys + +from packaging.requirements import Requirement +from packaging.utils import canonicalize_name + +# NOTE: Installer tooling that may be present in any fresh virtualenv. +INSTALLER_TOOLING = frozenset({'pip', 'setuptools', 'wheel'}) + + +def requirement_closure(names): + """Return the given distributions together with their runtime requirements.""" + 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 unexpected_distributions(allowed): + installed = { + canonicalize_name(dist.metadata['Name']) + for dist in importlib.metadata.distributions() + } + return sorted(installed - requirement_closure(allowed) - INSTALLER_TOOLING) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Check that no unexpected distributions are installed.' + ) + parser.add_argument('allowed', nargs='+', help='allowed distributions') + args = parser.parse_args() + + unexpected = unexpected_distributions(args.allowed) + if unexpected: + sys.exit(f'Unexpected distributions installed: {", ".join(unexpected)}') + + print('OK: no unexpected distributions installed.') diff --git a/tox.toml b/tox.toml index ec4e9d2c4..2e0ea54ea 100644 --- a/tox.toml +++ b/tox.toml @@ -75,7 +75,10 @@ set_env.PIP_CONFIG_FILE = "pip.conf" set_env.PYTHONASYNCIODEBUG = "0" set_env.FALCON_DISABLE_CYTHON = "Y" deps = ["-r requirements/mintest"] +# NOTE: Fail early if anything beyond requirements/mintest (and their own +# dependencies) has sneaked into the virtualenv. commands = [ + ["python", "tools/check_no_third_party_deps.py", "falcon", "coverage", "pytest"], ["coverage", "run", "-m", "pytest", "tests", "-k", "not slow", { replace = "posargs", default = [], extend = true }], ] From 3675b39573e4326bf0cdba32d8e6372f0faaa35b Mon Sep 17 00:00:00 2001 From: danielvjs <71919786+danielvjs@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:33:39 -0300 Subject: [PATCH 2/3] test(dist): check that Falcon pulls in no third-party distributions As suggested in review, move the check from the mintest env to tools/test_dist.py, where coverage is not installed, so the check is even stricter. packaging and pytest are now installed explicitly before the package under test, the package is installed, and then the venv is verified to contain no distributions beyond the requirement closure of falcon, packaging and pytest (plus pip, which venv bootstraps). Only after that are the rest of the test requirements installed. setuptools and wheel are no longer special-cased, as they should not be present in a modern setup. Refs #1790 --- tools/check_no_third_party_deps.py | 52 --------------------- tools/test_dist.py | 72 ++++++++++++++++++++++++++++-- tox.toml | 3 -- 3 files changed, 69 insertions(+), 58 deletions(-) delete mode 100644 tools/check_no_third_party_deps.py diff --git a/tools/check_no_third_party_deps.py b/tools/check_no_third_party_deps.py deleted file mode 100644 index 450bfc749..000000000 --- a/tools/check_no_third_party_deps.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python - -import argparse -import importlib.metadata -import sys - -from packaging.requirements import Requirement -from packaging.utils import canonicalize_name - -# NOTE: Installer tooling that may be present in any fresh virtualenv. -INSTALLER_TOOLING = frozenset({'pip', 'setuptools', 'wheel'}) - - -def requirement_closure(names): - """Return the given distributions together with their runtime requirements.""" - 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 unexpected_distributions(allowed): - installed = { - canonicalize_name(dist.metadata['Name']) - for dist in importlib.metadata.distributions() - } - return sorted(installed - requirement_closure(allowed) - INSTALLER_TOOLING) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser( - description='Check that no unexpected distributions are installed.' - ) - parser.add_argument('allowed', nargs='+', help='allowed distributions') - args = parser.parse_args() - - unexpected = unexpected_distributions(args.allowed) - if unexpected: - sys.exit(f'Unexpected distributions installed: {", ".join(unexpected)}') - - print('OK: no unexpected distributions installed.') diff --git a/tools/test_dist.py b/tools/test_dist.py index 6b35f6e9b..56020d0fa 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: 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: 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: 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) diff --git a/tox.toml b/tox.toml index 2e0ea54ea..ec4e9d2c4 100644 --- a/tox.toml +++ b/tox.toml @@ -75,10 +75,7 @@ set_env.PIP_CONFIG_FILE = "pip.conf" set_env.PYTHONASYNCIODEBUG = "0" set_env.FALCON_DISABLE_CYTHON = "Y" deps = ["-r requirements/mintest"] -# NOTE: Fail early if anything beyond requirements/mintest (and their own -# dependencies) has sneaked into the virtualenv. commands = [ - ["python", "tools/check_no_third_party_deps.py", "falcon", "coverage", "pytest"], ["coverage", "run", "-m", "pytest", "tests", "-k", "not slow", { replace = "posargs", default = [], extend = true }], ] From 6322b725bc28d6ad3db6f906b193e2ad8d17078a Mon Sep 17 00:00:00 2001 From: danielvjs <71919786+danielvjs@users.noreply.github.com> Date: Tue, 10 Nov 2026 00:00:31 -0300 Subject: [PATCH 3/3] chore: prefix code comments with GitHub nick --- tools/test_dist.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/test_dist.py b/tools/test_dist.py index 56020d0fa..e539fda51 100755 --- a/tools/test_dist.py +++ b/tools/test_dist.py @@ -18,10 +18,10 @@ EXPECTED_SCRIPTS = set({'falcon-bench', 'falcon-inspect-app', 'falcon-print-routes'}) EXPECTED_PACKAGES = set({'falcon'}) -# NOTE: These are installed explicitly before the package under test, so that +# 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: pip is bootstrapped by venv itself. +# NOTE(danielvjs): pip is bootstrapped by venv itself. VENV_TOOLING = frozenset({'pip'}) @@ -115,7 +115,7 @@ def main(): parser.add_argument( 'package', metavar='PACKAGE', nargs='*', help='sdist/wheel(s) to test' ) - # NOTE: Used internally by test_package() to run the check inside the + # 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