Skip to content

Commit

Permalink
Added: docstrings for extensions feature test files (#1373)
Browse files Browse the repository at this point in the history
  • Loading branch information
insspb committed Apr 18, 2020
1 parent aabfb6a commit 9ffbe89
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
# flake8: noqa

"""Sample post-gen hook for testing that custom extensions are available and exposed methods are callable."""
import sys

print('{% hello cookiecutter.name %}')
if '{% hello cookiecutter.name %}' == 'Hello Cookiemonster!':
sys.exit(0)
else:
sys.exit(1)
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@

"""Sample pre-gen hook for testing that custom extensions are available and exposed methods are callable."""

print('{% hello cookiecutter.name %}')
import sys

if '{% hello cookiecutter.name %}' == 'Hello Cookiemonster!':
sys.exit(0)
else:
sys.exit(1)
2 changes: 2 additions & 0 deletions tests/test-extensions/hello_extension/hello_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ def __init__(self, environment):
super(HelloExtension, self).__init__(environment)

def _hello(self, name):
"""Do actual tag replace when invoked by parser."""
return 'Hello {name}!'.format(name=name)

def parse(self, parser):
"""Work when something match `tags` variable."""
lineno = next(parser.stream).lineno
node = parser.parse_expression()
call_method = self.call_method('_hello', [node], lineno=lineno)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_custom_extensions_in_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,30 @@
'post_gen_hook',
])
def template(request):
"""Fixture. Allows to split pre and post hooks test directories."""
return 'tests/test-extensions/' + request.param


@pytest.fixture
def output_dir(tmpdir):
"""Fixture. Create and return custom temp directory for test."""
return str(tmpdir.mkdir('hello'))


@pytest.fixture(autouse=True)
def modify_syspath(monkeypatch):
# Make sure that the custom extension can be loaded
"""Fixture. Make sure that the custom extension can be loaded."""
monkeypatch.syspath_prepend(
'tests/test-extensions/hello_extension'
)


def test_hook_with_extension(template, output_dir):
"""Verify custom Jinja2 extension correctly work in hooks and file rendering.
Each file in hooks has simple tests inside and will raise error if not
correctly rendered.
"""
project_dir = main.cookiecutter(
template,
no_input=True,
Expand Down
12 changes: 5 additions & 7 deletions tests/test_default_extensions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# -*- coding: utf-8 -*-

"""
test_default_extensions.
Tests to ensure Jinja2 filters/extensions are available from within pre- and
post-gen hooks.
"""
"""Verify Jinja2 filters/extensions are available from pre-gen/post-gen hooks."""

import io
import os
Expand All @@ -16,15 +11,17 @@
from cookiecutter.main import cookiecutter


@pytest.yield_fixture(autouse=True)
@pytest.fixture(autouse=True)
def freeze():
"""Fixture. Make time stating during all tests in this file."""
freezer = freezegun.freeze_time("2015-12-09 23:33:01")
freezer.start()
yield
freezer.stop()


def test_jinja2_time_extension(tmpdir):
"""Verify Jinja2 time extension work correctly."""
project_dir = cookiecutter(
'tests/test-extensions/default/',
no_input=True,
Expand All @@ -49,6 +46,7 @@ def test_jinja2_time_extension(tmpdir):


def test_jinja2_slugify_extension(tmpdir):
"""Verify Jinja2 slugify extension work correctly."""
project_dir = cookiecutter(
'tests/test-extensions/default/',
no_input=True,
Expand Down

0 comments on commit 9ffbe89

Please sign in to comment.