def build_and_test(args, job):
    compile_with_clang = (args.compile_with_clang and (args.os == 'linux'))
    print('# BEGIN SUBSECTION: build')
    cmd = (([args.colcon_script, 'build', '--base-paths', ('"%s"' % args.sourcespace), '--build-base', ('"%s"' % args.buildspace), '--install-base', ('"%s"' % args.installspace)] + (['--merge-install'] if (not args.isolated) else [])) + args.build_args)
    cmake_args = ['-DBUILD_TESTING=ON', '--no-warn-unused-cli']
    if args.cmake_build_type:
        cmake_args.append(('-DCMAKE_BUILD_TYPE=' + args.cmake_build_type))
    if compile_with_clang:
        cmake_args.extend(['-DCMAKE_C_COMPILER=/usr/bin/clang', '-DCMAKE_CXX_COMPILER=/usr/bin/clang++'])
    if ('--cmake-args' in cmd):
        index = cmd.index('--cmake-args')
        cmd[(index + 1):(index + 1)] = cmake_args
    else:
        cmd.append('--cmake-args')
        cmd.extend(cmake_args)
    if args.coverage:
        if (args.os == 'linux'):
            ament_cmake_args = [(('-DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} ' + gcov_flags) + '"'), (('-DCMAKE_C_FLAGS="${CMAKE_C_FLAGS} ' + gcov_flags) + '"')]
            if ('--ament-cmake-args' in cmd):
                index = cmd.index('--ament-cmake-args')
                cmd[(index + 1):(index + 1)] = ament_cmake_args
            else:
                cmd.append('--ament-cmake-args')
                cmd.extend(ament_cmake_args)
    ret_build = job.run(cmd, shell=True)
    info("colcon build returned: '{0}'".format(ret_build))
    print('# END SUBSECTION')
    if ret_build:
        return ret_build
    print('# BEGIN SUBSECTION: test')
    test_cmd = [args.colcon_script, 'test', '--base-paths', ('"%s"' % args.sourcespace), '--build-base', ('"%s"' % args.buildspace), '--install-base', ('"%s"' % args.installspace)]
    if (not args.isolated):
        test_cmd.append('--merge-install')
    if args.coverage:
        test_cmd.append('--pytest-with-coverage')
    test_cmd.extend(args.test_args)
    if (args.ros_distro == 'foxy'):
        pytest_args = ['-o', 'junit_family=xunit2']
        if ('--pytest-args' in test_cmd):
            pytest_opts_index = (test_cmd.index('--pytest-args') + 1)
            test_cmd = ((test_cmd[:pytest_opts_index] + pytest_args) + test_cmd[pytest_opts_index:])
        else:
            test_cmd.append('--pytest-args')
            test_cmd.extend(pytest_args)
    ret_test = job.run(test_cmd, exit_on_error=False, shell=True)
    info("colcon test returned: '{0}'".format(ret_test))
    print('# END SUBSECTION')
    if ret_test:
        return ret_test
    print('# BEGIN SUBSECTION: test-result --all')
    ret_test_results = job.run([args.colcon_script, 'test-result', '--test-result-base', ('"%s"' % args.buildspace), '--all'], exit_on_error=False, shell=True)
    info("colcon test-result returned: '{0}'".format(ret_test_results))
    print('# END SUBSECTION')
    print('# BEGIN SUBSECTION: test-result')
    ret_test_results = job.run([args.colcon_script, 'test-result', '--test-result-base', ('"%s"' % args.buildspace)], exit_on_error=False, shell=True)
    info("colcon test-result returned: '{0}'".format(ret_test_results))
    print('# END SUBSECTION')
    if (args.coverage and (args.os == 'linux')):
        process_coverage(args, job)
    return 0
def build_and_test(args, job):
    compile_with_clang = (args.compile_with_clang and (args.os == 'linux'))
    print('# BEGIN SUBSECTION: build')
    cmd = (([args.colcon_script, 'build', '--base-paths', ('"%s"' % args.sourcespace), '--build-base', ('"%s"' % args.buildspace), '--install-base', ('"%s"' % args.installspace)] + (['--merge-install'] if (not args.isolated) else [])) + args.build_args)
    cmake_args = ['-DBUILD_TESTING=ON', '--no-warn-unused-cli']
    if args.cmake_build_type:
        cmake_args.append(('-DCMAKE_BUILD_TYPE=' + args.cmake_build_type))
    if compile_with_clang:
        cmake_args.extend(['-DCMAKE_C_COMPILER=/usr/bin/clang', '-DCMAKE_CXX_COMPILER=/usr/bin/clang++'])
    if ('--cmake-args' in cmd):
        index = cmd.index('--cmake-args')
        cmd[(index + 1):(index + 1)] = cmake_args
    else:
        cmd.append('--cmake-args')
        cmd.extend(cmake_args)
    if args.coverage:
        if (args.os == 'linux'):
            ament_cmake_args = [(('-DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} ' + gcov_flags) + '"'), (('-DCMAKE_C_FLAGS="${CMAKE_C_FLAGS} ' + gcov_flags) + '"')]
            if ('--ament-cmake-args' in cmd):
                index = cmd.index('--ament-cmake-args')
                cmd[(index + 1):(index + 1)] = ament_cmake_args
            else:
                cmd.append('--ament-cmake-args')
                cmd.extend(ament_cmake_args)
    ret_build = job.run(cmd, shell=True)
    info("colcon build returned: '{0}'".format(ret_build))
    print('# END SUBSECTION')
    if ret_build:
        return ret_build
    print('# BEGIN SUBSECTION: test')
    test_cmd = [args.colcon_script, 'test', '--base-paths', ('"%s"' % args.sourcespace), '--build-base', ('"%s"' % args.buildspace), '--install-base', ('"%s"' % args.installspace)]
    if (not args.isolated):
        test_cmd.append('--merge-install')
    if args.coverage:
        test_cmd.append('--pytest-with-coverage')
    test_cmd.extend(args.test_args)
    if (args.ros_distro == 'foxy'):
        pytest_args = ['-o', 'junit_family=xunit2']
        if ('--pytest-args' in test_cmd):
            pytest_opts_index = (test_cmd.index('--pytest-args') + 1)
            test_cmd = ((test_cmd[:pytest_opts_index] + pytest_args) + test_cmd[pytest_opts_index:])
        else:
            test_cmd.append('--pytest-args')
            test_cmd.extend(pytest_args)
    ret_test = job.run(test_cmd, exit_on_error=False, shell=True)
    info("colcon test returned: '{0}'".format(ret_test))
    print('# END SUBSECTION')
    if ret_test:
        return ret_test
    print('# BEGIN SUBSECTION: test-result --all')
    ret_test_results = job.run([args.colcon_script, 'test-result', '--test-result-base', ('"%s"' % args.buildspace), '--all'], exit_on_error=False, shell=True)
    info("colcon test-result returned: '{0}'".format(ret_test_results))
    print('# END SUBSECTION')
    print('# BEGIN SUBSECTION: test-result')
    ret_test_results = job.run([args.colcon_script, 'test-result', '--test-result-base', ('"%s"' % args.buildspace)], exit_on_error=False, shell=True)
    info("colcon test-result returned: '{0}'".format(ret_test_results))
    print('# END SUBSECTION')
    if (args.coverage and (args.os == 'linux')):
        process_coverage(args, job)
    return 0