1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
test_generate_hooks |
6
|
|
|
------------------- |
7
|
|
|
|
8
|
|
|
Tests formerly known from a unittest residing in test_generate.py named |
9
|
|
|
TestHooks.test_ignore_hooks_dirs |
10
|
|
|
TestHooks.test_run_python_hooks |
11
|
|
|
TestHooks.test_run_python_hooks_cwd |
12
|
|
|
TestHooks.test_run_shell_hooks |
13
|
|
|
""" |
14
|
|
|
|
15
|
|
|
from __future__ import unicode_literals |
16
|
|
|
import errno |
17
|
|
|
import os |
18
|
|
|
import sys |
19
|
|
|
import stat |
20
|
|
|
import pytest |
21
|
|
|
|
22
|
|
|
from cookiecutter import generate |
23
|
|
|
from cookiecutter import utils |
24
|
|
|
from cookiecutter.exceptions import FailedHookException |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
@pytest.fixture(scope='function') |
28
|
|
|
def remove_additional_folders(request): |
29
|
|
|
""" |
30
|
|
|
Remove some special folders which are created by the tests. |
31
|
|
|
""" |
32
|
|
|
def fin_remove_additional_folders(): |
33
|
|
|
if os.path.exists('tests/test-pyhooks/inputpyhooks'): |
34
|
|
|
utils.rmtree('tests/test-pyhooks/inputpyhooks') |
35
|
|
|
if os.path.exists('inputpyhooks'): |
36
|
|
|
utils.rmtree('inputpyhooks') |
37
|
|
|
if os.path.exists('tests/test-shellhooks'): |
38
|
|
|
utils.rmtree('tests/test-shellhooks') |
39
|
|
|
request.addfinalizer(fin_remove_additional_folders) |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
@pytest.mark.usefixtures('clean_system', 'remove_additional_folders') |
43
|
|
|
def test_ignore_hooks_dirs(): |
44
|
|
|
generate.generate_files( |
45
|
|
|
context={ |
46
|
|
|
'cookiecutter': {'pyhooks': 'pyhooks'} |
47
|
|
|
}, |
48
|
|
|
repo_dir='tests/test-pyhooks/', |
49
|
|
|
output_dir='tests/test-pyhooks/' |
50
|
|
|
) |
51
|
|
|
assert not os.path.exists('tests/test-pyhooks/inputpyhooks/hooks') |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
@pytest.mark.usefixtures('clean_system', 'remove_additional_folders') |
55
|
|
|
def test_run_python_hooks(): |
56
|
|
|
generate.generate_files( |
57
|
|
|
context={ |
58
|
|
|
'cookiecutter': {'pyhooks': 'pyhooks'} |
59
|
|
|
}, |
60
|
|
|
repo_dir='tests/test-pyhooks/'.replace("/", os.sep), |
61
|
|
|
output_dir='tests/test-pyhooks/'.replace("/", os.sep) |
62
|
|
|
) |
63
|
|
|
assert os.path.exists('tests/test-pyhooks/inputpyhooks/python_pre.txt') |
64
|
|
|
assert os.path.exists('tests/test-pyhooks/inputpyhooks/python_post.txt') |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
@pytest.mark.usefixtures('clean_system', 'remove_additional_folders') |
68
|
|
|
def test_run_python_hooks_cwd(): |
69
|
|
|
generate.generate_files( |
70
|
|
|
context={ |
71
|
|
|
'cookiecutter': {'pyhooks': 'pyhooks'} |
72
|
|
|
}, |
73
|
|
|
repo_dir='tests/test-pyhooks/' |
74
|
|
|
) |
75
|
|
|
assert os.path.exists('inputpyhooks/python_pre.txt') |
76
|
|
|
assert os.path.exists('inputpyhooks/python_post.txt') |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
@pytest.mark.usefixtures('clean_system', 'remove_additional_folders') |
80
|
|
|
def test_empty_hooks(): |
81
|
|
|
with pytest.raises(FailedHookException) as excinfo: |
82
|
|
|
generate.generate_files( |
83
|
|
|
context={ |
84
|
|
|
'cookiecutter': {'shellhooks': 'shellhooks'} |
85
|
|
|
}, |
86
|
|
|
repo_dir='tests/test-shellhooks-empty/', |
87
|
|
|
overwrite_if_exists=True |
88
|
|
|
) |
89
|
|
|
assert 'shebang' in str(excinfo.value) |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
@pytest.mark.usefixtures('clean_system', 'remove_additional_folders') |
93
|
|
|
def test_oserror_hooks(mocker): |
94
|
|
|
|
95
|
|
|
message = 'Out of memory' |
96
|
|
|
|
97
|
|
|
err = OSError(message) |
98
|
|
|
err.errno = errno.ENOMEM |
99
|
|
|
|
100
|
|
|
prompt = mocker.patch('subprocess.Popen') |
101
|
|
|
prompt.side_effect = err |
102
|
|
|
|
103
|
|
|
with pytest.raises(FailedHookException) as excinfo: |
104
|
|
|
generate.generate_files( |
105
|
|
|
context={ |
106
|
|
|
'cookiecutter': {'shellhooks': 'shellhooks'} |
107
|
|
|
}, |
108
|
|
|
repo_dir='tests/test-shellhooks-empty/', |
109
|
|
|
overwrite_if_exists=True |
110
|
|
|
) |
111
|
|
|
assert message in str(excinfo.value) |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
def make_test_repo(name): |
115
|
|
|
hooks = os.path.join(name, 'hooks') |
116
|
|
|
template = os.path.join(name, 'input{{cookiecutter.shellhooks}}') |
117
|
|
|
os.mkdir(name) |
118
|
|
|
os.mkdir(hooks) |
119
|
|
|
os.mkdir(template) |
120
|
|
|
|
121
|
|
|
with open(os.path.join(template, 'README.rst'), 'w') as f: |
122
|
|
|
f.write("foo\n===\n\nbar\n") |
123
|
|
|
|
124
|
|
|
if sys.platform.startswith('win'): |
125
|
|
|
filename = os.path.join(hooks, 'pre_gen_project.bat') |
126
|
|
|
with open(filename, 'w') as f: |
127
|
|
|
f.write("@echo off\n") |
128
|
|
|
f.write("\n") |
129
|
|
|
f.write("echo pre generation hook\n") |
130
|
|
|
f.write("echo. >shell_pre.txt\n") |
131
|
|
|
|
132
|
|
|
filename = os.path.join(hooks, 'post_gen_project.bat') |
133
|
|
|
with open(filename, 'w') as f: |
134
|
|
|
f.write("@echo off\n") |
135
|
|
|
f.write("\n") |
136
|
|
|
f.write("echo post generation hook\n") |
137
|
|
|
f.write("echo. >shell_post.txt\n") |
138
|
|
|
else: |
139
|
|
|
filename = os.path.join(hooks, 'pre_gen_project.sh') |
140
|
|
|
with open(filename, 'w') as f: |
141
|
|
|
f.write("#!/bin/bash\n") |
142
|
|
|
f.write("\n") |
143
|
|
|
f.write("echo 'pre generation hook';\n") |
144
|
|
|
f.write("touch 'shell_pre.txt'\n") |
145
|
|
|
# Set the execute bit |
146
|
|
|
os.chmod(filename, os.stat(filename).st_mode | stat.S_IXUSR) |
147
|
|
|
|
148
|
|
|
filename = os.path.join(hooks, 'post_gen_project.sh') |
149
|
|
|
with open(filename, 'w') as f: |
150
|
|
|
f.write("#!/bin/bash\n") |
151
|
|
|
f.write("\n") |
152
|
|
|
f.write("echo 'post generation hook';\n") |
153
|
|
|
f.write("touch 'shell_post.txt'\n") |
154
|
|
|
# Set the execute bit |
155
|
|
|
os.chmod(filename, os.stat(filename).st_mode | stat.S_IXUSR) |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
@pytest.mark.usefixtures('clean_system', 'remove_additional_folders') |
159
|
|
|
def test_run_shell_hooks(): |
160
|
|
|
make_test_repo('tests/test-shellhooks') |
161
|
|
|
generate.generate_files( |
162
|
|
|
context={ |
163
|
|
|
'cookiecutter': {'shellhooks': 'shellhooks'} |
164
|
|
|
}, |
165
|
|
|
repo_dir='tests/test-shellhooks/', |
166
|
|
|
output_dir='tests/test-shellhooks/' |
167
|
|
|
) |
168
|
|
|
shell_pre_file = 'tests/test-shellhooks/inputshellhooks/shell_pre.txt' |
169
|
|
|
shell_post_file = 'tests/test-shellhooks/inputshellhooks/shell_post.txt' |
170
|
|
|
assert os.path.exists(shell_pre_file) |
171
|
|
|
assert os.path.exists(shell_post_file) |
172
|
|
|
|