Skip to content

Commit bc11486

Browse files
committed
Fix up tests for new module structure
Signed-off-by: Emerson Knapp <[email protected]>
1 parent f6483ea commit bc11486

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

launch_frontend_py/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
def launch(actions: List[Entity]) -> LaunchDescription:
28-
"""Entrypoint of the launchfile, produces a LaunchDescription containing the listed entities."""
28+
"""Entrypoint of launchfile, produce a LaunchDescription of the provided entities."""
2929
parser = Parser()
3030
root_entity = Entity('launch', children=actions)
3131
return parser.parse_description(root_entity)
@@ -69,13 +69,13 @@ def __getattr__(name: str) -> Any:
6969
elif name.endswith('_'):
7070
# The name has a trailing underscore, which we add to actions with reserved Python names
7171
# Try again without the underscore
72-
__getattr__(name[:-1])
72+
try:
73+
return __getattr__(name[:-1])
74+
except AttributeError as e:
75+
raise AttributeError(f'{e} (or "{name}")')
7376
else:
7477
# It's not registered, raise the usual error
75-
msg = f'module {__name__} has no attribute "{name}"'
76-
if base_name != name:
77-
msg += ' (or "{base_name}")'
78-
raise AttributeError(msg)
78+
raise AttributeError(f'module {__name__} has no attribute "{name}"')
7979

8080

8181
def __preseed_all_actions() -> None:
@@ -95,4 +95,5 @@ def __preseed_all_actions() -> None:
9595
for action_name in action_names:
9696
__getattr__(action_name)
9797

98+
9899
__preseed_all_actions()

launch_frontend_py/entity.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
"""Module for launch_frontend_py Entity class."""
1616
import builtins
17-
import keyword
1817
from collections.abc import Iterable
18+
import keyword
1919
from typing import (
2020
List,
2121
Optional,
@@ -53,7 +53,6 @@ def make_valid_name(name: str) -> str:
5353
return name
5454

5555

56-
5756
class Entity(BaseEntity):
5857
"""Single item in the intermediate front_end representation."""
5958

test/test_action_list_preseed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ def test_action_list_preseed():
2020
# already filled where possible without explicit access
2121
# picking a representative subset of actions (that are present in Humble+)
2222
for x in ['arg', 'timer', 'executable', 'let', 'group', 'include', 'set_env']:
23-
assert x in launch_frontend_py.actions.__all__
23+
assert x in launch_frontend_py.__all__

test/test_dynamic_attrs.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from launch.action import Action
1616
from launch.frontend import expose_action
17-
from launch_frontend_py import actions
17+
import launch_frontend_py as actions
1818
import pytest
1919

2020

@@ -32,27 +32,38 @@ def parse(cls, entity, parser):
3232
class DynamicCreationTest(Action):
3333
"""Test action that exposes an action after first import."""
3434

35+
def __init__(self, value, **kwargs) -> None:
36+
super().__init__(**kwargs)
37+
self.value = value
38+
3539
@classmethod
3640
def parse(cls, entity, parser):
3741
_, kwargs = super().parse(entity, parser)
42+
kwargs['value'] = kwargs.get('value', None)
3843
return cls, kwargs
3944

4045

41-
def test_dynamic_attrs():
46+
def test_regular_action_load():
47+
test_group = actions.group()
48+
assert test_group.type_name == 'group'
49+
50+
51+
def test_dynamic_action_create():
4252
name = actions.let.__name__
4353
assert name == 'let'
4454
str_repr = str(actions.let)
4555
assert str_repr.startswith('<function let')
4656

57+
58+
def test_invalid_action_raise():
4759
with pytest.raises(AttributeError):
4860
getattr(actions, 'non_existent_action')
4961

5062
with pytest.raises(AttributeError):
5163
_ = actions.other_nonexistent
5264

53-
test_group = actions.group()
54-
assert test_group.type_name == 'group'
5565

66+
def test_dynamic_attrs():
5667
test_arg = actions.arg(name='argname', default='argvalue')
5768
assert test_arg.type_name == 'arg'
5869
assert test_arg.get_attr('name') == 'argname'
@@ -64,14 +75,12 @@ def test_dynamic_attrs():
6475

6576
def test_dynamic_create():
6677
assert actions.foo is not None
67-
6878
assert actions.foo.__name__ == 'foo'
6979
assert actions.foo().type_name == 'foo'
70-
71-
with pytest.raises(AttributeError):
72-
actions.node
80+
x = actions.foo(value='bar')
81+
assert x.get_attr('value') == 'bar'
7382

7483

75-
def test_bultin_suffix():
84+
def test_builtin_suffix():
7685
assert actions.while_.__name__ == 'while_'
7786
assert actions.while_().type_name == 'while_'

0 commit comments

Comments
 (0)