Beispiel #1
0
    def test_hook_is_yet_installed_upgrade_is_set___hook_is_installed(
            self, orig_content, new_content, url_front, file_name, hook_name):
        assume(new_content != orig_content)

        url = 'http://' + url_front + '/' + file_name

        with FakeRepoDir():
            with open(
                    os.path.join(repo.hook_type_directory(hook_name),
                                 file_name), 'w') as f:
                f.write(orig_content)

            responses.add(
                responses.GET,
                url,
                body=new_content,
                status=200,
            )

            sys.argv = ['foo', 'install', hook_name, url, '--upgrade', '-y']

            cmd.Hooks().run()

            with open(
                    os.path.join(repo.hook_type_directory(hook_name),
                                 file_name)) as f:
                self.assertEqual(new_content, f.read())
Beispiel #2
0
    def test_user_has_preexisitng_hooks_user_responds_yes_to_all___all_are_overwritten(
            self):
        with patch('githooks.cmd.repo.repo_root',
                   Mock(return_value=self.repo_dir)):
            with patch('githooks.cmd.get_input', Mock(return_value='yes')):
                with patch('githooks.cmd.logger') as log_mock:
                    sys.argv = ['foo', 'init']

                    for name in self.hook_names:
                        with open(os.path.join(self.hooks_dir, name),
                                  'w') as f:
                            f.write(name)

                    cmd.Hooks().run()

                    self.assertGreater(len(self.hook_names), 0)
                    for name in self.hook_names:
                        with open(os.path.join(self.hooks_dir, name)) as f:
                            with open(
                                    os.path.join(utils.get_hook_script_dir(),
                                                 name)) as new:
                                self.assertEqual(new.read(), f.read())

                        log_mock.info.assert_called_once_with(
                            u'A "{0}" already exists for this repository. Do you want to continue? y/[N]'
                            .format(name))
Beispiel #3
0
    def test_config_is_given___all_hooks_from_config_are_installed(
            self, hook_configs, setup_configs):
        with FakeRepoDir() as dir:
            config = ConfigParser()
            hook_type_setting = {}
            for hook_type, hooks in hook_configs.items():
                hook_type_setting.setdefault(hook_type, '')

                for hook in hooks:
                    url = 'http://' + hook['front'] + '/' + hook['filename']
                    hook_type_setting[hook_type] += url + '\n'

                    responses.add(
                        responses.GET,
                        url,
                        body=hook['content'],
                        status=200,
                    )

            config.add_section('install')
            for hook_type, value in hook_type_setting.items():
                config.set('install', hook_type, value)

            with open(os.path.join(str(dir), 'git-hooks.cfg'), 'w') as f:
                config.write(f)

            setup_config = ConfigParser()
            hook_type_setting = {}
            for hook_type, hooks in setup_configs.items():
                hook_type_setting.setdefault(hook_type, '')

                for hook in hooks:
                    url = 'http://' + hook['front'] + '/' + hook['filename']
                    hook_type_setting[hook_type] += url + '\n'

                    responses.add(
                        responses.GET,
                        url,
                        body=hook['content'],
                        status=200,
                    )

            setup_config.add_section('git-hooks.install')
            for hook_type, value in hook_type_setting.items():
                setup_config.set('git-hooks.install', hook_type, value)

            with open(os.path.join(str(dir), 'setup.cfg'), 'w') as f:
                setup_config.write(f)

            sys.argv = ['foo', 'install', '-y']

            cmd.Hooks().run()

            for hook_type, hooks in hook_configs.items():
                for hook in hooks:
                    self.assertTrue(
                        os.path.exists(
                            os.path.join(repo.hook_type_directory(hook_type),
                                         hook['filename'])))
Beispiel #4
0
    def test_hook_does_not_exist___logger_is_written_to(self, name, hook_type):
        with FakeRepoDir():
            with patch('githooks.cmd.logger.info') as mock_logger:
                sys.argv = ['foo', 'uninstall', hook_type, name]
                cmd.Hooks().run()

                mock_logger.assert_called_with(
                    '{} hook called "{}" could not be found. SKIPPING.'.format(
                        hook_type, name))
Beispiel #5
0
    def test_hook_exists_in___hook_is_deleted(self, name, hook_type):
        with FakeRepoDir():
            hook_path = os.path.join(repo.hook_type_directory(hook_type), name)

            with open(hook_path, 'w') as f:
                f.write('content')

            sys.argv = ['foo', 'uninstall', hook_type, name]
            cmd.Hooks().run()

            self.assertFalse(os.path.exists(hook_path))
Beispiel #6
0
    def test_repo_is_new___user_is_given_an_error(self):
        with patch('githooks.cmd.repo.get', Mock()) as get_mock:
            with patch('githooks.cmd.logger') as log_mock:
                repo_mock = Mock()
                repo_mock.heads = []
                get_mock.return_value = repo_mock

                self.assertEqual(1, cmd.Hooks().run())
                log_mock.error.assert_called_once_with(
                    'The hook runner doesnt currently work for new repos. Perform an initial commit before initialising githooks (see: https://github.com/wildfish/git-hooks/issues/4)'
                )
Beispiel #7
0
    def test_user_has_no_preexisitng_hooks___hooks_are_initialised(self):
        with patch('githooks.cmd.repo.repo_root',
                   Mock(return_value=self.repo_dir)):
            sys.argv = ['foo', 'init']

            cmd.Hooks().run()

            self.assertGreater(len(self.hook_names), 0)
            for name in self.hook_names:
                self.assertTrue(
                    os.path.exists(os.path.join(self.hooks_dir, name)))
                self.assertTrue(
                    os.path.exists(os.path.join(self.hooks_dir, name + '.d')))
Beispiel #8
0
    def test_one_of_the_hook_directories_already_exists___the_process_continues_as_normal(
            self):
        with patch('githooks.cmd.repo.repo_root',
                   Mock(return_value=self.repo_dir)):
            sys.argv = ['foo', 'init']

            os.mkdir(os.path.join(self.hooks_dir, self.hook_names[0] + '.d'))
            cmd.Hooks().run()

            self.assertGreater(len(self.hook_names), 0)
            for name in self.hook_names:
                self.assertTrue(
                    os.path.exists(os.path.join(self.hooks_dir, name)))
                self.assertTrue(
                    os.path.exists(os.path.join(self.hooks_dir, name + '.d')))
Beispiel #9
0
    def __enter__(self):
        self._make_repo_dir()

        git.Repo.init(self.repo_dir)
        self.patches = [
            patch('githooks.cmd.repo.repo_root',
                  Mock(return_value=self.repo_dir)),
            patch('githooks.repo.repo_root', Mock(return_value=self.repo_dir)),
        ]

        for p in self.patches:
            p.__enter__()

        sys.argv = ['foo', 'init', '-y']
        cmd.Hooks().run()

        return self
Beispiel #10
0
    def test_both_the_overwrite_and_no_overwrite_flags_are_set___the_user_is_given_an_error_and_the_process_exits(
            self):
        with patch('githooks.cmd.repo.repo_root',
                   Mock(return_value=self.repo_dir)):
            with patch('githooks.cmd.logger') as log_mock:
                sys.argv = ['foo', 'init', '-y', '-n']

                self.assertEqual(1, cmd.Hooks().run())

                self.assertGreater(len(self.hook_names), 0)
                for name in self.hook_names:
                    self.assertFalse(
                        os.path.exists(os.path.join(self.hooks_dir, name)))
                    self.assertFalse(
                        os.path.exists(
                            os.path.join(self.hooks_dir, name + '.d')))

                log_mock.error.assert_called_once_with(
                    'Both the overwrite and no overwrite flags were set')
Beispiel #11
0
    def test_user_has_preexisitng_hooks_with_no_overwrite_flag___no_are_overwritten(
            self):
        with patch('githooks.cmd.repo.repo_root',
                   Mock(return_value=self.repo_dir)):
            with patch('githooks.cmd.logger') as log_mock:

                sys.argv = ['foo', 'init', '-n']

                for name in self.hook_names:
                    with open(os.path.join(self.hooks_dir, name), 'w') as f:
                        f.write(name)

                cmd.Hooks().run()

                self.assertGreater(len(self.hook_names), 0)
                for name in self.hook_names:
                    with open(os.path.join(self.hooks_dir, name)) as f:
                        self.assertEqual(name, f.read())

                    log_mock.info.assert_not_called()
Beispiel #12
0
    def test_hook_is_not_yet_installed___hook_is_installed(
            self, content, url_front, file_name, hook_name):

        url = 'http://' + url_front + '/' + file_name

        with FakeRepoDir():
            responses.add(
                responses.GET,
                url,
                body=content,
                status=200,
            )

            sys.argv = ['foo', 'install', hook_name, url, '-y']

            cmd.Hooks().run()

            with open(
                    os.path.join(repo.hook_type_directory(hook_name),
                                 file_name)) as f:
                self.assertEqual(content, f.read())