Add mypy tests through ament_mypy

Signed-off-by: Christophe Bedard <bedard.christophe@gmail.com>
This commit is contained in:
Christophe Bedard 2020-03-01 11:50:31 -05:00
parent aa59950fe2
commit 9450bfeea9
15 changed files with 117 additions and 23 deletions

View file

@ -14,6 +14,7 @@
<test_depend>ament_copyright</test_depend> <test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend> <test_depend>ament_flake8</test_depend>
<test_depend>ament_mypy</test_depend>
<test_depend>ament_pep257</test_depend> <test_depend>ament_pep257</test_depend>
<test_depend>ament_xmllint</test_depend> <test_depend>ament_xmllint</test_depend>
<test_depend>python3-pytest</test_depend> <test_depend>python3-pytest</test_depend>

View file

@ -0,0 +1,22 @@
# Copyright 2019 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ament_mypy.main import main
import pytest
@pytest.mark.mypy
@pytest.mark.linter
def test_mypy():
assert main(argv=[]) == 0, 'Found errors'

View file

@ -15,6 +15,7 @@
<test_depend>ament_copyright</test_depend> <test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend> <test_depend>ament_flake8</test_depend>
<test_depend>ament_mypy</test_depend>
<test_depend>ament_pep257</test_depend> <test_depend>ament_pep257</test_depend>
<test_depend>ament_xmllint</test_depend> <test_depend>ament_xmllint</test_depend>
<test_depend>python3-pytest</test_depend> <test_depend>python3-pytest</test_depend>

View file

@ -0,0 +1,22 @@
# Copyright 2019 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ament_mypy.main import main
import pytest
@pytest.mark.mypy
@pytest.mark.linter
def test_mypy():
assert main(argv=[]) == 0, 'Found errors'

View file

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from typing import List
import unittest import unittest
from tracetools_launch.action import Trace from tracetools_launch.action import Trace
@ -26,7 +27,7 @@ class TestTraceAction(unittest.TestCase):
) )
def test_has_profiling_events(self) -> None: def test_has_profiling_events(self) -> None:
events_lists_match = [ events_lists_match: List[List[str]] = [
[ [
'lttng_ust_cyg_profile_fast:func_entry', 'lttng_ust_cyg_profile_fast:func_entry',
'hashtag:yopo', 'hashtag:yopo',
@ -37,7 +38,7 @@ class TestTraceAction(unittest.TestCase):
'lttng_ust_cyg_profile:func_exit', 'lttng_ust_cyg_profile:func_exit',
], ],
] ]
events_lists_no_match = [ events_lists_no_match: List[List[str]] = [
[ [
'lttng_ust_statedump:bin_info', 'lttng_ust_statedump:bin_info',
'ros2:event', 'ros2:event',
@ -50,7 +51,7 @@ class TestTraceAction(unittest.TestCase):
self.assertFalse(Trace.has_profiling_events(events)) self.assertFalse(Trace.has_profiling_events(events))
def test_has_ust_memory_events(self) -> None: def test_has_ust_memory_events(self) -> None:
events_lists_match = [ events_lists_match: List[List[str]] = [
[ [
'hashtag:yopo', 'hashtag:yopo',
'lttng_ust_libc:malloc', 'lttng_ust_libc:malloc',
@ -60,7 +61,7 @@ class TestTraceAction(unittest.TestCase):
'lttng_ust_libc:still_a_match', 'lttng_ust_libc:still_a_match',
], ],
] ]
events_lists_no_match = [ events_lists_no_match: List[List[str]] = [
[], [],
[ [
'my_random:event', 'my_random:event',

View file

@ -77,7 +77,7 @@ class Trace(Action):
self.__context_names = context_names self.__context_names = context_names
self.__profile_fast = profile_fast self.__profile_fast = profile_fast
self.__logger = logging.get_logger(__name__) self.__logger = logging.get_logger(__name__)
self.__ld_preload_actions = [] self.__ld_preload_actions: List[LdPreload] = []
# Add LD_PRELOAD actions if corresponding events are enabled # Add LD_PRELOAD actions if corresponding events are enabled
if self.has_profiling_events(self.__events_ust): if self.has_profiling_events(self.__events_ust):
self.__ld_preload_actions.append( self.__ld_preload_actions.append(

View file

@ -13,6 +13,7 @@
<test_depend>ament_copyright</test_depend> <test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend> <test_depend>ament_flake8</test_depend>
<test_depend>ament_mypy</test_depend>
<test_depend>ament_pep257</test_depend> <test_depend>ament_pep257</test_depend>
<test_depend>ament_xmllint</test_depend> <test_depend>ament_xmllint</test_depend>
<test_depend>python3-pytest</test_depend> <test_depend>python3-pytest</test_depend>

View file

@ -0,0 +1,22 @@
# Copyright 2019 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ament_mypy.main import main
import pytest
@pytest.mark.mypy
@pytest.mark.linter
def test_mypy():
assert main(argv=[]) == 0, 'Found errors'

View file

@ -23,9 +23,6 @@ import babeltrace
from . import DictEvent from . import DictEvent
BabeltraceEvent = babeltrace.babeltrace.Event
def is_trace_directory(path: str) -> bool: def is_trace_directory(path: str) -> bool:
""" """
Check recursively if a path is a trace directory. Check recursively if a path is a trace directory.
@ -42,7 +39,7 @@ def is_trace_directory(path: str) -> bool:
return traces is not None and len(traces) > 0 return traces is not None and len(traces) > 0
def get_trace_ctf_events(trace_directory: str) -> Iterable[BabeltraceEvent]: def get_trace_ctf_events(trace_directory: str) -> Iterable[babeltrace.babeltrace.Event]:
""" """
Get the events of a trace. Get the events of a trace.
@ -61,7 +58,10 @@ def get_trace_events(trace_directory: str) -> List[DictEvent]:
:param trace_directory: the path to the main/top trace directory :param trace_directory: the path to the main/top trace directory
:return: events :return: events
""" """
return [event_to_dict(event) for event in get_trace_ctf_events(trace_directory)] events: List[DictEvent] = [
event_to_dict(event) for event in get_trace_ctf_events(trace_directory)
]
return events
# List of ignored CTF fields # List of ignored CTF fields
@ -82,7 +82,7 @@ _IGNORED_FIELDS = [
_DISCARD = 'events_discarded' _DISCARD = 'events_discarded'
def event_to_dict(event: BabeltraceEvent) -> DictEvent: def event_to_dict(event: babeltrace.babeltrace.Event) -> DictEvent:
""" """
Convert name, timestamp, and all other keys except those in IGNORED_FIELDS into a dictionary. Convert name, timestamp, and all other keys except those in IGNORED_FIELDS into a dictionary.

View file

@ -20,6 +20,7 @@
<exec_depend>std_msgs</exec_depend> <exec_depend>std_msgs</exec_depend>
<exec_depend>std_srvs</exec_depend> <exec_depend>std_srvs</exec_depend>
<test_depend>ament_cmake_mypy</test_depend>
<test_depend>ament_cmake_pytest</test_depend> <test_depend>ament_cmake_pytest</test_depend>
<test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend> <test_depend>ament_lint_common</test_depend>

View file

@ -13,6 +13,7 @@
<test_depend>ament_copyright</test_depend> <test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend> <test_depend>ament_flake8</test_depend>
<test_depend>ament_mypy</test_depend>
<test_depend>ament_pep257</test_depend> <test_depend>ament_pep257</test_depend>
<test_depend>ament_xmllint</test_depend> <test_depend>ament_xmllint</test_depend>
<test_depend>python3-pytest</test_depend> <test_depend>python3-pytest</test_depend>

View file

@ -0,0 +1,22 @@
# Copyright 2019 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ament_mypy.main import main
import pytest
@pytest.mark.mypy
@pytest.mark.linter
def test_mypy():
assert main(argv=[]) == 0, 'Found errors'

View file

@ -31,14 +31,14 @@ class DefaultArgValueCompleter:
return self.list return self.list
def parse_args(): def parse_args() -> argparse.Namespace:
"""Parse args for tracing.""" """Parse args for tracing."""
parser = argparse.ArgumentParser(description='Setup and launch an LTTng tracing session.') parser = argparse.ArgumentParser(description='Setup and launch an LTTng tracing session.')
add_arguments(parser) add_arguments(parser)
return parser.parse_args() return parser.parse_args()
def add_arguments(parser: argparse.ArgumentParser): def add_arguments(parser: argparse.ArgumentParser) -> None:
parser.add_argument( parser.add_argument(
'-s', '--session-name', dest='session_name', '-s', '--session-name', dest='session_name',
default=path.append_timestamp('session'), default=path.append_timestamp('session'),
@ -47,27 +47,27 @@ def add_arguments(parser: argparse.ArgumentParser):
'-p', '--path', dest='path', '-p', '--path', dest='path',
default=path.DEFAULT_BASE_PATH, default=path.DEFAULT_BASE_PATH,
help='path of the base directory for trace data (default: %(default)s)') help='path of the base directory for trace data (default: %(default)s)')
arg = parser.add_argument( events_ust_arg = parser.add_argument( # type: ignore
'-u', '--ust', nargs='*', dest='events_ust', '-u', '--ust', nargs='*', dest='events_ust',
default=names.DEFAULT_EVENTS_ROS, default=names.DEFAULT_EVENTS_ROS,
help='the ROS UST events to enable (default: see tracetools_trace.tools.names) ' help='the ROS UST events to enable (default: see tracetools_trace.tools.names) '
'[to disable all UST events, ' '[to disable all UST events, '
'provide this flag without any event name]') 'provide this flag without any event name]')
arg.completer = DefaultArgValueCompleter(arg) events_ust_arg.completer = DefaultArgValueCompleter(events_ust_arg) # type: ignore
arg = parser.add_argument( events_kernel_arg = parser.add_argument( # type: ignore
'-k', '--kernel', nargs='*', dest='events_kernel', '-k', '--kernel', nargs='*', dest='events_kernel',
default=names.DEFAULT_EVENTS_KERNEL, default=names.DEFAULT_EVENTS_KERNEL,
help='the kernel events to enable (default: see tracetools_trace.tools.names) ' help='the kernel events to enable (default: see tracetools_trace.tools.names) '
'[to disable all kernel events, ' '[to disable all kernel events, '
'provide this flag without any event name]') 'provide this flag without any event name]')
arg.completer = DefaultArgValueCompleter(arg) events_kernel_arg.completer = DefaultArgValueCompleter(events_kernel_arg) # type: ignore
arg = parser.add_argument( context_arg = parser.add_argument( # type: ignore
'-c', '--context', nargs='*', dest='context_names', '-c', '--context', nargs='*', dest='context_names',
default=names.DEFAULT_CONTEXT, default=names.DEFAULT_CONTEXT,
help='the context names to enable (default: see tracetools_trace.tools.names) ' help='the context names to enable (default: see tracetools_trace.tools.names) '
'[to disable all context names, ' '[to disable all context names, '
'provide this flag without any name]') 'provide this flag without any name]')
arg.completer = DefaultArgValueCompleter(arg) context_arg.completer = DefaultArgValueCompleter(context_arg) # type: ignore
parser.add_argument( parser.add_argument(
'-l', '--list', dest='list', action='store_true', '-l', '--list', dest='list', action='store_true',
help='display lists of enabled events and context names (default: %(default)s)') help='display lists of enabled events and context names (default: %(default)s)')

View file

@ -22,7 +22,7 @@ from typing import Optional
try: try:
from . import lttng_impl from . import lttng_impl
_lttng = lttng_impl _lttng = lttng_impl # type: ignore
# Check lttng module version # Check lttng module version
from distutils.version import StrictVersion from distutils.version import StrictVersion
@ -37,7 +37,7 @@ except ImportError:
# Fall back on stub functions so that this still passes linter checks # Fall back on stub functions so that this still passes linter checks
from . import lttng_stub from . import lttng_stub
_lttng = lttng_stub _lttng = lttng_stub # type: ignore
from .names import DEFAULT_CONTEXT from .names import DEFAULT_CONTEXT
from .names import DEFAULT_EVENTS_KERNEL from .names import DEFAULT_EVENTS_KERNEL

View file

@ -41,7 +41,7 @@ def get_version() -> Union[StrictVersion, None]:
:return: the version as a StrictVersion object, or `None` if it cannot be extracted :return: the version as a StrictVersion object, or `None` if it cannot be extracted
""" """
doc_lines = lttng.__doc__.split('\n') doc_lines = lttng.__doc__.split('\n')
first_line = list(filter(None, doc_lines))[0] first_line: str = list(filter(None, doc_lines))[0]
version_string = first_line.split(' ')[1] version_string = first_line.split(' ')[1]
if not re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+$').match(version_string): if not re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+$').match(version_string):
return None return None
@ -144,7 +144,7 @@ def setup(
# TODO make it possible to add context in userspace and kernel separately, since some context # TODO make it possible to add context in userspace and kernel separately, since some context
# types might only apply to userspace OR kernel; only consider userspace contexts for now # types might only apply to userspace OR kernel; only consider userspace contexts for now
handles_context = [handle_ust] handles_context = [handle_ust]
enabled_handles = list(filter(None, handles_context)) enabled_handles: List[lttng.Handle] = list(filter(None, handles_context))
_add_context(enabled_handles, context_list) _add_context(enabled_handles, context_list)
return full_path return full_path