|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 |
|
3 | 3 | import json
|
| 4 | +import collections |
4 | 5 |
|
5 | 6 | import pytest
|
6 | 7 |
|
@@ -33,10 +34,10 @@ def test_valid_fixture(cookies):
|
33 | 34 | def cookiecutter_template(tmpdir):
|
34 | 35 | template = tmpdir.ensure('cookiecutter-template', dir=True)
|
35 | 36 |
|
36 |
| - template_config = { |
37 |
| - 'repo_name': 'foobar', |
38 |
| - 'short_description': 'Test Project', |
39 |
| - } |
| 37 | + template_config = collections.OrderedDict([ |
| 38 | + ('repo_name', 'foobar'), |
| 39 | + ('short_description', 'Test Project'), |
| 40 | + ]) |
40 | 41 | template.join('cookiecutter.json').write(json.dumps(template_config))
|
41 | 42 |
|
42 | 43 | template_readme = '\n'.join([
|
@@ -138,6 +139,85 @@ def test_bake_project(cookies):
|
138 | 139 | ])
|
139 | 140 |
|
140 | 141 |
|
| 142 | +def test_cookies_bake_result_context(testdir, cookiecutter_template): |
| 143 | + """Programmatically create a **Cookiecutter** template and use `bake` to |
| 144 | + create a project from it. |
| 145 | +
|
| 146 | + Check that the result holds the rendered context. |
| 147 | + """ |
| 148 | + |
| 149 | + testdir.makepyfile(""" |
| 150 | + # -*- coding: utf-8 -*- |
| 151 | +
|
| 152 | + import collections |
| 153 | +
|
| 154 | + def test_bake_project(cookies): |
| 155 | + result = cookies.bake(extra_context=collections.OrderedDict([ |
| 156 | + ('repo_name', 'cookies'), |
| 157 | + ('short_description', '{{cookiecutter.repo_name}} is awesome'), |
| 158 | + ])) |
| 159 | +
|
| 160 | + assert result.exit_code == 0 |
| 161 | + assert result.exception is None |
| 162 | + assert result.project.basename == 'cookies' |
| 163 | + assert result.project.isdir() |
| 164 | +
|
| 165 | + assert result.context == { |
| 166 | + 'repo_name': 'cookies', |
| 167 | + 'short_description': 'cookies is awesome', |
| 168 | + } |
| 169 | +
|
| 170 | + assert str(result) == '<Result {}>'.format(result.project) |
| 171 | + """) |
| 172 | + |
| 173 | + result = testdir.runpytest( |
| 174 | + '-v', |
| 175 | + '--template={}'.format(cookiecutter_template) |
| 176 | + ) |
| 177 | + |
| 178 | + result.stdout.fnmatch_lines([ |
| 179 | + '*::test_bake_project PASSED*', |
| 180 | + ]) |
| 181 | + |
| 182 | + |
| 183 | +def test_cookies_bake_result_context_exception(testdir, cookiecutter_template): |
| 184 | + """Programmatically create a **Cookiecutter** template and use `bake` to |
| 185 | + create a project from it. |
| 186 | +
|
| 187 | + Check that exceptions resulting from rendering the context are stored on |
| 188 | + result and that the rendered context is not set. |
| 189 | + """ |
| 190 | + |
| 191 | + testdir.makepyfile(""" |
| 192 | + # -*- coding: utf-8 -*- |
| 193 | +
|
| 194 | + import collections |
| 195 | +
|
| 196 | + def test_bake_project(cookies): |
| 197 | + result = cookies.bake(extra_context=collections.OrderedDict([ |
| 198 | + ('repo_name', 'cookies'), |
| 199 | + ('short_description', '{{cookiecutter.nope}}'), |
| 200 | + ])) |
| 201 | +
|
| 202 | + assert result.exit_code == -1 |
| 203 | + assert result.exception is not None |
| 204 | + assert result.project is None |
| 205 | +
|
| 206 | + assert result.context is None |
| 207 | +
|
| 208 | + assert str(result) == '<Result {!r}>'.format(result.exception) |
| 209 | + """) |
| 210 | + |
| 211 | + result = testdir.runpytest( |
| 212 | + '-v', |
| 213 | + '--template={}'.format(cookiecutter_template) |
| 214 | + ) |
| 215 | + |
| 216 | + result.stdout.fnmatch_lines([ |
| 217 | + '*::test_bake_project PASSED*', |
| 218 | + ]) |
| 219 | + |
| 220 | + |
141 | 221 | def test_cookies_bake_should_create_new_output_directories(
|
142 | 222 | testdir, cookiecutter_template
|
143 | 223 | ):
|
|
0 commit comments