cookiecutter
116 строк · 3.4 Кб
1"""Collection of tests around cookiecutter's replay feature."""
2
3from cookiecutter.main import cookiecutter4
5
6def test_original_cookiecutter_options_preserved_in__cookiecutter(7monkeypatch,8mocker,9user_config_file,10) -> None:11"""Preserve original context options.12
13Tests you can access the original context options via
14`context['_cookiecutter']`.
15"""
16monkeypatch.chdir('tests/fake-repo-tmpl-_cookiecutter')17mock_generate_files = mocker.patch('cookiecutter.main.generate_files')18cookiecutter(19'.',20no_input=True,21replay=False,22config_file=user_config_file,23)24assert mock_generate_files.call_args[1]['context']['_cookiecutter'][25'test_list'26] == [1, 2, 3, 4]27assert mock_generate_files.call_args[1]['context']['_cookiecutter'][28'test_dict'29] == {"foo": "bar"}30
31
32def test_replay_dump_template_name(33monkeypatch, mocker, user_config_data, user_config_file34) -> None:35"""Check that replay_dump is called with a valid template_name.36
37Template name must not be a relative path.
38
39Otherwise files such as ``..json`` are created, which are not just cryptic
40but also later mistaken for replay files of other templates if invoked with
41'.' and '--replay'.
42
43Change the current working directory temporarily to 'tests/fake-repo-tmpl'
44for this test and call cookiecutter with '.' for the target template.
45"""
46monkeypatch.chdir('tests/fake-repo-tmpl')47
48mock_replay_dump = mocker.patch('cookiecutter.main.dump')49mocker.patch('cookiecutter.main.generate_files')50
51cookiecutter(52'.',53no_input=True,54replay=False,55config_file=user_config_file,56)57
58mock_replay_dump.assert_called_once_with(59user_config_data['replay_dir'],60'fake-repo-tmpl',61mocker.ANY,62)63
64
65def test_replay_load_template_name(66monkeypatch, mocker, user_config_data, user_config_file67) -> None:68"""Check that replay_load is called correctly.69
70Calls require valid template_name that is not a relative path.
71
72Change the current working directory temporarily to 'tests/fake-repo-tmpl'
73for this test and call cookiecutter with '.' for the target template.
74"""
75monkeypatch.chdir('tests/fake-repo-tmpl')76
77mock_replay_load = mocker.patch('cookiecutter.main.load')78mocker.patch('cookiecutter.main.generate_context').return_value = {79'cookiecutter': {}80}81mocker.patch('cookiecutter.main.generate_files')82mocker.patch('cookiecutter.main.dump')83
84cookiecutter(85'.',86replay=True,87config_file=user_config_file,88)89
90mock_replay_load.assert_called_once_with(91user_config_data['replay_dir'],92'fake-repo-tmpl',93)94
95
96def test_custom_replay_file(monkeypatch, mocker, user_config_file) -> None:97"""Check that reply.load is called with the custom replay_file."""98monkeypatch.chdir('tests/fake-repo-tmpl')99
100mock_replay_load = mocker.patch('cookiecutter.main.load')101mocker.patch('cookiecutter.main.generate_context').return_value = {102'cookiecutter': {}103}104mocker.patch('cookiecutter.main.generate_files')105mocker.patch('cookiecutter.main.dump')106
107cookiecutter(108'.',109replay='./custom-replay-file',110config_file=user_config_file,111)112
113mock_replay_load.assert_called_once_with(114'.',115'custom-replay-file',116)117