Skip to content

Add test for choice variable #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
68 changes: 44 additions & 24 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-

import json
import collections

import pytest


Expand Down Expand Up @@ -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("""
Expand Down Expand Up @@ -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'},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #24, the example has just a string for the choice, not a list?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pytest marker injects a string into the choice variable.

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) == '<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))