Skip to content

Commit

Permalink
Port environment hook tests from colcon_core (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
cottsay authored Nov 22, 2024
1 parent 418b0f2 commit e2e679a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/spell_check.words
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cmake
cmakelists
colcon
completers
configs
contextlib
ctest
ctests
Expand Down Expand Up @@ -53,5 +54,6 @@ tagname
tempfile
thomas
tmpdir
unittest
vcxproj
xcode
41 changes: 41 additions & 0 deletions test/test_environment_cmake_module_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2016-2018 Dirk Thomas
# 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():
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
50 changes: 50 additions & 0 deletions test/test_environment_cmake_prefix_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2016-2018 Dirk Thomas
# 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():
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()

0 comments on commit e2e679a

Please sign in to comment.