forked from hackebrot/pytest-cookies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cookies.py
395 lines (292 loc) · 12 KB
/
test_cookies.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# -*- coding: utf-8 -*-
import json
import pytest
def test_cookies_fixture(testdir):
"""Make sure that pytest accepts the `cookies` fixture."""
# create a temporary pytest test module
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
def test_valid_fixture(cookies):
assert hasattr(cookies, 'bake')
assert callable(cookies.bake)
"""
)
# run pytest with the following cmd args
result = testdir.runpytest("-v")
# fnmatch_lines does an assertion internally
result.stdout.fnmatch_lines(["*::test_valid_fixture PASSED*"])
# make sure that that we get a '0' exit code for the testsuite
assert result.ret == 0
def test_cookies_bake_with_template_kwarg(testdir, cookiecutter_template):
"""bake accepts a template kwarg."""
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
def test_bake_project(cookies):
result = cookies.bake(
extra_context={'repo_name': 'helloworld'},
template=r'%s',
)
assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == 'helloworld'
assert result.project.isdir()
assert str(result) == '<Result {}>'.format(result.project)
"""
% cookiecutter_template
)
# run pytest without the template cli arg
result = testdir.runpytest("-v")
result.stdout.fnmatch_lines(["*::test_bake_project PASSED*"])
def test_cookies_bake_template_kwarg_overrides_cli_option(
testdir, cookiecutter_template
):
"""bake template kwarg overrides cli option."""
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
def test_bake_project(cookies):
result = cookies.bake(
extra_context={'repo_name': 'helloworld'},
template=r'%s',
)
assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == 'helloworld'
assert result.project.isdir()
assert str(result) == '<Result {}>'.format(result.project)
"""
% cookiecutter_template
)
# run pytest with a bogus template name
# it should use template directory passed to `cookies.bake`
result = testdir.runpytest("-v", "--template=foobar")
result.stdout.fnmatch_lines(["*::test_bake_project PASSED*"])
def test_cookies_bake(testdir, cookiecutter_template):
"""Programmatically create a **Cookiecutter** template and use `bake` to
create a project from it.
"""
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
def test_bake_project(cookies):
result = cookies.bake(extra_context={'repo_name': 'helloworld'})
assert result.exit_code == 0
assert result.exception is None
assert result.project_path.name == 'helloworld'
assert result.project_path.is_dir()
assert str(result) == '<Result {}>'.format(result.project_path)
assert result.project.basename == 'helloworld'
assert result.project.isdir()
assert str(result) == '<Result {}>'.format(result.project)
"""
)
result = testdir.runpytest("-v", "--template={}".format(cookiecutter_template))
result.stdout.fnmatch_lines(["*::test_bake_project PASSED*"])
def test_cookies_bake_project_warning(testdir, cookiecutter_template):
"""Programmatically create a **Cookiecutter** template and use `bake` to
create a project from it and check for warnings when accesssing the project
attribute.
"""
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
import warnings
def test_bake_project(cookies):
warning_message = (
"project is deprecated and will be removed in a future release, "
"please use project_path instead."
)
result = cookies.bake(extra_context={'repo_name': 'helloworld'})
assert result.exit_code == 0
assert result.exception is None
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
project_basename = result.project.basename
assert project_basename == 'helloworld'
[warning] = w
assert issubclass(warning.category, DeprecationWarning)
assert str(warning.message) == warning_message
assert str(result) == '<Result {}>'.format(result.project)
"""
)
result = testdir.runpytest("-v", "--template={}".format(cookiecutter_template))
result.stdout.fnmatch_lines(["*::test_bake_project PASSED*"])
def test_cookies_bake_result_context(testdir, cookiecutter_template):
"""Programmatically create a **Cookiecutter** template and use `bake` to
create a project from it.
Check that the result holds the rendered context.
"""
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
import collections
def test_bake_project(cookies):
result = cookies.bake(extra_context=collections.OrderedDict([
('repo_name', 'cookies'),
('short_description', '{{cookiecutter.repo_name}} is awesome'),
]))
assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == 'cookies'
assert result.project.isdir()
assert result.context == {
'repo_name': 'cookies',
'short_description': 'cookies is awesome',
}
assert str(result) == '<Result {}>'.format(result.project)
"""
)
result = testdir.runpytest("-v", "--template={}".format(cookiecutter_template))
result.stdout.fnmatch_lines(["*::test_bake_project PASSED*"])
def test_cookies_bake_result_context_exception(testdir, cookiecutter_template):
"""Programmatically create a **Cookiecutter** template and use `bake` to
create a project from it.
Check that exceptions resulting from rendering the context are stored on
result and that the rendered context is not set.
"""
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
import collections
def test_bake_project(cookies):
result = cookies.bake(extra_context=collections.OrderedDict([
('repo_name', 'cookies'),
('short_description', '{{cookiecutter.nope}}'),
]))
assert result.exit_code == -1
assert result.exception is not None
assert result.project is None
assert result.context is None
assert str(result) == '<Result {!r}>'.format(result.exception)
"""
)
result = testdir.runpytest("-v", "--template={}".format(cookiecutter_template))
result.stdout.fnmatch_lines(["*::test_bake_project PASSED*"])
def test_cookies_bake_should_create_new_output_directories(
testdir, cookiecutter_template
):
"""Programmatically create a **Cookiecutter** template and use `bake` to
create a project from it.
"""
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
def test_bake_should_create_new_output(cookies):
first_result = cookies.bake()
assert first_result.exception is None
assert first_result.project.dirname.endswith('bake00')
second_result = cookies.bake()
assert second_result.exception is None
assert second_result.project.dirname.endswith('bake01')
"""
)
result = testdir.runpytest("-v", "--template={}".format(cookiecutter_template))
result.stdout.fnmatch_lines(["*::test_bake_should_create_new_output PASSED*"])
def test_cookies_fixture_removes_output_directories(testdir, cookiecutter_template):
"""Programmatically create a **Cookiecutter** template and use `bake` to
create a project from it.
"""
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
import os
def test_to_create_result(cookies):
global result_dirname
result = cookies.bake()
result_dirname = result.project.dirname
assert result.exception is None
def test_previously_generated_directory_is_removed(cookies):
exists = os.path.isdir(result_dirname)
assert exists is False
"""
)
result = testdir.runpytest("-v", "--template={}".format(cookiecutter_template))
result.stdout.fnmatch_lines(
[
"*::test_to_create_result PASSED*",
"*::test_previously_generated_directory_is_removed PASSED*",
]
)
def test_cookies_fixture_doesnt_remove_output_directories(
testdir, cookiecutter_template
):
"""Programmatically create a **Cookiecutter** template and use `bake` to
create a project from it.
"""
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
import os
def test_to_create_result(cookies):
global result_dirname
result = cookies.bake()
result_dirname = result.project.dirname
assert result.exception is None
def test_previously_generated_directory_is_not_removed(cookies):
exists = os.path.isdir(result_dirname)
assert exists is True
"""
)
result = testdir.runpytest(
"-v", "--template={}".format(cookiecutter_template), "--keep-baked-projects"
)
result.stdout.fnmatch_lines(
[
"*::test_to_create_result PASSED*",
"*::test_previously_generated_directory_is_not_removed PASSED*",
]
)
def test_cookies_bake_should_handle_exception(testdir):
"""Programmatically create a **Cookiecutter** template and make sure that
cookies.bake() handles exceptions that happen during project generation.
We expect **Cookiecutter** to raise a `NonTemplatedInputDirException`.
"""
template = testdir.tmpdir.ensure("cookiecutter-fail", dir=True)
template_config = {"repo_name": "foobar", "short_description": "Test Project"}
template.join("cookiecutter.json").write(json.dumps(template_config))
template.ensure("cookiecutter.repo_name", dir=True)
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
def test_bake_should_fail(cookies):
result = cookies.bake()
assert result.exit_code == -1
assert result.exception is not None
assert result.project is None
"""
)
result = testdir.runpytest("-v", "--template={}".format(template))
result.stdout.fnmatch_lines(["*::test_bake_should_fail PASSED*"])
@pytest.mark.parametrize("choice", ["mkdocs", "sphinx", "none"])
def test_cookies_bake_choices(testdir, choice):
"""Programmatically create a **Cookiecutter** template and make sure that
cookies.bake() works with choice variables.
"""
template = testdir.tmpdir.ensure("cookiecutter-choices", dir=True)
template_config = {"repo_name": "docs", "docs_tool": ["mkdocs", "sphinx", "none"]}
template.join("cookiecutter.json").write(json.dumps(template_config))
repo = template.ensure("{{cookiecutter.repo_name}}", dir=True)
repo.join("README.rst").write("docs_tool: {{cookiecutter.docs_tool}}")
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
def test_bake_project(cookies):
result = cookies.bake(
extra_context={'docs_tool': '%s'},
template=r'%s',
)
assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == 'docs'
assert result.project.isdir()
assert result.project.join('README.rst').read() == 'docs_tool: %s'
assert str(result) == '<Result {}>'.format(result.project)
"""
% (choice, template, choice)
)
# run pytest without the template cli arg
result = testdir.runpytest("-v")
result.stdout.fnmatch_lines(["*::test_bake_project PASSED*"])
result = testdir.runpytest("-v", "--template={}".format(template))