|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
import os |
|
4
|
|
|
import pytest |
|
5
|
|
|
import sys |
|
6
|
|
|
import textwrap |
|
7
|
|
|
|
|
8
|
|
|
from cookiecutter import exceptions, hooks |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
@pytest.fixture |
|
12
|
|
|
def repo_dir_with_hooks(tmpdir): |
|
13
|
|
|
repo_dir = tmpdir |
|
14
|
|
|
hooks_dir = repo_dir.mkdir('hooks') |
|
15
|
|
|
repo_dir.mkdir('input{{hooks}}') |
|
16
|
|
|
|
|
17
|
|
|
pre_hook_content = textwrap.dedent( |
|
18
|
|
|
u"""\ |
|
19
|
|
|
#!/usr/bin/env python |
|
20
|
|
|
# -*- coding: utf-8 -*- |
|
21
|
|
|
from __future__ import print_function |
|
22
|
|
|
|
|
23
|
|
|
print('pre generation hook') |
|
24
|
|
|
f = open('python_pre.txt', 'w') |
|
25
|
|
|
f.close() |
|
26
|
|
|
""" |
|
27
|
|
|
) |
|
28
|
|
|
pre_gen_hook_file = hooks_dir / 'pre_gen_project.py' |
|
29
|
|
|
pre_gen_hook_file.write_text(pre_hook_content, encoding='utf8') |
|
30
|
|
|
|
|
31
|
|
|
if sys.platform.startswith('win'): |
|
32
|
|
|
post_gen_hook_file = hooks_dir / 'post_gen_project.bat' |
|
33
|
|
|
post_hook_content = textwrap.dedent( |
|
34
|
|
|
u"""\ |
|
35
|
|
|
@echo off |
|
36
|
|
|
|
|
37
|
|
|
echo post generation hook |
|
38
|
|
|
echo. >shell_post.txt |
|
39
|
|
|
""" |
|
40
|
|
|
) |
|
41
|
|
|
post_gen_hook_file.write_text(post_hook_content, encoding='utf8') |
|
42
|
|
|
else: |
|
43
|
|
|
post_gen_hook_file = hooks_dir / 'post_gen_project.sh' |
|
44
|
|
|
post_hook_content = textwrap.dedent( |
|
45
|
|
|
u"""\ |
|
46
|
|
|
#!/bin/bash |
|
47
|
|
|
|
|
48
|
|
|
echo 'post generation hook'; |
|
49
|
|
|
touch 'shell_post.txt' |
|
50
|
|
|
""" |
|
51
|
|
|
) |
|
52
|
|
|
post_gen_hook_file.write_text(post_hook_content, encoding='utf8') |
|
53
|
|
|
|
|
54
|
|
|
return str(repo_dir) |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
def test_run_hook(monkeypatch, repo_dir_with_hooks): |
|
58
|
|
|
"""Execute hook from specified template in specified output |
|
59
|
|
|
directory. |
|
60
|
|
|
""" |
|
61
|
|
|
tests_dir = os.path.join(repo_dir_with_hooks, 'input{{hooks}}') |
|
62
|
|
|
monkeypatch.chdir(repo_dir_with_hooks) |
|
63
|
|
|
|
|
64
|
|
|
hooks.run_hook('pre_gen_project', tests_dir, {}) |
|
65
|
|
|
assert os.path.isfile(os.path.join(tests_dir, 'python_pre.txt')) |
|
66
|
|
|
|
|
67
|
|
|
hooks.run_hook('post_gen_project', tests_dir, {}) |
|
68
|
|
|
assert os.path.isfile(os.path.join(tests_dir, 'shell_post.txt')) |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
def test_run_failing_hook(monkeypatch, repo_dir_with_hooks): |
|
72
|
|
|
hook_path = os.path.join( |
|
73
|
|
|
os.path.join(repo_dir_with_hooks, 'hooks'), |
|
74
|
|
|
'pre_gen_project.py' |
|
75
|
|
|
) |
|
76
|
|
|
tests_dir = os.path.join(repo_dir_with_hooks, 'input{{hooks}}') |
|
77
|
|
|
|
|
78
|
|
|
with open(hook_path, 'w') as f: |
|
79
|
|
|
f.write("#!/usr/bin/env python\n") |
|
80
|
|
|
f.write("import sys; sys.exit(1)\n") |
|
81
|
|
|
|
|
82
|
|
|
monkeypatch.chdir(repo_dir_with_hooks) |
|
83
|
|
|
with pytest.raises(exceptions.FailedHookException) as excinfo: |
|
84
|
|
|
hooks.run_hook('pre_gen_project', tests_dir, {}) |
|
85
|
|
|
assert 'Hook script failed' in str(excinfo.value) |
|
86
|
|
|
|