Пример #1
0
    def test_config_can_be_set_for_file_pattern(self):
        """Local configuration options should not affect to global config."""
        global_acceptedcoderate = 9
        local_acceptedcoderate = 7
        precommit_yaml_contents = yaml.dump({
            'file-checkers': {
                '*.py': ['pylint'],
                'tests/*.py': [{'pylint': {
                    'accepted-code-rate': local_acceptedcoderate
                }}]
            },
            'config': {
                'pylint': {'accepted-code-rate': global_acceptedcoderate}
            }
        })
        staged_files = ['module.py', 'tests/module.py']
        self.patch_git_repository(precommit_yaml_contents, staged_files)
        self.patch_file_checker(
            'pylint',
            taskname='Pylint ${file_relpath}',
            command='pylint -f parseable ${file_abspath} ${options}',
            defaultconfig={
                'rcfile': None,
                'accepted-code-rate': global_acceptedcoderate
            },
            command_options={'rcfile': '--rcfile=${value}'}
        )

        runner.main()

        expected_checkers = []
        expected_checkers.append(
            Task(
                'Pylint {}'.format('module.py'),
                'pylint -f parseable {}'.format(git.abspath('module.py')),
                config={
                    'rcfile': None,
                    'accepted-code-rate': global_acceptedcoderate
                }
            )
        )
        expected_checkers.append(
            Task(
                'Pylint {}'.format('tests/module.py'),
                'pylint -f parseable {}'.format(git.abspath('tests/module.py')),
                config={
                    'rcfile': None,
                    'accepted-code-rate': local_acceptedcoderate
                }
            )
        )
        self.worker.execute_checkers.assert_called_once_with(
            UnOrderedCollectionMatcher(expected_checkers)
        )
Пример #2
0
    def test_checkers_can_be_configured_globally(self):
        precommit_yaml_contents = yaml.dump({
            'file-checkers': {'*.js': ['jshint']},
            'config': {
                'jshint': {'config': '.jshintrc'}
            }
        })
        staged_files = ['module.js']
        self.patch_git_repository(precommit_yaml_contents, staged_files)
        self.patch_file_checker(
            'jshint',
            taskname='JSHint ${file_relpath}',
            command='jshint ${options} ${file_abspath}',
            defaultconfig={
                'config': '.jshintrc'
            },
            command_options={'config': '--config ${value}'}
        )

        runner.main()

        expected_task = Task(
            taskname='JSHint module.js',
            command='jshint --config .jshintrc {}' \
                .format(git.abspath('module.js')),
            config={
                'config': '.jshintrc'
            }
        )
        self.worker.execute_checkers.assert_called_once_with(
            UnOrderedCollectionMatcher(
                [expected_task]
            )
        )
Пример #3
0
    def test_configuration_can_contain_additional_command_options(self):
        rcfile = 'tests/pylintrc'
        precommit_yaml_contents = yaml.dump({
            'file-checkers': {
                'tests/*.py': [{'pylint': {'rcfile': rcfile}}]
            }
        })
        staged_files = ['tests/module.py']
        self.patch_git_repository(precommit_yaml_contents, staged_files)
        self.patch_file_checker(
            'pylint',
            taskname='Pylint ${file_relpath}',
            command='pylint -f parseable ${file_abspath} ${rcfile}',
            defaultconfig={
                'rcfile': None,
                'accepted-code-rate': 9
            },
            command_options={'rcfile': '--rcfile=${value}'}
        )

        runner.main()

        expected_checker = Task(
            'Pylint tests/module.py',
            'pylint -f parseable {abspath} --rcfile={rcfile}' \
                .format(abspath=git.abspath('tests/module.py'), rcfile=rcfile),
            config={
                'rcfile': rcfile,
                'accepted-code-rate': 9
            }
        )
        expected_checker.rcfile = 'tests/pylintrc'
        self.worker.execute_checkers.assert_called_once_with(
            UnOrderedCollectionMatcher([expected_checker])
        )
Пример #4
0
    def test_checker_is_created_for_every_staged_file_matching_pattern(self):
        """Only files matching pattern should have created checkers."""
        accepted_code_rate = 9
        pylint_config = {
            'rcfile': None,
            'accepted-code-rate': accepted_code_rate
        }
        precommit_yaml_contents = yaml.dump({
            'file-checkers': {'*.py': ['pylint']}
        })
        staged_files = ['module.py',
                        'module2.py',
                        'module.js']
        self.patch_git_repository(precommit_yaml_contents, staged_files)
        self.patch_file_checker(
            'pylint',
            taskname='Pylint ${file_relpath}',
            command='pylint -f parseable ${file_abspath} ${options}',
            defaultconfig=pylint_config,
            command_options={'rcfile': '--rcfile=${value}'}
        )

        runner.main()

        py_files = [f for f in staged_files if f.endswith('.py')]
        expected_tasks = []
        for each_file in py_files:
            taskname = 'Pylint {}'.format(each_file)
            abspath = git.abspath(each_file)
            command = 'pylint -f parseable {}'.format(abspath)
            task = Task(taskname, command, pylint_config)
            expected_tasks.append(task)
        self.worker.execute_checkers.assert_called_once_with(
            UnOrderedCollectionMatcher(expected_tasks)
        )
Пример #5
0
    def test_each_file_match_most_specific_pattern(self):
        """For each file only checkers from one pattern should be created"""
        accepted_code_rate = 9
        pylint_config = {
            'rcfile': None,
            'accepted-code-rate': accepted_code_rate
        }
        precommit_yaml_contents = yaml.dump({
            'file-checkers': {
                '*.py': ['pylint'],
                'tests/*.py': ['pep8']
            }
        })

        staged_files = ['module.py',
                        'tests/module2.py']
        self.patch_git_repository(precommit_yaml_contents, staged_files)
        self.patch_file_checker(
            'pylint',
            taskname='Pylint ${file_relpath}',
            command='pylint -f parseable ${file_abspath} ${options}',
            defaultconfig=pylint_config,
            command_options={'rcfile': '--rcfile=${value}'}
        )
        self.patch_file_checker(
            'pep8',
            taskname='PEP8 ${file_relpath}',
            command='pep8 ${file_abspath}'
        )

        runner.main()

        expected_pylintchecker = Task(
            'Pylint module.py',
            'pylint -f parseable {}'.format(git.abspath('module.py')),
            pylint_config
        )
        expected_pep8_checker = Task(
            'PEP8 {}'.format('tests/module2.py'),
            'pep8 {}'.format(git.abspath('tests/module2.py'))
        )
        expected_checkers = [expected_pylintchecker, expected_pep8_checker]
        self.worker.execute_checkers.assert_called_once_with(
            UnOrderedCollectionMatcher(expected_checkers)
        )
Пример #6
0
    def create(self, relpath=None, config=None):
        """Create Task for specified file."""
        config = self._mix_config(config)
        if relpath:
            abspath = git.abspath(relpath)
            command = self._command.safe_substitute(file_abspath=abspath)
            taskname = self._taskname.substitute(file_relpath=relpath)
        else:
            taskname = self._taskname.template
            command = self._command.template

        task = Task(taskname, command, config)
        if self._command_options:
            task.command_options = self._command_options
        if self._result_creator:
            task.result_creator = self._result_creator
        return task
Пример #7
0
    def create(self, relpath=None, config=None):
        """Create Task for specified file."""
        config = self._mix_config(config)
        if relpath:
            abspath = git.abspath(relpath)
            command = self._command.safe_substitute(file_abspath=abspath)
            taskname = self._taskname.substitute(file_relpath=relpath)
        else:
            taskname = self._taskname.template
            command = self._command.template

        task = Task(taskname, command, config)
        if self._command_options:
            task.command_options = self._command_options
        if self._result_creator:
            task.result_creator = self._result_creator
        return task
Пример #8
0
    def test_file_checker_can_be_run_with_custom_config(self):
        """Config passed after file pattern should replace global one."""
        accepted_code_rate = 8
        precommit_yaml_contents = yaml.dump({
            'file-checkers': {'*.py': ['pylint']},
            'config': {
                'pylint': {'accepted-code-rate': accepted_code_rate}
            }
        })
        staged_files = ['module.py',
                        'module2.py']
        self.patch_git_repository(precommit_yaml_contents, staged_files)
        self.patch_file_checker(
            'pylint',
            taskname='Pylint ${file_relpath}',
            command='pylint -f parseable ${file_abspath} ${options}',
            defaultconfig={
                'rcfile': None,
                'accepted-code-rate': 9
            },
            command_options={'rcfile': '--rcfile=${value}'}
        )

        runner.main()

        expected_checkers = []
        for each_file in staged_files:
            task = Task(
                'Pylint {}'.format(each_file),
                'pylint -f parseable {}'.format(git.abspath(each_file)),
                config={
                    'rcfile': None,
                    'accepted-code-rate': accepted_code_rate
                }
            )
            expected_checkers.append(task)
        self.worker.execute_checkers.assert_called_once_with(
            UnOrderedCollectionMatcher(expected_checkers)
        )
Пример #9
0
    def test_file_checker_is_created_for_files_in_index(self):
        """File checker should be created for files from git index."""
        precommit_yaml_contents = yaml.dump({
            'file-checkers': {'*.py': ['pep8']}
        })
        staged_files = ['module.py',
                        'module2.py']
        self.patch_git_repository(precommit_yaml_contents, staged_files)
        self.patch_file_checker('pep8',
                                taskname='PEP8 ${file_relpath}',
                                command='pep8 ${file_abspath}')

        runner.main()

        expected_checkers = []
        for file_path in staged_files:
            command = 'pep8 {}'.format(git.abspath(file_path))
            task_name = 'PEP8 {}'.format(file_path)
            expected_checkers.append(
                Task(task_name, command)
            )
        expected_checkers = UnOrderedCollectionMatcher(expected_checkers)
        self.worker.execute_checkers \
            .assert_called_once_with(expected_checkers)
Пример #10
0
    def test_checker_can_be_defined_with_custom_result_creator(self):
        """Checker can have own result creator.

        Result creator builds CheckResult based on stdout, returncode and
        config.
        """
        accepted_code_rate = 9
        pylint_config = {
            'rcfile': None,
            'accepted-code-rate': accepted_code_rate
        }
        precommit_yaml_contents = yaml.dump({
            'file-checkers': {'*.py': ['pylint']}
        })
        staged_files = ['module.py']
        self.patch_git_repository(precommit_yaml_contents, staged_files)
        self.patch_file_checker(
            'pylint',
            taskname='Pylint ${file_relpath}',
            command='pylint -f parseable ${file_abspath} ${options}',
            defaultconfig=pylint_config,
            command_options={'rcfile': '--rcfile=${value}'},
            result_creator=create_pylint_result
        )

        runner.main()

        expected_task = Task(
            'Pylint module.py',
            'pylint -f parseable {}'.format(git.abspath('module.py')),
            pylint_config
        )
        expected_task.result_creator = create_pylint_result
        self.worker.execute_checkers.assert_called_once_with(
            UnOrderedCollectionMatcher([expected_task])
        )