Skip to content

Commit

Permalink
fix: Working on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christokur committed Feb 4, 2023
1 parent 7004717 commit 3ad378d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
3 changes: 2 additions & 1 deletion cookiecutter/context.py
Expand Up @@ -14,6 +14,7 @@
https://github.com/hackebrot/cookiecutter/tree/new-context-format
"""
import shutil

import collections
import json
Expand Down Expand Up @@ -602,7 +603,7 @@ def jinja_render(string):

# for prompt esthetics
if verbose:
width, _ = click.get_terminal_size()
width, _ = shutil.get_terminal_size()
click.echo('-' * width)

# updating the skipping variables for the continuation
Expand Down
22 changes: 0 additions & 22 deletions tests/test_context.py
Expand Up @@ -583,28 +583,6 @@ def test_variable_str():
assert "validate='<_sre.SRE_Pattern object at" in v_str


def test_variable_option_raise_invalid_type_value_error():

VAR_NAME = 'module_name'
OPT_VALUE_OF_INCORRECT_TYPE = 12 # should be a string

with pytest.raises(ValueError) as excinfo:
context.Variable(
VAR_NAME,
"{{cookiecutter.plugin_name|lower|replace('-','_')}}",
prompt="Please enter a name for your base python module",
type='string',
validation=OPT_VALUE_OF_INCORRECT_TYPE,
validation_flags=['ignorecase'],
hide_input=True,
)

msg = "Variable: '{var_name}' Option: 'validation' requires a value of type str, but has a value of: {value}"
assert msg.format(var_name=VAR_NAME, value=OPT_VALUE_OF_INCORRECT_TYPE) in str(
excinfo.value
)


def test_cookiecutter_template_repr():
# name, cookiecutter_version, variables, **info

Expand Down
48 changes: 25 additions & 23 deletions tests/test_generate_context_v2.py
Expand Up @@ -317,6 +317,16 @@ def context_data_serializer():
yield context_choices_with_default_not_in_choices


@pytest.mark.usefixtures('clean_system')
@pytest.mark.parametrize('input_params, expected_context', context_data_serializer())
def test_generate_context(input_params, expected_context):
"""
Test the generated context for several input parameters against the
according expected context.
"""
assert generate.generate_context(**input_params) == expected_context


def context_data_misses():
context_choices_with_default = (
{
Expand Down Expand Up @@ -370,7 +380,7 @@ def context_data_misses():
OrderedDict(
[
("name", "license"),
("default", "MIT"),
("default", "Apache2"),
(
"choices",
[
Expand All @@ -394,6 +404,17 @@ def context_data_misses():
yield context_choices_with_extra


@pytest.mark.usefixtures('clean_system')
@pytest.mark.parametrize('input_params, expected_context', context_data_misses())
def test_generate_context_misses(input_params, expected_context):
"""
Test the generated context for several input parameters against the
according expected context.
"""
generated_context = generate.generate_context(**input_params)
assert generated_context == expected_context


def context_data_value_errors():
context_choices_with_default_value_error = (
{
Expand Down Expand Up @@ -429,13 +450,13 @@ def context_data_value_errors():
]
)
},
False,
True,
)
context_choices_with_extra_value_error = (
{
'context_file': 'tests/test-generate-context-v2/test_choices.json',
'default_context': {'license': 'Apache2'},
'extra_context': [{'license': 'MIT'}],
'extra_context': [{'name': 'license', 'default': 'MIT'}],
},
{
"test_choices": OrderedDict(
Expand Down Expand Up @@ -466,31 +487,12 @@ def context_data_value_errors():
]
)
},
True,
False,
)
yield context_choices_with_default_value_error
yield context_choices_with_extra_value_error


def test_generate_context(input_params, expected_context):
"""
Test the generated context for several input parameters against the
according expected context.
"""
assert generate.generate_context(**input_params) == expected_context


@pytest.mark.usefixtures('clean_system')
@pytest.mark.parametrize('input_params, expected_context', context_data_misses())
def test_generate_context_misses(input_params, expected_context):
"""
Test the generated context for several input parameters against the
according expected context.
"""
generated_context = generate.generate_context(**input_params)
assert generated_context == expected_context


@pytest.mark.usefixtures('clean_system')
@pytest.mark.parametrize(
'input_params, expected_context, raise_exception', context_data_value_errors()
Expand Down

0 comments on commit 3ad378d

Please sign in to comment.