|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
""" |
|
4
|
|
|
test_generate_context |
|
5
|
|
|
--------------------- |
|
6
|
|
|
|
|
7
|
|
|
Tests formerly known from a unittest residing in test_generate.py named |
|
8
|
|
|
TestGenerateContext.test_generate_context |
|
9
|
|
|
TestGenerateContext.test_generate_context_with_default |
|
10
|
|
|
TestGenerateContext.test_generate_context_with_extra |
|
11
|
|
|
TestGenerateContext.test_generate_context_with_default_and_extra |
|
12
|
|
|
""" |
|
13
|
|
|
|
|
14
|
|
|
from __future__ import unicode_literals |
|
15
|
|
|
import pytest |
|
16
|
|
|
import os |
|
17
|
|
|
import re |
|
18
|
|
|
from collections import OrderedDict |
|
19
|
|
|
|
|
20
|
|
|
from cookiecutter import generate |
|
21
|
|
|
from cookiecutter.exceptions import ContextDecodingException |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def context_data(): |
|
25
|
|
|
context = ( |
|
26
|
|
|
{ |
|
27
|
|
|
'repo_dir': os.getcwd(), |
|
28
|
|
|
'context_file': 'tests/test-generate-context/test.json' |
|
29
|
|
|
}, |
|
30
|
|
|
{ |
|
31
|
|
|
'_repo_dir': os.getcwd(), |
|
32
|
|
|
'test': {'1': 2, 'some_key': 'some_val'} |
|
33
|
|
|
} |
|
34
|
|
|
) |
|
35
|
|
|
|
|
36
|
|
|
context_with_default = ( |
|
37
|
|
|
{ |
|
38
|
|
|
'repo_dir': os.getcwd(), |
|
39
|
|
|
'context_file': 'tests/test-generate-context/test.json', |
|
40
|
|
|
'default_context': {'1': 3} |
|
41
|
|
|
}, |
|
42
|
|
|
{ |
|
43
|
|
|
'_repo_dir': os.getcwd(), |
|
44
|
|
|
'test': {'1': 3, 'some_key': 'some_val'} |
|
45
|
|
|
} |
|
46
|
|
|
) |
|
47
|
|
|
|
|
48
|
|
|
context_with_extra = ( |
|
49
|
|
|
{ |
|
50
|
|
|
'repo_dir': os.getcwd(), |
|
51
|
|
|
'context_file': 'tests/test-generate-context/test.json', |
|
52
|
|
|
'extra_context': {'1': 4}, |
|
53
|
|
|
}, |
|
54
|
|
|
{ |
|
55
|
|
|
'_repo_dir': os.getcwd(), |
|
56
|
|
|
'test': {'1': 4, 'some_key': 'some_val'} |
|
57
|
|
|
} |
|
58
|
|
|
) |
|
59
|
|
|
|
|
60
|
|
|
context_with_default_and_extra = ( |
|
61
|
|
|
{ |
|
62
|
|
|
'repo_dir': os.getcwd(), |
|
63
|
|
|
'context_file': 'tests/test-generate-context/test.json', |
|
64
|
|
|
'default_context': {'1': 3}, |
|
65
|
|
|
'extra_context': {'1': 5}, |
|
66
|
|
|
}, |
|
67
|
|
|
{ |
|
68
|
|
|
'_repo_dir': os.getcwd(), |
|
69
|
|
|
'test': {'1': 5, 'some_key': 'some_val'} |
|
70
|
|
|
} |
|
71
|
|
|
) |
|
72
|
|
|
|
|
73
|
|
|
yield context |
|
74
|
|
|
yield context_with_default |
|
75
|
|
|
yield context_with_extra |
|
76
|
|
|
yield context_with_default_and_extra |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
@pytest.mark.usefixtures('clean_system') |
|
80
|
|
|
@pytest.mark.parametrize('input_params, expected_context', context_data()) |
|
81
|
|
|
def test_generate_context(input_params, expected_context): |
|
82
|
|
|
""" |
|
83
|
|
|
Test the generated context for several input parameters against the |
|
84
|
|
|
according expected context. |
|
85
|
|
|
""" |
|
86
|
|
|
assert generate.generate_context(**input_params) == expected_context |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
@pytest.mark.usefixtures('clean_system') |
|
90
|
|
|
def test_generate_context_with_json_decoding_error(): |
|
91
|
|
|
with pytest.raises(ContextDecodingException) as excinfo: |
|
92
|
|
|
generate.generate_context( |
|
93
|
|
|
os.getcwd(), |
|
94
|
|
|
'tests/test-generate-context/invalid-syntax.json' |
|
95
|
|
|
) |
|
96
|
|
|
# original message from json module should be included |
|
97
|
|
|
pattern = ( |
|
98
|
|
|
'Expecting \'{0,1}:\'{0,1} delimiter: ' |
|
99
|
|
|
'line 1 column (19|20) \(char 19\)' |
|
100
|
|
|
) |
|
101
|
|
|
assert re.search(pattern, str(excinfo.value)) |
|
102
|
|
|
# File name should be included too...for testing purposes, just test the |
|
103
|
|
|
# last part of the file. If we wanted to test the absolute path, we'd have |
|
104
|
|
|
# to do some additional work in the test which doesn't seem that needed at |
|
105
|
|
|
# this point. |
|
106
|
|
|
path = os.path.sep.join( |
|
107
|
|
|
['tests', 'test-generate-context', 'invalid-syntax.json'] |
|
108
|
|
|
) |
|
109
|
|
|
assert path in str(excinfo.value) |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
@pytest.fixture |
|
113
|
|
|
def default_context(): |
|
114
|
|
|
return { |
|
115
|
|
|
'not_in_template': 'foobar', |
|
116
|
|
|
'project_name': 'Kivy Project', |
|
117
|
|
|
'orientation': 'landscape' |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
@pytest.fixture |
|
122
|
|
|
def extra_context(): |
|
123
|
|
|
return { |
|
124
|
|
|
'also_not_in_template': 'foobar2', |
|
125
|
|
|
'github_username': 'hackebrot', |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
@pytest.fixture |
|
130
|
|
|
def context_file(): |
|
131
|
|
|
return 'tests/test-generate-context/choices_template.json' |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
def test_choices(context_file, default_context, extra_context): |
|
135
|
|
|
"""Make sure that the default for list variables is based on the user |
|
136
|
|
|
config and the list as such is not changed to a single value. |
|
137
|
|
|
""" |
|
138
|
|
|
repo_dir = os.getcwd() |
|
139
|
|
|
expected_context = { |
|
140
|
|
|
'_repo_dir': repo_dir, |
|
141
|
|
|
'choices_template': OrderedDict([ |
|
142
|
|
|
('full_name', 'Raphael Pierzina'), |
|
143
|
|
|
('github_username', 'hackebrot'), |
|
144
|
|
|
('project_name', 'Kivy Project'), |
|
145
|
|
|
('repo_name', '{{cookiecutter.project_name|lower}}'), |
|
146
|
|
|
('orientation', ['landscape', 'all', 'portrait']), |
|
147
|
|
|
]) |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
generated_context = generate.generate_context( |
|
151
|
|
|
repo_dir, context_file, default_context, extra_context |
|
152
|
|
|
) |
|
153
|
|
|
|
|
154
|
|
|
assert generated_context == expected_context |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
@pytest.fixture |
|
158
|
|
|
def template_context(): |
|
159
|
|
|
return OrderedDict([ |
|
160
|
|
|
('full_name', 'Raphael Pierzina'), |
|
161
|
|
|
('github_username', 'hackebrot'), |
|
162
|
|
|
('project_name', 'Kivy Project'), |
|
163
|
|
|
('repo_name', '{{cookiecutter.project_name|lower}}'), |
|
164
|
|
|
('orientation', ['all', 'landscape', 'portrait']), |
|
165
|
|
|
]) |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
def test_apply_overwrites_does_include_unused_variables(template_context): |
|
169
|
|
|
generate.apply_overwrites_to_context( |
|
170
|
|
|
template_context, |
|
171
|
|
|
{'not in template': 'foobar'} |
|
172
|
|
|
) |
|
173
|
|
|
|
|
174
|
|
|
assert 'not in template' not in template_context |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
def test_apply_overwrites_sets_non_list_value(template_context): |
|
178
|
|
|
generate.apply_overwrites_to_context( |
|
179
|
|
|
template_context, |
|
180
|
|
|
{'repo_name': 'foobar'} |
|
181
|
|
|
) |
|
182
|
|
|
|
|
183
|
|
|
assert template_context['repo_name'] == 'foobar' |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
def test_apply_overwrites_does_not_modify_choices_for_invalid_overwrite( |
|
187
|
|
|
template_context): |
|
188
|
|
|
generate.apply_overwrites_to_context( |
|
189
|
|
|
template_context, |
|
190
|
|
|
{'orientation': 'foobar'} |
|
191
|
|
|
) |
|
192
|
|
|
|
|
193
|
|
|
assert template_context['orientation'] == ['all', 'landscape', 'portrait'] |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
def test_apply_overwrites_sets_default_for_choice_variable(template_context): |
|
197
|
|
|
generate.apply_overwrites_to_context( |
|
198
|
|
|
template_context, |
|
199
|
|
|
{'orientation': 'landscape'} |
|
200
|
|
|
) |
|
201
|
|
|
|
|
202
|
|
|
assert template_context['orientation'] == ['landscape', 'all', 'portrait'] |
|
203
|
|
|
|