|
| 1 | +# Copyright 2024 Open Source Robotics Foundation, Inc. |
| 2 | +# Licensed under the Apache License, Version 2.0 |
| 3 | + |
| 4 | +import asyncio |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +import shutil |
| 8 | +import tempfile |
| 9 | +from types import SimpleNamespace |
| 10 | +import xml.etree.ElementTree as eTree |
| 11 | + |
| 12 | +from colcon_core.event_handler.console_direct import ConsoleDirectEventHandler |
| 13 | +from colcon_core.package_descriptor import PackageDescriptor |
| 14 | +from colcon_core.subprocess import new_event_loop |
| 15 | +from colcon_core.task import TaskContext |
| 16 | +from colcon_ros_cargo.package_identification.ament_cargo import AmentCargoPackageIdentification # noqa: E501 |
| 17 | +from colcon_ros_cargo.task.ament_cargo.build import AmentCargoBuildTask |
| 18 | +from colcon_ros_cargo.task.ament_cargo.test import AmentCargoTestTask |
| 19 | +import pytest |
| 20 | + |
| 21 | +TEST_PACKAGE_NAME = 'rust-sample-package' |
| 22 | + |
| 23 | +test_project_path = Path(__file__).parent / TEST_PACKAGE_NAME |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(autouse=True) |
| 27 | +def monkey_patch_put_event_into_queue(monkeypatch): |
| 28 | + event_handler = ConsoleDirectEventHandler() |
| 29 | + monkeypatch.setattr( |
| 30 | + TaskContext, |
| 31 | + 'put_event_into_queue', |
| 32 | + lambda self, event: event_handler((event, 'cargo')), |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def test_package_identification(): |
| 37 | + cpi = AmentCargoPackageIdentification() |
| 38 | + desc = PackageDescriptor(test_project_path) |
| 39 | + cpi.identify(desc) |
| 40 | + assert desc.type == 'ament_cargo' |
| 41 | + assert desc.name == TEST_PACKAGE_NAME |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.skipif( |
| 45 | + not shutil.which('cargo'), |
| 46 | + reason='Rust must be installed to run this test') |
| 47 | +def test_build_and_test_package(): |
| 48 | + event_loop = new_event_loop() |
| 49 | + asyncio.set_event_loop(event_loop) |
| 50 | + |
| 51 | + try: |
| 52 | + cpi = AmentCargoPackageIdentification() |
| 53 | + package = PackageDescriptor(test_project_path) |
| 54 | + cpi.identify(package) |
| 55 | + |
| 56 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 57 | + tmpdir = Path(tmpdir) |
| 58 | + # TODO(luca) Also test clean build and cargo args |
| 59 | + context = TaskContext(pkg=package, |
| 60 | + args=SimpleNamespace( |
| 61 | + path=str(test_project_path), |
| 62 | + build_base=str(tmpdir / 'build'), |
| 63 | + install_base=str(tmpdir / 'install'), |
| 64 | + clean_build=None, |
| 65 | + cargo_args=None, |
| 66 | + lookup_in_workspace=None, |
| 67 | + ), |
| 68 | + dependencies={} |
| 69 | + ) |
| 70 | + |
| 71 | + task = AmentCargoBuildTask() |
| 72 | + task.set_context(context=context) |
| 73 | + |
| 74 | + src_base = test_project_path / 'src' |
| 75 | + |
| 76 | + source_files_before = set(src_base.rglob('*')) |
| 77 | + rc = event_loop.run_until_complete(task.build()) |
| 78 | + assert not rc |
| 79 | + source_files_after = set(src_base.rglob('*')) |
| 80 | + assert source_files_before == source_files_after |
| 81 | + |
| 82 | + # Make sure the binary is compiled |
| 83 | + install_base = Path(task.context.args.install_base) |
| 84 | + app_name = TEST_PACKAGE_NAME |
| 85 | + executable = TEST_PACKAGE_NAME |
| 86 | + # Executable in windows have a .exe extension |
| 87 | + if os.name == 'nt': |
| 88 | + executable += '.exe' |
| 89 | + assert (install_base / 'lib' / app_name / executable).is_file() |
| 90 | + |
| 91 | + # Now compile tests |
| 92 | + task = AmentCargoTestTask() |
| 93 | + task.set_context(context=context) |
| 94 | + |
| 95 | + # Expect tests to have failed but return code will still be 0 |
| 96 | + # since testing run succeeded |
| 97 | + rc = event_loop.run_until_complete(task.test()) |
| 98 | + assert not rc |
| 99 | + build_base = Path(task.context.args.build_base) |
| 100 | + |
| 101 | + # Make sure the testing files are built |
| 102 | + assert (build_base / 'debug' / 'deps').is_dir() |
| 103 | + assert len(os.listdir(build_base / 'debug' / 'deps')) > 0 |
| 104 | + result_file_path = build_base / 'cargo_test.xml' |
| 105 | + assert result_file_path.is_file() |
| 106 | + check_result_file(result_file_path) |
| 107 | + |
| 108 | + finally: |
| 109 | + event_loop.close() |
| 110 | + |
| 111 | + |
| 112 | +# Check the testing result file, expect cargo test and doc test to fail |
| 113 | +# but fmt to succeed |
| 114 | +def check_result_file(path): |
| 115 | + tree = eTree.parse(path) |
| 116 | + root = tree.getroot() |
| 117 | + testsuite = root.find('testsuite') |
| 118 | + assert testsuite is not None |
| 119 | + unit_result = testsuite.find("testcase[@name='unit']") |
| 120 | + assert unit_result is not None |
| 121 | + assert unit_result.find('failure') is not None |
| 122 | + fmt_result = testsuite.find("testcase[@name='fmt']") |
| 123 | + assert fmt_result is not None |
| 124 | + assert fmt_result.find('failure') is None |
0 commit comments