diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 0000000..b774561 --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,22 @@ +# Copyright 2026 Open Source Robotics Foundation, Inc. +# Licensed under the Apache License, Version 2.0 + +from itertools import takewhile +from pathlib import Path + +import pytest + +pytest_version = tuple( + int(x) for x in takewhile(str.isdigit, pytest.__version__.split('.')) +) + +if pytest_version < (3, 9): + @pytest.fixture + def tmp_path(tmpdir): + """ + Compatibility fixture for temporary directory allocation. + + This can be removed when we drop support for platforms with Pytest + versions older than 3.9 (namely Enterprise Linux 8). + """ + return Path(tmpdir) diff --git a/test/spell_check.words b/test/spell_check.words index 71a015d..ab41f6c 100644 --- a/test/spell_check.words +++ b/test/spell_check.words @@ -20,11 +20,14 @@ getaffinity github https importorskip +isdigit iterdir +itertools kazys kislyuk libx linter +linux lstrip makefile makefiles @@ -54,7 +57,7 @@ setuptools skipif stepanas tagname -tempfile +takewhile thomas tmpdir unittest diff --git a/test/test_environment_cmake_module_path.py b/test/test_environment_cmake_module_path.py index 386d6db..35b5fb4 100644 --- a/test/test_environment_cmake_module_path.py +++ b/test/test_environment_cmake_module_path.py @@ -2,40 +2,37 @@ # Copyright 2024 Open Source Robotics Foundation, Inc. # Licensed under the Apache License, Version 2.0 -from pathlib import Path -from tempfile import TemporaryDirectory from unittest.mock import patch from colcon_cmake.environment.cmake_module_path \ import CmakeModulePathEnvironment -def test_cmake_module_path(): +def test_cmake_module_path(tmp_path): extension = CmakeModulePathEnvironment() - with TemporaryDirectory(prefix='test_colcon_') as prefix_path: - prefix_path = Path(prefix_path) - with patch( - 'colcon_cmake.environment.cmake_module_path.' - 'create_environment_hook', - return_value=['/some/hook', '/other/hook'] - ): - # No CMake configs exist - hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') - assert len(hooks) == 0 - - pkg_share_path = prefix_path / 'share' / 'pkg_name' - - # Unrelated file - unrelated_file = pkg_share_path / 'cmake' / 'README.md' - unrelated_file.parent.mkdir(parents=True, exist_ok=True) - unrelated_file.touch() - hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') - assert len(hooks) == 0 - - # FindPkgName.cmake exists - cmake_module = pkg_share_path / 'cmake' / 'FindPkgName.cmake' - cmake_module.parent.mkdir(parents=True, exist_ok=True) - cmake_module.touch() - hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') - assert len(hooks) == 2 + prefix_path = tmp_path + with patch( + 'colcon_cmake.environment.cmake_module_path.' + 'create_environment_hook', + return_value=['/some/hook', '/other/hook'] + ): + # No CMake configs exist + hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') + assert len(hooks) == 0 + + pkg_share_path = prefix_path / 'share' / 'pkg_name' + + # Unrelated file + unrelated_file = pkg_share_path / 'cmake' / 'README.md' + unrelated_file.parent.mkdir(parents=True, exist_ok=True) + unrelated_file.touch() + hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') + assert len(hooks) == 0 + + # FindPkgName.cmake exists + cmake_module = pkg_share_path / 'cmake' / 'FindPkgName.cmake' + cmake_module.parent.mkdir(parents=True, exist_ok=True) + cmake_module.touch() + hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') + assert len(hooks) == 2 diff --git a/test/test_environment_cmake_prefix_path.py b/test/test_environment_cmake_prefix_path.py index c19061f..a73146d 100644 --- a/test/test_environment_cmake_prefix_path.py +++ b/test/test_environment_cmake_prefix_path.py @@ -2,49 +2,46 @@ # Copyright 2024 Open Source Robotics Foundation, Inc. # Licensed under the Apache License, Version 2.0 -from pathlib import Path -from tempfile import TemporaryDirectory from unittest.mock import patch from colcon_cmake.environment.cmake_prefix_path \ import CmakePrefixPathEnvironment -def test_cmake_prefix_path(): +def test_cmake_prefix_path(tmp_path): extension = CmakePrefixPathEnvironment() - with TemporaryDirectory(prefix='test_colcon_') as prefix_path: - prefix_path = Path(prefix_path) - with patch( - 'colcon_cmake.environment.cmake_prefix_path' - '.create_environment_hook', - return_value=['/some/hook', '/other/hook'] - ): - # No CMake configs exist - hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') - assert len(hooks) == 0 - - pkg_share_path = prefix_path / 'share' / 'pkg_name' - - # Unrelated file - unrelated_file = pkg_share_path / 'cmake' / 'README.md' - unrelated_file.parent.mkdir(parents=True, exist_ok=True) - unrelated_file.touch() - hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') - assert len(hooks) == 0 - - # PkgNameConfig.cmake exists - cmake_config = pkg_share_path / 'cmake' / 'PkgNameConfig.cmake' - cmake_config.parent.mkdir(parents=True, exist_ok=True) - cmake_config.touch() - hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') - assert len(hooks) == 2 - cmake_config.unlink() - - # pkg_name-config.cmake exists - cmake_config = pkg_share_path / 'cmake' / 'pkg_name-config.cmake' - cmake_config.parent.mkdir(parents=True, exist_ok=True) - cmake_config.touch() - hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') - assert len(hooks) == 2 - cmake_config.unlink() + prefix_path = tmp_path + with patch( + 'colcon_cmake.environment.cmake_prefix_path' + '.create_environment_hook', + return_value=['/some/hook', '/other/hook'] + ): + # No CMake configs exist + hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') + assert len(hooks) == 0 + + pkg_share_path = prefix_path / 'share' / 'pkg_name' + + # Unrelated file + unrelated_file = pkg_share_path / 'cmake' / 'README.md' + unrelated_file.parent.mkdir(parents=True, exist_ok=True) + unrelated_file.touch() + hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') + assert len(hooks) == 0 + + # PkgNameConfig.cmake exists + cmake_config = pkg_share_path / 'cmake' / 'PkgNameConfig.cmake' + cmake_config.parent.mkdir(parents=True, exist_ok=True) + cmake_config.touch() + hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') + assert len(hooks) == 2 + cmake_config.unlink() + + # pkg_name-config.cmake exists + cmake_config = pkg_share_path / 'cmake' / 'pkg_name-config.cmake' + cmake_config.parent.mkdir(parents=True, exist_ok=True) + cmake_config.touch() + hooks = extension.create_environment_hooks(prefix_path, 'pkg_name') + assert len(hooks) == 2 + cmake_config.unlink() diff --git a/test/test_flake8.py b/test/test_flake8.py index bc7928c..b696fc2 100644 --- a/test/test_flake8.py +++ b/test/test_flake8.py @@ -21,11 +21,16 @@ def test_flake8(): logging.getLogger('pydocstyle').setLevel(logging.WARNING) style_guide = get_style_guide( - extend_ignore=['D100', 'D104'], + extend_ignore=[ + 'D100', 'D104', 'F824', 'I100', 'I201' + ], show_source=True, ) style_guide_tests = get_style_guide( - extend_ignore=['D100', 'D101', 'D102', 'D103', 'D104', 'D105', 'D107'], + extend_ignore=[ + 'D100', 'D101', 'D102', 'D103', 'D104', 'D105', 'D107', + 'F824', 'I100', 'I201' + ], show_source=True, ) diff --git a/test/test_package_identification_cmake.py b/test/test_package_identification_cmake.py index e768447..44a7dd9 100644 --- a/test/test_package_identification_cmake.py +++ b/test/test_package_identification_cmake.py @@ -3,83 +3,82 @@ # Licensed under the Apache License, Version 2.0 from pathlib import Path -from tempfile import TemporaryDirectory from colcon_cmake.package_identification.cmake \ import CmakePackageIdentification from colcon_core.package_descriptor import PackageDescriptor -def test_identify(): +def test_identify(tmp_path): extension = CmakePackageIdentification() - with TemporaryDirectory(prefix='test_colcon_') as basepath: - desc = PackageDescriptor(basepath) - desc.type = 'other' - assert extension.identify(desc) is None - assert desc.name is None + basepath = tmp_path + desc = PackageDescriptor(basepath) + desc.type = 'other' + assert extension.identify(desc) is None + assert desc.name is None - desc.type = None - assert extension.identify(desc) is None - assert desc.name is None - assert desc.type is None + desc.type = None + assert extension.identify(desc) is None + assert desc.name is None + assert desc.type is None - basepath = Path(basepath) - (basepath / 'CMakeLists.txt').write_text('') - assert extension.identify(desc) is None - assert desc.name == basepath.name - assert desc.type == 'cmake' + basepath = Path(basepath) + (basepath / 'CMakeLists.txt').write_text('') + assert extension.identify(desc) is None + assert desc.name == basepath.name + assert desc.type == 'cmake' - desc = PackageDescriptor(basepath) - (basepath / 'CMakeLists.txt').write_text( - 'cmake_minimum_required(VERSION 3.10)\n' - 'project(Project NONE)\n') - assert extension.identify(desc) is None - assert desc.name == 'Project' - assert desc.type == 'cmake' + desc = PackageDescriptor(basepath) + (basepath / 'CMakeLists.txt').write_text( + 'cmake_minimum_required(VERSION 3.10)\n' + 'project(Project NONE)\n') + assert extension.identify(desc) is None + assert desc.name == 'Project' + assert desc.type == 'cmake' - desc = PackageDescriptor(basepath) - (basepath / 'CMakeLists.txt').write_text( - 'cmake_minimum_required(VERSION 3.10)\n' - 'project(Project NONE)\n' - 'catkin_workspace()\n') - assert extension.identify(desc) is None - assert desc.name is None - assert desc.type is None + desc = PackageDescriptor(basepath) + (basepath / 'CMakeLists.txt').write_text( + 'cmake_minimum_required(VERSION 3.10)\n' + 'project(Project NONE)\n' + 'catkin_workspace()\n') + assert extension.identify(desc) is None + assert desc.name is None + assert desc.type is None - desc = PackageDescriptor(basepath) - (basepath / 'CMakeLists.txt').write_text( - 'cmake_minimum_required(VERSION 3.10)\n' - 'project(pkg-name NONE)\n') - assert extension.identify(desc) is None - assert desc.name == 'pkg-name' - assert desc.type == 'cmake' - assert set(desc.dependencies.keys()) == {'build', 'run'} - assert not desc.dependencies['build'] - assert not desc.dependencies['run'] - assert extension.identify(desc) is None - assert desc.name == 'pkg-name' - assert desc.type == 'cmake' + desc = PackageDescriptor(basepath) + (basepath / 'CMakeLists.txt').write_text( + 'cmake_minimum_required(VERSION 3.10)\n' + 'project(pkg-name NONE)\n') + assert extension.identify(desc) is None + assert desc.name == 'pkg-name' + assert desc.type == 'cmake' + assert set(desc.dependencies.keys()) == {'build', 'run'} + assert not desc.dependencies['build'] + assert not desc.dependencies['run'] + assert extension.identify(desc) is None + assert desc.name == 'pkg-name' + assert desc.type == 'cmake' - desc = PackageDescriptor(basepath) - (basepath / 'CMakeLists.txt').write_text( - 'cmake_minimum_required(VERSION 3.10)\n' - 'project(other-name NONE)\n' - 'find_package(PkgConfig REQUIRED)\n' - 'pkg_check_modules(DEP_NAME REQUIRED dep-name>=1.1)\n' - 'add_subdirectory(src)\n') - (basepath / 'src').mkdir(parents=True, exist_ok=True) - (basepath / 'src' / 'CMakeLists.txt').write_text( - 'find_package(dep-name2 REQUIRED)\n') - (basepath / 'src' / 'README.txt').write_text( - 'find_package(other-dep-name REQUIRED)\n') - assert extension.identify(desc) is None - assert desc.name == 'other-name' - assert desc.type == 'cmake' - assert set(desc.dependencies.keys()) == {'build', 'run'} - assert desc.dependencies['build'] == { - 'dep-name', 'dep-name2', 'PkgConfig', - } - assert desc.dependencies['run'] == { - 'dep-name', 'dep-name2', 'PkgConfig', - } + desc = PackageDescriptor(basepath) + (basepath / 'CMakeLists.txt').write_text( + 'cmake_minimum_required(VERSION 3.10)\n' + 'project(other-name NONE)\n' + 'find_package(PkgConfig REQUIRED)\n' + 'pkg_check_modules(DEP_NAME REQUIRED dep-name>=1.1)\n' + 'add_subdirectory(src)\n') + (basepath / 'src').mkdir(parents=True, exist_ok=True) + (basepath / 'src' / 'CMakeLists.txt').write_text( + 'find_package(dep-name2 REQUIRED)\n') + (basepath / 'src' / 'README.txt').write_text( + 'find_package(other-dep-name REQUIRED)\n') + assert extension.identify(desc) is None + assert desc.name == 'other-name' + assert desc.type == 'cmake' + assert set(desc.dependencies.keys()) == {'build', 'run'} + assert desc.dependencies['build'] == { + 'dep-name', 'dep-name2', 'PkgConfig', + } + assert desc.dependencies['run'] == { + 'dep-name', 'dep-name2', 'PkgConfig', + } diff --git a/test/test_task_cmake_build.py b/test/test_task_cmake_build.py index 66fbbde..ee8f8e1 100644 --- a/test/test_task_cmake_build.py +++ b/test/test_task_cmake_build.py @@ -94,6 +94,5 @@ def _test_build_package( @pytest.mark.skipif( os.name == 'nt' and 'VisualStudioVersion' not in os.environ, reason='Must be run from a developer command prompt') -def test_build_package(tmpdir, cmake_target): - tmp_path = Path(tmpdir) +def test_build_package(tmp_path, cmake_target): _test_build_package(tmp_path, cmake_target=cmake_target)