Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for event_handler #150

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions test/test_event_handler_compile_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Copyright 2024 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

import time
from types import SimpleNamespace

from colcon_cmake.event_handler.compile_commands \
import CompileCommandsEventHandler
from colcon_cmake.task.cmake import CMAKE_EXECUTABLE
from colcon_core.command import CommandContext
from colcon_core.event.command import Command
from colcon_core.event.job import JobQueued
from colcon_core.event.timer import TimerEvent
from colcon_core.event_reactor import EventReactorShutdown


def test_compile_commands_simple(tmp_path):
extension = CompileCommandsEventHandler()
extension.context = CommandContext(
command_name='colcon',
args=SimpleNamespace(build_base=str(tmp_path)))

event = JobQueued('job_name')
extension((event, None))

event = JobQueued('another_job_name')
extension((event, None))

# CMake invocation
event = Command([CMAKE_EXECUTABLE, 'foo'], cwd=str(tmp_path))
extension((event, None))
(tmp_path / 'job_name').mkdir()
(tmp_path / 'job_name' / extension.FILENAME).write_text('["foo"]')
(tmp_path / 'another_job_name').mkdir()
(tmp_path / 'another_job_name' / extension.FILENAME).write_text('["bar"]')

# Fin
event = EventReactorShutdown()
extension((event, None))

assert (tmp_path / extension.FILENAME).is_file()


def test_compile_commands_skip(tmp_path):
extension = CompileCommandsEventHandler()
extension.context = CommandContext(
command_name='colcon',
args=SimpleNamespace(build_base=str(tmp_path)))

# Unrelated event
event = TimerEvent()
extension((event, None))

# Non-CMake invocation
event = Command(['not-cmake', 'foo'], cwd=str(tmp_path))
extension((event, None))

# Fin
event = EventReactorShutdown()
extension((event, None))

# No CMake invocations, so nothing to do
assert not any(tmp_path.iterdir())

# CMake invocation
event = Command([CMAKE_EXECUTABLE, 'foo'], cwd=str(tmp_path))
extension((event, None))

# Fin
event = EventReactorShutdown()
extension((event, None))

# No package-level output, so nothing to do
assert not any(tmp_path.iterdir())


def test_compile_commands_cleanup(tmp_path):
extension = CompileCommandsEventHandler()
extension.context = CommandContext(
command_name='colcon',
args=SimpleNamespace(build_base=str(tmp_path)))

# Simulate previous run
(tmp_path / extension.FILENAME).write_text('["foo"]')

# CMake invocation
event = Command([CMAKE_EXECUTABLE, 'foo'], cwd=str(tmp_path))
extension((event, None))

# Fin
event = EventReactorShutdown()
extension((event, None))

# The previous invocation's output should have been removed
assert not any(tmp_path.iterdir())


def test_compile_commands_rerun(tmp_path):
extension = CompileCommandsEventHandler()
extension.context = CommandContext(
command_name='colcon',
args=SimpleNamespace(build_base=str(tmp_path)))

# Simulate previous run 0.1s ago
(tmp_path / extension.FILENAME).write_text('["foo"]')
time.sleep(0.1)

event = JobQueued('job_name')
extension((event, None))

# CMake invocation
event = Command([CMAKE_EXECUTABLE, 'foo'], cwd=str(tmp_path))
extension((event, None))
(tmp_path / 'job_name').mkdir()
(tmp_path / 'job_name' / extension.FILENAME).write_text('["foo"]')

# Fin
event = EventReactorShutdown()
extension((event, None))

assert (tmp_path / extension.FILENAME).is_file()

# Simulate another run which doesn't touch any job-level output

# Fin
event = EventReactorShutdown()
extension((event, None))

assert (tmp_path / extension.FILENAME).is_file()


def test_compile_commands_bad_data(tmp_path):
extension = CompileCommandsEventHandler()
extension.context = CommandContext(
command_name='colcon',
args=SimpleNamespace(build_base=str(tmp_path)))

event = JobQueued('job_name')
extension((event, None))

# CMake invocation
event = Command([CMAKE_EXECUTABLE, 'foo'], cwd=str(tmp_path))
extension((event, None))
(tmp_path / 'job_name').mkdir()
(tmp_path / 'job_name' / extension.FILENAME).write_text('')

# Fin
event = EventReactorShutdown()
extension((event, None))

assert (tmp_path / extension.FILENAME).is_file()
Loading