Skip to content

Commit

Permalink
Test python module import order using flake8 (#63)
Browse files Browse the repository at this point in the history
* Add flake8 linter

* Don't deal with flake8-import-order just yet

* Debugging prints

* Reinstate import order rule

* Fix reporting bug by using the inner flake8 style guide

* Fixup

* Add comment on wrapper StyleGuide use

* use flake8 v3 (#1)

* Reorder package.xml

* Get the filenames from the file checkers because input_file isn't called by flake8 anymore

* Output count of all error types

* Get flake8 to use the config file

The current implementation of get_style_guide does not process the config file correctly.

* Error when flake8 v2 found

* Print errors like pep8

* remove __future__ imports

* add schema to manifest files

* Support flake8 v2 as well as v3

* Output checked files

otherwise it's not present in xunit files for tests run directly with nose (not ament_cmake_flake8)

* Prevent v2 imports from happening on systems with v3

* Flake8 replaces pep8+pyflakes
  • Loading branch information
dhood committed Feb 1, 2017
1 parent 9cb400e commit 80ffd3c
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 0 deletions.
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")
19 changes: 19 additions & 0 deletions ament_cmake_flake8/cmake/ament_cmake_flake8_lint_hook.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

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>

0 comments on commit 80ffd3c

Please sign in to comment.