Completed
Push — master ( 56245b...a9d00e )
by
unknown
38s
created

test_cookiecutter_template_cleanup()   B

Complexity

Conditions 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 28
rs 8.5806
1
# -*- coding: utf-8 -*-
2
3
"""
4
test_cookiecutter_local_no_input
5
--------------------------------
6
7
Tests formerly known from a unittest residing in test_main.py named
8
TestCookiecutterLocalNoInput.test_cookiecutter
9
TestCookiecutterLocalNoInput.test_cookiecutter_no_slash
10
TestCookiecutterLocalNoInput.test_cookiecutter_no_input_extra_context
11
TestCookiecutterLocalNoInput.test_cookiecutter_templated_context
12
"""
13
14
import os
15
import textwrap
16
import pytest
17
18
from cookiecutter import main, utils
19
20
21
@pytest.fixture(scope='function')
22
def remove_additional_dirs(request):
23
    """
24
    Remove special directories which are created during the tests.
25
    """
26
    def fin_remove_additional_dirs():
27
        if os.path.isdir('fake-project'):
28
            utils.rmtree('fake-project')
29
        if os.path.isdir('fake-project-extra'):
30
            utils.rmtree('fake-project-extra')
31
        if os.path.isdir('fake-project-templated'):
32
            utils.rmtree('fake-project-templated')
33
        if os.path.isdir('fake-project-dict'):
34
            utils.rmtree('fake-project-dict')
35
        if os.path.isdir('fake-tmp'):
36
            utils.rmtree('fake-tmp')
37
    request.addfinalizer(fin_remove_additional_dirs)
38
39
40
@pytest.fixture(params=['tests/fake-repo-pre/', 'tests/fake-repo-pre'])
41
def bake(request):
42
    """
43
    Run cookiecutter with the given input_dir path.
44
    """
45
    main.cookiecutter(request.param, no_input=True)
46
47
48
@pytest.mark.usefixtures('clean_system', 'remove_additional_dirs', 'bake')
49
def test_cookiecutter():
50
    assert os.path.isdir('tests/fake-repo-pre/{{cookiecutter.repo_name}}')
51
    assert not os.path.isdir('tests/fake-repo-pre/fake-project')
52
    assert os.path.isdir('fake-project')
53
    assert os.path.isfile('fake-project/README.rst')
54
    assert not os.path.exists('fake-project/json/')
55
56
57
@pytest.mark.usefixtures('clean_system', 'remove_additional_dirs')
58
def test_cookiecutter_no_input_extra_context():
59
    """
60
    `Call cookiecutter()` with `no_input=True` and `extra_context
61
    """
62
    main.cookiecutter(
63
        'tests/fake-repo-pre',
64
        no_input=True,
65
        extra_context={'repo_name': 'fake-project-extra'}
66
    )
67
    assert os.path.isdir('fake-project-extra')
68
69
70
@pytest.mark.usefixtures('clean_system', 'remove_additional_dirs')
71
def test_cookiecutter_templated_context():
72
    """
73
    `Call cookiecutter()` with `no_input=True` and templates in the
74
    cookiecutter.json file
75
    """
76
    main.cookiecutter(
77
        'tests/fake-repo-tmpl',
78
        no_input=True
79
    )
80
    assert os.path.isdir('fake-project-templated')
81
82
83
@pytest.mark.usefixtures('clean_system', 'remove_additional_dirs')
84
def test_cookiecutter_no_input_return_project_dir():
85
    """Call `cookiecutter()` with `no_input=True`."""
86
    project_dir = main.cookiecutter('tests/fake-repo-pre', no_input=True)
87
    assert project_dir == os.path.abspath('fake-project')
88
89
90
@pytest.mark.usefixtures('clean_system', 'remove_additional_dirs')
91
def test_cookiecutter_dict_values_in_context():
92
    project_dir = main.cookiecutter('tests/fake-repo-dict', no_input=True)
93
    assert project_dir == os.path.abspath('fake-project-dict')
94
95
    with open(os.path.join(project_dir, 'README.md')) as fh:
96
        contents = fh.read()
97
98
    assert contents == textwrap.dedent("""
99
        # README
100
101
102
        <dl>
103
          <dt>Format name:</dt>
104
          <dd>Bitmap</dd>
105
106
          <dt>Extension:</dt>
107
          <dd>bmp</dd>
108
109
          <dt>Applications:</dt>
110
          <dd>
111
              <ul>
112
              <li>Paint</li>
113
              <li>GIMP</li>
114
              </ul>
115
          </dd>
116
        </dl>
117
118
        <dl>
119
          <dt>Format name:</dt>
120
          <dd>Portable Network Graphic</dd>
121
122
          <dt>Extension:</dt>
123
          <dd>png</dd>
124
125
          <dt>Applications:</dt>
126
          <dd>
127
              <ul>
128
              <li>GIMP</li>
129
              </ul>
130
          </dd>
131
        </dl>
132
133
    """).lstrip()
134
135
136
@pytest.mark.usefixtures('clean_system', 'remove_additional_dirs')
137
def test_cookiecutter_template_cleanup(mocker):
138
    """
139
    `Call cookiecutter()` with `no_input=True` and templates in the
140
    cookiecutter.json file
141
    """
142
    mocker.patch(
143
        'tempfile.mkdtemp',
144
        return_value='fake-tmp',
145
        autospec=True
146
    )
147
148
    mocker.patch(
149
        'cookiecutter.utils.prompt_and_delete',
150
        return_value=True,
151
        autospec=True
152
    )
153
154
    main.cookiecutter(
155
        'tests/files/fake-repo-tmpl.zip',
156
        no_input=True
157
    )
158
    assert os.path.isdir('fake-project-templated')
159
160
    # The tmp directory will still exist, but the
161
    # extracted template directory *in* the temp directory will not.
162
    assert os.path.exists('fake-tmp')
163
    assert not os.path.exists('fake-tmp/fake-repo-tmpl')
164