Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 69 additions & 3 deletions tools/test_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()}
Expand All @@ -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.')

Expand All @@ -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)

Expand Down
Loading