Skip to content

Commit

Permalink
Flake8 script file filter properties misbehave pybuilder#202
Browse files Browse the repository at this point in the history
fixes pybuilder#202, connected to pybuilder#202
  • Loading branch information
arcivanov committed Sep 20, 2015
1 parent d902383 commit 81a440d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/main/python/pybuilder/pluginhelper/external_command.py
Expand Up @@ -21,7 +21,6 @@


class ExternalCommandResult(object):

def __init__(self, exit_code, report_file, report_lines, error_report_file, error_report_lines):
self.exit_code = exit_code
self.report_file = report_file
Expand All @@ -31,7 +30,6 @@ def __init__(self, exit_code, report_file, report_lines, error_report_file, erro


class ExternalCommandBuilder(object):

def __init__(self, command_name, project):
self.command_name = command_name
self.parts = [command_name]
Expand Down Expand Up @@ -73,13 +71,15 @@ def run(self, outfile_name):
outfile_name, outfile_lines,
error_file_name, error_file_lines)

def run_on_production_source_files(self, logger, include_test_sources=False, include_scripts=False):
def run_on_production_source_files(self, logger, include_test_sources=False, include_scripts=False,
include_dirs_only=False):
execution_result = execute_tool_on_source_files(project=self.project,
name=self.command_name,
command_and_arguments=self.parts,
include_test_sources=include_test_sources,
include_scripts=include_scripts,
logger=logger)
logger=logger,
include_dirs_only=include_dirs_only)
exit_code, report_file = execution_result
report_lines = read_file(report_file)
error_report_file = '{0}.err'.format(report_file) # TODO @mriehl not dry, execute_tool... should return this
Expand Down
5 changes: 4 additions & 1 deletion src/main/python/pybuilder/plugins/python/flake8_plugin.py
Expand Up @@ -39,6 +39,7 @@ def initialize_flake8_plugin(project):
project.build_depends_on("flake8")
project.set_property_if_unset("flake8_break_build", False)
project.set_property_if_unset("flake8_max_line_length", 120)
project.set_property_if_unset("flake8_include_patterns", None)
project.set_property_if_unset("flake8_exclude_patterns", None)
project.set_property_if_unset("flake8_include_test_sources", False)
project.set_property_if_unset("flake8_include_scripts", False)
Expand Down Expand Up @@ -66,14 +67,16 @@ def analyze(project, logger):
command = ExternalCommandBuilder('flake8', project)
command.use_argument('--ignore={0}').formatted_with_truthy_property('flake8_ignore')
command.use_argument('--max-line-length={0}').formatted_with_property('flake8_max_line_length')
command.use_argument('--filename={0}').formatted_with_truthy_property('flake8_include_patterns')
command.use_argument('--exclude={0}').formatted_with_truthy_property('flake8_exclude_patterns')

include_test_sources = project.get_property("flake8_include_test_sources")
include_scripts = project.get_property("flake8_include_scripts")

result = command.run_on_production_source_files(logger,
include_test_sources=include_test_sources,
include_scripts=include_scripts)
include_scripts=include_scripts,
include_dirs_only=True)

count_of_warnings = len(result.report_lines)
count_of_errors = len(result.error_report_lines)
Expand Down
25 changes: 21 additions & 4 deletions src/main/python/pybuilder/plugins/python/python_plugin_helper.py
Expand Up @@ -16,9 +16,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import itertools

import os

from pybuilder.utils import (discover_modules,
discover_files_matching,
execute_command,
Expand Down Expand Up @@ -50,13 +51,29 @@ def discover_affected_files(include_test_sources, include_scripts, project):
files = itertools.chain(files, discover_python_files(integrationtest_dir))
if include_scripts and project.get_property("dir_source_main_scripts"):
scripts_dir = project.get_property("dir_source_main_scripts")
files = itertools.chain(files, discover_files_matching(scripts_dir, "*")) # we have no idea how scripts might look
files = itertools.chain(files,
discover_files_matching(scripts_dir, "*")) # we have no idea how scripts might look
return files


def discover_affected_dirs(include_test_sources, include_scripts, project):
files = [project.get_property("dir_source_main_python")]
if include_test_sources:
if project.get_property("dir_source_unittest_python"):
files.append(project.get_property("dir_source_unittest_python"))
if project.get_property("dir_source_integrationtest_python"):
files.append(project.get_property("dir_source_integrationtest_python"))
if include_scripts and project.get_property("dir_source_main_scripts"):
files.append(project.get_property("dir_source_main_scripts"))
return files


def execute_tool_on_source_files(project, name, command_and_arguments, logger=None,
include_test_sources=False, include_scripts=False):
files = discover_affected_files(include_test_sources, include_scripts, project)
include_test_sources=False, include_scripts=False, include_dirs_only=False):
if include_dirs_only:
files = discover_affected_dirs(include_test_sources, include_scripts, project)
else:
files = discover_affected_files(include_test_sources, include_scripts, project)

command = as_list(command_and_arguments) + [f for f in files]

Expand Down
18 changes: 18 additions & 0 deletions src/unittest/python/external_command_tests.py
Expand Up @@ -113,6 +113,22 @@ def test_should_execute_external_command(self, _, execute_command):

execute_command.assert_called_with(['command-name', '--foo', '--bar'], 'any-outfile-name')

@patch('pybuilder.pluginhelper.external_command.read_file')
@patch('pybuilder.pluginhelper.external_command.execute_tool_on_source_files')
def test_should_execute_external_command_on_production_source_files_dirs_only(self, execution, read):
execution.return_value = 0, '/tmp/reports/command-name'
logger = Mock()
self.command.run_on_production_source_files(logger, include_dirs_only=True)

execution.assert_called_with(
include_dirs_only=True,
include_test_sources=False,
include_scripts=False,
project=self.project,
logger=logger,
command_and_arguments=['command-name', '--foo', '--bar'],
name='command-name')

@patch('pybuilder.pluginhelper.external_command.read_file')
@patch('pybuilder.pluginhelper.external_command.execute_tool_on_source_files')
def test_should_execute_external_command_on_production_source_files(self, execution, read):
Expand All @@ -121,6 +137,7 @@ def test_should_execute_external_command_on_production_source_files(self, execut
self.command.run_on_production_source_files(logger)

execution.assert_called_with(
include_dirs_only=False,
include_test_sources=False,
include_scripts=False,
project=self.project,
Expand All @@ -136,6 +153,7 @@ def test_should_execute_external_command_on_production_and_test_source_files(sel
self.command.run_on_production_and_test_source_files(logger)

execution.assert_called_with(
include_dirs_only=False,
include_test_sources=True,
include_scripts=False,
project=self.project,
Expand Down
3 changes: 3 additions & 0 deletions src/unittest/python/plugins/python/flake8_plugin_tests.py
Expand Up @@ -19,6 +19,7 @@ def test_should_leave_user_specified_properties_when_initializing_plugin(self):
expected_properties = {
"flake8_break_build": True,
"flake8_max_line_length": 80,
"flake8_include_patterns": "*.py",
"flake8_exclude_patterns": ".svn",
"flake8_include_test_sources": True,
"flake8_include_scripts": True
Expand All @@ -33,6 +34,8 @@ def test_should_leave_user_specified_properties_when_initializing_plugin(self):
self.project.get_property("flake8_break_build"), True)
self.assertEquals(
self.project.get_property("flake8_max_line_length"), 80)
self.assertEquals(
self.project.get_property("flake8_include_patterns"), "*.py")
self.assertEquals(
self.project.get_property("flake8_exclude_patterns"), ".svn")
self.assertEquals(
Expand Down
27 changes: 22 additions & 5 deletions src/unittest/python/plugins/python/python_plugin_helper_tests.py
Expand Up @@ -17,6 +17,7 @@
# limitations under the License.

import unittest

from mock import Mock, call, patch

from pybuilder.plugins.python.python_plugin_helper import (log_report,
Expand All @@ -25,7 +26,6 @@


class LogReportsTest(unittest.TestCase):

def test_should_not_warn_when_report_lines_is_empty(self):
logger = Mock()
log_report(logger, 'name', [])
Expand All @@ -41,7 +41,6 @@ def test_should_warn_when_report_lines_present(self):


class DiscoverAffectedFilesTest(unittest.TestCase):

@patch('pybuilder.plugins.python.python_plugin_helper.discover_python_files')
def test_should_discover_source_files_when_test_sources_not_included(self, discover_python_files):
project = Mock()
Expand Down Expand Up @@ -78,13 +77,15 @@ def test_should_discover_source_files_when_scripts_are_included(self, discover_f
discover_files_matching.assert_called_with('dir_source_main_scripts', '*')

@patch('pybuilder.plugins.python.python_plugin_helper.discover_python_files')
def test_should_discover_source_files_when_test_sources_are_included_and_only_unittests(self, discover_python_files):
def test_should_discover_source_files_when_test_sources_are_included_and_only_unittests(self,
discover_python_files):
project = Mock()

def get_property(property):
if property == 'dir_source_integrationtest_python':
return None
return property

project.get_property.side_effect = get_property

discover_affected_files(True, False, project)
Expand All @@ -94,13 +95,15 @@ def get_property(property):
call('dir_source_unittest_python')])

@patch('pybuilder.plugins.python.python_plugin_helper.discover_python_files')
def test_should_discover_source_files_when_test_sources_are_included_and_only_integrationtests(self, discover_python_files):
def test_should_discover_source_files_when_test_sources_are_included_and_only_integrationtests(self,
discover_python_files):
project = Mock()

def get_property(property):
if property == 'dir_source_unittest_python':
return None
return property

project.get_property.side_effect = get_property

discover_affected_files(True, False, project)
Expand All @@ -117,6 +120,7 @@ def get_property(property):
if property == 'dir_source_main_python':
return property
return None

project.get_property.side_effect = get_property

discover_affected_files(True, False, project)
Expand All @@ -126,7 +130,6 @@ def get_property(property):


class ExecuteToolOnSourceFilesTest(unittest.TestCase):

@patch('pybuilder.plugins.python.python_plugin_helper.log_report')
@patch('pybuilder.plugins.python.python_plugin_helper.read_file')
@patch('pybuilder.plugins.python.python_plugin_helper.execute_command')
Expand All @@ -141,6 +144,20 @@ def test_should_execute_tool_on_source_files(self, affected,

execute.assert_called_with(['foo --bar', 'file1', 'file2'], '/path/to/report')

@patch('pybuilder.plugins.python.python_plugin_helper.log_report')
@patch('pybuilder.plugins.python.python_plugin_helper.read_file')
@patch('pybuilder.plugins.python.python_plugin_helper.execute_command')
@patch('pybuilder.plugins.python.python_plugin_helper.discover_affected_dirs')
def test_should_execute_tool_on_source_dirs(self, affected,
execute, read, log):
project = Mock()
project.expand_path.return_value = '/path/to/report'
affected.return_value = ['/dir1', '/dir2']

execute_tool_on_source_files(project, 'name', 'foo --bar', include_dirs_only=True)

execute.assert_called_with(['foo --bar', '/dir1', '/dir2'], '/path/to/report')

@patch('pybuilder.plugins.python.python_plugin_helper.log_report')
@patch('pybuilder.plugins.python.python_plugin_helper.read_file')
@patch('pybuilder.plugins.python.python_plugin_helper.execute_command')
Expand Down

0 comments on commit 81a440d

Please sign in to comment.