Repository: cookiecutter/cookiecutter
File: tests/test_hooks.py (Python)

Code before:
        tests_dir = os.path.join(self.repo_path, 'input{{hooks}}')

        with open(hook_path, 'w') as f:
            f.write("#!/usr/bin/env python\n")
            f.write("import sys; sys.exit(1)\n")

        with utils.work_in(self.repo_path):
            with pytest.raises(exceptions.FailedHookException) as excinfo:
                hooks.run_hook('pre_gen_project', tests_dir, {})
            assert 'Hook script failed' in str(excinfo.value)


@pytest.yield_fixture
def hook_backup_dir(tmpdir):
    """Yield a directory that contains hook backup files."""

    hooks_dir = tmpdir.mkdir('hook-files')

    pre_hook_content = textwrap.dedent(
        u"""
        #!/usr/bin/env python
        # -*- coding: utf-8 -*-
        print('pre_gen_project.py~')
        """
    )
    pre_gen_hook_file = hooks_dir / 'pre_gen_project.py~'
    pre_gen_hook_file.write_text(pre_hook_content, encoding='utf8')

    post_hook_content = textwrap.dedent(
        u"""
        #!/usr/bin/env python
        # -*- coding: utf-8 -*-
        print('post_gen_project.py~')
        """
    )

    post_gen_hook_file = hooks_dir / 'post_gen_project.py~'
    post_gen_hook_file.write_text(post_hook_content, encoding='utf8')

    yield str(hooks_dir)

    pre_gen_hook_file.remove()
    post_gen_hook_file.remove()


def test_ignore_hook_backup_files(monkeypatch, hook_backup_dir):
    monkeypatch.chdir(hook_backup_dir)
    assert hooks.find_hooks() == {}


Review comment (suggestion):
Good catch! 

In the long run we probably want to change the code to accept a path rather than using the current working directory + `hooks`.


Code after:
        tests_dir = os.path.join(self.repo_path, 'input{{hooks}}')

        with open(hook_path, 'w') as f:
            f.write("#!/usr/bin/env python\n")
            f.write("import sys; sys.exit(1)\n")

        with utils.work_in(self.repo_path):
            with pytest.raises(exceptions.FailedHookException) as excinfo:
                hooks.run_hook('pre_gen_project', tests_dir, {})
            assert 'Hook script failed' in str(excinfo.value)


@pytest.yield_fixture
def dir_with_hooks(tmpdir):
    """Yield a directory that contains hook backup files."""

    hooks_dir = tmpdir.mkdir('hooks')

    pre_hook_content = textwrap.dedent(
        u"""
        #!/usr/bin/env python
        # -*- coding: utf-8 -*-
        print('pre_gen_project.py~')
        """
    )
    pre_gen_hook_file = hooks_dir / 'pre_gen_project.py~'
    pre_gen_hook_file.write_text(pre_hook_content, encoding='utf8')

    post_hook_content = textwrap.dedent(
        u"""
        #!/usr/bin/env python
        # -*- coding: utf-8 -*-
        print('post_gen_project.py~')
        """
    )

    post_gen_hook_file = hooks_dir / 'post_gen_project.py~'
    post_gen_hook_file.write_text(post_hook_content, encoding='utf8')

    # Make sure to yield the parent directory as `find_hooks()`
    # looks into `hooks/` in the current working directory
    yield str(tmpdir)

    pre_gen_hook_file.remove()
    post_gen_hook_file.remove()


def test_ignore_hook_backup_files(monkeypatch, dir_with_hooks):
    # Change the current working directory that contains `hooks/`
    monkeypatch.chdir(dir_with_hooks)
    assert hooks.find_hooks() == {}