1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
test_dump |
5
|
|
|
--------- |
6
|
|
|
""" |
7
|
|
|
import json |
8
|
|
|
import shutil |
9
|
|
|
|
10
|
|
|
import os |
11
|
|
|
import pytest |
12
|
|
|
|
13
|
|
|
from cookiecutter import replay |
14
|
|
|
from tests.utils import dir_tests |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
@pytest.fixture |
18
|
|
|
def temp_replay_dir(tmpdir): |
19
|
|
|
dir_replay = tmpdir.join('test-replay') |
20
|
|
|
shutil.copytree(dir_tests('test-replay'), str(dir_replay)) |
21
|
|
|
return str(dir_replay) |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def test_type_error_if_no_template_name(context, temp_replay_dir): |
25
|
|
|
"""Test that replay.dump raises if the tempate_name is not a valid str.""" |
26
|
|
|
with pytest.raises(TypeError): |
27
|
|
|
replay.dump(temp_replay_dir, None, context) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def test_type_error_if_not_dict_context(temp_replay_dir): |
31
|
|
|
"""Test that replay.dump raises if the context is not of type dict.""" |
32
|
|
|
with pytest.raises(TypeError): |
33
|
|
|
replay.dump(temp_replay_dir, 'cookiedozer', 'not_a_dict') |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def test_value_error_if_key_missing_in_context(temp_replay_dir): |
37
|
|
|
"""Test that replay.dump raises if the context does not contain a key |
38
|
|
|
named 'cookiecutter'. |
39
|
|
|
""" |
40
|
|
|
with pytest.raises(ValueError): |
41
|
|
|
replay.dump(temp_replay_dir, 'cookiedozer', {'foo': 'bar'}) |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
@pytest.fixture |
45
|
|
|
def mock_ensure_failure(mocker): |
46
|
|
|
return mocker.patch( |
47
|
|
|
'cookiecutter.replay.make_sure_path_exists', |
48
|
|
|
return_value=False |
49
|
|
|
) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
@pytest.fixture |
53
|
|
|
def mock_ensure_success(mocker): |
54
|
|
|
return mocker.patch( |
55
|
|
|
'cookiecutter.replay.make_sure_path_exists', |
56
|
|
|
return_value=True |
57
|
|
|
) |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def test_ioerror_if_replay_dir_creation_fails( |
61
|
|
|
mock_ensure_failure, temp_replay_dir): |
62
|
|
|
"""Test that replay.dump raises when the replay_dir cannot be created.""" |
63
|
|
|
|
64
|
|
|
with pytest.raises(IOError): |
65
|
|
|
replay.dump( |
66
|
|
|
temp_replay_dir, |
67
|
|
|
'foo', {'cookiecutter': {'hello': 'world'}} |
68
|
|
|
) |
69
|
|
|
|
70
|
|
|
mock_ensure_failure.assert_called_once_with(temp_replay_dir) |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
def test_run_json_dump(mocker, mock_ensure_success, mock_user_config, context, |
74
|
|
|
temp_replay_dir): |
75
|
|
|
"""Test that replay.dump runs json.dump under the hood and that the context |
76
|
|
|
is correctly written to the expected file in the replay_dir. |
77
|
|
|
""" |
78
|
|
|
spy_get_replay_file = mocker.spy(replay, 'get_file_name') |
79
|
|
|
|
80
|
|
|
mock_json_dump = mocker.patch('json.dump', side_effect=json.dump) |
81
|
|
|
|
82
|
|
|
replay.dump(temp_replay_dir, 'cookiedozer', context) |
83
|
|
|
|
84
|
|
|
assert not mock_user_config.called |
85
|
|
|
mock_ensure_success.assert_called_once_with(temp_replay_dir) |
86
|
|
|
spy_get_replay_file.assert_called_once_with(temp_replay_dir, 'cookiedozer') |
87
|
|
|
|
88
|
|
|
assert mock_json_dump.call_count == 1 |
89
|
|
|
(dumped_context, outfile_handler), kwargs = mock_json_dump.call_args |
90
|
|
|
assert outfile_handler.name == os.path.join( |
91
|
|
|
temp_replay_dir, |
92
|
|
|
'cookiedozer.json' |
93
|
|
|
) |
94
|
|
|
assert dumped_context == context |
95
|
|
|
|