Skip to content

Commit

Permalink
Run pre-commit linting
Browse files Browse the repository at this point in the history
- Run tox twice, which lints the code base via pre-commit
  • Loading branch information
audreyfeldroy committed Nov 5, 2021
1 parent 3b5a434 commit c9e36a4
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 36 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ repos:
language: python
files: \.rst$
require_serial: true
# - repo: https://github.com/psf/black.git
# rev: 21.10b0
# hooks:
# - id: black
# language_version: python3
# exclude: ^(tests\/hooks-abort-render\/hooks|docs\/HelloCookieCutter1)
- repo: https://github.com/psf/black.git
rev: 21.10b0
hooks:
- id: black
language_version: python3
exclude: ^(tests\/hooks-abort-render\/hooks|docs\/HelloCookieCutter1)
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
Expand Down
4 changes: 3 additions & 1 deletion cookiecutter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def list_installed_templates(default_config, passed_config_file):
help='Do not prompt for parameters and only use cookiecutter.json file content',
)
@click.option(
'-c', '--checkout', help='branch, tag or commit to checkout after git clone',
'-c',
'--checkout',
help='branch, tag or commit to checkout after git clone',
)
@click.option(
'--directory',
Expand Down
3 changes: 2 additions & 1 deletion cookiecutter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def cookiecutter(
raise InvalidModeException(err_msg)

config_dict = get_user_config(
config_file=config_file, default_config=default_config,
config_file=config_file,
default_config=default_config,
)

repo_dir, cleanup = determine_repo_dir(
Expand Down
42 changes: 34 additions & 8 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ def test_default_user_config_overwrite(mocker, cli_runner, user_config_path):

template_path = 'tests/fake-repo-pre/'
result = cli_runner(
template_path, '--config-file', user_config_path, '--default-config',
template_path,
'--config-file',
user_config_path,
'--default-config',
)

assert result.exit_code == 0
Expand Down Expand Up @@ -362,7 +365,11 @@ def test_echo_undefined_variable_error(output_dir, cli_runner):
template_path = 'tests/undefined-variable/file-name/'

result = cli_runner(
'--no-input', '--default-config', '--output-dir', output_dir, template_path,
'--no-input',
'--default-config',
'--output-dir',
output_dir,
template_path,
)

assert result.exit_code == 1
Expand Down Expand Up @@ -392,7 +399,11 @@ def test_echo_unknown_extension_error(output_dir, cli_runner):
template_path = 'tests/test-extensions/unknown/'

result = cli_runner(
'--no-input', '--default-config', '--output-dir', output_dir, template_path,
'--no-input',
'--default-config',
'--output-dir',
output_dir,
template_path,
)

assert result.exit_code == 1
Expand All @@ -404,7 +415,10 @@ def test_echo_unknown_extension_error(output_dir, cli_runner):
def test_cli_extra_context(cli_runner):
"""Cli invocation replace content if called with replacement pairs."""
result = cli_runner(
'tests/fake-repo-pre/', '--no-input', '-v', 'project_name=Awesomez',
'tests/fake-repo-pre/',
'--no-input',
'-v',
'project_name=Awesomez',
)
assert result.exit_code == 0
assert os.path.isdir('fake-project')
Expand All @@ -416,7 +430,10 @@ def test_cli_extra_context(cli_runner):
def test_cli_extra_context_invalid_format(cli_runner):
"""Cli invocation raise error if called with unknown argument."""
result = cli_runner(
'tests/fake-repo-pre/', '--no-input', '-v', 'ExtraContextWithNoEqualsSoInvalid',
'tests/fake-repo-pre/',
'--no-input',
'-v',
'ExtraContextWithNoEqualsSoInvalid',
)
assert result.exit_code == 2
assert "Error: Invalid value for '[EXTRA_CONTEXT]...'" in result.output
Expand All @@ -438,7 +455,10 @@ def test_debug_file_non_verbose(cli_runner, debug_file):
assert not debug_file.exists()

result = cli_runner(
'--no-input', '--debug-file', str(debug_file), 'tests/fake-repo-pre/',
'--no-input',
'--debug-file',
str(debug_file),
'tests/fake-repo-pre/',
)
assert result.exit_code == 0

Expand Down Expand Up @@ -493,7 +513,10 @@ def test_debug_list_installed_templates(cli_runner, debug_file, user_config_path
open(os.path.join('fake-project', 'cookiecutter.json'), 'w').write('{}')

result = cli_runner(
'--list-installed', '--config-file', user_config_path, str(debug_file),
'--list-installed',
'--config-file',
user_config_path,
str(debug_file),
)

assert "1 installed templates:" in result.output
Expand All @@ -520,7 +543,10 @@ def test_debug_list_installed_templates_failure(
def test_directory_repo(cli_runner):
"""Test cli invocation works with `directory` option."""
result = cli_runner(
'tests/fake-repo-dir/', '--no-input', '-v', '--directory=my-dir',
'tests/fake-repo-dir/',
'--no-input',
'-v',
'--directory=my-dir',
)
assert result.exit_code == 0
assert os.path.isdir("fake-project")
Expand Down
8 changes: 7 additions & 1 deletion tests/test_generate_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ def test_default_context_replacement_in_generate_context():

def test_generate_context_decodes_non_ascii_chars():
"""Verify `generate_context` correctly decodes non-ascii chars."""
expected_context = {'non_ascii': OrderedDict([('full_name', 'éèà'),])}
expected_context = {
'non_ascii': OrderedDict(
[
('full_name', 'éèà'),
]
)
}

generated_context = generate.generate_context(
context_file='tests/test-generate-context/non_ascii.json'
Expand Down
6 changes: 5 additions & 1 deletion tests/test_get_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ def test_get_config():
'github_username': 'example',
'project': {
'description': 'description',
'tags': ['first', 'second', 'third',],
'tags': [
'first',
'second',
'third',
],
},
},
'abbreviations': {
Expand Down
6 changes: 5 additions & 1 deletion tests/test_get_user_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def custom_config():
'github_username': 'example',
'project': {
'description': 'description',
'tags': ['first', 'second', 'third',],
'tags': [
'first',
'second',
'third',
],
},
},
'cookiecutters_dir': '/home/example/some-path-to-templates',
Expand Down
23 changes: 17 additions & 6 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ def test_replay_dump_template_name(
mocker.patch('cookiecutter.main.generate_files')

cookiecutter(
'.', no_input=True, replay=False, config_file=user_config_file,
'.',
no_input=True,
replay=False,
config_file=user_config_file,
)

mock_replay_dump.assert_called_once_with(
user_config_data['replay_dir'], 'fake-repo-tmpl', mocker.ANY,
user_config_data['replay_dir'],
'fake-repo-tmpl',
mocker.ANY,
)


Expand All @@ -46,11 +51,14 @@ def test_replay_load_template_name(
mocker.patch('cookiecutter.main.generate_files')

cookiecutter(
'.', replay=True, config_file=user_config_file,
'.',
replay=True,
config_file=user_config_file,
)

mock_replay_load.assert_called_once_with(
user_config_data['replay_dir'], 'fake-repo-tmpl',
user_config_data['replay_dir'],
'fake-repo-tmpl',
)


Expand All @@ -62,9 +70,12 @@ def test_custom_replay_file(monkeypatch, mocker, user_config_file):
mocker.patch('cookiecutter.main.generate_files')

cookiecutter(
'.', replay='./custom-replay-file', config_file=user_config_file,
'.',
replay='./custom-replay-file',
config_file=user_config_file,
)

mock_replay_load.assert_called_once_with(
'.', 'custom-replay-file',
'.',
'custom-replay-file',
)
3 changes: 2 additions & 1 deletion tests/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class TestPrompt(object):
def test_prompt_for_config(self, monkeypatch, context):
"""Verify `prompt_for_config` call `read_user_variable` on text request."""
monkeypatch.setattr(
'cookiecutter.prompt.read_user_variable', lambda var, default: default,
'cookiecutter.prompt.read_user_variable',
lambda var, default: default,
)

cookiecutter_dict = prompt.prompt_for_config(context)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ def test_prompt_should_ask_and_exit_on_user_no_answer(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user decline to delete/reclone the \
repo, cookiecutter should exit."""
mock_read_user = mocker.patch(
'cookiecutter.utils.read_user_yes_no', return_value=False,
'cookiecutter.utils.read_user_yes_no',
return_value=False,
)
mock_sys_exit = mocker.patch('sys.exit', return_value=True)
repo_dir = Path(tmp_path, 'repo')
Expand Down
12 changes: 8 additions & 4 deletions tests/vcs/test_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def test_clone_should_rstrip_trailing_slash_in_repo_url(mocker, clone_dir):
mocker.patch('cookiecutter.vcs.is_vcs_installed', autospec=True, return_value=True)

mock_subprocess = mocker.patch(
'cookiecutter.vcs.subprocess.check_output', autospec=True,
'cookiecutter.vcs.subprocess.check_output',
autospec=True,
)

vcs.clone('https://github.com/foo/bar/', clone_to_dir=str(clone_dir), no_input=True)
Expand All @@ -44,7 +45,8 @@ def test_clone_should_abort_if_user_does_not_want_to_reclone(mocker, clone_dir):
'cookiecutter.vcs.prompt_and_delete', side_effect=SystemExit, autospec=True
)
mock_subprocess = mocker.patch(
'cookiecutter.vcs.subprocess.check_output', autospec=True,
'cookiecutter.vcs.subprocess.check_output',
autospec=True,
)

# Create repo_dir to trigger prompt_and_delete
Expand All @@ -66,7 +68,8 @@ def test_clone_should_silent_exit_if_ok_to_reuse(mocker, tmpdir):
'cookiecutter.vcs.prompt_and_delete', return_value=False, autospec=True
)
mock_subprocess = mocker.patch(
'cookiecutter.vcs.subprocess.check_output', autospec=True,
'cookiecutter.vcs.subprocess.check_output',
autospec=True,
)

clone_to_dir = tmpdir.mkdir('clone')
Expand Down Expand Up @@ -103,7 +106,8 @@ def test_clone_should_invoke_vcs_command(
mocker.patch('cookiecutter.vcs.is_vcs_installed', autospec=True, return_value=True)

mock_subprocess = mocker.patch(
'cookiecutter.vcs.subprocess.check_output', autospec=True,
'cookiecutter.vcs.subprocess.check_output',
autospec=True,
)
expected_repo_dir = os.path.normpath(os.path.join(clone_dir, repo_name))

Expand Down
19 changes: 14 additions & 5 deletions tests/zipfile/test_unzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ def test_unzip_url(mocker, clone_dir):
request.iter_content.return_value = mock_download()

mocker.patch(
'cookiecutter.zipfile.requests.get', return_value=request, autospec=True,
'cookiecutter.zipfile.requests.get',
return_value=request,
autospec=True,
)

output_dir = zipfile.unzip(
Expand All @@ -191,7 +193,9 @@ def test_unzip_url_with_empty_chunks(mocker, clone_dir):
request.iter_content.return_value = mock_download_with_empty_chunks()

mocker.patch(
'cookiecutter.zipfile.requests.get', return_value=request, autospec=True,
'cookiecutter.zipfile.requests.get',
return_value=request,
autospec=True,
)

output_dir = zipfile.unzip(
Expand All @@ -214,7 +218,9 @@ def test_unzip_url_existing_cache(mocker, clone_dir):
request.iter_content.return_value = mock_download()

mocker.patch(
'cookiecutter.zipfile.requests.get', return_value=request, autospec=True,
'cookiecutter.zipfile.requests.get',
return_value=request,
autospec=True,
)

# Create an existing cache of the zipfile
Expand All @@ -237,7 +243,9 @@ def test_unzip_url_existing_cache_no_input(mocker, clone_dir):
request.iter_content.return_value = mock_download()

mocker.patch(
'cookiecutter.zipfile.requests.get', return_value=request, autospec=True,
'cookiecutter.zipfile.requests.get',
return_value=request,
autospec=True,
)

# Create an existing cache of the zipfile
Expand All @@ -261,7 +269,8 @@ def test_unzip_should_abort_if_no_redownload(mocker, clone_dir):
)

mock_requests_get = mocker.patch(
'cookiecutter.zipfile.requests.get', autospec=True,
'cookiecutter.zipfile.requests.get',
autospec=True,
)

# Create an existing cache of the zipfile
Expand Down

0 comments on commit c9e36a4

Please sign in to comment.