|
| 1 | +# Copyright 2024 Open Source Robotics Foundation, Inc. |
| 2 | +# Licensed under the Apache License, Version 2.0 |
| 3 | + |
| 4 | +import os |
| 5 | +from unittest.mock import Mock |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +from colcon_core.command import CommandContext |
| 9 | +from colcon_core.package_decorator import get_decorators |
| 10 | +from colcon_core.package_descriptor import PackageDescriptor |
| 11 | +from colcon_core.plugin_system import satisfies_version |
| 12 | +from colcon_core.task import TaskExtensionPoint |
| 13 | +from colcon_core.verb.build import BuildVerb |
| 14 | +import pytest |
| 15 | + |
| 16 | + |
| 17 | +class NoopBuildTask(TaskExtensionPoint): |
| 18 | + |
| 19 | + TASK_NAME = 'build' |
| 20 | + PACKAGE_TYPE = 'baz' |
| 21 | + |
| 22 | + def __init__(self): # noqa: D107 |
| 23 | + super().__init__() |
| 24 | + satisfies_version(TaskExtensionPoint.EXTENSION_POINT_VERSION, '^1.0') |
| 25 | + |
| 26 | + async def build(self, *, additional_hooks=None): |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope='module', autouse=True) |
| 31 | +def patch_other_extension_args(): |
| 32 | + with patch('colcon_core.verb.build.add_event_handler_arguments'), \ |
| 33 | + patch('colcon_core.verb.build.add_executor_arguments'), \ |
| 34 | + patch('colcon_core.verb.build.add_packages_arguments'), \ |
| 35 | + patch('colcon_core.verb.build.add_task_arguments'): |
| 36 | + yield |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture(scope='module', autouse=True) |
| 40 | +def patch_get_task_extension(): |
| 41 | + with patch( |
| 42 | + 'colcon_core.verb.build.get_task_extension', |
| 43 | + return_value=NoopBuildTask(), |
| 44 | + ) as get_task_extension: |
| 45 | + yield get_task_extension |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture(scope='module', autouse=True) |
| 49 | +def patch_get_packages(): |
| 50 | + desc1 = PackageDescriptor('foo_bar') |
| 51 | + desc1.type = 'foo' |
| 52 | + desc1.name = 'bar' |
| 53 | + |
| 54 | + desc2 = PackageDescriptor('foo_baz') |
| 55 | + desc2.type = 'foo' |
| 56 | + desc2.name = 'baz' |
| 57 | + |
| 58 | + descriptors = {desc1, desc2} |
| 59 | + decorators = get_decorators(descriptors) |
| 60 | + |
| 61 | + for decorator in decorators: |
| 62 | + decorator.recursive_dependencies = [] |
| 63 | + |
| 64 | + for decorator in decorators: |
| 65 | + decorator.selected = False |
| 66 | + break |
| 67 | + |
| 68 | + with patch( |
| 69 | + 'colcon_core.verb.build.get_packages', |
| 70 | + return_value=decorators, |
| 71 | + ) as get_packages: |
| 72 | + yield get_packages |
| 73 | + |
| 74 | + |
| 75 | +@pytest.fixture(scope='module', autouse=True) |
| 76 | +def patch_execute_jobs(): |
| 77 | + with patch( |
| 78 | + 'colcon_core.verb.build.execute_jobs', |
| 79 | + return_value=0, |
| 80 | + ) as execute_jobs: |
| 81 | + yield execute_jobs |
| 82 | + |
| 83 | + |
| 84 | +def test_add_arguments(): |
| 85 | + extension = BuildVerb() |
| 86 | + parser = Mock() |
| 87 | + parser.add_argument = Mock() |
| 88 | + extension.add_arguments(parser=parser) |
| 89 | + # This extension calls argument adders from other extensions. |
| 90 | + # Verify only that *some* arguments were added. |
| 91 | + assert parser.add_argument.call_count > 4 |
| 92 | + |
| 93 | + |
| 94 | +def test_verb_test(tmpdir): |
| 95 | + extension = BuildVerb() |
| 96 | + extension.add_arguments(parser=Mock()) |
| 97 | + |
| 98 | + context = CommandContext( |
| 99 | + command_name='colcon', |
| 100 | + args=Mock()) |
| 101 | + |
| 102 | + context.args.build_base = os.path.join(tmpdir, 'build') |
| 103 | + context.args.install_base = os.path.join(tmpdir, 'install') |
| 104 | + context.args.test_result_base = os.path.join(tmpdir, 'test_results') |
| 105 | + |
| 106 | + assert 0 == extension.main(context=context) |
0 commit comments