Skip to content

Commit

Permalink
fix: Fix tests and improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
christokur committed Feb 5, 2023
1 parent 3ad378d commit 61427f6
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 448 deletions.
2 changes: 1 addition & 1 deletion cookiecutter/context.py
Expand Up @@ -479,7 +479,7 @@ def __init__(self, template, requires=None, extensions=None, **kwargs):
self.extensions = extensions

if self.requirements:
self.cookiecutter_version = self.requirements.get('cookiecutter')
self.cookiecutter_version = self.requirements.get('cookiecutter', None)
if self.cookiecutter_version:
validate_requirement(
self.cookiecutter_version,
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -85,7 +85,7 @@ max-line-length = 88

[tool:pytest]
testpaths = tests
addopts = -vvv --cov-report=html --cov-report=xml --cov-branch --cov-fail-under=100 --cov-report term-missing --cov=cookiecutter
addopts = --cov-report=html --cov-report=xml --cov-branch --cov-fail-under=100 --cov-report term-missing --cov=cookiecutter

[doc8]
# TODO: Remove current max-line-length ignore in follow-up and adopt black limit.
Expand Down
154 changes: 154 additions & 0 deletions tests/test-context/cookiecutter-no-requires.json
@@ -0,0 +1,154 @@
{
"version": "2.0",
"requires": {
"python": ">=3"
},
"jinja": {
"optimized": true,
"extensions": [
"cookiecutter.extensions.SlugifyExtension",
"jinja2_time.TimeExtension"
]
},
"template": {
"name": "cookiecutter-pytest-plugin",
"version": "0.1",
"description": "a cookiecutter to create pytest plugins with ease.",
"authors": [
"Raphael Pierzina <raphael@hackebrot.de>",
"Audrey Roy Greenfeld <aroy@alum.mit.edu>"
],
"license": "MIT",
"keywords": [
"pytest",
"python",
"plugin"
],
"url": "https://github.com/pytest-dev/cookiecutter-pytest-plugin",
"variables": [
{
"name": "full_name",
"default": "Raphael Pierzina",
"prompt": "What's your full name?",
"description": "Please enter your full name. It will be displayed on the README file and used for the PyPI package definition.",
"type": "string"
},
{
"name": "email",
"default": "raphael@hackebrot.de",
"prompt": "What's your email?",
"description": "Please enter an email address for the meta information in setup.py.",
"type": "string"
},
{
"name": "secret_token",
"prompt": "Please enter your secret token",
"type": "string",
"hide_input": true
},
{
"name": "plugin_name",
"default": "emoji",
"prompt": "What should be the name for your plugin?",
"description": "Please enter a name for your plugin. We will prepend the name with 'pytest-'",
"type": "string"
},
{
"name": "module_name",
"default": "{{cookiecutter.plugin_name|lower|replace('-','_')}}",
"prompt": "Please enter a name for your base python module",
"type": "string",
"validation": "^[a-z_]+$"
},
{
"name": "license",
"default": "MIT",
"prompt": "Please choose a license!",
"description": "Cookiecutter will add an according LICENSE file for you and set the according classifier in setup.py.",
"type": "string",
"choices": [
"MIT",
"BSD-3",
"GNU GPL v3.0",
"Apache Software License 2.0",
"Mozilla Public License 2.0"
]
},
{
"name": "docs",
"default": false,
"prompt": "Do you want to generate a base for docs?",
"description": "Would you like to generate documentation for your plugin? You will be able to choose from a number of generators.",
"type": "yes_no"
},
{
"name": "docs_tool",
"default": "mkdocs",
"prompt": "Which tool do you want to choose for generating docs?",
"description": "There are a number of options for documentation generators. Please choose one. We will create a separate folder for you",
"type": "string",
"choices": [
"mkdocs",
"sphinx"
],
"skip_if": "{{cookiecutter.docs == False}}"
},
{
"name": "year",
"default": "{% now 'utc', '%Y' %}",
"prompt_user": false,
"type": "string"
},
{
"name": "incept_year",
"default": 2017,
"prompt_user": false,
"type": "int"
},
{
"name": "released",
"default": false,
"prompt_user": false,
"type": "boolean"
},
{
"name": "temperature",
"default": 77.3,
"prompt_user": false,
"type": "float"
},
{
"name": "Release-GUID",
"default": "04f5eaa9ee7345469dccffc538b27194",
"prompt_user": false,
"type": "uuid"
},
{
"name": "copy_with_out_render",
"default": [
"*.html",
"*not_rendered_dir",
"rendered_dir/not_rendered_file.ini"
],
"prompt_user": false,
"type": "string"
},
{
"name": "fixtures",
"default": {
"foo": {
"scope": "session",
"autouse": true
},
"bar": {
"scope": "function",
"autouse": false
}
},
"description": "Please enter a valid JSON string to set up fixtures for your plugin.",
"prompt_user": true,
"type": "json"
}
]
}
}
35 changes: 35 additions & 0 deletions tests/test_context.py
Expand Up @@ -90,6 +90,41 @@ def test_load_context_defaults():
)


@pytest.mark.usefixtures('clean_system')
def test_load_context_defaults_no_requires():

cc = load_cookiecutter('tests/test-context/cookiecutter-no-requires.json')
cc_cfg = context.load_context(cc['cookiecutter-no-requires'], no_input=True, verbose=False)

assert cc_cfg['full_name'] == 'Raphael Pierzina'
assert cc_cfg['email'] == 'raphael@hackebrot.de'
assert cc_cfg['plugin_name'] == 'emoji'
assert cc_cfg['module_name'] == 'emoji'
assert cc_cfg['license'] == 'MIT'
assert cc_cfg['docs'] is False
assert 'docs_tool' not in cc_cfg.keys() # skip_if worked
assert cc_cfg['year'] == time.strftime('%Y')
assert cc_cfg['incept_year'] == 2017
assert cc_cfg['released'] is False
assert cc_cfg['temperature'] == 77.3
assert cc_cfg['Release-GUID'] == UUID('04f5eaa9ee7345469dccffc538b27194').hex
assert cc_cfg['_extensions'] == [
'cookiecutter.extensions.SlugifyExtension',
'jinja2_time.TimeExtension',
]
assert cc_cfg['_jinja2_env_vars'] == {"optimized": True}
assert (
cc_cfg['copy_with_out_render']
== "['*.html', '*not_rendered_dir', 'rendered_dir/not_rendered_file.ini']"
)
assert cc_cfg['fixtures'] == OrderedDict(
[
('foo', OrderedDict([('scope', 'session'), ('autouse', True)])),
('bar', OrderedDict([('scope', 'function'), ('autouse', False)])),
]
)


@pytest.mark.usefixtures('clean_system')
def test_load_context_defaults_skips_branch():
"""
Expand Down

0 comments on commit 61427f6

Please sign in to comment.