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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
|
"""Tests for `cookiecutter.prompt` module."""
import json
import platform
import sys
from collections import OrderedDict
from pathlib import Path
import click
import pytest
from cookiecutter import environment, exceptions, prompt
@pytest.fixture(autouse=True)
def patch_readline_on_win(monkeypatch):
"""Fixture. Overwrite windows end of line to linux standard."""
if 'windows' in platform.platform().lower():
monkeypatch.setattr('sys.stdin.readline', lambda: '\n')
class TestRenderVariable:
"""Class to unite simple and complex tests for render_variable function."""
@pytest.mark.parametrize(
'raw_var, rendered_var',
[
(1, '1'),
(True, True),
('foo', 'foo'),
('{{cookiecutter.project}}', 'foobar'),
(None, None),
],
)
def test_convert_to_str(self, mocker, raw_var, rendered_var):
"""Verify simple items correctly rendered to strings."""
env = environment.StrictEnvironment()
from_string = mocker.patch(
'cookiecutter.utils.StrictEnvironment.from_string', wraps=env.from_string
)
context = {'project': 'foobar'}
result = prompt.render_variable(env, raw_var, context)
assert result == rendered_var
# Make sure that non None non str variables are converted beforehand
if raw_var is not None and not isinstance(raw_var, bool):
if not isinstance(raw_var, str):
raw_var = str(raw_var)
from_string.assert_called_once_with(raw_var)
else:
assert not from_string.called
@pytest.mark.parametrize(
'raw_var, rendered_var',
[
({1: True, 'foo': False}, {'1': True, 'foo': False}),
(
{'{{cookiecutter.project}}': ['foo', 1], 'bar': False},
{'foobar': ['foo', '1'], 'bar': False},
),
(['foo', '{{cookiecutter.project}}', None], ['foo', 'foobar', None]),
],
)
def test_convert_to_str_complex_variables(self, raw_var, rendered_var):
"""Verify tree items correctly rendered."""
env = environment.StrictEnvironment()
context = {'project': 'foobar'}
result = prompt.render_variable(env, raw_var, context)
assert result == rendered_var
class TestPrompt:
"""Class to unite user prompt related tests."""
@pytest.mark.parametrize(
'context',
[
{'cookiecutter': {'full_name': 'Your Name'}},
{'cookiecutter': {'full_name': 'Řekni či napiš své jméno'}},
],
ids=['ASCII default prompt/input', 'Unicode default prompt/input'],
)
def test_prompt_for_config(self, monkeypatch, context):
"""Verify `prompt_for_config` call `read_user_variable` on text request."""
monkeypatch.setattr(
'cookiecutter.prompt.read_user_variable',
lambda var, default, prompts, prefix: default,
)
cookiecutter_dict = prompt.prompt_for_config(context)
assert cookiecutter_dict == context['cookiecutter']
@pytest.mark.parametrize(
'context',
[
{
'cookiecutter': {
'full_name': 'Your Name',
'check': ['yes', 'no'],
'nothing': 'ok',
'__prompts__': {
'full_name': 'Name please',
'check': 'Checking',
},
}
},
],
ids=['ASCII default prompt/input'],
)
def test_prompt_for_config_with_human_prompts(self, monkeypatch, context):
"""Verify call `read_user_variable` on request when human-readable prompts."""
monkeypatch.setattr(
'cookiecutter.prompt.read_user_variable',
lambda var, default, prompts, prefix: default,
)
monkeypatch.setattr(
'cookiecutter.prompt.read_user_yes_no',
lambda var, default, prompts, prefix: default,
)
monkeypatch.setattr(
'cookiecutter.prompt.read_user_choice',
lambda var, default, prompts, prefix: default,
)
cookiecutter_dict = prompt.prompt_for_config(context)
assert cookiecutter_dict == context['cookiecutter']
@pytest.mark.parametrize(
'context',
[
{
'cookiecutter': {
'full_name': 'Your Name',
'check': ['yes', 'no'],
'__prompts__': {
'check': 'Checking',
},
}
},
{
'cookiecutter': {
'full_name': 'Your Name',
'check': ['yes', 'no'],
'__prompts__': {
'full_name': 'Name please',
'check': {'__prompt__': 'Checking', 'yes': 'Yes', 'no': 'No'},
},
}
},
{
'cookiecutter': {
'full_name': 'Your Name',
'check': ['yes', 'no'],
'__prompts__': {
'full_name': 'Name please',
'check': {'no': 'No'},
},
}
},
],
)
def test_prompt_for_config_with_human_choices(self, monkeypatch, context):
"""Test prompts when human-readable labels for user choices."""
runner = click.testing.CliRunner()
with runner.isolation(input="\n\n\n"):
cookiecutter_dict = prompt.prompt_for_config(context)
assert dict(cookiecutter_dict) == {'full_name': 'Your Name', 'check': 'yes'}
def test_prompt_for_config_dict(self, monkeypatch):
"""Verify `prompt_for_config` call `read_user_variable` on dict request."""
monkeypatch.setattr(
'cookiecutter.prompt.read_user_dict',
lambda var, default, prompts, prefix: {"key": "value", "integer": 37},
)
context = {'cookiecutter': {'details': {}}}
cookiecutter_dict = prompt.prompt_for_config(context)
assert cookiecutter_dict == {'details': {'key': 'value', 'integer': 37}}
def test_should_render_dict(self):
"""Verify template inside dictionary variable rendered."""
context = {
'cookiecutter': {
'project_name': 'Slartibartfast',
'details': {
'{{cookiecutter.project_name}}': '{{cookiecutter.project_name}}'
},
}
}
cookiecutter_dict = prompt.prompt_for_config(context, no_input=True)
assert cookiecutter_dict == {
'project_name': 'Slartibartfast',
'details': {'Slartibartfast': 'Slartibartfast'},
}
def test_should_render_deep_dict(self):
"""Verify nested structures like dict in dict, rendered correctly."""
context = {
'cookiecutter': {
'project_name': "Slartibartfast",
'details': {
"key": "value",
"integer_key": 37,
"other_name": '{{cookiecutter.project_name}}',
"dict_key": {
"deep_key": "deep_value",
"deep_integer": 42,
"deep_other_name": '{{cookiecutter.project_name}}',
"deep_list": [
"deep value 1",
"{{cookiecutter.project_name}}",
"deep value 3",
],
},
"list_key": [
"value 1",
"{{cookiecutter.project_name}}",
"value 3",
],
},
}
}
cookiecutter_dict = prompt.prompt_for_config(context, no_input=True)
assert cookiecutter_dict == {
'project_name': "Slartibartfast",
'details': {
"key": "value",
"integer_key": "37",
"other_name": "Slartibartfast",
"dict_key": {
"deep_key": "deep_value",
"deep_integer": "42",
"deep_other_name": "Slartibartfast",
"deep_list": ["deep value 1", "Slartibartfast", "deep value 3"],
},
"list_key": ["value 1", "Slartibartfast", "value 3"],
},
}
def test_should_render_deep_dict_with_human_prompts(self):
"""Verify dict rendered correctly when human-readable prompts."""
context = {
'cookiecutter': {
'project_name': "Slartibartfast",
'details': {
"key": "value",
"integer_key": 37,
"other_name": '{{cookiecutter.project_name}}',
"dict_key": {
"deep_key": "deep_value",
},
},
'__prompts__': {'project_name': 'Project name'},
}
}
cookiecutter_dict = prompt.prompt_for_config(context, no_input=True)
assert cookiecutter_dict == {
'project_name': "Slartibartfast",
'details': {
"key": "value",
"integer_key": "37",
"other_name": "Slartibartfast",
"dict_key": {
"deep_key": "deep_value",
},
},
}
def test_internal_use_no_human_prompts(self):
"""Verify dict rendered correctly when human-readable prompts empty."""
context = {
'cookiecutter': {
'project_name': "Slartibartfast",
'__prompts__': {},
}
}
cookiecutter_dict = prompt.prompt_for_config(context, no_input=True)
assert cookiecutter_dict == {
'project_name': "Slartibartfast",
}
def test_prompt_for_templated_config(self, monkeypatch):
"""Verify Jinja2 templating works in unicode prompts."""
monkeypatch.setattr(
'cookiecutter.prompt.read_user_variable',
lambda var, default, prompts, prefix: default,
)
context = {
'cookiecutter': OrderedDict(
[
('project_name', 'A New Project'),
(
'pkg_name',
'{{ cookiecutter.project_name|lower|replace(" ", "") }}',
),
]
)
}
exp_cookiecutter_dict = {
'project_name': 'A New Project',
'pkg_name': 'anewproject',
}
cookiecutter_dict = prompt.prompt_for_config(context)
assert cookiecutter_dict == exp_cookiecutter_dict
def test_dont_prompt_for_private_context_var(self, monkeypatch):
"""Verify `read_user_variable` not called for private context variables."""
monkeypatch.setattr(
'cookiecutter.prompt.read_user_variable',
lambda var, default: pytest.fail(
'Should not try to read a response for private context var'
),
)
context = {'cookiecutter': {'_copy_without_render': ['*.html']}}
cookiecutter_dict = prompt.prompt_for_config(context)
assert cookiecutter_dict == {'_copy_without_render': ['*.html']}
def test_should_render_private_variables_with_two_underscores(self):
"""Test rendering of private variables with two underscores.
There are three cases:
1. Variables beginning with a single underscore are private and not rendered.
2. Variables beginning with a double underscore are private and are rendered.
3. Variables beginning with anything other than underscores are not private and
are rendered.
"""
context = {
'cookiecutter': OrderedDict(
[
('foo', 'Hello world'),
('bar', 123),
('rendered_foo', '{{ cookiecutter.foo|lower }}'),
('rendered_bar', 123),
('_hidden_foo', '{{ cookiecutter.foo|lower }}'),
('_hidden_bar', 123),
('__rendered_hidden_foo', '{{ cookiecutter.foo|lower }}'),
('__rendered_hidden_bar', 123),
]
)
}
cookiecutter_dict = prompt.prompt_for_config(context, no_input=True)
assert cookiecutter_dict == OrderedDict(
[
('foo', 'Hello world'),
('bar', '123'),
('rendered_foo', 'hello world'),
('rendered_bar', '123'),
('_hidden_foo', '{{ cookiecutter.foo|lower }}'),
('_hidden_bar', 123),
('__rendered_hidden_foo', 'hello world'),
('__rendered_hidden_bar', '123'),
]
)
def test_should_not_render_private_variables(self):
"""Verify private(underscored) variables not rendered by `prompt_for_config`.
Private variables designed to be raw, same as context input.
"""
context = {
'cookiecutter': {
'project_name': 'Skip render',
'_skip_jinja_template': '{{cookiecutter.project_name}}',
'_skip_float': 123.25,
'_skip_integer': 123,
'_skip_boolean': True,
'_skip_nested': True,
}
}
cookiecutter_dict = prompt.prompt_for_config(context, no_input=True)
assert cookiecutter_dict == context['cookiecutter']
DEFAULT_PREFIX = ' [dim][1/1][/] '
class TestReadUserChoice:
"""Class to unite choices prompt related tests."""
def test_should_invoke_read_user_choice(self, mocker):
"""Verify correct function called for select(list) variables."""
prompt_choice = mocker.patch(
'cookiecutter.prompt.prompt_choice_for_config',
wraps=prompt.prompt_choice_for_config,
)
read_user_choice = mocker.patch('cookiecutter.prompt.read_user_choice')
read_user_choice.return_value = 'all'
read_user_variable = mocker.patch('cookiecutter.prompt.read_user_variable')
choices = ['landscape', 'portrait', 'all']
context = {'cookiecutter': {'orientation': choices}}
cookiecutter_dict = prompt.prompt_for_config(context)
assert not read_user_variable.called
assert prompt_choice.called
read_user_choice.assert_called_once_with(
'orientation', choices, {}, DEFAULT_PREFIX
)
assert cookiecutter_dict == {'orientation': 'all'}
def test_should_invoke_read_user_variable(self, mocker):
"""Verify correct function called for string input variables."""
read_user_variable = mocker.patch('cookiecutter.prompt.read_user_variable')
read_user_variable.return_value = 'Audrey Roy'
prompt_choice = mocker.patch('cookiecutter.prompt.prompt_choice_for_config')
read_user_choice = mocker.patch('cookiecutter.prompt.read_user_choice')
context = {'cookiecutter': {'full_name': 'Your Name'}}
cookiecutter_dict = prompt.prompt_for_config(context)
assert not prompt_choice.called
assert not read_user_choice.called
read_user_variable.assert_called_once_with(
'full_name', 'Your Name', {}, DEFAULT_PREFIX
)
assert cookiecutter_dict == {'full_name': 'Audrey Roy'}
def test_should_render_choices(self, mocker):
"""Verify Jinja2 templating engine works inside choices variables."""
read_user_choice = mocker.patch('cookiecutter.prompt.read_user_choice')
read_user_choice.return_value = 'anewproject'
read_user_variable = mocker.patch('cookiecutter.prompt.read_user_variable')
read_user_variable.return_value = 'A New Project'
rendered_choices = ['foo', 'anewproject', 'bar']
context = {
'cookiecutter': OrderedDict(
[
('project_name', 'A New Project'),
(
'pkg_name',
[
'foo',
'{{ cookiecutter.project_name|lower|replace(" ", "") }}',
'bar',
],
),
]
)
}
expected = {
'project_name': 'A New Project',
'pkg_name': 'anewproject',
}
cookiecutter_dict = prompt.prompt_for_config(context)
read_user_variable.assert_called_once_with(
'project_name', 'A New Project', {}, ' [dim][1/2][/] '
)
read_user_choice.assert_called_once_with(
'pkg_name', rendered_choices, {}, ' [dim][2/2][/] '
)
assert cookiecutter_dict == expected
class TestPromptChoiceForConfig:
"""Class to unite choices prompt related tests with config test."""
@pytest.fixture
def choices(self):
"""Fixture. Just populate choices variable."""
return ['landscape', 'portrait', 'all']
@pytest.fixture
def context(self, choices):
"""Fixture. Just populate context variable."""
return {'cookiecutter': {'orientation': choices}}
def test_should_return_first_option_if_no_input(self, mocker, choices, context):
"""Verify prompt_choice_for_config return first list option on no_input=True."""
read_user_choice = mocker.patch('cookiecutter.prompt.read_user_choice')
expected_choice = choices[0]
actual_choice = prompt.prompt_choice_for_config(
cookiecutter_dict=context,
env=environment.StrictEnvironment(),
key='orientation',
options=choices,
no_input=True, # Suppress user input
)
assert not read_user_choice.called
assert expected_choice == actual_choice
def test_should_read_user_choice(self, mocker, choices, context):
"""Verify prompt_choice_for_config return user selection on no_input=False."""
read_user_choice = mocker.patch('cookiecutter.prompt.read_user_choice')
read_user_choice.return_value = 'all'
expected_choice = 'all'
actual_choice = prompt.prompt_choice_for_config(
cookiecutter_dict=context,
env=environment.StrictEnvironment(),
key='orientation',
options=choices,
no_input=False, # Ask the user for input
)
read_user_choice.assert_called_once_with('orientation', choices, None, '')
assert expected_choice == actual_choice
class TestReadUserYesNo(object):
"""Class to unite boolean prompt related tests."""
@pytest.mark.parametrize(
'run_as_docker',
(
True,
False,
),
)
def test_should_invoke_read_user_yes_no(self, mocker, run_as_docker):
"""Verify correct function called for boolean variables."""
read_user_yes_no = mocker.patch('cookiecutter.prompt.read_user_yes_no')
read_user_yes_no.return_value = run_as_docker
read_user_variable = mocker.patch('cookiecutter.prompt.read_user_variable')
context = {'cookiecutter': {'run_as_docker': run_as_docker}}
cookiecutter_dict = prompt.prompt_for_config(context)
assert not read_user_variable.called
read_user_yes_no.assert_called_once_with(
'run_as_docker', run_as_docker, {}, DEFAULT_PREFIX
)
assert cookiecutter_dict == {'run_as_docker': run_as_docker}
def test_boolean_parameter_no_input(self):
"""Verify boolean parameter sent to prompt for config with no input."""
context = {
'cookiecutter': {
'run_as_docker': True,
}
}
cookiecutter_dict = prompt.prompt_for_config(context, no_input=True)
assert cookiecutter_dict == context['cookiecutter']
@pytest.mark.parametrize(
'context',
(
{'cookiecutter': {'foo': '{{cookiecutter.nope}}'}},
{'cookiecutter': {'foo': ['123', '{{cookiecutter.nope}}', '456']}},
{'cookiecutter': {'foo': {'{{cookiecutter.nope}}': 'value'}}},
{'cookiecutter': {'foo': {'key': '{{cookiecutter.nope}}'}}},
),
ids=[
'Undefined variable in cookiecutter dict',
'Undefined variable in cookiecutter dict with choices',
'Undefined variable in cookiecutter dict with dict_key',
'Undefined variable in cookiecutter dict with key_value',
],
)
def test_undefined_variable(context):
"""Verify `prompt.prompt_for_config` raises correct error."""
with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
prompt.prompt_for_config(context, no_input=True)
error = err.value
assert error.message == "Unable to render variable 'foo'"
assert error.context == context
@pytest.mark.parametrize(
"template_dir,expected",
[
["fake-nested-templates", "fake-project"],
["fake-nested-templates-old-style", "fake-package"],
],
)
def test_cookiecutter_nested_templates(template_dir: str, expected: str):
"""Test nested_templates generation."""
from cookiecutter import prompt
main_dir = (Path("tests") / template_dir).resolve()
cookiecuter_context = json.loads((main_dir / "cookiecutter.json").read_text())
context = {"cookiecutter": cookiecuter_context}
output_dir = prompt.choose_nested_template(context, main_dir, no_input=True)
expected = (Path(main_dir) / expected).resolve()
assert output_dir == f"{expected}"
@pytest.mark.skipif(sys.platform.startswith('win'), reason="Linux / macos test")
@pytest.mark.parametrize(
"path",
[
"",
"/tmp",
"/foo",
],
)
def test_cookiecutter_nested_templates_invalid_paths(path: str):
"""Test nested_templates generation."""
from cookiecutter import prompt
main_dir = (Path("tests") / "fake-nested-templates").resolve()
cookiecuter_context = json.loads((main_dir / "cookiecutter.json").read_text())
cookiecuter_context["templates"]["fake-project"]["path"] = path
context = {"cookiecutter": cookiecuter_context}
with pytest.raises(ValueError) as exc:
prompt.choose_nested_template(context, main_dir, no_input=True)
assert "Illegal template path" in str(exc)
@pytest.mark.skipif(not sys.platform.startswith('win'), reason="Win only test")
@pytest.mark.parametrize(
"path",
[
"",
"C:/tmp",
"D:/tmp",
],
)
def test_cookiecutter_nested_templates_invalid_win_paths(path: str):
"""Test nested_templates generation."""
from cookiecutter import prompt
main_dir = (Path("tests") / "fake-nested-templates").resolve()
cookiecuter_context = json.loads((main_dir / "cookiecutter.json").read_text())
cookiecuter_context["templates"]["fake-project"]["path"] = path
context = {"cookiecutter": cookiecuter_context}
with pytest.raises(ValueError) as exc:
prompt.choose_nested_template(context, main_dir, no_input=True)
assert "Illegal template path" in str(exc)
def test_prompt_should_ask_and_rm_repo_dir(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user agrees to delete/reclone the \
repo, the repo should be deleted."""
mock_read_user = mocker.patch(
'cookiecutter.prompt.read_user_yes_no', return_value=True
)
repo_dir = Path(tmp_path, 'repo')
repo_dir.mkdir()
deleted = prompt.prompt_and_delete(str(repo_dir))
assert mock_read_user.called
assert not repo_dir.exists()
assert deleted
def test_prompt_should_ask_and_exit_on_user_no_answer(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user decline to delete/reclone the \
repo, cookiecutter should exit."""
mock_read_user = mocker.patch(
'cookiecutter.prompt.read_user_yes_no',
return_value=False,
)
mock_sys_exit = mocker.patch('sys.exit', return_value=True)
repo_dir = Path(tmp_path, 'repo')
repo_dir.mkdir()
deleted = prompt.prompt_and_delete(str(repo_dir))
assert mock_read_user.called
assert repo_dir.exists()
assert not deleted
assert mock_sys_exit.called
def test_prompt_should_ask_and_rm_repo_file(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user agrees to delete/reclone a \
repo file, the repo should be deleted."""
mock_read_user = mocker.patch(
'cookiecutter.prompt.read_user_yes_no', return_value=True, autospec=True
)
repo_file = tmp_path.joinpath('repo.zip')
repo_file.write_text('this is zipfile content')
deleted = prompt.prompt_and_delete(str(repo_file))
assert mock_read_user.called
assert not repo_file.exists()
assert deleted
def test_prompt_should_ask_and_keep_repo_on_no_reuse(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user wants to keep their old \
cloned template repo, it should not be deleted."""
mock_read_user = mocker.patch(
'cookiecutter.prompt.read_user_yes_no', return_value=False, autospec=True
)
repo_dir = Path(tmp_path, 'repo')
repo_dir.mkdir()
with pytest.raises(SystemExit):
prompt.prompt_and_delete(str(repo_dir))
assert mock_read_user.called
assert repo_dir.exists()
def test_prompt_should_ask_and_keep_repo_on_reuse(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user wants to keep their old \
cloned template repo, it should not be deleted."""
def answer(question, default):
return 'okay to delete' not in question
mock_read_user = mocker.patch(
'cookiecutter.prompt.read_user_yes_no', side_effect=answer, autospec=True
)
repo_dir = Path(tmp_path, 'repo')
repo_dir.mkdir()
deleted = prompt.prompt_and_delete(str(repo_dir))
assert mock_read_user.called
assert repo_dir.exists()
assert not deleted
def test_prompt_should_not_ask_if_no_input_and_rm_repo_dir(mocker, tmp_path):
"""Prompt should not ask if no input and rm dir.
In `prompt_and_delete()`, if `no_input` is True, the call to
`prompt.read_user_yes_no()` should be suppressed.
"""
mock_read_user = mocker.patch(
'cookiecutter.prompt.read_user_yes_no', return_value=True, autospec=True
)
repo_dir = Path(tmp_path, 'repo')
repo_dir.mkdir()
deleted = prompt.prompt_and_delete(str(repo_dir), no_input=True)
assert not mock_read_user.called
assert not repo_dir.exists()
assert deleted
def test_prompt_should_not_ask_if_no_input_and_rm_repo_file(mocker, tmp_path):
"""Prompt should not ask if no input and rm file.
In `prompt_and_delete()`, if `no_input` is True, the call to
`prompt.read_user_yes_no()` should be suppressed.
"""
mock_read_user = mocker.patch(
'cookiecutter.prompt.read_user_yes_no', return_value=True, autospec=True
)
repo_file = tmp_path.joinpath('repo.zip')
repo_file.write_text('this is zipfile content')
deleted = prompt.prompt_and_delete(str(repo_file), no_input=True)
assert not mock_read_user.called
assert not repo_file.exists()
assert deleted
|