cookiecutter

Форк
0
/
test_main.py 
116 строк · 3.4 Кб
1
"""Collection of tests around cookiecutter's replay feature."""
2

3
from cookiecutter.main import cookiecutter
4

5

6
def test_original_cookiecutter_options_preserved_in__cookiecutter(
7
    monkeypatch,
8
    mocker,
9
    user_config_file,
10
) -> None:
11
    """Preserve original context options.
12

13
    Tests you can access the original context options via
14
    `context['_cookiecutter']`.
15
    """
16
    monkeypatch.chdir('tests/fake-repo-tmpl-_cookiecutter')
17
    mock_generate_files = mocker.patch('cookiecutter.main.generate_files')
18
    cookiecutter(
19
        '.',
20
        no_input=True,
21
        replay=False,
22
        config_file=user_config_file,
23
    )
24
    assert mock_generate_files.call_args[1]['context']['_cookiecutter'][
25
        'test_list'
26
    ] == [1, 2, 3, 4]
27
    assert mock_generate_files.call_args[1]['context']['_cookiecutter'][
28
        'test_dict'
29
    ] == {"foo": "bar"}
30

31

32
def test_replay_dump_template_name(
33
    monkeypatch, mocker, user_config_data, user_config_file
34
) -> None:
35
    """Check that replay_dump is called with a valid template_name.
36

37
    Template name must not be a relative path.
38

39
    Otherwise files such as ``..json`` are created, which are not just cryptic
40
    but also later mistaken for replay files of other templates if invoked with
41
    '.' and '--replay'.
42

43
    Change the current working directory temporarily to 'tests/fake-repo-tmpl'
44
    for this test and call cookiecutter with '.' for the target template.
45
    """
46
    monkeypatch.chdir('tests/fake-repo-tmpl')
47

48
    mock_replay_dump = mocker.patch('cookiecutter.main.dump')
49
    mocker.patch('cookiecutter.main.generate_files')
50

51
    cookiecutter(
52
        '.',
53
        no_input=True,
54
        replay=False,
55
        config_file=user_config_file,
56
    )
57

58
    mock_replay_dump.assert_called_once_with(
59
        user_config_data['replay_dir'],
60
        'fake-repo-tmpl',
61
        mocker.ANY,
62
    )
63

64

65
def test_replay_load_template_name(
66
    monkeypatch, mocker, user_config_data, user_config_file
67
) -> None:
68
    """Check that replay_load is called correctly.
69

70
    Calls require valid template_name that is not a relative path.
71

72
    Change the current working directory temporarily to 'tests/fake-repo-tmpl'
73
    for this test and call cookiecutter with '.' for the target template.
74
    """
75
    monkeypatch.chdir('tests/fake-repo-tmpl')
76

77
    mock_replay_load = mocker.patch('cookiecutter.main.load')
78
    mocker.patch('cookiecutter.main.generate_context').return_value = {
79
        'cookiecutter': {}
80
    }
81
    mocker.patch('cookiecutter.main.generate_files')
82
    mocker.patch('cookiecutter.main.dump')
83

84
    cookiecutter(
85
        '.',
86
        replay=True,
87
        config_file=user_config_file,
88
    )
89

90
    mock_replay_load.assert_called_once_with(
91
        user_config_data['replay_dir'],
92
        'fake-repo-tmpl',
93
    )
94

95

96
def test_custom_replay_file(monkeypatch, mocker, user_config_file) -> None:
97
    """Check that reply.load is called with the custom replay_file."""
98
    monkeypatch.chdir('tests/fake-repo-tmpl')
99

100
    mock_replay_load = mocker.patch('cookiecutter.main.load')
101
    mocker.patch('cookiecutter.main.generate_context').return_value = {
102
        'cookiecutter': {}
103
    }
104
    mocker.patch('cookiecutter.main.generate_files')
105
    mocker.patch('cookiecutter.main.dump')
106

107
    cookiecutter(
108
        '.',
109
        replay='./custom-replay-file',
110
        config_file=user_config_file,
111
    )
112

113
    mock_replay_load.assert_called_once_with(
114
        '.',
115
        'custom-replay-file',
116
    )
117

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.