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