1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
test_generate_file |
6
|
|
|
------------------ |
7
|
|
|
|
8
|
|
|
Tests formerly known from a unittest residing in test_generate.py named |
9
|
|
|
TestGenerateFile.test_generate_file |
10
|
|
|
TestGenerateFile.test_generate_file_verbose_template_syntax_error |
11
|
|
|
""" |
12
|
|
|
|
13
|
|
|
from __future__ import unicode_literals |
14
|
|
|
import os |
15
|
|
|
import pytest |
16
|
|
|
|
17
|
|
|
from jinja2 import FileSystemLoader |
18
|
|
|
from jinja2.exceptions import TemplateSyntaxError |
19
|
|
|
|
20
|
|
|
from cookiecutter import generate |
21
|
|
|
from cookiecutter.environment import StrictEnvironment |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
@pytest.fixture(scope='function') |
25
|
|
|
def remove_cheese_file(request): |
26
|
|
|
""" |
27
|
|
|
Remove the cheese text file which is created by the tests. |
28
|
|
|
""" |
29
|
|
|
def fin_remove_cheese_file(): |
30
|
|
|
if os.path.exists('tests/files/cheese.txt'): |
31
|
|
|
os.remove('tests/files/cheese.txt') |
32
|
|
|
if os.path.exists('tests/files/cheese_lf_newlines.txt'): |
33
|
|
|
os.remove('tests/files/cheese_lf_newlines.txt') |
34
|
|
|
if os.path.exists('tests/files/cheese_crlf_newlines.txt'): |
35
|
|
|
os.remove('tests/files/cheese_crlf_newlines.txt') |
36
|
|
|
request.addfinalizer(fin_remove_cheese_file) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
@pytest.fixture |
40
|
|
|
def env(): |
41
|
|
|
environment = StrictEnvironment() |
42
|
|
|
environment.loader = FileSystemLoader('.') |
43
|
|
|
return environment |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
@pytest.mark.usefixtures('remove_cheese_file') |
47
|
|
|
def test_generate_file(env): |
48
|
|
|
infile = 'tests/files/{{generate_file}}.txt' |
49
|
|
|
generate.generate_file( |
50
|
|
|
project_dir=".", |
51
|
|
|
infile=infile, |
52
|
|
|
context={'generate_file': 'cheese'}, |
53
|
|
|
env=env |
54
|
|
|
) |
55
|
|
|
assert os.path.isfile('tests/files/cheese.txt') |
56
|
|
|
with open('tests/files/cheese.txt', 'rt') as f: |
57
|
|
|
generated_text = f.read() |
58
|
|
|
assert generated_text == 'Testing cheese' |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
@pytest.mark.usefixtures('remove_cheese_file') |
62
|
|
|
def test_generate_file_with_false_condition(env): |
63
|
|
|
infile = 'tests/files/{% if generate_file == \'y\' %}cheese.txt{% endif %}' |
64
|
|
|
generate.generate_file( |
65
|
|
|
project_dir=".", |
66
|
|
|
infile=infile, |
67
|
|
|
context={'generate_file': 'n'}, |
68
|
|
|
env=env |
69
|
|
|
) |
70
|
|
|
assert not os.path.exists('tests/files/cheese.txt') |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
@pytest.mark.usefixtures('remove_cheese_file') |
74
|
|
|
def test_generate_file_with_true_conditional(env): |
75
|
|
|
infile = 'tests/files/{% if generate_file == \'y\' %}cheese.txt{% endif %}' |
76
|
|
|
generate.generate_file( |
77
|
|
|
project_dir=".", |
78
|
|
|
infile=infile, |
79
|
|
|
context={'generate_file': 'y'}, |
80
|
|
|
env=env |
81
|
|
|
) |
82
|
|
|
assert os.path.isfile('tests/files/cheese.txt') |
83
|
|
|
with open('tests/files/cheese.txt', 'rt') as f: |
84
|
|
|
generated_text = f.read() |
85
|
|
|
assert generated_text == 'Testing that generate_file was y' |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
@pytest.fixture |
89
|
|
|
def expected_msg(): |
90
|
|
|
msg = ( |
91
|
|
|
'Missing end of comment tag\n' |
92
|
|
|
' File "./tests/files/syntax_error.txt", line 1\n' |
93
|
|
|
' I eat {{ syntax_error }} {# this comment is not closed}' |
94
|
|
|
) |
95
|
|
|
return msg.replace("/", os.sep) |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
@pytest.mark.usefixtures('remove_cheese_file') |
99
|
|
|
def test_generate_file_verbose_template_syntax_error(env, expected_msg): |
100
|
|
|
try: |
101
|
|
|
generate.generate_file( |
102
|
|
|
project_dir=".", |
103
|
|
|
infile='tests/files/syntax_error.txt', |
104
|
|
|
context={'syntax_error': 'syntax_error'}, |
105
|
|
|
env=env |
106
|
|
|
) |
107
|
|
|
except TemplateSyntaxError as exception: |
108
|
|
|
assert str(exception) == expected_msg |
109
|
|
|
except Exception as exception: |
110
|
|
|
pytest.fail('Unexpected exception thrown: {0}'.format(exception)) |
111
|
|
|
else: |
112
|
|
|
pytest.fail('TemplateSyntaxError not thrown') |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
@pytest.mark.usefixtures('remove_cheese_file') |
116
|
|
|
def test_generate_file_does_not_translate_lf_newlines(env): |
117
|
|
|
infile = 'tests/files/{{generate_file}}_lf_newlines.txt' |
118
|
|
|
generate.generate_file( |
119
|
|
|
project_dir=".", |
120
|
|
|
infile=infile, |
121
|
|
|
context={'generate_file': 'cheese'}, |
122
|
|
|
env=env |
123
|
|
|
) |
124
|
|
|
lines = open('tests/files/cheese_lf_newlines.txt', 'rb').readlines() |
125
|
|
|
assert all(l.endswith('\n') for l in lines) |
126
|
|
|
assert all(not l.endswith('\r\n') for l in lines) |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
@pytest.mark.usefixtures('remove_cheese_file') |
130
|
|
|
def test_generate_file_does_translate_crlf_newlines_lf(env): |
131
|
|
|
infile = 'tests/files/{{generate_file}}_crlf_newlines.txt' |
132
|
|
|
generate.generate_file( |
133
|
|
|
project_dir=".", |
134
|
|
|
infile=infile, |
135
|
|
|
context={'generate_file': 'cheese'}, |
136
|
|
|
env=env |
137
|
|
|
) |
138
|
|
|
lines = open('tests/files/cheese_crlf_newlines.txt', 'rb').readlines() |
139
|
|
|
assert all(l.endswith('\n') for l in lines) |
140
|
|
|
assert all(not l.endswith('\r\n') for l in lines) |
141
|
|
|
|