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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
"""Tests for `generate_files` function and related errors raising.
Use the global clean_system fixture and run additional teardown code to remove
some special folders.
"""
from pathlib import Path
import pytest
from binaryornot.check import is_binary
from cookiecutter import exceptions, generate
def test_generate_files_nontemplated_exception(tmp_path):
"""
Verify `generate_files` raises when no directories to render exist.
Note: Check `tests/test-generate-files-nontemplated` location to understand.
"""
with pytest.raises(exceptions.NonTemplatedInputDirException):
generate.generate_files(
context={'cookiecutter': {'food': 'pizza'}},
repo_dir='tests/test-generate-files-nontemplated',
output_dir=tmp_path,
)
def test_generate_files(tmp_path):
"""Verify directory name correctly rendered with unicode containing context."""
generate.generate_files(
context={'cookiecutter': {'food': 'pizzä'}},
repo_dir='tests/test-generate-files',
output_dir=tmp_path,
)
simple_file = Path(tmp_path, 'inputpizzä/simple.txt')
assert simple_file.exists()
assert simple_file.is_file()
simple_text = Path(simple_file).read_text(encoding='utf-8')
assert simple_text == 'I eat pizzä\n'
def test_generate_files_with_linux_newline(tmp_path):
"""Verify new line not removed by templating engine after folder generation."""
generate.generate_files(
context={'cookiecutter': {'food': 'pizzä'}},
repo_dir='tests/test-generate-files',
output_dir=tmp_path,
)
newline_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt')
assert newline_file.is_file()
assert newline_file.exists()
with Path(newline_file).open(encoding='utf-8', newline='') as f:
simple_text = f.readline()
assert simple_text == 'newline is LF\n'
assert f.newlines == '\n'
def test_generate_files_with_jinja2_environment(tmp_path):
"""Extend StrictEnvironment with _jinja2_env_vars cookiecutter template option."""
generate.generate_files(
context={
'cookiecutter': {
'food': 'pizzä',
'_jinja2_env_vars': {'lstrip_blocks': True, 'trim_blocks': True},
}
},
repo_dir='tests/test-generate-files',
output_dir=tmp_path,
)
conditions_file = tmp_path.joinpath('inputpizzä/simple-with-conditions.txt')
assert conditions_file.is_file()
assert conditions_file.exists()
simple_text = conditions_file.read_text(encoding='utf-8')
assert simple_text == 'I eat pizzä\n'
def test_generate_files_with_trailing_newline_forced_to_linux_by_context(tmp_path):
"""Verify new line not removed by templating engine after folder generation."""
generate.generate_files(
context={'cookiecutter': {'food': 'pizzä', '_new_lines': '\r\n'}},
repo_dir='tests/test-generate-files',
output_dir=tmp_path,
)
# assert 'Overwritting endline character with %s' in caplog.messages
newline_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt')
assert newline_file.is_file()
assert newline_file.exists()
with Path(newline_file).open(encoding='utf-8', newline='') as f:
simple_text = f.readline()
assert simple_text == 'newline is LF\r\n'
assert f.newlines == '\r\n'
def test_generate_files_with_windows_newline(tmp_path):
"""Verify windows source line end not changed during files generation."""
generate.generate_files(
context={'cookiecutter': {'food': 'pizzä'}},
repo_dir='tests/test-generate-files',
output_dir=tmp_path,
)
newline_file = Path(tmp_path, 'inputpizzä/simple-with-newline-crlf.txt')
assert newline_file.is_file()
assert newline_file.exists()
with Path(newline_file).open(encoding='utf-8', newline='') as f:
simple_text = f.readline()
assert simple_text == 'newline is CRLF\r\n'
assert f.newlines == '\r\n'
def test_generate_files_with_windows_newline_forced_to_linux_by_context(tmp_path):
"""Verify windows line end changed to linux during files generation."""
generate.generate_files(
context={'cookiecutter': {'food': 'pizzä', '_new_lines': '\n'}},
repo_dir='tests/test-generate-files',
output_dir=tmp_path,
)
newline_file = Path(tmp_path, 'inputpizzä/simple-with-newline-crlf.txt')
assert newline_file.is_file()
assert newline_file.exists()
with Path(newline_file).open(encoding='utf-8', newline='') as f:
simple_text = f.readline()
assert simple_text == 'newline is CRLF\n'
assert f.newlines == '\n'
def test_generate_files_binaries(tmp_path):
"""Verify binary files created during directory generation."""
generate.generate_files(
context={'cookiecutter': {'binary_test': 'binary_files'}},
repo_dir='tests/test-generate-binaries',
output_dir=tmp_path,
)
dst_dir = Path(tmp_path, 'inputbinary_files')
assert is_binary(str(Path(dst_dir, 'logo.png')))
assert not is_binary(str(Path(dst_dir, 'readme.txt')))
assert is_binary(str(Path(dst_dir, 'some_font.otf')))
assert is_binary(str(Path(dst_dir, 'binary_files/logo.png')))
assert not is_binary(str(Path(dst_dir, 'binary_files/readme.txt')))
assert is_binary(str(Path(dst_dir, 'binary_files/some_font.otf')))
assert is_binary(str(Path(dst_dir, 'binary_files/binary_files/logo.png')))
def test_generate_files_absolute_path(tmp_path):
"""Verify usage of absolute path does not change files generation behaviour."""
generate.generate_files(
context={'cookiecutter': {'food': 'pizzä'}},
repo_dir=Path('tests/test-generate-files').absolute(),
output_dir=tmp_path,
)
assert Path(tmp_path, 'inputpizzä/simple.txt').is_file()
def test_generate_files_output_dir(tmp_path):
"""Verify `output_dir` option for `generate_files` changing location correctly."""
output_dir = Path(tmp_path, 'custom_output_dir')
output_dir.mkdir()
project_dir = generate.generate_files(
context={'cookiecutter': {'food': 'pizzä'}},
repo_dir=Path('tests/test-generate-files').absolute(),
output_dir=output_dir,
)
assert Path(output_dir, 'inputpizzä/simple.txt').exists()
assert Path(output_dir, 'inputpizzä/simple.txt').is_file()
assert Path(project_dir) == Path(tmp_path, 'custom_output_dir/inputpizzä')
def test_generate_files_permissions(tmp_path):
"""Verify generates files respect source files permissions.
simple.txt and script.sh should retain their respective 0o644 and 0o755
permissions.
"""
generate.generate_files(
context={'cookiecutter': {'permissions': 'permissions'}},
repo_dir='tests/test-generate-files-permissions',
output_dir=tmp_path,
)
assert Path(tmp_path, 'inputpermissions/simple.txt').is_file()
# Verify source simple.txt should still be 0o644
tests_simple_file = Path(
'tests',
'test-generate-files-permissions',
'input{{cookiecutter.permissions}}',
'simple.txt',
)
tests_simple_file_mode = tests_simple_file.stat().st_mode
input_simple_file = Path(tmp_path, 'inputpermissions', 'simple.txt')
input_simple_file_mode = input_simple_file.stat().st_mode
assert tests_simple_file_mode == input_simple_file_mode
assert Path(tmp_path, 'inputpermissions/script.sh').exists()
assert Path(tmp_path, 'inputpermissions/script.sh').is_file()
# Verify source script.sh should still be 0o755
tests_script_file = Path(
'tests',
'test-generate-files-permissions',
'input{{cookiecutter.permissions}}',
'script.sh',
)
tests_script_file_mode = tests_script_file.stat().st_mode
input_script_file = Path(tmp_path, 'inputpermissions', 'script.sh')
input_script_file_mode = input_script_file.stat().st_mode
assert tests_script_file_mode == input_script_file_mode
def test_generate_files_with_overwrite_if_exists_with_skip_if_file_exists(tmp_path):
"""Verify `skip_if_file_exist` has priority over `overwrite_if_exists`."""
simple_file = Path(tmp_path, 'inputpizzä/simple.txt')
simple_with_new_line_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt')
Path(tmp_path, 'inputpizzä').mkdir(parents=True)
with Path(simple_file).open('w') as f:
f.write('temp')
generate.generate_files(
context={'cookiecutter': {'food': 'pizzä'}},
repo_dir='tests/test-generate-files',
overwrite_if_exists=True,
skip_if_file_exists=True,
output_dir=tmp_path,
)
assert Path(simple_file).is_file()
assert Path(simple_file).exists()
assert Path(simple_with_new_line_file).is_file()
assert Path(simple_with_new_line_file).exists()
simple_text = Path(simple_file).read_text(encoding='utf-8')
assert simple_text == 'temp'
def test_generate_files_with_skip_if_file_exists(tmp_path):
"""Verify existed files not removed if error raised with `skip_if_file_exists`."""
simple_file = Path(tmp_path, 'inputpizzä/simple.txt')
simple_with_new_line_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt')
Path(tmp_path, 'inputpizzä').mkdir(parents=True)
Path(simple_file).write_text('temp')
with pytest.raises(exceptions.OutputDirExistsException):
generate.generate_files(
context={'cookiecutter': {'food': 'pizzä'}},
repo_dir='tests/test-generate-files',
skip_if_file_exists=True,
output_dir=tmp_path,
)
assert Path(simple_file).is_file()
assert not Path(simple_with_new_line_file).is_file()
assert not Path(simple_with_new_line_file).exists()
simple_text = Path(simple_file).read_text(encoding='utf-8')
assert simple_text == 'temp'
def test_generate_files_with_overwrite_if_exists(tmp_path):
"""Verify overwrite_if_exists overwrites old files."""
simple_file = Path(tmp_path, 'inputpizzä/simple.txt')
simple_with_new_line_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt')
Path(tmp_path, 'inputpizzä').mkdir(parents=True)
Path(simple_file).write_text('temp')
generate.generate_files(
context={'cookiecutter': {'food': 'pizzä'}},
repo_dir='tests/test-generate-files',
overwrite_if_exists=True,
output_dir=tmp_path,
)
assert Path(simple_file).is_file()
assert Path(simple_file).exists()
assert Path(simple_with_new_line_file).is_file()
assert Path(simple_with_new_line_file).exists()
simple_text = Path(simple_file).read_text(encoding='utf-8')
assert simple_text == 'I eat pizzä\n'
@pytest.fixture
def undefined_context():
"""Fixture. Populate context variable for future tests."""
return {
'cookiecutter': {'project_slug': 'testproject', 'github_username': 'hackebrot'}
}
def test_raise_undefined_variable_file_name(output_dir, undefined_context):
"""Verify correct error raised when file name cannot be rendered."""
with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
generate.generate_files(
repo_dir='tests/undefined-variable/file-name/',
output_dir=output_dir,
context=undefined_context,
)
error = err.value
assert "Unable to create file '{{cookiecutter.foobar}}'" == error.message
assert error.context == undefined_context
assert not Path(output_dir).joinpath('testproject').exists()
def test_raise_undefined_variable_file_name_existing_project(
output_dir, undefined_context
):
"""Verify correct error raised when file name cannot be rendered."""
testproj_path = Path(output_dir, 'testproject')
testproj_path.mkdir()
with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
generate.generate_files(
repo_dir='tests/undefined-variable/file-name/',
output_dir=output_dir,
context=undefined_context,
overwrite_if_exists=True,
)
error = err.value
assert "Unable to create file '{{cookiecutter.foobar}}'" == error.message
assert error.context == undefined_context
assert testproj_path.exists()
def test_raise_undefined_variable_file_content(output_dir, undefined_context):
"""Verify correct error raised when file content cannot be rendered."""
with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
generate.generate_files(
repo_dir='tests/undefined-variable/file-content/',
output_dir=output_dir,
context=undefined_context,
)
error = err.value
assert "Unable to create file 'README.rst'" == error.message
assert error.context == undefined_context
assert not Path(output_dir).joinpath('testproject').exists()
def test_raise_undefined_variable_dir_name(output_dir, undefined_context):
"""Verify correct error raised when directory name cannot be rendered."""
with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
generate.generate_files(
repo_dir='tests/undefined-variable/dir-name/',
output_dir=output_dir,
context=undefined_context,
)
error = err.value
directory = Path('testproject', '{{cookiecutter.foobar}}')
msg = f"Unable to create directory '{directory}'"
assert msg == error.message
assert error.context == undefined_context
assert not Path(output_dir).joinpath('testproject').exists()
def test_keep_project_dir_on_failure(output_dir, undefined_context):
"""Verify correct error raised when directory name cannot be rendered."""
with pytest.raises(exceptions.UndefinedVariableInTemplate):
generate.generate_files(
repo_dir='tests/undefined-variable/dir-name/',
output_dir=output_dir,
context=undefined_context,
keep_project_on_failure=True,
)
assert Path(output_dir).joinpath('testproject').exists()
def test_raise_undefined_variable_dir_name_existing_project(
output_dir, undefined_context
):
"""Verify correct error raised when directory name cannot be rendered."""
testproj_path = Path(output_dir, 'testproject')
testproj_path.mkdir()
with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
generate.generate_files(
repo_dir='tests/undefined-variable/dir-name/',
output_dir=output_dir,
context=undefined_context,
overwrite_if_exists=True,
)
error = err.value
directory = Path('testproject', '{{cookiecutter.foobar}}')
msg = f"Unable to create directory '{directory}'"
assert msg == error.message
assert error.context == undefined_context
assert testproj_path.exists()
def test_raise_undefined_variable_project_dir(tmp_path):
"""Verify correct error raised when directory name cannot be rendered."""
with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
generate.generate_files(
repo_dir='tests/undefined-variable/dir-name/',
output_dir=tmp_path,
context={},
)
error = err.value
msg = "Unable to create project directory '{{cookiecutter.project_slug}}'"
assert msg == error.message
assert error.context == {}
assert not Path(tmp_path, 'testproject').exists()
|