Completed
Push — break-up-hooks-tests ( 75c50f...87fe28 )
by Michael
34s
created

test_ignore_hook_backup_files()   A

Complexity

Conditions 3

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
1
# -*- coding: utf-8 -*-
2
3
import os
4
import sys
5
import textwrap
6
7
import pytest
8
9
from cookiecutter import hooks
10
11
12
def test_find_hook(monkeypatch, repo_dir_with_hooks):
13
    """Finds the specified hook."""
14
15
    monkeypatch.chdir(repo_dir_with_hooks)
16
    assert (
17
        os.path.abspath('hooks/pre_gen_project.py') ==
18
        hooks.find_hook('pre_gen_project')
19
    )
20
21
    post_hook_name = 'post_gen_project.{}'.format(
22
        'bat' if sys.platform.startswith('win') else 'sh'
23
    )
24
    assert (
25
        os.path.abspath('hooks/{}'.format(post_hook_name)) ==
26
        hooks.find_hook('post_gen_project')
27
    )
28
29
30
def test_no_hooks(monkeypatch):
31
    """find_hooks should return None if the hook could not be found."""
32
    monkeypatch.chdir('tests/fake-repo')
33
    assert None is hooks.find_hook('pre_gen_project')
34
35
36
def test_unknown_hooks_dir(monkeypatch, repo_dir_with_hooks):
37
    monkeypatch.chdir(repo_dir_with_hooks)
38
    assert hooks.find_hook(
39
        'pre_gen_project',
40
        hooks_dir='chooks'
41
    ) is None
42
43
44
def test_hook_not_found(monkeypatch, repo_dir_with_hooks):
45
    monkeypatch.chdir(repo_dir_with_hooks)
46
    assert hooks.find_hook('unknown_hook') is None
47
48
49
@pytest.yield_fixture
50
def dir_with_hooks(tmpdir):
51
    """Yield a directory that contains hook backup files."""
52
53
    hooks_dir = tmpdir.mkdir('hooks')
54
55
    pre_hook_content = textwrap.dedent(
56
        u"""
57
        #!/usr/bin/env python
58
        # -*- coding: utf-8 -*-
59
        print('pre_gen_project.py~')
60
        """
61
    )
62
    pre_gen_hook_file = hooks_dir / 'pre_gen_project.py~'
63
    pre_gen_hook_file.write_text(pre_hook_content, encoding='utf8')
64
65
    post_hook_content = textwrap.dedent(
66
        u"""
67
        #!/usr/bin/env python
68
        # -*- coding: utf-8 -*-
69
        print('post_gen_project.py~')
70
        """
71
    )
72
73
    post_gen_hook_file = hooks_dir / 'post_gen_project.py~'
74
    post_gen_hook_file.write_text(post_hook_content, encoding='utf8')
75
76
    # Make sure to yield the parent directory as `find_hooks()`
77
    # looks into `hooks/` in the current working directory
78
    yield str(tmpdir)
79
80
    pre_gen_hook_file.remove()
81
    post_gen_hook_file.remove()
82
83
84
def test_ignore_hook_backup_files(monkeypatch, dir_with_hooks):
85
    # Change the current working directory that contains `hooks/`
86
    monkeypatch.chdir(dir_with_hooks)
87
    assert hooks.find_hook('pre_gen_project') is None
88
    assert hooks.find_hook('post_gen_project') is None
89