Skip to content

Commit

Permalink
Add package identification / build tests (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-della-vedova authored Jun 28, 2024
1 parent dd1c038 commit 9fdb14f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/rust-sample-package/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "app"
name = "rust-sample-package"
version = "0.1.0"
authors = ["Nikos Koukis <nickkouk@gmail.com>"]
edition = "2018"
Expand Down
9 changes: 9 additions & 0 deletions test/spell_check.words
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
87 changes: 87 additions & 0 deletions test/test_build.py
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()

0 comments on commit 9fdb14f

Please sign in to comment.