|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
import os |
|
4
|
|
|
import pytest |
|
5
|
|
|
import sys |
|
6
|
|
|
import textwrap |
|
7
|
|
|
|
|
8
|
|
|
from cookiecutter import hooks |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
@pytest.yield_fixture |
|
12
|
|
|
def repo_dir_with_hooks(tmpdir): |
|
13
|
|
|
"""Yield a cookiecutter directory with hook files.""" |
|
14
|
|
|
|
|
15
|
|
|
repo_dir = tmpdir |
|
16
|
|
|
hooks_dir = repo_dir.mkdir('hooks') |
|
17
|
|
|
|
|
18
|
|
|
pre_hook_content = textwrap.dedent( |
|
19
|
|
|
u"""\ |
|
20
|
|
|
#!/usr/bin/env python |
|
21
|
|
|
# -*- coding: utf-8 -*- |
|
22
|
|
|
from __future__ import print_function |
|
23
|
|
|
|
|
24
|
|
|
print('pre generation hook') |
|
25
|
|
|
f = open('python_pre.txt', 'w') |
|
26
|
|
|
f.close() |
|
27
|
|
|
""" |
|
28
|
|
|
) |
|
29
|
|
|
pre_gen_hook_file = hooks_dir / 'pre_gen_project.py' |
|
30
|
|
|
pre_gen_hook_file.write_text(pre_hook_content, encoding='utf8') |
|
31
|
|
|
|
|
32
|
|
|
if sys.platform.startswith('win'): |
|
33
|
|
|
post_gen_hook_file = hooks_dir / 'post_gen_project.bat' |
|
34
|
|
|
post_hook_content = textwrap.dedent( |
|
35
|
|
|
u"""\ |
|
36
|
|
|
@echo off |
|
37
|
|
|
|
|
38
|
|
|
echo post generation hook |
|
39
|
|
|
echo. >shell_post.txt |
|
40
|
|
|
""" |
|
41
|
|
|
) |
|
42
|
|
|
post_gen_hook_file.write_text(post_hook_content, encoding='utf8') |
|
43
|
|
|
else: |
|
44
|
|
|
post_gen_hook_file = hooks_dir / 'post_gen_project.sh' |
|
45
|
|
|
post_hook_content = textwrap.dedent( |
|
46
|
|
|
u"""\ |
|
47
|
|
|
#!/bin/bash |
|
48
|
|
|
|
|
49
|
|
|
echo 'post generation hook'; |
|
50
|
|
|
touch 'shell_post.txt' |
|
51
|
|
|
""" |
|
52
|
|
|
) |
|
53
|
|
|
post_gen_hook_file.write_text(post_hook_content, encoding='utf8') |
|
54
|
|
|
|
|
55
|
|
|
yield str(repo_dir) |
|
56
|
|
|
|
|
57
|
|
|
repo_dir.remove() |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def test_find_hook(monkeypatch, repo_dir_with_hooks): |
|
61
|
|
|
"""Finds the specified hook.""" |
|
62
|
|
|
|
|
63
|
|
|
monkeypatch.chdir(repo_dir_with_hooks) |
|
64
|
|
|
assert ( |
|
65
|
|
|
os.path.abspath('hooks/pre_gen_project.py') == |
|
66
|
|
|
hooks.find_hook('pre_gen_project') |
|
67
|
|
|
) |
|
68
|
|
|
|
|
69
|
|
|
post_hook_name = 'post_gen_project.{}'.format( |
|
70
|
|
|
'bat' if sys.platform.startswith('win') else 'sh' |
|
71
|
|
|
) |
|
72
|
|
|
assert ( |
|
73
|
|
|
os.path.abspath('hooks/{}'.format(post_hook_name)) == |
|
74
|
|
|
hooks.find_hook('post_gen_project') |
|
75
|
|
|
) |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
def test_no_hooks(monkeypatch): |
|
79
|
|
|
"""find_hooks should return None if the hook could not be found.""" |
|
80
|
|
|
monkeypatch.chdir('tests/fake-repo') |
|
81
|
|
|
assert None is hooks.find_hook('pre_gen_project') |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
def test_unknown_hooks_dir(monkeypatch, repo_dir_with_hooks): |
|
85
|
|
|
monkeypatch.chdir(repo_dir_with_hooks) |
|
86
|
|
|
assert hooks.find_hook( |
|
87
|
|
|
'pre_gen_project', |
|
88
|
|
|
hooks_dir='chooks' |
|
89
|
|
|
) is None |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
def test_hook_not_found(monkeypatch, repo_dir_with_hooks): |
|
93
|
|
|
monkeypatch.chdir(repo_dir_with_hooks) |
|
94
|
|
|
assert hooks.find_hook('unknown_hook') is None |
|
95
|
|
|
|