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
|
"""Verify generate context behaviour and context overwrite priorities."""
import os
import re
from collections import OrderedDict
import pytest
from cookiecutter import generate
from cookiecutter.exceptions import ContextDecodingException
def context_data():
"""Generate pytest parametrization variables for test.
Return ('input_params, expected_context') tuples.
"""
context = (
{'context_file': 'tests/test-generate-context/test.json'},
{'test': {'1': 2, 'some_key': 'some_val'}},
)
context_with_default = (
{
'context_file': 'tests/test-generate-context/test.json',
'default_context': {'1': 3},
},
{'test': {'1': 3, 'some_key': 'some_val'}},
)
context_with_extra = (
{
'context_file': 'tests/test-generate-context/test.json',
'extra_context': {'1': 4},
},
{'test': {'1': 4, 'some_key': 'some_val'}},
)
context_with_default_and_extra = (
{
'context_file': 'tests/test-generate-context/test.json',
'default_context': {'1': 3},
'extra_context': {'1': 5},
},
{'test': {'1': 5, 'some_key': 'some_val'}},
)
yield context
yield context_with_default
yield context_with_extra
yield context_with_default_and_extra
@pytest.mark.usefixtures('clean_system')
@pytest.mark.parametrize('input_params, expected_context', context_data())
def test_generate_context(input_params, expected_context):
"""Verify input contexts combinations result in expected content on output."""
assert generate.generate_context(**input_params) == expected_context
@pytest.mark.usefixtures('clean_system')
def test_generate_context_with_json_decoding_error():
"""Verify malformed JSON file generates expected error output."""
with pytest.raises(ContextDecodingException) as excinfo:
generate.generate_context('tests/test-generate-context/invalid-syntax.json')
# original message from json module should be included
pattern = 'Expecting \'{0,1}:\'{0,1} delimiter: line 1 column (19|20) \\(char 19\\)'
assert re.search(pattern, str(excinfo.value))
# File name should be included too...for testing purposes, just test the
# last part of the file. If we wanted to test the absolute path, we'd have
# to do some additional work in the test which doesn't seem that needed at
# this point.
path = os.path.sep.join(['tests', 'test-generate-context', 'invalid-syntax.json'])
assert path in str(excinfo.value)
def test_default_context_replacement_in_generate_context():
"""Verify default content settings are correctly replaced by template settings.
Make sure that the default for list variables of `orientation` is based on
the user config (`choices_template.json`) and not changed to a single value
from `default_context`.
"""
expected_context = {
'choices_template': OrderedDict(
[
('full_name', 'Raphael Pierzina'),
('github_username', 'hackebrot'),
('project_name', 'Kivy Project'),
('repo_name', '{{cookiecutter.project_name|lower}}'),
('orientation', ['landscape', 'all', 'portrait']),
]
)
}
generated_context = generate.generate_context(
context_file='tests/test-generate-context/choices_template.json',
default_context={
'not_in_template': 'foobar',
'project_name': 'Kivy Project',
'orientation': 'landscape',
},
extra_context={
'also_not_in_template': 'foobar2',
'github_username': 'hackebrot',
},
)
assert generated_context == expected_context
def test_generate_context_decodes_non_ascii_chars():
"""Verify `generate_context` correctly decodes non-ascii chars."""
expected_context = {
'non_ascii': OrderedDict(
[
('full_name', 'éèà'),
]
)
}
generated_context = generate.generate_context(
context_file='tests/test-generate-context/non_ascii.json'
)
assert generated_context == expected_context
@pytest.fixture
def template_context():
"""Fixture. Populates template content for future tests."""
return OrderedDict(
[
('full_name', 'Raphael Pierzina'),
('github_username', 'hackebrot'),
('project_name', 'Kivy Project'),
('repo_name', '{{cookiecutter.project_name|lower}}'),
('orientation', ['all', 'landscape', 'portrait']),
('deployment_regions', ['eu', 'us', 'ap']),
(
'deployments',
{
'preprod': ['eu', 'us', 'ap'],
'prod': ['eu', 'us', 'ap'],
},
),
]
)
def test_apply_overwrites_does_include_unused_variables(template_context):
"""Verify `apply_overwrites_to_context` skips variables that are not in context."""
generate.apply_overwrites_to_context(
context=template_context, overwrite_context={'not in template': 'foobar'}
)
assert 'not in template' not in template_context
def test_apply_overwrites_sets_non_list_value(template_context):
"""Verify `apply_overwrites_to_context` work with string variables."""
generate.apply_overwrites_to_context(
context=template_context, overwrite_context={'repo_name': 'foobar'}
)
assert template_context['repo_name'] == 'foobar'
def test_apply_overwrites_does_not_modify_choices_for_invalid_overwrite():
"""Verify variables overwrite for list if variable not in list ignored."""
expected_context = {
'choices_template': OrderedDict(
[
('full_name', 'Raphael Pierzina'),
('github_username', 'hackebrot'),
('project_name', 'Kivy Project'),
('repo_name', '{{cookiecutter.project_name|lower}}'),
('orientation', ['all', 'landscape', 'portrait']),
]
)
}
with pytest.warns(UserWarning, match="Invalid default received"):
generated_context = generate.generate_context(
context_file='tests/test-generate-context/choices_template.json',
default_context={
'not_in_template': 'foobar',
'project_name': 'Kivy Project',
'orientation': 'foobar',
},
extra_context={
'also_not_in_template': 'foobar2',
'github_username': 'hackebrot',
},
)
assert generated_context == expected_context
def test_apply_overwrites_invalid_overwrite(template_context):
"""Verify variables overwrite for list if variable not in list not ignored."""
with pytest.raises(ValueError):
generate.apply_overwrites_to_context(
context=template_context, overwrite_context={'orientation': 'foobar'}
)
def test_apply_overwrites_sets_multichoice_values(template_context):
"""Verify variable overwrite for list given multiple valid values."""
generate.apply_overwrites_to_context(
context=template_context,
overwrite_context={'deployment_regions': ['eu']},
)
assert template_context['deployment_regions'] == ['eu']
def test_apply_overwrites_invalid_multichoice_values(template_context):
"""Verify variable overwrite for list given invalid list entries not ignored."""
with pytest.raises(ValueError):
generate.apply_overwrites_to_context(
context=template_context,
overwrite_context={'deployment_regions': ['na']},
)
def test_apply_overwrites_error_additional_values(template_context):
"""Verify variable overwrite for list given additional entries not ignored."""
with pytest.raises(ValueError):
generate.apply_overwrites_to_context(
context=template_context,
overwrite_context={'deployment_regions': ['eu', 'na']},
)
def test_apply_overwrites_in_dictionaries(template_context):
"""Verify variable overwrite for lists nested in dictionary variables."""
generate.apply_overwrites_to_context(
context=template_context,
overwrite_context={'deployments': {'preprod': ['eu'], 'prod': ['ap']}},
)
assert template_context['deployments']['preprod'] == ['eu']
assert template_context['deployments']['prod'] == ['ap']
def test_apply_overwrites_sets_default_for_choice_variable(template_context):
"""Verify overwritten list member became a default value."""
generate.apply_overwrites_to_context(
context=template_context, overwrite_context={'orientation': 'landscape'}
)
assert template_context['orientation'] == ['landscape', 'all', 'portrait']
def test_apply_overwrites_in_nested_dict():
"""Verify nested dict in default content settings are correctly replaced."""
expected_context = {
'nested_dict': OrderedDict(
[
('full_name', 'Raphael Pierzina'),
('github_username', 'hackebrot'),
(
'project',
OrderedDict(
[
('name', 'My Kivy Project'),
('description', 'My Kivy Project'),
('repo_name', '{{cookiecutter.project_name|lower}}'),
('orientation', ["all", "landscape", "portrait"]),
]
),
),
]
)
}
generated_context = generate.generate_context(
context_file='tests/test-generate-context/nested_dict.json',
default_context={
'not_in_template': 'foobar',
'project': {
'description': 'My Kivy Project',
},
},
extra_context={
'also_not_in_template': 'foobar2',
'github_username': 'hackebrot',
'project': {
'name': 'My Kivy Project',
},
},
)
assert generated_context == expected_context
def test_apply_overwrite_context_as_in_nested_dict_with_additional_values():
"""Verify nested dict in default content settings are correctly added.
The `apply_overwrites_to_context` function should add the extra values to the dict.
"""
expected = OrderedDict({"key1": "value1", "key2": "value2"})
context = OrderedDict({"key1": "value1"})
overwrite_context = OrderedDict({"key2": "value2"})
generate.apply_overwrites_to_context(
context,
overwrite_context,
in_dictionary_variable=True,
)
assert context == expected
def test_apply_overwrites_in_nested_dict_additional_values():
"""Verify nested dict in default content settings are correctly added."""
expected_context = {
'nested_dict_additional': OrderedDict(
[
('mainkey1', 'mainvalue1'),
(
'mainkey2',
OrderedDict(
[
('subkey1', 'subvalue1'),
(
'subkey2',
OrderedDict(
[
('subsubkey1', 'subsubvalue1'),
('subsubkey2', 'subsubvalue2_default'),
('subsubkey3', 'subsubvalue3_extra'),
]
),
),
('subkey4', 'subvalue4_default'),
('subkey5', 'subvalue5_extra'),
]
),
),
]
)
}
generated_context = generate.generate_context(
context_file='tests/test-generate-context/nested_dict_additional.json',
default_context={
'not_in_template': 'foobar',
'mainkey2': {
'subkey2': {
'subsubkey2': 'subsubvalue2_default',
},
'subkey4': 'subvalue4_default',
},
},
extra_context={
'also_not_in_template': 'foobar2',
'mainkey2': {
'subkey2': {
'subsubkey3': 'subsubvalue3_extra',
},
'subkey5': 'subvalue5_extra',
},
},
)
assert generated_context == expected_context
|