diff --git a/tests/conftest.py b/tests/conftest.py index bc711e5..ca700dd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1 +1,30 @@ +# -*- coding: utf-8 -*- + +import collections +import json + +import pytest + pytest_plugins = 'pytester' + + +@pytest.fixture(name='cookiecutter_template') +def fixture_cookiecutter_template(tmpdir): + template = tmpdir.ensure('cookiecutter-template', dir=True) + + template_config = collections.OrderedDict([ + ('repo_name', 'foobar'), + ('short_description', 'Test Project'), + ]) + template.join('cookiecutter.json').write(json.dumps(template_config)) + + template_readme = '\n'.join([ + '{{cookiecutter.repo_name}}', + '{% for _ in cookiecutter.repo_name %}={% endfor %}', + '{{cookiecutter.short_description}}', + ]) + + repo = template.ensure('{{cookiecutter.repo_name}}', dir=True) + repo.join('README.rst').write(template_readme) + + return template diff --git a/tests/test_cookies.py b/tests/test_cookies.py index 6ff40a2..eb47820 100644 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -1,8 +1,6 @@ # -*- coding: utf-8 -*- import json -import collections - import pytest @@ -30,28 +28,6 @@ def test_valid_fixture(cookies): assert result.ret == 0 -@pytest.fixture -def cookiecutter_template(tmpdir): - template = tmpdir.ensure('cookiecutter-template', dir=True) - - template_config = collections.OrderedDict([ - ('repo_name', 'foobar'), - ('short_description', 'Test Project'), - ]) - template.join('cookiecutter.json').write(json.dumps(template_config)) - - template_readme = '\n'.join([ - '{{cookiecutter.repo_name}}', - '{% for _ in cookiecutter.repo_name %}={% endfor %}', - '{{cookiecutter.short_description}}', - ]) - - repo = template.ensure('{{cookiecutter.repo_name}}', dir=True) - repo.join('README.rst').write(template_readme) - - return template - - def test_cookies_bake_with_template_kwarg(testdir, cookiecutter_template): """bake accepts a template kwarg.""" testdir.makepyfile(""" @@ -344,3 +320,47 @@ def test_bake_should_fail(cookies): result.stdout.fnmatch_lines([ '*::test_bake_should_fail PASSED*', ]) + + +@pytest.mark.parametrize('choice', ['mkdocs', 'sphinx', 'none']) +def test_cookies_bake_choices(testdir, choice): + """Programmatically create a **Cookiecutter** template and make sure that + cookies.bake() works with choice variables. + """ + template = testdir.tmpdir.ensure('cookiecutter-choices', dir=True) + template_config = { + 'repo_name': 'docs', + 'docs_tool': ['mkdocs', 'sphinx', 'none'], + } + template.join('cookiecutter.json').write(json.dumps(template_config)) + + repo = template.ensure('{{cookiecutter.repo_name}}', dir=True) + repo.join('README.rst').write("docs_tool: {{cookiecutter.docs_tool}}") + + testdir.makepyfile(""" + # -*- coding: utf-8 -*- + + def test_bake_project(cookies): + result = cookies.bake( + extra_context={'docs_tool': '%s'}, + template=r'%s', + ) + + assert result.exit_code == 0 + assert result.exception is None + assert result.project.basename == 'docs' + assert result.project.isdir() + + assert result.project.join('README.rst').read() == 'docs_tool: %s' + + assert str(result) == ''.format(result.project) + """ % (choice, template, choice)) + + # run pytest without the template cli arg + result = testdir.runpytest('-v') + + result.stdout.fnmatch_lines([ + '*::test_bake_project PASSED*', + ]) + + result = testdir.runpytest('-v', '--template={}'.format(template))