Completed
Pull Request — master (#694)
by Eric
01:15
created

TestRealHooks   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 128
rs 10
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
C test_run_hook_returns_context() 0 46 7
A test_run_script_with_context() 0 16 2
A test_run_script_with_context_runs_hook_in_place() 0 20 2
A test_run_script_get_updated_context() 0 22 2
A test_run_script_with_context_returns_context() 0 15 2
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
"""
5
test_real_hooks_with_context_serialized
6
---------------------------------------
7
8
Additional tests for `cookiecutter.hooks` module.
9
"""
10
11
import os
12
13
from cookiecutter import hooks, utils
14
15
16
class TestRealHooks(object):
17
    repo_path = os.path.abspath(
18
        'tests/test-real-hooks-with-serialized-context')
19
    hooks_path = repo_path + '/hooks'
20
21
    def test_run_script_with_context(self):
22
        """
23
        Execute a hook script, passing a serialized context object and
24
        getting the context not updated
25
        """
26
27
        context = {
28
            "my_key": "my_val"
29
        }
30
        actual = hooks.run_script(
31
            os.path.join(
32
                self.repo_path, 'simple', 'hooks', 'pre_gen_project.py'),
33
            context=context
34
        )
35
36
        assert actual == context
37
38
    def test_run_script_get_updated_context(self):
39
        """
40
        Execute a hook script, passing a serialized context object and
41
        getting the context updated
42
        """
43
        context = {
44
            "my_key": "my_val"
45
        }
46
        expected = {
47
            "my_key": "my_val_updated"
48
        }
49
        actual = hooks.run_script(
50
            os.path.join(
51
                self.repo_path,
52
                'update_context',
53
                'hooks',
54
                'pre_gen_project.py'
55
            ),
56
            context=context
57
        )
58
59
        assert actual == expected
60
61
    def test_run_script_with_context_returns_context(self):
62
        """
63
        Execute a hook script, passing a serialized context object
64
        """
65
        context = {
66
            "my_key": "my_val"
67
        }
68
        actual = hooks.run_script_with_context(
69
            os.path.join(
70
                self.repo_path, 'simple', 'hooks', 'pre_gen_project.py'),
71
            'tests',
72
            context
73
        )
74
75
        assert actual == context
76
77
    def test_run_hook_returns_context(self):
78
        """
79
        Execute a hook script, passing a serialized context object
80
        """
81
        context = {
82
            "my_key": "my_val"
83
        }
84
85
        with utils.work_in(os.path.join(self.repo_path, 'simple')):
86
            actual = hooks.run_hook(
87
                'pre_gen_project',
88
                os.path.join(
89
                    self.repo_path,
90
                    'simple',
91
                    'input{{simple_hooks}}'
92
                ),
93
                context
94
            )
95
            assert actual == context
96
97
        with utils.work_in(os.path.join(self.repo_path, 'update_context')):
98
            expected = {
99
                "my_key": "my_val_updated"
100
            }
101
            actual = hooks.run_hook(
102
                'pre_gen_project',
103
                os.path.join(
104
                    self.repo_path,
105
                    'update_context',
106
                    'input{{update_context_hooks}}'
107
                ),
108
                context
109
            )
110
            assert actual == expected
111
112
        with utils.work_in(os.path.join(self.repo_path, 'simple')):
113
            actual = hooks.run_hook(
114
                'not_handled_hook',
115
                os.path.join(
116
                    self.repo_path,
117
                    'simple',
118
                    'input{{simple_hooks}}'
119
                ),
120
                context
121
            )
122
            assert actual == context
123
124
    def test_run_script_with_context_runs_hook_in_place(self):
125
        """
126
        Execute a hook script in place, passing a serialized context object
127
        """
128
        hook = os.path.join(
129
            self.repo_path,
130
            'inplace',
131
            'hooks',
132
            'pre_gen_project.py'
133
        )
134
        context = {
135
            "_no_hookcopy": "yes"
136
        }
137
        expected = {
138
            "_no_hookcopy": "yes",
139
            "inplace": hook
140
        }
141
        actual = hooks.run_script_with_context(hook, 'tests', context)
142
143
        assert actual == expected
144