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

Test python module import order using flake8 #63

Merged
merged 20 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions ament_cmake_flake8/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.5)

project(ament_cmake_flake8 NONE)

find_package(ament_cmake_core REQUIRED)
find_package(ament_cmake_test REQUIRED)

ament_package(
CONFIG_EXTRAS "ament_cmake_flake8-extras.cmake"
)

install(
DIRECTORY cmake
DESTINATION share/${PROJECT_NAME}
)

if(BUILD_TESTING)
find_package(ament_cmake_copyright REQUIRED)
ament_copyright()

find_package(ament_cmake_lint_cmake REQUIRED)
ament_lint_cmake()
endif()
22 changes: 22 additions & 0 deletions ament_cmake_flake8/ament_cmake_flake8-extras.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# 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.

# copied from ament_cmake_flake8/ament_cmake_flake8-extras.cmake

find_package(ament_cmake_test QUIET REQUIRED)

include("${ament_cmake_flake8_DIR}/ament_flake8.cmake")

ament_register_extension("ament_lint_auto" "ament_cmake_flake8"
"ament_cmake_flake8_lint_hook.cmake")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 Open Source Robotics Foundation, Inc.
# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,10 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ament_pyflakes.main import main


def test_pyflakes():
excluded = ['cmakelint.py']
rc = main(argv=['--exclude'] + excluded)
assert rc == 0, 'Found errors'
file(GLOB_RECURSE _python_files FOLLOW_SYMLINKS "*.py")
if(_python_files)
message(STATUS "Added test 'flake8' to check Python code syntax and style conventions")
ament_flake8()
endif()
60 changes: 60 additions & 0 deletions ament_cmake_flake8/cmake/ament_flake8.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# 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.

#
# Add a test to check the Python code for syntax and style compliance
# using flake8.
#
# :param TESTNAME: the name of the test, default: "flake8"
# :type TESTNAME: string
# :param MAX_LINE_LENGTH: override the maximum line length,
# the default is defined in ament_flake8
# :type MAX_LINE_LENGTH: integer
# :param ARGN: the files or directories to check
# :type ARGN: list of strings
#
# @public
#
function(ament_flake8)
cmake_parse_arguments(ARG "" "MAX_LINE_LENGTH;TESTNAME" "" ${ARGN})
if(NOT ARG_TESTNAME)
set(ARG_TESTNAME "flake8")
endif()

find_program(ament_flake8_BIN NAMES "ament_flake8")
if(NOT ament_flake8_BIN)
message(FATAL_ERROR "ament_flake8() could not find program 'ament_flake8'")
endif()

set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${ARG_TESTNAME}.xunit.xml")
set(cmd "${ament_flake8_BIN}" "--xunit-file" "${result_file}")
if(ARG_MAX_LINE_LENGTH)
list(APPEND cmd "--linelength" "${ARG_MAX_LINE_LENGTH}")
endif()
list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS})

file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/ament_flake8")
ament_add_test(
"${ARG_TESTNAME}"
COMMAND ${cmd}
OUTPUT_FILE "${CMAKE_BINARY_DIR}/ament_flake8/${ARG_TESTNAME}.txt"
RESULT_FILE "${result_file}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
set_tests_properties(
"${ARG_TESTNAME}"
PROPERTIES
LABELS "flake8;linter"
)
endfunction()
42 changes: 42 additions & 0 deletions ament_cmake_flake8/doc/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
ament_cmake_flake8
==================

Checks the code syntax and style of Python source files using `flake8
<http://flake8.readthedocs.org/>`_.
Files with the following extensions are being considered: ``.py``.


How to run the check from the command line?
-------------------------------------------

The command line tool is provided by the package `ament_flake8
<https://github.com/ament/ament_lint>`_.


How to run the check from within a CMake ament package as part of the tests?
----------------------------------------------------------------------------

``package.xml``:

.. code:: xml

<buildtool_depend>ament_cmake</buildtool_depend>
<test_depend>ament_cmake_flake8</test_depend>

``CMakeLists.txt``:

.. code:: cmake

find_package(ament_cmake REQUIRED)
if(BUILD_TESTING)
find_package(ament_cmake_flake8 REQUIRED)
ament_flake8()
endif()

When running multiple linters as part of the CMake tests the documentation of
the package `ament_lint_auto <https://github.com/ament/ament_lint>`_ might
contain some useful information.

The documentation of the package `ament_cmake_test
<https://github.com/ament/ament_cmake>`_ provides more information on testing
in CMake ament packages.
25 changes: 25 additions & 0 deletions ament_cmake_flake8/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
<name>ament_cmake_flake8</name>
<version>0.0.0</version>
<description>
The CMake API for ament_flake8 to check code syntax and style conventions
with flake8.
</description>
<maintainer email="dhood@osrfoundation.org">D. Hood</maintainer>
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake_core</buildtool_depend>
<buildtool_depend>ament_cmake_test</buildtool_depend>

<buildtool_export_depend>ament_cmake_test</buildtool_export_depend>
<buildtool_export_depend>ament_flake8</buildtool_export_depend>

<test_depend>ament_cmake_copyright</test_depend>
<test_depend>ament_cmake_lint_cmake</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Empty file.
6 changes: 6 additions & 0 deletions ament_flake8/ament_flake8/configuration/ament_flake8.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[flake8]
ignore = D100,D101,D102,D103,D104,D105,D203,D212,D404
import-order-style = google
max-line-length = 99
show-source = true
statistics = true
79 changes: 79 additions & 0 deletions ament_flake8/ament_flake8/legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3

# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# 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 distutils.version import LooseVersion

import flake8

# Ensure that importing this module doesn't fail when imported on systems with flake8>=3.0.
# This can happen during coverage tests on systems with flake8>=3.0, for example.
if LooseVersion(flake8.__version__) < '3.0':
import flake8.engine
from flake8.reporter import QueueReport

class CustomReport(QueueReport):

errors = []
files = []

def error(self, line_number, offset, text, check):
code = super(CustomReport, self).error(
line_number, offset, text, check)
line = self.lines[line_number - 1] \
if line_number <= len(self.lines) else ''
self.errors.append({
'path': self.filename,
'row': self.line_offset + line_number,
'column': offset + 1,
'error_code': code,
'error_message': text,
'source_line': line.splitlines()[0] if line else '',
})
return code

class CustomStyleGuide(flake8.engine.NoQAStyleGuide):

def input_file(self, filename, **kwargs):
self.options.reporter.files.append(filename)
return super(CustomStyleGuide, self).input_file(filename, **kwargs)

def generate_flake8_report(config_file, paths, excludes, max_line_length=None):
kwargs = {
'repeat': True,
'show_source': True,
'verbose': True,
'reporter': CustomReport,
'config_file': config_file,
'jobs': 1,
}
if max_line_length is not None:
kwargs['max_line_length'] = max_line_length

# add options for flake8 plugins
kwargs['parser'], options_hooks = flake8.engine.get_parser()
flake8style = CustomStyleGuide(**kwargs)
options = flake8style.options
for options_hook in options_hooks:
options_hook(options)

if excludes:
flake8style.options.exclude += excludes

# flake8 uses a wrapper StyleGuide to handle some particular OSErrors
kwargs['styleguide'] = flake8style
wrapperStyleGuide = flake8.engine.StyleGuide(**kwargs)

return wrapperStyleGuide.check_files(paths)
Loading