Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 4 additions & 1 deletion test/spell_check.words
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ getaffinity
github
https
importorskip
isdigit
iterdir
itertools
kazys
kislyuk
libx
linter
linux
lstrip
makefile
makefiles
Expand Down Expand Up @@ -54,7 +57,7 @@ setuptools
skipif
stepanas
tagname
tempfile
takewhile
thomas
tmpdir
unittest
Expand Down
55 changes: 26 additions & 29 deletions test/test_environment_cmake_module_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
73 changes: 35 additions & 38 deletions test/test_environment_cmake_prefix_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
9 changes: 7 additions & 2 deletions test/test_flake8.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
131 changes: 65 additions & 66 deletions test/test_package_identification_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
3 changes: 1 addition & 2 deletions test/test_task_cmake_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading