-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add package identification / build tests (#35)
- Loading branch information
1 parent
dd1c038
commit 9fdb14f
Showing
3 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
apache | ||
argcomplete | ||
asyncio | ||
autouse | ||
colcon | ||
completers | ||
easymov | ||
iterdir | ||
linter | ||
lstrip | ||
luca | ||
monkeypatch | ||
nargs | ||
noqa | ||
pathlib | ||
plugin | ||
pydocstyle | ||
pytest | ||
returncode | ||
rglob | ||
rmtree | ||
rtype | ||
scspell | ||
setuptools | ||
skipif | ||
symlink | ||
tempfile | ||
thomas | ||
tmpdir | ||
todo | ||
toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Copyright 2024 Open Source Robotics Foundation, Inc. | ||
# Licensed under the Apache License, Version 2.0 | ||
|
||
import asyncio | ||
import os | ||
from pathlib import Path | ||
import shutil | ||
import tempfile | ||
from types import SimpleNamespace | ||
|
||
from colcon_cargo.package_identification.cargo import CargoPackageIdentification # noqa: E501 | ||
from colcon_cargo.task.cargo.build import CargoBuildTask | ||
from colcon_core.event_handler.console_direct import ConsoleDirectEventHandler | ||
from colcon_core.package_descriptor import PackageDescriptor | ||
from colcon_core.subprocess import new_event_loop | ||
from colcon_core.task import TaskContext | ||
import pytest | ||
|
||
TEST_PACKAGE_NAME = 'rust-sample-package' | ||
|
||
test_project_path = Path(__file__).parent / TEST_PACKAGE_NAME | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def monkey_patch_put_event_into_queue(monkeypatch): | ||
event_handler = ConsoleDirectEventHandler() | ||
monkeypatch.setattr( | ||
TaskContext, | ||
'put_event_into_queue', | ||
lambda self, event: event_handler((event, 'cargo')), | ||
) | ||
|
||
|
||
def test_package_identification(): | ||
cpi = CargoPackageIdentification() | ||
desc = PackageDescriptor(test_project_path) | ||
cpi.identify(desc) | ||
assert desc.type == 'cargo' | ||
assert desc.name == TEST_PACKAGE_NAME | ||
|
||
|
||
@pytest.mark.skipif( | ||
not shutil.which('cargo'), | ||
reason='Rust must be installed to run this test') | ||
def test_build_package(): | ||
event_loop = new_event_loop() | ||
asyncio.set_event_loop(event_loop) | ||
|
||
try: | ||
cpi = CargoPackageIdentification() | ||
package = PackageDescriptor(test_project_path) | ||
cpi.identify(package) | ||
|
||
with tempfile.TemporaryDirectory() as tmpdir: | ||
tmpdir = Path(tmpdir) | ||
# TODO(luca) Also test clean build and cargo args | ||
context = TaskContext(pkg=package, | ||
args=SimpleNamespace( | ||
path=str(test_project_path), | ||
build_base=str(tmpdir / 'build'), | ||
install_base=str(tmpdir / 'install'), | ||
clean_build=None, | ||
cargo_args=None, | ||
), | ||
dependencies={} | ||
) | ||
|
||
task = CargoBuildTask() | ||
task.set_context(context=context) | ||
|
||
src_base = test_project_path / 'src' | ||
|
||
source_files_before = set(src_base.rglob('*')) | ||
rc = event_loop.run_until_complete(task.build()) | ||
assert not rc | ||
source_files_after = set(src_base.rglob('*')) | ||
assert source_files_before == source_files_after | ||
|
||
# Make sure the binary is compiled | ||
install_base = Path(task.context.args.install_base) | ||
app_name = TEST_PACKAGE_NAME | ||
# Executable in windows have a .exe extension | ||
if os.name == 'nt': | ||
app_name += '.exe' | ||
assert (install_base / 'bin' / app_name).is_file() | ||
finally: | ||
event_loop.close() |