1
|
|
|
"""Tests for cookiecutter's output directory customization feature.""" |
2
|
|
|
import pytest |
3
|
|
|
|
4
|
|
|
from cookiecutter import main |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
@pytest.fixture |
8
|
|
|
def context(): |
9
|
|
|
"""Fixture to return a valid context as known from a cookiecutter.json.""" |
10
|
|
|
return { |
11
|
|
|
'cookiecutter': { |
12
|
|
|
'email': '[email protected]', |
13
|
|
|
'full_name': 'Raphael Pierzina', |
14
|
|
|
'github_username': 'hackebrot', |
15
|
|
|
'version': '0.1.0', |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
@pytest.fixture |
21
|
|
|
def template(tmp_path): |
22
|
|
|
"""Fixture to prepare test template directory.""" |
23
|
|
|
template_dir = tmp_path.joinpath("template") |
24
|
|
|
template_dir.mkdir() |
25
|
|
|
template_dir.joinpath('cookiecutter.json').touch() |
26
|
|
|
return str(template_dir) |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
@pytest.fixture(autouse=True) |
30
|
|
|
def mock_gen_context(mocker, context): |
31
|
|
|
"""Fixture. Automatically mock cookiecutter's function with expected output.""" |
32
|
|
|
mocker.patch('cookiecutter.main.generate_context', return_value=context) |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
@pytest.fixture(autouse=True) |
36
|
|
|
def mock_prompt(mocker): |
37
|
|
|
"""Fixture. Automatically mock cookiecutter's function with expected output.""" |
38
|
|
|
mocker.patch('cookiecutter.main.prompt_for_config') |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
@pytest.fixture(autouse=True) |
42
|
|
|
def mock_replay(mocker): |
43
|
|
|
"""Fixture. Automatically mock cookiecutter's function with expected output.""" |
44
|
|
|
mocker.patch('cookiecutter.main.dump') |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def test_api_invocation(mocker, template, output_dir, context): |
48
|
|
|
"""Verify output dir location is correctly passed.""" |
49
|
|
|
mock_gen_files = mocker.patch('cookiecutter.main.generate_files') |
50
|
|
|
|
51
|
|
|
main.cookiecutter(template, output_dir=output_dir) |
52
|
|
|
|
53
|
|
|
mock_gen_files.assert_called_once_with( |
54
|
|
|
repo_dir=template, |
55
|
|
|
context=context, |
56
|
|
|
overwrite_if_exists=False, |
57
|
|
|
skip_if_file_exists=False, |
58
|
|
|
output_dir=output_dir, |
59
|
|
|
accept_hooks=True, |
60
|
|
|
keep_project_on_failure=False, |
61
|
|
|
) |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def test_default_output_dir(mocker, template, context): |
65
|
|
|
"""Verify default output dir is current working folder.""" |
66
|
|
|
mock_gen_files = mocker.patch('cookiecutter.main.generate_files') |
67
|
|
|
|
68
|
|
|
main.cookiecutter(template) |
69
|
|
|
|
70
|
|
|
mock_gen_files.assert_called_once_with( |
71
|
|
|
repo_dir=template, |
72
|
|
|
context=context, |
73
|
|
|
overwrite_if_exists=False, |
74
|
|
|
skip_if_file_exists=False, |
75
|
|
|
output_dir='.', |
76
|
|
|
accept_hooks=True, |
77
|
|
|
keep_project_on_failure=False, |
78
|
|
|
) |
79
|
|
|
|