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

TestRealHooks   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 133
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_get_updated_context() 0 23 2
A test_run_script_with_context_runs_hook_in_place() 0 20 2
A test_run_script_with_context_returns_context() 0 15 2
A test_getting_bad_json_returns_original_context() 0 20 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_get_updated_context(self):
22
        """
23
        Execute a hook script, passing a serialized context object and
24
        getting the context updated
25
        """
26
        context = {
27
            "my_key": "my_val"
28
        }
29
        expected = {
30
            "my_key": "my_val_updated"
31
        }
32
        actual = hooks.run_script_with_context(
33
            os.path.join(
34
                self.repo_path,
35
                'update_context',
36
                'hooks',
37
                'pre_gen_project.py'
38
            ),
39
            'tests',
40
            context
41
        )
42
43
        assert actual == expected
44
45
    def test_run_script_with_context_returns_context(self):
46
        """
47
        Execute a hook script, passing a serialized context object
48
        """
49
        context = {
50
            "my_key": "my_val"
51
        }
52
        actual = hooks.run_script_with_context(
53
            os.path.join(
54
                self.repo_path, 'simple', 'hooks', 'pre_gen_project.py'),
55
            'tests',
56
            context
57
        )
58
59
        assert actual == context
60
61
    def test_run_hook_returns_context(self):
62
        """
63
        Execute a hook script, passing a serialized context object
64
        """
65
        context = {
66
            "my_key": "my_val"
67
        }
68
69
        with utils.work_in(os.path.join(self.repo_path, 'simple')):
70
            actual = hooks.run_hook(
71
                'pre_gen_project',
72
                os.path.join(
73
                    self.repo_path,
74
                    'simple',
75
                    'input{{simple_hooks}}'
76
                ),
77
                context
78
            )
79
            assert actual == context
80
81
        with utils.work_in(os.path.join(self.repo_path, 'update_context')):
82
            expected = {
83
                "my_key": "my_val_updated"
84
            }
85
            actual = hooks.run_hook(
86
                'pre_gen_project',
87
                os.path.join(
88
                    self.repo_path,
89
                    'update_context',
90
                    'input{{update_context_hooks}}'
91
                ),
92
                context
93
            )
94
            assert actual == expected
95
96
        with utils.work_in(os.path.join(self.repo_path, 'simple')):
97
            actual = hooks.run_hook(
98
                'not_handled_hook',
99
                os.path.join(
100
                    self.repo_path,
101
                    'simple',
102
                    'input{{simple_hooks}}'
103
                ),
104
                context
105
            )
106
            assert actual == context
107
108
    def test_run_script_with_context_runs_hook_in_place(self):
109
        """
110
        Execute a hook script in place, passing a serialized context object
111
        """
112
        hook = os.path.join(
113
            self.repo_path,
114
            'inplace',
115
            'hooks',
116
            'pre_gen_project.py'
117
        )
118
        context = {
119
            "_run_hook_in_place": True
120
        }
121
        expected = {
122
            "_run_hook_in_place": True,
123
            "inplace": hook
124
        }
125
        actual = hooks.run_script_with_context(hook, 'tests', context)
126
127
        assert actual == expected
128
129
    def test_getting_bad_json_returns_original_context(self):
130
        """
131
        Execute a hook script that returns bad json
132
        """
133
        context = {
134
            "my_key": "my_val",
135
        }
136
137
        actual = hooks.run_script_with_context(
138
            os.path.join(
139
                self.repo_path,
140
                'bad_json',
141
                'hooks',
142
                'pre_gen_project.py'
143
            ),
144
            'tests',
145
            context
146
        )
147
148
        assert actual == context
149