Passed
Push — master ( f85bad...8a07ca )
by
unknown
01:04
created

test_generate_files_with_windows_newline_forced_to_linux_by_context()   A

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 12
dl 17
loc 17
rs 9.8
c 0
b 0
f 0
cc 2
nop 1
1
"""Tests for `generate_files` function and related errors raising.
2
3
Use the global clean_system fixture and run additional teardown code to remove
4
some special folders.
5
"""
6
import io
7
import os
8
from pathlib import Path
9
10
import pytest
11
from binaryornot.check import is_binary
12
13
from cookiecutter import exceptions, generate
14
15
16
@pytest.mark.parametrize('invalid_dirname', ['', '{foo}', '{{foo', 'bar}}'])
17
def test_ensure_dir_is_templated_raises(invalid_dirname):
18
    """Verify `ensure_dir_is_templated` raises on wrong directories names input."""
19
    with pytest.raises(exceptions.NonTemplatedInputDirException):
20
        generate.ensure_dir_is_templated(invalid_dirname)
21
22
23
def test_generate_files_nontemplated_exception(tmp_path):
24
    """
25
    Verify `generate_files` raises when no directories to render exist.
26
27
    Note: Check `tests/test-generate-files-nontemplated` location to understand.
28
    """
29
    with pytest.raises(exceptions.NonTemplatedInputDirException):
30
        generate.generate_files(
31
            context={'cookiecutter': {'food': 'pizza'}},
32
            repo_dir='tests/test-generate-files-nontemplated',
33
            output_dir=tmp_path,
34
        )
35
36
37
def test_generate_files(tmp_path):
38
    """Verify directory name correctly rendered with unicode containing context."""
39
    generate.generate_files(
40
        context={'cookiecutter': {'food': 'pizzä'}},
41
        repo_dir='tests/test-generate-files',
42
        output_dir=tmp_path,
43
    )
44
45
    simple_file = Path(tmp_path, 'inputpizzä/simple.txt')
46
    assert simple_file.exists()
47
    assert simple_file.is_file()
48
49
    simple_text = open(simple_file, 'rt', encoding='utf-8').read()
50
    assert simple_text == 'I eat pizzä'
51
52
53 View Code Duplication
def test_generate_files_with_linux_newline(tmp_path):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
54
    """Verify new line not removed by templating engine after folder generation."""
55
    generate.generate_files(
56
        context={'cookiecutter': {'food': 'pizzä'}},
57
        repo_dir='tests/test-generate-files',
58
        output_dir=tmp_path,
59
    )
60
61
    newline_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt')
62
    assert newline_file.is_file()
63
    assert newline_file.exists()
64
65
    with io.open(newline_file, 'r', encoding='utf-8', newline='') as f:
66
        simple_text = f.readline()
67
    assert simple_text == 'newline is LF\n'
68
    assert f.newlines == '\n'
69
70
71 View Code Duplication
def test_generate_files_with_trailing_newline_forced_to_linux_by_context(tmp_path):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
72
    """Verify new line not removed by templating engine after folder generation."""
73
    generate.generate_files(
74
        context={'cookiecutter': {'food': 'pizzä', '_new_lines': '\r\n'}},
75
        repo_dir='tests/test-generate-files',
76
        output_dir=tmp_path,
77
    )
78
79
    # assert 'Overwritting endline character with %s' in caplog.messages
80
    newline_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt')
81
    assert newline_file.is_file()
82
    assert newline_file.exists()
83
84
    with io.open(newline_file, 'r', encoding='utf-8', newline='') as f:
85
        simple_text = f.readline()
86
    assert simple_text == 'newline is LF\r\n'
87
    assert f.newlines == '\r\n'
88
89
90 View Code Duplication
def test_generate_files_with_windows_newline(tmp_path):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
91
    """Verify windows source line end not changed during files generation."""
92
    generate.generate_files(
93
        context={'cookiecutter': {'food': 'pizzä'}},
94
        repo_dir='tests/test-generate-files',
95
        output_dir=tmp_path,
96
    )
97
98
    newline_file = Path(tmp_path, 'inputpizzä/simple-with-newline-crlf.txt')
99
    assert newline_file.is_file()
100
    assert newline_file.exists()
101
102
    with io.open(newline_file, 'r', encoding='utf-8', newline='') as f:
103
        simple_text = f.readline()
104
    assert simple_text == 'newline is CRLF\r\n'
105
    assert f.newlines == '\r\n'
106
107
108 View Code Duplication
def test_generate_files_with_windows_newline_forced_to_linux_by_context(tmp_path):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
109
    """Verify windows line end changed to linux during files generation."""
110
    generate.generate_files(
111
        context={'cookiecutter': {'food': 'pizzä', '_new_lines': '\n'}},
112
        repo_dir='tests/test-generate-files',
113
        output_dir=tmp_path,
114
    )
115
116
    newline_file = Path(tmp_path, 'inputpizzä/simple-with-newline-crlf.txt')
117
    assert newline_file.is_file()
118
    assert newline_file.exists()
119
120
    with io.open(newline_file, 'r', encoding='utf-8', newline='') as f:
121
        simple_text = f.readline()
122
123
    assert simple_text == 'newline is CRLF\n'
124
    assert f.newlines == '\n'
125
126
127
def test_generate_files_binaries(tmp_path):
128
    """Verify binary files created during directory generation."""
129
    generate.generate_files(
130
        context={'cookiecutter': {'binary_test': 'binary_files'}},
131
        repo_dir='tests/test-generate-binaries',
132
        output_dir=tmp_path,
133
    )
134
135
    dst_dir = Path(tmp_path, 'inputbinary_files')
136
137
    assert is_binary(str(Path(dst_dir, 'logo.png')))
138
    assert is_binary(str(Path(dst_dir, '.DS_Store')))
139
    assert not is_binary(str(Path(dst_dir, 'readme.txt')))
140
    assert is_binary(str(Path(dst_dir, 'some_font.otf')))
141
    assert is_binary(str(Path(dst_dir, 'binary_files/logo.png')))
142
    assert is_binary(str(Path(dst_dir, 'binary_files/.DS_Store')))
143
    assert not is_binary(str(Path(dst_dir, 'binary_files/readme.txt')))
144
    assert is_binary(str(Path(dst_dir, 'binary_files/some_font.otf')))
145
    assert is_binary(str(Path(dst_dir, 'binary_files/binary_files/logo.png')))
146
147
148
def test_generate_files_absolute_path(tmp_path):
149
    """Verify usage of absolute path does not change files generation behaviour."""
150
    generate.generate_files(
151
        context={'cookiecutter': {'food': 'pizzä'}},
152
        repo_dir=Path('tests/test-generate-files').absolute(),
153
        output_dir=tmp_path,
154
    )
155
    assert Path(tmp_path, 'inputpizzä/simple.txt').is_file()
156
157
158
def test_generate_files_output_dir(tmp_path):
159
    """Verify `output_dir` option for `generate_files` changing location correctly."""
160
    output_dir = Path(tmp_path, 'custom_output_dir')
161
    output_dir.mkdir()
162
163
    project_dir = generate.generate_files(
164
        context={'cookiecutter': {'food': 'pizzä'}},
165
        repo_dir=Path('tests/test-generate-files').absolute(),
166
        output_dir=output_dir,
167
    )
168
169
    assert Path(output_dir, 'inputpizzä/simple.txt').exists()
170
    assert Path(output_dir, 'inputpizzä/simple.txt').is_file()
171
    assert Path(project_dir) == Path(tmp_path, 'custom_output_dir/inputpizzä')
172
173
174
def test_generate_files_permissions(tmp_path):
175
    """Verify generates files respect source files permissions.
176
177
    simple.txt and script.sh should retain their respective 0o644 and 0o755
178
    permissions.
179
    """
180
    generate.generate_files(
181
        context={'cookiecutter': {'permissions': 'permissions'}},
182
        repo_dir='tests/test-generate-files-permissions',
183
        output_dir=tmp_path,
184
    )
185
186
    assert Path(tmp_path, 'inputpermissions/simple.txt').exists()
187
    assert Path(tmp_path, 'inputpermissions/simple.txt').is_file()
188
189
    # Verify source simple.txt should still be 0o644
190
    tests_simple_file = Path(
191
        'tests',
192
        'test-generate-files-permissions',
193
        'input{{cookiecutter.permissions}}',
194
        'simple.txt',
195
    )
196
    tests_simple_file_mode = tests_simple_file.stat().st_mode
197
198
    input_simple_file = Path(tmp_path, 'inputpermissions', 'simple.txt')
199
    input_simple_file_mode = input_simple_file.stat().st_mode
200
    assert tests_simple_file_mode == input_simple_file_mode
201
202
    assert Path(tmp_path, 'inputpermissions/script.sh').exists()
203
    assert Path(tmp_path, 'inputpermissions/script.sh').is_file()
204
205
    # Verify source script.sh should still be 0o755
206
    tests_script_file = Path(
207
        'tests',
208
        'test-generate-files-permissions',
209
        'input{{cookiecutter.permissions}}',
210
        'script.sh',
211
    )
212
    tests_script_file_mode = tests_script_file.stat().st_mode
213
214
    input_script_file = Path(tmp_path, 'inputpermissions', 'script.sh')
215
    input_script_file_mode = input_script_file.stat().st_mode
216
    assert tests_script_file_mode == input_script_file_mode
217
218
219 View Code Duplication
def test_generate_files_with_overwrite_if_exists_with_skip_if_file_exists(tmp_path):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
220
    """Verify `skip_if_file_exist` has priority over `overwrite_if_exists`."""
221
    simple_file = Path(tmp_path, 'inputpizzä/simple.txt')
222
    simple_with_new_line_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt')
223
224
    Path(tmp_path, 'inputpizzä').mkdir(parents=True)
225
    with open(simple_file, 'w') as f:
226
        f.write('temp')
227
228
    generate.generate_files(
229
        context={'cookiecutter': {'food': 'pizzä'}},
230
        repo_dir='tests/test-generate-files',
231
        overwrite_if_exists=True,
232
        skip_if_file_exists=True,
233
        output_dir=tmp_path,
234
    )
235
236
    assert Path(simple_file).is_file()
237
    assert Path(simple_file).exists()
238
    assert Path(simple_with_new_line_file).is_file()
239
    assert Path(simple_with_new_line_file).exists()
240
241
    simple_text = io.open(simple_file, 'rt', encoding='utf-8').read()
242
    assert simple_text == 'temp'
243
244
245
def test_generate_files_with_skip_if_file_exists(tmp_path):
246
    """Verify existed files not removed if error raised with `skip_if_file_exists`."""
247
    simple_file = Path(tmp_path, 'inputpizzä/simple.txt')
248
    simple_with_new_line_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt')
249
250
    Path(tmp_path, 'inputpizzä').mkdir(parents=True)
251
    with open(simple_file, 'w') as f:
252
        f.write('temp')
253
254
    with pytest.raises(exceptions.OutputDirExistsException):
255
        generate.generate_files(
256
            context={'cookiecutter': {'food': 'pizzä'}},
257
            repo_dir='tests/test-generate-files',
258
            skip_if_file_exists=True,
259
            output_dir=tmp_path,
260
        )
261
262
    assert Path(simple_file).is_file()
263
    assert Path(simple_file).exists()
264
    assert not Path(simple_with_new_line_file).is_file()
265
    assert not Path(simple_with_new_line_file).exists()
266
267
    simple_text = io.open(simple_file, 'rt', encoding='utf-8').read()
268
    assert simple_text == 'temp'
269
270
271 View Code Duplication
def test_generate_files_with_overwrite_if_exists(tmp_path):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
272
    """Verify overwrite_if_exists overwrites old files."""
273
    simple_file = Path(tmp_path, 'inputpizzä/simple.txt')
274
    simple_with_new_line_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt')
275
276
    Path(tmp_path, 'inputpizzä').mkdir(parents=True)
277
    with open(simple_file, 'w') as f:
278
        f.write('temp')
279
280
    generate.generate_files(
281
        context={'cookiecutter': {'food': 'pizzä'}},
282
        repo_dir='tests/test-generate-files',
283
        overwrite_if_exists=True,
284
        output_dir=tmp_path,
285
    )
286
287
    assert Path(simple_file).is_file()
288
    assert Path(simple_file).exists()
289
    assert Path(simple_with_new_line_file).is_file()
290
    assert Path(simple_with_new_line_file).exists()
291
292
    simple_text = io.open(simple_file, 'rt', encoding='utf-8').read()
293
    assert simple_text == 'I eat pizzä'
294
295
296
@pytest.fixture
297
def undefined_context():
298
    """Fixture. Populate context variable for future tests."""
299
    return {
300
        'cookiecutter': {'project_slug': 'testproject', 'github_username': 'hackebrot'}
301
    }
302
303
304 View Code Duplication
def test_raise_undefined_variable_file_name(tmpdir, undefined_context):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
305
    """Verify correct error raised when file name cannot be rendered."""
306
    output_dir = tmpdir.mkdir('output')
307
308
    with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
309
        generate.generate_files(
310
            repo_dir='tests/undefined-variable/file-name/',
311
            output_dir=str(output_dir),
312
            context=undefined_context,
313
        )
314
    error = err.value
315
    assert "Unable to create file '{{cookiecutter.foobar}}'" == error.message
316
    assert error.context == undefined_context
317
318
    assert not output_dir.join('testproject').exists()
319
320
321 View Code Duplication
def test_raise_undefined_variable_file_name_existing_project(tmpdir, undefined_context):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
322
    """Verify correct error raised when file name cannot be rendered."""
323
    output_dir = tmpdir.mkdir('output')
324
325
    output_dir.join('testproject').mkdir()
326
327
    with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
328
        generate.generate_files(
329
            repo_dir='tests/undefined-variable/file-name/',
330
            output_dir=str(output_dir),
331
            context=undefined_context,
332
            overwrite_if_exists=True,
333
        )
334
    error = err.value
335
    assert "Unable to create file '{{cookiecutter.foobar}}'" == error.message
336
    assert error.context == undefined_context
337
338
    assert output_dir.join('testproject').exists()
339
340
341 View Code Duplication
def test_raise_undefined_variable_file_content(tmpdir, undefined_context):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
342
    """Verify correct error raised when file content cannot be rendered."""
343
    output_dir = tmpdir.mkdir('output')
344
345
    with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
346
        generate.generate_files(
347
            repo_dir='tests/undefined-variable/file-content/',
348
            output_dir=str(output_dir),
349
            context=undefined_context,
350
        )
351
    error = err.value
352
    assert "Unable to create file 'README.rst'" == error.message
353
    assert error.context == undefined_context
354
355
    assert not output_dir.join('testproject').exists()
356
357
358 View Code Duplication
def test_raise_undefined_variable_dir_name(tmpdir, undefined_context):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
359
    """Verify correct error raised when directory name cannot be rendered."""
360
    output_dir = tmpdir.mkdir('output')
361
362
    with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
363
        generate.generate_files(
364
            repo_dir='tests/undefined-variable/dir-name/',
365
            output_dir=str(output_dir),
366
            context=undefined_context,
367
        )
368
    error = err.value
369
370
    directory = os.path.join('testproject', '{{cookiecutter.foobar}}')
371
    msg = "Unable to create directory '{}'".format(directory)
372
    assert msg == error.message
373
374
    assert error.context == undefined_context
375
376
    assert not output_dir.join('testproject').exists()
377
378
379 View Code Duplication
def test_raise_undefined_variable_dir_name_existing_project(tmpdir, undefined_context):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
380
    """Verify correct error raised when directory name cannot be rendered."""
381
    output_dir = tmpdir.mkdir('output')
382
383
    output_dir.join('testproject').mkdir()
384
385
    with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
386
        generate.generate_files(
387
            repo_dir='tests/undefined-variable/dir-name/',
388
            output_dir=str(output_dir),
389
            context=undefined_context,
390
            overwrite_if_exists=True,
391
        )
392
    error = err.value
393
394
    directory = os.path.join('testproject', '{{cookiecutter.foobar}}')
395
    msg = "Unable to create directory '{}'".format(directory)
396
    assert msg == error.message
397
398
    assert error.context == undefined_context
399
400
    assert output_dir.join('testproject').exists()
401
402
403
def test_raise_undefined_variable_project_dir(tmp_path):
404
    """Verify correct error raised when directory name cannot be rendered."""
405
    with pytest.raises(exceptions.UndefinedVariableInTemplate) as err:
406
        generate.generate_files(
407
            repo_dir='tests/undefined-variable/dir-name/',
408
            output_dir=tmp_path,
409
            context={},
410
        )
411
    error = err.value
412
    msg = "Unable to create project directory '{{cookiecutter.project_slug}}'"
413
    assert msg == error.message
414
    assert error.context == {}
415
416
    assert not Path(tmp_path, 'testproject').exists()
417