From 3ad378d94d3296b7546fd0db33bacf11f89cc690 Mon Sep 17 00:00:00 2001 From: Christo De Lange <111363591+christokur@users.noreply.github.com> Date: Sat, 4 Feb 2023 14:11:00 -0500 Subject: [PATCH] fix: Working on tests --- cookiecutter/context.py | 3 +- tests/test_context.py | 22 -------------- tests/test_generate_context_v2.py | 48 ++++++++++++++++--------------- 3 files changed, 27 insertions(+), 46 deletions(-) diff --git a/cookiecutter/context.py b/cookiecutter/context.py index 35917b6d3..9bf5a8fd8 100644 --- a/cookiecutter/context.py +++ b/cookiecutter/context.py @@ -14,6 +14,7 @@ https://github.com/hackebrot/cookiecutter/tree/new-context-format """ +import shutil import collections import json @@ -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 diff --git a/tests/test_context.py b/tests/test_context.py index a917a902e..c8e7711db 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -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 diff --git a/tests/test_generate_context_v2.py b/tests/test_generate_context_v2.py index 1eb0b91c9..4fbfa7eb9 100644 --- a/tests/test_generate_context_v2.py +++ b/tests/test_generate_context_v2.py @@ -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 = ( { @@ -370,7 +380,7 @@ def context_data_misses(): OrderedDict( [ ("name", "license"), - ("default", "MIT"), + ("default", "Apache2"), ( "choices", [ @@ -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 = ( { @@ -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( @@ -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()