Esempio n. 1
0
def run() -> int:
    term = Terminal()

    _set_terminal(term)

    config = load_config_from_pyproject_toml()

    pre_commit_hook = PreCommitHook()

    check_hook_is_current(term, pre_commit_hook)

    if config.has_autohooks_config():
        check_hook_mode(term, config.get_mode(), pre_commit_hook.read_mode())

    plugins = get_project_autohooks_plugins_path()
    plugins_dir_name = str(plugins)

    if plugins.is_dir():
        sys.path.append(plugins_dir_name)

    term.bold_info('autohooks => pre-commit')

    with autohooks_module_path(), term.indent():
        for name in config.get_pre_commit_script_names():
            term.info('Running {}'.format(name))

            with term.indent():
                try:
                    plugin = load_plugin(name)
                    if not has_precommit_function(plugin):
                        term.fail(
                            'No precommit function found in plugin {}. '
                            'Your autohooks settings may be invalid.'.format(
                                name))
                        return 1

                    if has_precommit_parameters(plugin):
                        retval = plugin.precommit(config=config.get_config())
                    else:
                        term.warning(
                            'precommit function without kwargs is deprecated. '
                            'Please update {} to a newer version.'.format(
                                name))
                        retval = plugin.precommit()

                    if retval:
                        return retval

                except ImportError as e:
                    term.error('An error occurred while importing pre-commit '
                               'hook {}. {}.'.format(name, e))
                    return 1
                except Exception as e:  # pylint: disable=broad-except
                    term.error('An error occurred while running pre-commit '
                               'hook {}. {}.'.format(name, e))
                    return 1

    return 0
Esempio n. 2
0
    def test_with_indent(self, mock_stdout):
        term = Terminal()

        expected_msg = '  foo\n'

        with term.indent(2):
            term.print('foo')

            ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

        # clear the buffer
        mock_stdout.truncate(0)
        mock_stdout.seek(0)

        term.print('bar')

        expected_msg = 'bar\n'

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)
Esempio n. 3
0
def install_hooks(term: Terminal, args: Namespace) -> None:
    pre_commit_hook = PreCommitHook()
    pyproject_toml = get_pyproject_toml_path()
    config = load_config_from_pyproject_toml(pyproject_toml)

    if pre_commit_hook.exists() and not args.force:
        term.ok('autohooks pre-commit hook is already installed at {}.'.format(
            str(pre_commit_hook)))
        with term.indent():
            term.print()
            term.info(
                "Run 'autohooks activate --force' to override the current "
                "installed pre-commit hook.")
            term.info(
                "Run 'autohooks check' to validate the current status of "
                "the installed pre-commit hook.")
    else:
        if not config.is_autohooks_enabled():
            term.warning('autohooks is not enabled in your {} file. '
                         'Run \'autohooks check\' for more '
                         'details.'.format(str(pyproject_toml)))

        if args.mode:
            mode = Mode.from_string(args.mode)
        else:
            mode = config.get_mode()

        pre_commit_hook.write(mode=mode)

        term.ok(
            'autohooks pre-commit hook installed at {} using {} mode.'.format(
                str(pre_commit_hook), str(mode.get_effective_mode())))
Esempio n. 4
0
    def test_with_indent(self):
        term = Terminal()

        with term.indent(2):
            term.print('foo')

        self.terminal_mock.location.assert_called_with(x=2)
        self.print_mock.assert_called_with('foo')

        term.print('bar')

        # indentation has been removed
        self.terminal_mock.location.assert_called_with(x=0)
        self.print_mock.assert_called_with('bar')
Esempio n. 5
0
class TerminalTestCase(unittest.TestCase):
    def setUp(self):
        self.maxDiff = 180
        # getting the bash-color-codes from the colorful module
        self.red = cf.red
        self.green = cf.green
        self.yellow = cf.yellow
        self.cyan = cf.cyan
        self.reset = cf.reset
        self.bold = cf.bold
        # every colors second value is the reset value ...
        self.term = Terminal()
        self.term.get_width = MagicMock(return_value=80)

    @patch('sys.stdout', new_callable=StringIO)
    def test_error(self, mock_stdout):
        status = '{} '.format(self.red(Signs.ERROR))
        msg = 'foo bar'

        expected_msg = (self.reset('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.error(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_fail(self, mock_stdout):
        status = '{} '.format(self.red(Signs.FAIL))
        msg = 'foo bar baz'

        expected_msg = (self.reset('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.fail(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_info(self, mock_stdout):
        status = '{} '.format(self.cyan(Signs.INFO))
        msg = 'foo bar'

        expected_msg = (self.reset('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.info(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_bold_info(self, mock_stdout):
        status = '{} '.format(self.cyan(Signs.INFO))
        msg = 'bold foo bar'

        expected_msg = (self.bold('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.bold_info(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_ok(self, mock_stdout):
        status = '{} '.format(self.green(Signs.OK))
        msg = 'foo bar'

        expected_msg = (self.reset('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.ok(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_warning(self, mock_stdout):
        msg = 'foo bar'

        status = '{} '.format(self.yellow(Signs.WARNING))

        expected_msg = (self.reset('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.warning(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_print(self, mock_stdout):
        expected_msg = self.reset('  foo bar').styled_string + '\n'

        self.term.print('foo bar')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

    @patch('sys.stdout', new_callable=StringIO)
    def test_add_indent(self, mock_stdout):
        i = 6
        expected_msg = self.reset(' ' * i + 'foo').styled_string + '\n'

        self.term.add_indent(i - 2)
        self.term.print('foo')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

        # clear the buffer
        mock_stdout.truncate(0)
        mock_stdout.seek(0)

        j = 4
        expected_msg = self.reset(' ' * (i + j) + 'bar').styled_string + '\n'

        self.term.add_indent(j)
        self.term.print('bar')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

        # clear the buffer
        mock_stdout.truncate(0)
        mock_stdout.seek(0)

    @patch('sys.stdout', new_callable=StringIO)
    def test_reset_indent(self, mock_stdout):
        i = 6
        expected_msg = self.reset(' ' * i + 'foo').styled_string + '\n'

        self.term.add_indent(i - 2)
        self.term.print('foo')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

        # clear the buffer
        mock_stdout.truncate(0)
        mock_stdout.seek(0)

        expected_msg = self.reset('  bar').styled_string + '\n'

        self.term.reset_indent()
        self.term.print('bar')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

    @patch('sys.stdout', new_callable=StringIO)
    def test_with_indent(self, mock_stdout):
        expected_msg = self.reset('    foo').styled_string + '\n'

        with self.term.indent(2):
            self.term.print('foo')

            ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

        # clear the buffer
        mock_stdout.truncate(0)
        mock_stdout.seek(0)

        expected_msg = self.reset('  bar').styled_string + '\n'
        self.term.print('bar')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

    @patch('sys.stdout', new_callable=StringIO)
    def test_long_msg(self, mock_stdout):
        long_msg = (
            'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, '
            'sed diam nonumy eirmod tempor invidunt ut labore et dolore magna'
            ' aliquyam erat, sed diam voluptua.')
        expected_msg = (self.reset(
            '  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, '
            'sed diam nonumy eirmo\n  d tempor invidunt ut labore et'
            ' dolore magna aliquyam erat, sed diam voluptua.').styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.print(long_msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)