Skip to content

Commit

Permalink
fix: Tests broken by progress since this feature was developed
Browse files Browse the repository at this point in the history
  • Loading branch information
christokur committed Feb 3, 2023
1 parent afa9af0 commit d00e424
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
7 changes: 6 additions & 1 deletion cookiecutter/context.py
Expand Up @@ -21,7 +21,9 @@
import logging
import collections
import json
import posix
import re
import sys

import click
from jinja2 import Environment
Expand Down Expand Up @@ -556,7 +558,10 @@ def load_context(json_object, no_input=False, verbose=True):
break

if verbose:
width, _ = click.get_terminal_size()
if sys.stdout.isatty():
width, _ = posix.get_terminal_size()
else:
width = 80
click.echo('-' * width)

context[variable.name] = deserialize(value)
Expand Down
Empty file.
27 changes: 14 additions & 13 deletions tests/test_context.py
Expand Up @@ -583,19 +583,20 @@ def test_variable_str():
validation_flags=['ignorecase'],
hide_input=True)

assert '<Variable module_name>:' in str(v)
assert "name='module_name'" in str(v)
assert "default='{{cookiecutter.plugin_name|lower|replace('-','_')}}'" in str(v)
assert "description='None'" in str(v)
assert "prompt='Please enter a name for your base python module'" in str(v)
assert "hide_input='True'" in str(v)
assert "var_type='string'" in str(v)
assert "skip_if=''" in str(v)
assert "prompt_user='True'" in str(v)
assert "choices='[]'" in str(v)
assert "validation='^[a-z_]+$'" in str(v)
assert "validation_flag_names='['ignorecase']'" in str(v)
assert "validation_flags='2'" in str(v)
str_v = str(v)
assert '<Variable module_name>:' in str_v
assert "name='module_name'" in str_v
assert "default='{{cookiecutter.plugin_name|lower|replace('-','_')}}'" in str_v
assert "description='None'" in str_v
assert "prompt='Please enter a name for your base python module'" in str_v
assert "hide_input='True'" in str_v
assert "var_type='string'" in str_v
assert "skip_if=''" in str_v
assert "prompt_user='True'" in str_v
assert "choices='[]'" in str_v
assert "validation='^[a-z_]+$'" in str_v
assert "validation_flag_names='['ignorecase']'" in str_v
assert "validation_flags='2'" in str_v or "validation_flags='re.IGNORECASE'" in str_v

if sys.version_info >= (3, 4):
assert "validate='re.compile('^[a-z_]+$', re.IGNORECASE)'" in str(v)
Expand Down
37 changes: 30 additions & 7 deletions tests/test_main.py
@@ -1,4 +1,10 @@
"""Collection of tests around cookiecutter's replay feature."""
import collections
import os
import pathlib
import shutil

from cookiecutter.find import find_template
from cookiecutter.main import cookiecutter


Expand Down Expand Up @@ -71,13 +77,28 @@ def test_version_2_load_context_call(
for this test and call cookiecutter with '.' for the target template.
"""
monkeypatch.chdir('tests/test-generate-context-v2/min-v2-cookiecutter')

if os.path.exists('test-repo'):
shutil.rmtree('test-repo')
mock_replay_dump = mocker.patch('cookiecutter.main.dump')

mock_version_1_prompt_for_config = mocker.patch(
'cookiecutter.main.prompt_for_config')
mock_version_2_load_context = mocker.patch(
'cookiecutter.main.load_context')
counts = {}
def patch_load_context(counts):
counts['load_context'] = 0
def load_context(json_object, no_input=False, verbose=True, counts=counts):
counts["load_context"] += 1
return collections.OrderedDict({
'repo_name': 'test-repo',
})
return load_context

def patch_prompt_for_config(counts):
counts['prompt_for_config'] = 0
def prompt_for_config(context, no_input=False):
counts["prompt_for_config"] += 1
return {}

mocker.patch('cookiecutter.main.prompt_for_config', patch_prompt_for_config(counts))
mocker.patch('cookiecutter.main.load_context', patch_load_context(counts))

cookiecutter(
'.',
Expand All @@ -86,9 +107,11 @@ def test_version_2_load_context_call(
config_file=user_config_file,
)

assert mock_version_1_prompt_for_config.call_count == 0
assert mock_version_2_load_context.call_count == 1
if os.path.exists('test-repo'):
shutil.rmtree('test-repo')
assert mock_replay_dump.call_count == 1
assert counts["load_context"] == 1
assert counts["prompt_for_config"] == 0


def test_custom_replay_file(monkeypatch, mocker, user_config_file):
Expand Down

0 comments on commit d00e424

Please sign in to comment.