class ExternalCommandExecutionTests(unittest.TestCase):

    def setUp(self):
        self.project = Project('/base/dir')
        self.command = ExternalCommandBuilder('command-name', self.project)
        self.command.use_argument('--foo').use_argument('--bar')

    @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):
        execution.return_value = 0, '/tmp/reports/command-name'
        logger = Mock()
        self.command.run_on_production_source_files(logger)

        execution.assert_called_with(
            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_and_test_source_files(self, execution, read):
        execution.return_value = 0, '/tmp/reports/command-name'
        logger = Mock()
        self.command.run_on_production_and_test_source_files(logger)

        execution.assert_called_with(
            include_test_sources=True,
            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_and_return_execution_result(self, execution, read):
        execution.return_value = 0, '/tmp/reports/command-name'
        read.side_effect = lambda argument: {
            '/tmp/reports/command-name': ['Running...', 'OK all done!'],
            '/tmp/reports/command-name.err': ['Oh no! I am not python8 compatible!', 'I will explode now.']
        }[argument]
        logger = Mock()

        result = self.command.run_on_production_source_files(logger)

        self.assertEqual(result.exit_code, 0)
        self.assertEqual(result.report_file, '/tmp/reports/command-name')
        self.assertEqual(read.call_args_list[0], call('/tmp/reports/command-name'))
        self.assertEqual(result.report_lines, ['Running...', 'OK all done!'])
        self.assertEqual(result.error_report_file, '/tmp/reports/command-name.err')
        self.assertEqual(read.call_args_list[1], call('/tmp/reports/command-name.err'))
        self.assertEqual(result.error_report_lines, ['Oh no! I am not python8 compatible!', 'I will explode now.'])
def analyze(project, logger):
    """ Applies the frosted script to the sources of the given project. """
    logger.info("Executing frosted on project sources.")

    verbose = project.get_property("verbose")
    project.set_property_if_unset("frosted_verbose_output", verbose)

    command = ExternalCommandBuilder('frosted', project)
    for ignored_error_code in project.get_property('frosted_ignore', []):
        command.use_argument('--ignore={0}'.format(ignored_error_code))

    include_test_sources = project.get_property("frosted_include_test_sources")
    include_scripts = project.get_property("frosted_include_scripts")

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

    count_of_warnings = len(result.report_lines)
    count_of_errors = len(result.error_report_lines)

    if count_of_errors > 0:
        logger.error('Errors while running frosted, see {0}'.format(
            result.error_report_file))

    if count_of_warnings > 0:
        if project.get_property("frosted_break_build"):
            error_message = "frosted found {0} warning(s)".format(
                count_of_warnings)
            raise BuildFailedException(error_message)
        else:
            logger.warn("frosted found %d warning(s).", count_of_warnings)
def analyze(project, logger):
    """ Applies the frosted script to the sources of the given project. """
    logger.info("Executing frosted on project sources.")

    verbose = project.get_property("verbose")
    project.set_property_if_unset("frosted_verbose_output", verbose)

    command = ExternalCommandBuilder("frosted", project)
    for ignored_error_code in project.get_property("frosted_ignore", []):
        command.use_argument("--ignore={0}".format(ignored_error_code))

    include_test_sources = project.get_property("frosted_include_test_sources")
    include_scripts = project.get_property("frosted_include_scripts")

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

    count_of_warnings = len(result.report_lines)
    count_of_errors = len(result.error_report_lines)

    if count_of_errors > 0:
        logger.error("Errors while running frosted, see {0}".format(result.error_report_file))

    if count_of_warnings > 0:
        if project.get_property("frosted_break_build"):
            error_message = "frosted found {0} warning(s)".format(count_of_warnings)
            raise BuildFailedException(error_message)
        else:
            logger.warn("frosted found %d warning(s).", count_of_warnings)
Exemple #4
0
def analyze(project, logger):
    """ Applies the flake8 script to the sources of the given project. """
    logger.info("Executing flake8 on project sources.")

    verbose = project.get_property("verbose")
    project.set_property_if_unset("flake8_verbose_output", verbose)

    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')
    command.use_argument('--max-complexity={0}').formatted_with_truthy_property('flake8_max_complexity')

    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_dirs_only=True)

    count_of_warnings = len(result.report_lines)
    count_of_errors = len(result.error_report_lines)

    if count_of_errors > 0:
        logger.error('Errors while running flake8. See %s for full details:\n%s' % (
            result.error_report_file, tail_log(result.error_report_file)))

    if count_of_warnings > 0:
        if project.get_property("flake8_break_build"):
            error_message = "flake8 found {0} warning(s)".format(count_of_warnings)
            raise BuildFailedException(error_message)
        else:
            logger.warn("flake8 found %d warning(s).", count_of_warnings)
Exemple #5
0
def positive_test(project, logger):
    print("Running a postive test")
    command = ExternalCommandBuilder('hydra', project)
    command.use_argument('positive-test')
    result = command.run_on_production_source_files(logger)
    if result.exit_code:
        raise BuildFailedException("Exit code is set")
Exemple #6
0
def positive_test(project, logger):
    print("Running a postive test")
    command = ExternalCommandBuilder('hydra', project)
    command.use_argument('positive-test')
    result = command.run_on_production_source_files(logger)
    if result.exit_code:
        raise BuildFailedException("Exit code is set")
Exemple #7
0
def analyze(project, logger):
    """ Applies the flake8 script to the sources of the given project. """
    logger.info("Executing flake8 on project sources.")

    verbose = project.get_property("verbose")
    project.set_property_if_unset("flake8_verbose_output", verbose)

    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('--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)

    count_of_warnings = len(result.report_lines)
    count_of_errors = len(result.error_report_lines)

    if count_of_errors > 0:
        logger.error('Errors while running flake8, see {0}'.format(result.error_report_file))

    if count_of_warnings > 0:
        if project.get_property("flake8_break_build"):
            error_message = "flake8 found {0} warning(s)".format(count_of_warnings)
            raise BuildFailedException(error_message)
        else:
            logger.warn("flake8 found %d warning(s).", count_of_warnings)
Exemple #8
0
def cyclomatic_complexity(project, logger):
    try:
        command = ExternalCommandBuilder('radon', project)
        command.use_argument('cc')
        command.use_argument('-a')
        result = command.run_on_production_source_files(logger)
        if len(result.error_report_lines) > 0:
            logger.error('Errors while running radon, see {0}'.format(result.error_report_file))
        for line in result.report_lines[:-1]:
            logger.debug(line.strip())
        if not result.report_lines:
            return
        average_complexity_line = result.report_lines[-1].strip()
        logger.info(average_complexity_line)

    except Exception as exception:
        print('ERROR: unable to execute cyclomatic complexity due to: {}'.format(str(exception)))
Exemple #9
0
def cyclomatic_complexity(project, logger):

    command = ExternalCommandBuilder('radon', project)
    command.use_argument('cc')
    command.use_argument('-a')

    result = command.run_on_production_source_files(logger)

    if len(result.error_report_lines) > 0:
        logger.error('Errors while running radon, see {0}'.format(
            result.error_report_file))

    for line in result.report_lines[:-1]:
        logger.debug(line.strip())

    average_complexity_line = result.report_lines[-1].strip()
    logger.info(average_complexity_line)
def analyze(project, logger, reactor):
    """ Applies the flake8 script to the sources of the given project. """
    logger.info("Executing flake8 on project sources.")

    verbose = project.get_property("verbose")
    project.set_property_if_unset("flake8_verbose_output", verbose)

    command = ExternalCommandBuilder("flake8", project, reactor)
    command.use_argument("--ignore={0}").formatted_with_truthy_property(
        "flake8_ignore")
    command.use_argument("--extend-ignore={0}").formatted_with_truthy_property(
        "flake8_extend_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")
    command.use_argument(
        "--max-complexity={0}").formatted_with_truthy_property(
            "flake8_max_complexity")

    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_dirs_only=True)

    count_of_warnings = len(result.report_lines)
    count_of_errors = len(result.error_report_lines)

    if count_of_errors > 0:
        logger.error(
            "Errors while running flake8. See %s for full details:\n%s" %
            (result.error_report_file, tail_log(result.error_report_file)))

    if count_of_warnings > 0:
        if project.get_property("flake8_break_build"):
            error_message = "flake8 found {0} warning(s)".format(
                count_of_warnings)
            raise BuildFailedException(error_message)
        else:
            logger.warn("flake8 found %d warning(s).", count_of_warnings)
class ExternalCommandExecutionTests(unittest.TestCase):
    def setUp(self):
        self.project = Project('/base/dir')
        self.reactor = Mock()
        pyb_env = Mock()
        self.reactor.python_env_registry = {"pybuilder": pyb_env}
        self.reactor.pybuilder_venv = pyb_env

        self.command = ExternalCommandBuilder('command-name', self.project,
                                              self.reactor)
        self.command.use_argument('--foo').use_argument('--bar')

    @patch("pybuilder.pluginhelper.external_command.read_file")
    def test_should_execute_external_command(self, _):
        self.command.run("any-outfile-name")

        self.reactor.pybuilder_venv.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(
            python_env=self.reactor.pybuilder_venv,
            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):
        execution.return_value = 0, '/tmp/reports/command-name'
        logger = Mock()
        self.command.run_on_production_source_files(logger)

        execution.assert_called_with(
            python_env=self.reactor.pybuilder_venv,
            include_dirs_only=False,
            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_and_test_source_files(
            self, execution, read):
        execution.return_value = 0, '/tmp/reports/command-name'
        logger = Mock()
        self.command.run_on_production_and_test_source_files(logger)

        execution.assert_called_with(
            python_env=self.reactor.pybuilder_venv,
            include_dirs_only=False,
            include_test_sources=True,
            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_and_return_execution_result(
            self, execution, read):
        execution.return_value = 0, '/tmp/reports/command-name'
        read.side_effect = lambda argument: {
            '/tmp/reports/command-name': ['Running...', 'OK all done!'],
            '/tmp/reports/command-name.err':
            ['Oh no! I am not python8 compatible!', 'I will explode now.']
        }[argument]
        logger = Mock()

        result = self.command.run_on_production_source_files(logger)

        self.assertEqual(result.exit_code, 0)
        self.assertEqual(result.report_file, '/tmp/reports/command-name')
        self.assertEqual(read.call_args_list[0],
                         call('/tmp/reports/command-name'))
        self.assertEqual(result.report_lines, ['Running...', 'OK all done!'])
        self.assertEqual(result.error_report_file,
                         '/tmp/reports/command-name.err')
        self.assertEqual(read.call_args_list[1],
                         call('/tmp/reports/command-name.err'))
        self.assertEqual(
            result.error_report_lines,
            ['Oh no! I am not python8 compatible!', 'I will explode now.'])