Пример #1
0
def test_logging_long_messages():
    launch_descriptor = LaunchDescriptor()

    output_file = os.path.join(os.path.dirname(__file__), 'test_logging_long_messages')
    handler = create_handler(
        'test_logging_long_messages', launch_descriptor, output_file)
    assert handler, 'Cannot find appropriate handler for %s' % output_file

    # Set the output format to a "verbose" format that is expected by the executable output
    os.environ['RCUTILS_CONSOLE_OUTPUT_FORMAT'] = \
        '[{severity}] [{name}]: {message} ({function_name}() at {file_name}:{line_number})'
    executable = os.path.join(os.getcwd(), 'test_logging_long_messages')
    if os.name == 'nt':
        executable += '.exe'
    launch_descriptor.add_process(
        cmd=[executable],
        name='test_logging_long_messages',
        exit_handler=ignore_exit_handler,  # The process will automatically exit after printing.
        output_handlers=[ConsoleOutput(), handler],
    )

    launcher = DefaultLauncher()
    launcher.add_launch_descriptor(launch_descriptor)
    rc = launcher.launch()

    assert rc == 0, \
        "The launch file failed with exit code '" + str(rc) + "'"

    handler.check()
Пример #2
0
def test_logging_long_messages():
    launch_descriptor = LaunchDescriptor()

    output_file = os.path.join(os.path.dirname(__file__),
                               'test_logging_long_messages')
    filtered = get_default_filtered_prefixes()
    handler = create_handler('test_logging_long_messages',
                             launch_descriptor,
                             output_file,
                             filtered_prefixes=filtered)
    assert handler, 'Cannot find appropriate handler for %s' % output_file

    executable = os.path.join(os.getcwd(), 'test_logging_long_messages')
    if os.name == 'nt':
        executable += '.exe'
    launch_descriptor.add_process(
        cmd=[executable],
        name='test_logging_long_messages',
        exit_handler=default_exit_handler,
        output_handlers=[ConsoleOutput(), handler],
    )

    launcher = DefaultLauncher()
    launcher.add_launch_descriptor(launch_descriptor)
    rc = launcher.launch()

    assert rc == 0, \
        "The launch file failed with exit code '" + str(rc) + "'"

    handler.check()
Пример #3
0
def test_matching():
    output_handlers = []

    launch_descriptor = LaunchDescriptor()

    # This temporary directory and files contained in it will be deleted when the process ends.
    tempdir = tempfile.mkdtemp()
    output_file = tempdir + os.sep + 'testfile'
    full_output_file = output_file + '.regex'
    with open(full_output_file, 'w+') as f:
        f.write('this is line \d\nthis is line [a-z]')

    name = 'test_executable_0'

    handler = create_handler(name, launch_descriptor, output_file)

    assert handler, 'Cannot find appropriate handler for %s' % output_file

    output_handlers.append(handler)

    executable_command = [
        sys.executable,
        os.path.join(os.path.abspath(os.path.dirname(__file__)), 'matching.py')
    ]

    launch_descriptor.add_process(cmd=executable_command,
                                  name=name,
                                  exit_handler=ignore_exit_handler,
                                  output_handlers=output_handlers)

    launcher = DefaultLauncher()
    launcher.add_launch_descriptor(launch_descriptor)
    rc = launcher.launch()

    assert rc == 0, \
        "The launch file failed with exit code '" + str(rc) + "'. "

    for handler in output_handlers:
        handler.check()
Пример #4
0
def test_matching():
    output_handlers = []

    launch_descriptor = LaunchDescriptor()

    # This temporary directory and files contained in it will be deleted when the process ends.
    tempdir = tempfile.mkdtemp()
    output_file = tempdir + os.sep + "testfile"
    full_output_file = output_file + ".regex"
    with open(full_output_file, 'w+') as f:
        f.write('this is line \d\nthis is line [a-z]')

    name = "test_executable_0"

    handler = create_handler(name, launch_descriptor, output_file)

    assert handler, 'Cannot find appropriate handler for %s' % output_file

    output_handlers.append(handler)

    executable_command = [
        sys.executable,
        os.path.join(os.path.abspath(os.path.dirname(__file__)), 'matching.py')]

    launch_descriptor.add_process(
        cmd=executable_command,
        name=name,
        exit_handler=ignore_exit_handler,
        output_handlers=output_handlers)

    launcher = DefaultLauncher()
    launcher.add_launch_descriptor(launch_descriptor)
    rc = launcher.launch()

    assert rc == 0, \
        "The launch file failed with exit code '" + str(rc) + "'. "

    for handler in output_handlers:
        handler.check()
Пример #5
0
def test_logging_output_format():
    launch_descriptor = LaunchDescriptor()

    handlers = []

    # Re-use the test_logging_long_messages test binary and modify the output format from an
    # environment variable.
    executable = os.path.join(os.getcwd(), 'test_logging_long_messages')
    if os.name == 'nt':
        executable += '.exe'
    env_long = dict(os.environ)
    # In this custom output, the long message is output twice, to test both dynamic allocation and
    # re-allocation.
    env_long['RCUTILS_CONSOLE_OUTPUT_FORMAT'] = \
        '[{{name}}].({severity}) output: {file_name}:{line_number} {message}, again: {message} ({function_name}()){'  # noqa
    name = 'test_logging_output_format_long'
    output_file = os.path.join(os.path.dirname(__file__), name)
    handler = create_handler(name, launch_descriptor, output_file)
    assert handler, 'Cannot find appropriate handler for %s' % output_file
    launch_descriptor.add_process(
        cmd=[executable],
        env=env_long,
        name=name,
        exit_handler=ignore_exit_handler,
        output_handlers=[ConsoleOutput(), handler],
    )
    handlers.append(handler)

    env_edge_cases = dict(os.environ)
    # This custom output is to check different edge cases of the output format string parsing.
    env_edge_cases[
        'RCUTILS_CONSOLE_OUTPUT_FORMAT'] = '{}}].({unknown_token}) {{{{'
    name = 'test_logging_output_format_edge_cases'
    output_file = os.path.join(os.path.dirname(__file__), name)
    handler = create_handler(name, launch_descriptor, output_file)
    assert handler, 'Cannot find appropriate handler for %s' % output_file
    launch_descriptor.add_process(
        cmd=[executable],
        env=env_edge_cases,
        name=name,
        exit_handler=ignore_exit_handler,
        output_handlers=[ConsoleOutput(), handler],
    )
    handlers.append(handler)

    env_no_tokens = dict(os.environ)
    # This custom output is to check that there are no issues when no tokens are used.
    env_no_tokens['RCUTILS_CONSOLE_OUTPUT_FORMAT'] = 'no_tokens'
    name = 'test_logging_output_format_no_tokens'
    output_file = os.path.join(os.path.dirname(__file__), name)
    handler = create_handler(name, launch_descriptor, output_file)
    assert handler, 'Cannot find appropriate handler for %s' % output_file
    launch_descriptor.add_process(
        cmd=[executable],
        env=env_no_tokens,
        name=name,
        exit_handler=ignore_exit_handler,
        output_handlers=[ConsoleOutput(), handler],
    )
    handlers.append(handler)

    env_time_tokens = dict(os.environ)
    # This custom output is to check that time stamps work correctly
    env_time_tokens[
        'RCUTILS_CONSOLE_OUTPUT_FORMAT'] = "'{time}' '{time_as_nanoseconds}'"
    name = 'test_logging_output_timestamps'
    output_file = os.path.join(os.path.dirname(__file__), name)
    handler = create_handler(name, launch_descriptor, output_file)
    assert handler, 'Cannot find appropriate handler for %s' % output_file
    launch_descriptor.add_process(
        cmd=[executable],
        env=env_time_tokens,
        name=name,
        exit_handler=ignore_exit_handler,
        output_handlers=[ConsoleOutput(), handler],
    )
    handlers.append(handler)

    launcher = DefaultLauncher()
    launcher.add_launch_descriptor(launch_descriptor)
    rc = launcher.launch()

    assert rc == 0, \
        "The launch file failed with exit code '" + str(rc) + "'"

    for handler in handlers:
        handler.check()