From c5a3d1ac4858dee5165247c900d5b7bf081bd7e3 Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Fri, 2 Feb 2024 11:52:44 -0600 Subject: [PATCH] Refactor python build test to improve coverage Switching to a parametrized approach also significantly reduces duplicated code in the test. --- test/spell_check.words | 2 + test/test_build_python.py | 212 ++++++++++++++++++-------------------- 2 files changed, 104 insertions(+), 110 deletions(-) diff --git a/test/spell_check.words b/test/spell_check.words index 28095ef9..d44f7a23 100644 --- a/test/spell_check.words +++ b/test/spell_check.words @@ -47,6 +47,7 @@ hookimpl hookwrapper https importlib +importorskip isatty iterdir junit @@ -56,6 +57,7 @@ lineno linter linux lstrip +minversion mkdtemp monkeypatch namedtuple diff --git a/test/test_build_python.py b/test/test_build_python.py index 630935f5..7780a567 100644 --- a/test/test_build_python.py +++ b/test/test_build_python.py @@ -45,17 +45,32 @@ def monkey_patch_put_event_into_queue(monkeypatch): ) -def _test_build_package(tmp_path_str, *, symlink_install): +def _test_build_package( + tmp_path_str, *, symlink_install, setup_cfg, libexec_pattern, data_files +): + assert not libexec_pattern or setup_cfg, \ + 'The libexec pattern requires use of setup.cfg' + + if setup_cfg and data_files: + pytest.importorskip('setuptools', minversion='40.5.0') + event_loop = new_event_loop() asyncio.set_event_loop(event_loop) try: tmp_path = Path(tmp_path_str) python_build_task = PythonBuildTask() package = PackageDescriptor(tmp_path / 'src') - package.name = 'test_package' + package.name = 'test-package' package.type = 'python' package.metadata['get_python_setup_options'] = lambda _: { 'packages': ['my_module'], + **( + { + 'data_files': [ + ('share/test_package', ['test-resource']), + ] + } if data_files else {} + ) } context = TaskContext( @@ -73,15 +88,56 @@ def _test_build_package(tmp_path_str, *, symlink_install): pkg = python_build_task.context.pkg pkg.path.mkdir(exist_ok=True) - (pkg.path / 'setup.py').write_text( - 'from setuptools import setup\n' - 'setup(\n' - ' name="test-package",\n' - ' packages=["my_module"],\n' - ')\n' - ) + if setup_cfg: + (pkg.path / 'setup.py').write_text( + 'from setuptools import setup\n' + 'setup()\n' + ) + (pkg.path / 'setup.cfg').write_text( + '[metadata]\n' + 'name = test-package\n' + '[options]\n' + 'packages = find:\n' + '[options.entry_points]\n' + 'console_scripts =\n' + ' my_command = my_module:main\n' + + ( + '[develop]\n' + 'script-dir=$base/lib/test_package\n' + '[install]\n' + 'install-scripts=$base/lib/test_package\n' + if libexec_pattern else '' + ) + ( + '[options.data_files]\n' + 'share/test_package = test-resource\n' + if data_files else '' + ) + ) + else: + (pkg.path / 'setup.py').write_text( + 'from setuptools import setup\n' + 'setup(\n' + ' name="test-package",\n' + ' packages=["my_module"],\n' + ' entry_points={\n' + ' "console_scripts": ["my_command = my_module:main"],\n' + ' },\n' + + ( + ' data_files=[\n' + ' ("share/test_package", [\n' + ' "test-resource",\n' + ' ]),\n' + ' ],\n' + if data_files else '' + ) + + ')\n' + ) (pkg.path / 'my_module').mkdir(exist_ok=True) - (pkg.path / 'my_module' / '__init__.py').touch() + (pkg.path / 'test-resource').touch() + (pkg.path / 'my_module' / '__init__.py').write_text( + 'def main():\n' + ' print("Hello, World!")\n' + ) src_base = Path(python_build_task.context.args.path) @@ -94,108 +150,44 @@ def _test_build_package(tmp_path_str, *, symlink_install): build_base = Path(python_build_task.context.args.build_base) assert build_base.rglob('my_module/__init__.py') - return Path(python_build_task.context.args.install_base) + install_base = Path(python_build_task.context.args.install_base) + assert symlink_install == any(install_base.rglob( + 'test-package.egg-link')) + assert symlink_install != any(install_base.rglob( + 'PKG-INFO')) + assert libexec_pattern == any(install_base.rglob( + 'lib/test_package/my_command*')) + assert libexec_pattern != ( + any(install_base.rglob('bin/my_command*')) or + any(install_base.rglob('Scripts/my_command*'))) + assert data_files == any(install_base.rglob( + 'share/test_package/test-resource')) + + if not symlink_install: + pkg_info, = install_base.rglob('PKG-INFO') + assert 'Name: test-package' in pkg_info.read_text().splitlines() finally: event_loop.close() -def test_build_package(): - with TemporaryDirectory(prefix='test_colcon_') as tmp_path_str: - install_base = _test_build_package(tmp_path_str, symlink_install=False) - - assert 1 == len(list(install_base.rglob('my_module/__init__.py'))) - - pkg_info, = install_base.rglob('PKG-INFO') - assert 'Name: test-package' in pkg_info.read_text().splitlines() - - -def test_build_package_symlink(): - with TemporaryDirectory(prefix='test_colcon_') as tmp_path_str: - install_base = _test_build_package(tmp_path_str, symlink_install=True) - - assert 1 == len(list(install_base.rglob('test-package.egg-link'))) - - -def test_build_package_symlink_first(): +@pytest.mark.parametrize( + 'data_files', + [False, True]) +@pytest.mark.parametrize( + 'setup_cfg,libexec_pattern', + [(False, False), (True, False), (True, True)]) +@pytest.mark.parametrize( + 'symlink_first', + [False, True]) +def test_build_package(symlink_first, setup_cfg, libexec_pattern, data_files): with TemporaryDirectory(prefix='test_colcon_') as tmp_path_str: - install_base = _test_build_package(tmp_path_str, symlink_install=True) - - assert 1 == len(list(install_base.rglob('test-package.egg-link'))) - assert 0 == len(list(install_base.rglob('PKG-INFO'))) - - install_base = _test_build_package(tmp_path_str, symlink_install=False) - - assert 0 == len(list(install_base.rglob('test-package.egg-link'))) - assert 1 == len(list(install_base.rglob('PKG-INFO'))) - - -def test_build_package_symlink_second(): - with TemporaryDirectory(prefix='test_colcon_') as tmp_path_str: - install_base = _test_build_package(tmp_path_str, symlink_install=False) - - assert 0 == len(list(install_base.rglob('test-package.egg-link'))) - assert 1 == len(list(install_base.rglob('PKG-INFO'))) - - install_base = _test_build_package(tmp_path_str, symlink_install=True) - - assert 1 == len(list(install_base.rglob('test-package.egg-link'))) - assert 0 == len(list(install_base.rglob('PKG-INFO'))) - - -def test_build_package_libexec_pattern(): - event_loop = new_event_loop() - asyncio.set_event_loop(event_loop) - try: - with TemporaryDirectory(prefix='test_colcon_') as tmp_path_str: - tmp_path = Path(tmp_path_str) - python_build_task = PythonBuildTask() - package = PackageDescriptor(tmp_path / 'src') - package.name = 'test_package' - package.type = 'python' - - context = TaskContext( - pkg=package, - args=SimpleNamespace( - path=str(tmp_path / 'src'), - build_base=str(tmp_path / 'build'), - install_base=str(tmp_path / 'install'), - symlink_install=False, - ), - dependencies={} - ) - python_build_task.set_context(context=context) - - pkg = python_build_task.context.pkg - - pkg.path.mkdir(exist_ok=True) - (pkg.path / 'setup.py').write_text( - 'from setuptools import setup\n' - 'setup()\n' - ) - (pkg.path / 'setup.cfg').write_text( - '[metadata]\n' - 'name = test_package\n' - '[options]\n' - 'packages = find:\n' - '[options.entry_points]\n' - 'console_scripts =\n' - ' my_command = my_module:main\n' - '[develop]\n' - 'script-dir=$base/lib/test_package\n' - '[install]\n' - 'install-scripts=$base/lib/test_package\n' - ) - (pkg.path / 'my_module').mkdir(exist_ok=True) - (pkg.path / 'my_module' / '__init__.py').write_text( - 'def main():\n' - ' print("Hello, World!")\n' - ) - - rc = event_loop.run_until_complete(python_build_task.build()) - assert not rc - - install_base = Path(python_build_task.context.args.install_base) - assert list(install_base.rglob( - '**/lib/test_package/my_command*')) - finally: - event_loop.close() + _test_build_package( + tmp_path_str, symlink_install=symlink_first, + setup_cfg=setup_cfg, libexec_pattern=libexec_pattern, + data_files=data_files) + + # Test again with the symlink flag inverted to validate cleanup + _test_build_package( + tmp_path_str, symlink_install=not symlink_first, + setup_cfg=setup_cfg, libexec_pattern=libexec_pattern, + data_files=data_files)