|
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 os |
|
17
|
|
|
import sys |
|
18
|
|
|
import stat |
|
19
|
|
|
import pytest |
|
20
|
|
|
|
|
21
|
|
|
from cookiecutter import generate |
|
22
|
|
|
from cookiecutter import utils |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
@pytest.fixture(scope='function') |
|
26
|
|
|
def remove_additional_folders(request): |
|
27
|
|
|
""" |
|
28
|
|
|
Remove some special folders which are created by the tests. |
|
29
|
|
|
""" |
|
30
|
|
|
def fin_remove_additional_folders(): |
|
31
|
|
|
if os.path.exists('tests/test-pyhooks/inputpyhooks'): |
|
32
|
|
|
utils.rmtree('tests/test-pyhooks/inputpyhooks') |
|
33
|
|
|
if os.path.exists('inputpyhooks'): |
|
34
|
|
|
utils.rmtree('inputpyhooks') |
|
35
|
|
|
if os.path.exists('tests/test-shellhooks'): |
|
36
|
|
|
utils.rmtree('tests/test-shellhooks') |
|
37
|
|
|
request.addfinalizer(fin_remove_additional_folders) |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
@pytest.mark.usefixtures('clean_system', 'remove_additional_folders') |
|
41
|
|
|
def test_ignore_hooks_dirs(): |
|
42
|
|
|
generate.generate_files( |
|
43
|
|
|
context={ |
|
44
|
|
|
'cookiecutter': {'pyhooks': 'pyhooks'} |
|
45
|
|
|
}, |
|
46
|
|
|
repo_dir='tests/test-pyhooks/', |
|
47
|
|
|
output_dir='tests/test-pyhooks/' |
|
48
|
|
|
) |
|
49
|
|
|
assert not os.path.exists('tests/test-pyhooks/inputpyhooks/hooks') |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
@pytest.mark.usefixtures('clean_system', 'remove_additional_folders') |
|
53
|
|
|
def test_run_python_hooks(): |
|
54
|
|
|
generate.generate_files( |
|
55
|
|
|
context={ |
|
56
|
|
|
'cookiecutter': {'pyhooks': 'pyhooks'} |
|
57
|
|
|
}, |
|
58
|
|
|
repo_dir='tests/test-pyhooks/'.replace("/", os.sep), |
|
59
|
|
|
output_dir='tests/test-pyhooks/'.replace("/", os.sep) |
|
60
|
|
|
) |
|
61
|
|
|
assert os.path.exists('tests/test-pyhooks/inputpyhooks/python_pre.txt') |
|
62
|
|
|
assert os.path.exists('tests/test-pyhooks/inputpyhooks/python_post.txt') |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
@pytest.mark.usefixtures('clean_system', 'remove_additional_folders') |
|
66
|
|
|
def test_run_python_hooks_cwd(): |
|
67
|
|
|
generate.generate_files( |
|
68
|
|
|
context={ |
|
69
|
|
|
'cookiecutter': {'pyhooks': 'pyhooks'} |
|
70
|
|
|
}, |
|
71
|
|
|
repo_dir='tests/test-pyhooks/' |
|
72
|
|
|
) |
|
73
|
|
|
assert os.path.exists('inputpyhooks/python_pre.txt') |
|
74
|
|
|
assert os.path.exists('inputpyhooks/python_post.txt') |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
def make_test_repo(name): |
|
78
|
|
|
hooks = os.path.join(name, 'hooks') |
|
79
|
|
|
template = os.path.join(name, 'input{{cookiecutter.shellhooks}}') |
|
80
|
|
|
os.mkdir(name) |
|
81
|
|
|
os.mkdir(hooks) |
|
82
|
|
|
os.mkdir(template) |
|
83
|
|
|
|
|
84
|
|
|
with open(os.path.join(template, 'README.rst'), 'w') as f: |
|
85
|
|
|
f.write("foo\n===\n\nbar\n") |
|
86
|
|
|
|
|
87
|
|
|
if sys.platform.startswith('win'): |
|
88
|
|
|
filename = os.path.join(hooks, 'pre_gen_project.bat') |
|
89
|
|
|
with open(filename, 'w') as f: |
|
90
|
|
|
f.write("@echo off\n") |
|
91
|
|
|
f.write("\n") |
|
92
|
|
|
f.write("echo pre generation hook\n") |
|
93
|
|
|
f.write("echo. >shell_pre.txt\n") |
|
94
|
|
|
|
|
95
|
|
|
filename = os.path.join(hooks, 'post_gen_project.bat') |
|
96
|
|
|
with open(filename, 'w') as f: |
|
97
|
|
|
f.write("@echo off\n") |
|
98
|
|
|
f.write("\n") |
|
99
|
|
|
f.write("echo post generation hook\n") |
|
100
|
|
|
f.write("echo. >shell_post.txt\n") |
|
101
|
|
|
else: |
|
102
|
|
|
filename = os.path.join(hooks, 'pre_gen_project.sh') |
|
103
|
|
|
with open(filename, 'w') as f: |
|
104
|
|
|
f.write("#!/bin/bash\n") |
|
105
|
|
|
f.write("\n") |
|
106
|
|
|
f.write("echo 'pre generation hook';\n") |
|
107
|
|
|
f.write("touch 'shell_pre.txt'\n") |
|
108
|
|
|
# Set the execute bit |
|
109
|
|
|
os.chmod(filename, os.stat(filename).st_mode | stat.S_IXUSR) |
|
110
|
|
|
|
|
111
|
|
|
filename = os.path.join(hooks, 'post_gen_project.sh') |
|
112
|
|
|
with open(filename, 'w') as f: |
|
113
|
|
|
f.write("#!/bin/bash\n") |
|
114
|
|
|
f.write("\n") |
|
115
|
|
|
f.write("echo 'post generation hook';\n") |
|
116
|
|
|
f.write("touch 'shell_post.txt'\n") |
|
117
|
|
|
# Set the execute bit |
|
118
|
|
|
os.chmod(filename, os.stat(filename).st_mode | stat.S_IXUSR) |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
@pytest.mark.usefixtures('clean_system', 'remove_additional_folders') |
|
122
|
|
|
def test_run_shell_hooks(): |
|
123
|
|
|
make_test_repo('tests/test-shellhooks') |
|
124
|
|
|
generate.generate_files( |
|
125
|
|
|
context={ |
|
126
|
|
|
'cookiecutter': {'shellhooks': 'shellhooks'} |
|
127
|
|
|
}, |
|
128
|
|
|
repo_dir='tests/test-shellhooks/', |
|
129
|
|
|
output_dir='tests/test-shellhooks/' |
|
130
|
|
|
) |
|
131
|
|
|
shell_pre_file = 'tests/test-shellhooks/inputshellhooks/shell_pre.txt' |
|
132
|
|
|
shell_post_file = 'tests/test-shellhooks/inputshellhooks/shell_post.txt' |
|
133
|
|
|
assert os.path.exists(shell_pre_file) |
|
134
|
|
|
assert os.path.exists(shell_post_file) |
|
135
|
|
|
|