forked from cookiecutter/cookiecutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli.py
426 lines (326 loc) · 11.1 KB
/
test_cli.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# -*- coding: utf-8 -*-
import os
import json
from click.testing import CliRunner
import pytest
from cookiecutter.__main__ import main
from cookiecutter.main import cookiecutter
from cookiecutter import utils, config
@pytest.fixture(scope='session')
def cli_runner():
"""Fixture that returns a helper function to run the cookiecutter cli."""
runner = CliRunner()
def cli_main(*cli_args):
"""Run cookiecutter cli main with the given args."""
return runner.invoke(main, cli_args)
return cli_main
@pytest.fixture
def remove_fake_project_dir(request):
"""
Remove the fake project directory created during the tests.
"""
def fin_remove_fake_project_dir():
if os.path.isdir('fake-project'):
utils.rmtree('fake-project')
request.addfinalizer(fin_remove_fake_project_dir)
@pytest.fixture
def make_fake_project_dir(request):
"""Create a fake project to be overwritten in the according tests."""
os.makedirs('fake-project')
@pytest.fixture(params=['-V', '--version'])
def version_cli_flag(request):
return request.param
def test_cli_version(cli_runner, version_cli_flag):
result = cli_runner(version_cli_flag)
assert result.exit_code == 0
assert result.output.startswith('Cookiecutter')
@pytest.mark.usefixtures('make_fake_project_dir', 'remove_fake_project_dir')
def test_cli_error_on_existing_output_directory(cli_runner):
result = cli_runner('tests/fake-repo-pre/', '--no-input')
assert result.exit_code != 0
expected_error_msg = 'Error: "fake-project" directory already exists\n'
assert result.output == expected_error_msg
@pytest.mark.usefixtures('remove_fake_project_dir')
def test_cli(cli_runner):
result = cli_runner('tests/fake-repo-pre/', '--no-input')
assert result.exit_code == 0
assert os.path.isdir('fake-project')
with open(os.path.join('fake-project', 'README.rst')) as f:
assert 'Project name: **Fake Project**' in f.read()
@pytest.mark.usefixtures('remove_fake_project_dir')
def test_cli_verbose(cli_runner):
result = cli_runner('tests/fake-repo-pre/', '--no-input', '-v')
assert result.exit_code == 0
assert os.path.isdir('fake-project')
with open(os.path.join('fake-project', 'README.rst')) as f:
assert 'Project name: **Fake Project**' in f.read()
@pytest.mark.usefixtures('remove_fake_project_dir')
def test_cli_replay(mocker, cli_runner):
mock_cookiecutter = mocker.patch(
'cookiecutter.cli.cookiecutter'
)
template_path = 'tests/fake-repo-pre/'
result = cli_runner(template_path, '--replay', '-v')
assert result.exit_code == 0
mock_cookiecutter.assert_called_once_with(
template_path,
None,
False,
replay=True,
overwrite_if_exists=False,
output_dir='.',
config_file=config.USER_CONFIG_PATH,
extra_context=None
)
@pytest.mark.usefixtures('remove_fake_project_dir')
def test_cli_exit_on_noinput_and_replay(mocker, cli_runner):
mock_cookiecutter = mocker.patch(
'cookiecutter.cli.cookiecutter',
side_effect=cookiecutter
)
template_path = 'tests/fake-repo-pre/'
result = cli_runner(template_path, '--no-input', '--replay', '-v')
assert result.exit_code == 1
expected_error_msg = (
"You can not use both replay and no_input or extra_context "
"at the same time."
)
assert expected_error_msg in result.output
mock_cookiecutter.assert_called_once_with(
template_path,
None,
True,
replay=True,
overwrite_if_exists=False,
output_dir='.',
config_file=config.USER_CONFIG_PATH,
extra_context=None
)
@pytest.fixture(params=['-f', '--overwrite-if-exists'])
def overwrite_cli_flag(request):
return request.param
@pytest.mark.usefixtures('remove_fake_project_dir')
def test_run_cookiecutter_on_overwrite_if_exists_and_replay(
mocker, cli_runner, overwrite_cli_flag):
mock_cookiecutter = mocker.patch(
'cookiecutter.cli.cookiecutter',
side_effect=cookiecutter
)
template_path = 'tests/fake-repo-pre/'
result = cli_runner(template_path, '--replay', '-v', overwrite_cli_flag)
assert result.exit_code == 0
mock_cookiecutter.assert_called_once_with(
template_path,
None,
False,
replay=True,
overwrite_if_exists=True,
output_dir='.',
config_file=config.USER_CONFIG_PATH,
extra_context=None
)
@pytest.mark.usefixtures('remove_fake_project_dir')
def test_cli_overwrite_if_exists_when_output_dir_does_not_exist(
cli_runner, overwrite_cli_flag):
result = cli_runner(
'tests/fake-repo-pre/',
'--no-input',
overwrite_cli_flag,
)
assert result.exit_code == 0
assert os.path.isdir('fake-project')
@pytest.mark.usefixtures('make_fake_project_dir', 'remove_fake_project_dir')
def test_cli_overwrite_if_exists_when_output_dir_exists(
cli_runner, overwrite_cli_flag):
result = cli_runner(
'tests/fake-repo-pre/',
'--no-input',
overwrite_cli_flag,
)
assert result.exit_code == 0
assert os.path.isdir('fake-project')
@pytest.fixture(params=['-o', '--output-dir'])
def output_dir_flag(request):
return request.param
@pytest.fixture
def output_dir(tmpdir):
return str(tmpdir.mkdir('output'))
def test_cli_output_dir(mocker, cli_runner, output_dir_flag, output_dir):
mock_cookiecutter = mocker.patch(
'cookiecutter.cli.cookiecutter'
)
template_path = 'tests/fake-repo-pre/'
result = cli_runner(template_path, output_dir_flag, output_dir)
assert result.exit_code == 0
mock_cookiecutter.assert_called_once_with(
template_path,
None,
False,
replay=False,
overwrite_if_exists=False,
output_dir=output_dir,
config_file=config.USER_CONFIG_PATH,
extra_context=None
)
@pytest.fixture(params=['-h', '--help', 'help'])
def help_cli_flag(request):
return request.param
def test_cli_help(cli_runner, help_cli_flag):
result = cli_runner(help_cli_flag)
assert result.exit_code == 0
assert result.output.startswith('Usage')
@pytest.fixture
def user_config_path(tmpdir):
return str(tmpdir.join('tests/config.yaml'))
def test_user_config(mocker, cli_runner, user_config_path):
mock_cookiecutter = mocker.patch(
'cookiecutter.cli.cookiecutter'
)
template_path = 'tests/fake-repo-pre/'
result = cli_runner(template_path, '--config-file', user_config_path)
assert result.exit_code == 0
mock_cookiecutter.assert_called_once_with(
template_path,
None,
False,
replay=False,
overwrite_if_exists=False,
output_dir='.',
config_file=user_config_path,
extra_context=None
)
def test_default_user_config_overwrite(mocker, cli_runner, user_config_path):
mock_cookiecutter = mocker.patch(
'cookiecutter.cli.cookiecutter'
)
template_path = 'tests/fake-repo-pre/'
result = cli_runner(
template_path,
'--config-file',
user_config_path,
'--default-config',
)
assert result.exit_code == 0
mock_cookiecutter.assert_called_once_with(
template_path,
None,
False,
replay=False,
overwrite_if_exists=False,
output_dir='.',
config_file=None,
extra_context=None
)
def test_default_user_config(mocker, cli_runner):
mock_cookiecutter = mocker.patch(
'cookiecutter.cli.cookiecutter'
)
template_path = 'tests/fake-repo-pre/'
result = cli_runner(template_path, '--default-config')
assert result.exit_code == 0
mock_cookiecutter.assert_called_once_with(
template_path,
None,
False,
replay=False,
overwrite_if_exists=False,
output_dir='.',
config_file=None,
extra_context=None
)
def test_echo_undefined_variable_error(tmpdir, cli_runner):
output_dir = str(tmpdir.mkdir('output'))
template_path = 'tests/undefined-variable/file-name/'
result = cli_runner(
'--no-input',
'--default-config',
'--output-dir',
output_dir,
template_path,
)
assert result.exit_code == 1
error = "Unable to create file '{{cookiecutter.foobar}}'"
assert error in result.output
message = "Error message: 'dict object' has no attribute 'foobar'"
assert message in result.output
context = {
'cookiecutter': {
'github_username': 'hackebrot',
'project_slug': 'testproject'
}
}
context_str = json.dumps(context, indent=4, sort_keys=True)
assert context_str in result.output
def test_echo_unknown_extension_error(tmpdir, cli_runner):
output_dir = str(tmpdir.mkdir('output'))
template_path = 'tests/test-extensions/unknown/'
result = cli_runner(
'--no-input',
'--default-config',
'--output-dir',
output_dir,
template_path,
)
assert result.exit_code == 1
assert 'Unable to load extension: ' in result.output
@pytest.mark.usefixtures('remove_fake_project_dir')
def test_cli_extra_context(cli_runner):
result = cli_runner(
'tests/fake-repo-pre/',
'--no-input',
'-v',
'project_name=Awesomez',
)
assert result.exit_code == 0
assert os.path.isdir('fake-project')
with open(os.path.join('fake-project', 'README.rst')) as f:
assert 'Project name: **Awesomez**' in f.read()
@pytest.mark.usefixtures('remove_fake_project_dir')
def test_cli_extra_context_invalid_format(cli_runner):
result = cli_runner(
'tests/fake-repo-pre/',
'--no-input',
'-v',
'ExtraContextWithNoEqualsSoInvalid',
)
assert result.exit_code == 2
assert 'Error: Invalid value for "extra_context"' in result.output
assert 'should contain items of the form key=value' in result.output
@pytest.fixture
def debug_file(tmpdir):
return tmpdir / 'fake-repo.log'
@pytest.mark.usefixtures('remove_fake_project_dir')
def test_debug_file_non_verbose(cli_runner, debug_file):
assert not debug_file.exists()
result = cli_runner(
'--no-input',
'--debug-file',
str(debug_file),
'tests/fake-repo-pre/',
)
assert result.exit_code == 0
assert debug_file.exists()
context_log = (
"DEBUG cookiecutter.main: context_file is "
"tests/fake-repo-pre/cookiecutter.json"
)
assert context_log in debug_file.readlines(cr=False)
assert context_log not in result.output
@pytest.mark.usefixtures('remove_fake_project_dir')
def test_debug_file_verbose(cli_runner, debug_file):
assert not debug_file.exists()
result = cli_runner(
'--verbose',
'--no-input',
'--debug-file',
str(debug_file),
'tests/fake-repo-pre/',
)
assert result.exit_code == 0
assert debug_file.exists()
context_log = (
"DEBUG cookiecutter.main: context_file is "
"tests/fake-repo-pre/cookiecutter.json"
)
assert context_log in debug_file.readlines(cr=False)
assert context_log in result.output