1
|
|
|
"""Tests for `generate_file` function, part of `generate_files` function workflow.""" |
2
|
|
|
import json |
3
|
|
|
import os |
4
|
|
|
|
5
|
|
|
import pytest |
6
|
|
|
from jinja2 import FileSystemLoader |
7
|
|
|
from jinja2.exceptions import TemplateSyntaxError |
8
|
|
|
|
9
|
|
|
from cookiecutter import generate |
10
|
|
|
from cookiecutter.environment import StrictEnvironment |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
@pytest.fixture(scope='function', autouse=True) |
14
|
|
|
def tear_down(): |
15
|
|
|
""" |
16
|
|
|
Fixture. Remove the test text file which is created by the tests. |
17
|
|
|
|
18
|
|
|
Used for all tests in this file. |
19
|
|
|
""" |
20
|
|
|
yield |
21
|
|
|
if os.path.exists('tests/files/cheese.txt'): |
22
|
|
|
os.remove('tests/files/cheese.txt') |
23
|
|
|
if os.path.exists('tests/files/cheese_lf_newlines.txt'): |
24
|
|
|
os.remove('tests/files/cheese_lf_newlines.txt') |
25
|
|
|
if os.path.exists('tests/files/cheese_crlf_newlines.txt'): |
26
|
|
|
os.remove('tests/files/cheese_crlf_newlines.txt') |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
@pytest.fixture |
30
|
|
|
def env(): |
31
|
|
|
"""Fixture. Set Jinja2 environment settings for other tests.""" |
32
|
|
|
environment = StrictEnvironment() |
33
|
|
|
environment.loader = FileSystemLoader('.') |
34
|
|
|
return environment |
35
|
|
|
|
36
|
|
|
|
37
|
|
View Code Duplication |
def test_generate_file(env): |
|
|
|
|
38
|
|
|
"""Verify simple file is generated with rendered context data.""" |
39
|
|
|
infile = 'tests/files/{{cookiecutter.generate_file}}.txt' |
40
|
|
|
generate.generate_file( |
41
|
|
|
project_dir=".", |
42
|
|
|
infile=infile, |
43
|
|
|
context={'cookiecutter': {'generate_file': 'cheese'}}, |
44
|
|
|
env=env, |
45
|
|
|
) |
46
|
|
|
assert os.path.isfile('tests/files/cheese.txt') |
47
|
|
|
with open('tests/files/cheese.txt', 'rt') as f: |
48
|
|
|
generated_text = f.read() |
49
|
|
|
assert generated_text == 'Testing cheese' |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def test_generate_file_jsonify_filter(env): |
53
|
|
|
"""Verify jsonify filter works during files generation process.""" |
54
|
|
|
infile = 'tests/files/{{cookiecutter.jsonify_file}}.txt' |
55
|
|
|
data = {'jsonify_file': 'cheese', 'type': 'roquefort'} |
56
|
|
|
generate.generate_file( |
57
|
|
|
project_dir=".", infile=infile, context={'cookiecutter': data}, env=env |
58
|
|
|
) |
59
|
|
|
assert os.path.isfile('tests/files/cheese.txt') |
60
|
|
|
with open('tests/files/cheese.txt', 'rt') as f: |
61
|
|
|
generated_text = f.read() |
62
|
|
|
assert json.loads(generated_text) == data |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
@pytest.mark.parametrize("length", (10, 40)) |
66
|
|
|
@pytest.mark.parametrize("punctuation", (True, False)) |
67
|
|
|
def test_generate_file_random_ascii_string(env, length, punctuation): |
68
|
|
|
"""Verify correct work of random_ascii_string extension on file generation.""" |
69
|
|
|
infile = 'tests/files/{{cookiecutter.random_string_file}}.txt' |
70
|
|
|
data = {'random_string_file': 'cheese'} |
71
|
|
|
context = {"cookiecutter": data, "length": length, "punctuation": punctuation} |
72
|
|
|
generate.generate_file(project_dir=".", infile=infile, context=context, env=env) |
73
|
|
|
assert os.path.isfile('tests/files/cheese.txt') |
74
|
|
|
with open('tests/files/cheese.txt', 'rt') as f: |
75
|
|
|
generated_text = f.read() |
76
|
|
|
assert len(generated_text) == length |
77
|
|
|
|
78
|
|
|
|
79
|
|
View Code Duplication |
def test_generate_file_with_true_condition(env): |
|
|
|
|
80
|
|
|
"""Verify correct work of boolean condition in file name on file generation. |
81
|
|
|
|
82
|
|
|
This test has positive answer, so file should be rendered. |
83
|
|
|
""" |
84
|
|
|
infile = ( |
85
|
|
|
'tests/files/{% if cookiecutter.generate_file == \'y\' %}cheese.txt{% endif %}' |
86
|
|
|
) |
87
|
|
|
generate.generate_file( |
88
|
|
|
project_dir=".", |
89
|
|
|
infile=infile, |
90
|
|
|
context={'cookiecutter': {'generate_file': 'y'}}, |
91
|
|
|
env=env, |
92
|
|
|
) |
93
|
|
|
assert os.path.isfile('tests/files/cheese.txt') |
94
|
|
|
with open('tests/files/cheese.txt', 'rt') as f: |
95
|
|
|
generated_text = f.read() |
96
|
|
|
assert generated_text == 'Testing that generate_file was y' |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def test_generate_file_with_false_condition(env): |
100
|
|
|
"""Verify correct work of boolean condition in file name on file generation. |
101
|
|
|
|
102
|
|
|
This test has negative answer, so file should not be rendered. |
103
|
|
|
""" |
104
|
|
|
infile = ( |
105
|
|
|
'tests/files/{% if cookiecutter.generate_file == \'y\' %}cheese.txt{% endif %}' |
106
|
|
|
) |
107
|
|
|
generate.generate_file( |
108
|
|
|
project_dir=".", |
109
|
|
|
infile=infile, |
110
|
|
|
context={'cookiecutter': {'generate_file': 'n'}}, |
111
|
|
|
env=env, |
112
|
|
|
) |
113
|
|
|
assert not os.path.isfile('tests/files/cheese.txt') |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
@pytest.fixture |
117
|
|
|
def expected_msg(): |
118
|
|
|
"""Fixture. Used to ensure that exception generated text contain full data.""" |
119
|
|
|
msg = ( |
120
|
|
|
'Missing end of comment tag\n' |
121
|
|
|
' File "./tests/files/syntax_error.txt", line 1\n' |
122
|
|
|
' I eat {{ syntax_error }} {# this comment is not closed}' |
123
|
|
|
) |
124
|
|
|
return msg.replace("/", os.sep) |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
def test_generate_file_verbose_template_syntax_error(env, expected_msg): |
128
|
|
|
"""Verify correct exception raised on syntax error in file before generation.""" |
129
|
|
|
with pytest.raises(TemplateSyntaxError) as exception: |
130
|
|
|
generate.generate_file( |
131
|
|
|
project_dir=".", |
132
|
|
|
infile='tests/files/syntax_error.txt', |
133
|
|
|
context={'syntax_error': 'syntax_error'}, |
134
|
|
|
env=env, |
135
|
|
|
) |
136
|
|
|
assert str(exception.value) == expected_msg |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
def test_generate_file_does_not_translate_lf_newlines_to_crlf(env, tmp_path): |
140
|
|
|
"""Verify that file generation use same line ending, as in source file.""" |
141
|
|
|
infile = 'tests/files/{{cookiecutter.generate_file}}_lf_newlines.txt' |
142
|
|
|
generate.generate_file( |
143
|
|
|
project_dir=".", |
144
|
|
|
infile=infile, |
145
|
|
|
context={'cookiecutter': {'generate_file': 'cheese'}}, |
146
|
|
|
env=env, |
147
|
|
|
) |
148
|
|
|
|
149
|
|
|
# this generated file should have a LF line ending |
150
|
|
|
gf = 'tests/files/cheese_lf_newlines.txt' |
151
|
|
|
with open(gf, 'r', encoding='utf-8', newline='') as f: |
152
|
|
|
simple_text = f.readline() |
153
|
|
|
assert simple_text == 'newline is LF\n' |
154
|
|
|
assert f.newlines == '\n' |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
def test_generate_file_does_not_translate_crlf_newlines_to_lf(env): |
158
|
|
|
"""Verify that file generation use same line ending, as in source file.""" |
159
|
|
|
infile = 'tests/files/{{cookiecutter.generate_file}}_crlf_newlines.txt' |
160
|
|
|
generate.generate_file( |
161
|
|
|
project_dir=".", |
162
|
|
|
infile=infile, |
163
|
|
|
context={'cookiecutter': {'generate_file': 'cheese'}}, |
164
|
|
|
env=env, |
165
|
|
|
) |
166
|
|
|
|
167
|
|
|
# this generated file should have a CRLF line ending |
168
|
|
|
gf = 'tests/files/cheese_crlf_newlines.txt' |
169
|
|
|
with open(gf, 'r', encoding='utf-8', newline='') as f: |
170
|
|
|
simple_text = f.readline() |
171
|
|
|
assert simple_text == 'newline is CRLF\r\n' |
172
|
|
|
assert f.newlines == '\r\n' |
173
|
|
|
|