Skip to content

Commit 6892ee6

Browse files
authored
Merge pull request #25 from hackebrot/add-test-for-choice-variable
Add test for choice variable
2 parents 1365d14 + 3c5f346 commit 6892ee6

File tree

2 files changed

+73
-24
lines changed

2 files changed

+73
-24
lines changed

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import collections
4+
import json
5+
6+
import pytest
7+
18
pytest_plugins = 'pytester'
9+
10+
11+
@pytest.fixture(name='cookiecutter_template')
12+
def fixture_cookiecutter_template(tmpdir):
13+
template = tmpdir.ensure('cookiecutter-template', dir=True)
14+
15+
template_config = collections.OrderedDict([
16+
('repo_name', 'foobar'),
17+
('short_description', 'Test Project'),
18+
])
19+
template.join('cookiecutter.json').write(json.dumps(template_config))
20+
21+
template_readme = '\n'.join([
22+
'{{cookiecutter.repo_name}}',
23+
'{% for _ in cookiecutter.repo_name %}={% endfor %}',
24+
'{{cookiecutter.short_description}}',
25+
])
26+
27+
repo = template.ensure('{{cookiecutter.repo_name}}', dir=True)
28+
repo.join('README.rst').write(template_readme)
29+
30+
return template

tests/test_cookies.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# -*- coding: utf-8 -*-
22

33
import json
4-
import collections
5-
64
import pytest
75

86

@@ -30,28 +28,6 @@ def test_valid_fixture(cookies):
3028
assert result.ret == 0
3129

3230

33-
@pytest.fixture
34-
def cookiecutter_template(tmpdir):
35-
template = tmpdir.ensure('cookiecutter-template', dir=True)
36-
37-
template_config = collections.OrderedDict([
38-
('repo_name', 'foobar'),
39-
('short_description', 'Test Project'),
40-
])
41-
template.join('cookiecutter.json').write(json.dumps(template_config))
42-
43-
template_readme = '\n'.join([
44-
'{{cookiecutter.repo_name}}',
45-
'{% for _ in cookiecutter.repo_name %}={% endfor %}',
46-
'{{cookiecutter.short_description}}',
47-
])
48-
49-
repo = template.ensure('{{cookiecutter.repo_name}}', dir=True)
50-
repo.join('README.rst').write(template_readme)
51-
52-
return template
53-
54-
5531
def test_cookies_bake_with_template_kwarg(testdir, cookiecutter_template):
5632
"""bake accepts a template kwarg."""
5733
testdir.makepyfile("""
@@ -344,3 +320,47 @@ def test_bake_should_fail(cookies):
344320
result.stdout.fnmatch_lines([
345321
'*::test_bake_should_fail PASSED*',
346322
])
323+
324+
325+
@pytest.mark.parametrize('choice', ['mkdocs', 'sphinx', 'none'])
326+
def test_cookies_bake_choices(testdir, choice):
327+
"""Programmatically create a **Cookiecutter** template and make sure that
328+
cookies.bake() works with choice variables.
329+
"""
330+
template = testdir.tmpdir.ensure('cookiecutter-choices', dir=True)
331+
template_config = {
332+
'repo_name': 'docs',
333+
'docs_tool': ['mkdocs', 'sphinx', 'none'],
334+
}
335+
template.join('cookiecutter.json').write(json.dumps(template_config))
336+
337+
repo = template.ensure('{{cookiecutter.repo_name}}', dir=True)
338+
repo.join('README.rst').write("docs_tool: {{cookiecutter.docs_tool}}")
339+
340+
testdir.makepyfile("""
341+
# -*- coding: utf-8 -*-
342+
343+
def test_bake_project(cookies):
344+
result = cookies.bake(
345+
extra_context={'docs_tool': '%s'},
346+
template=r'%s',
347+
)
348+
349+
assert result.exit_code == 0
350+
assert result.exception is None
351+
assert result.project.basename == 'docs'
352+
assert result.project.isdir()
353+
354+
assert result.project.join('README.rst').read() == 'docs_tool: %s'
355+
356+
assert str(result) == '<Result {}>'.format(result.project)
357+
""" % (choice, template, choice))
358+
359+
# run pytest without the template cli arg
360+
result = testdir.runpytest('-v')
361+
362+
result.stdout.fnmatch_lines([
363+
'*::test_bake_project PASSED*',
364+
])
365+
366+
result = testdir.runpytest('-v', '--template={}'.format(template))

0 commit comments

Comments
 (0)