Skip to content

Commit

Permalink
Replace tmpdir in favour of tmp_path (#1545)
Browse files Browse the repository at this point in the history
* Remove tmpdir

* Deduplicate fixtures

* Fix lint
  • Loading branch information
Fabio Todaro committed May 12, 2021
1 parent c48119d commit a6113ea
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 194 deletions.
32 changes: 25 additions & 7 deletions tests/conftest.py
Expand Up @@ -137,9 +137,9 @@ def restore_backup():


@pytest.fixture(scope='session')
def user_dir(tmpdir_factory):
def user_dir(tmp_path_factory):
"""Fixture that simulates the user's home directory."""
return tmpdir_factory.mktemp('user_dir')
return tmp_path_factory.mktemp('user_dir')


@pytest.fixture(scope='session')
Expand All @@ -153,9 +153,10 @@ def user_config_data(user_dir):
:returns: Dict with name of both user config dirs
"""
cookiecutters_dir = user_dir.mkdir('cookiecutters')
replay_dir = user_dir.mkdir('cookiecutter_replay')

cookiecutters_dir = user_dir.joinpath('cookiecutters')
cookiecutters_dir.mkdir()
replay_dir = user_dir.joinpath('cookiecutter_replay')
replay_dir.mkdir()
return {
'cookiecutters_dir': str(cookiecutters_dir),
'replay_dir': str(replay_dir),
Expand All @@ -173,8 +174,25 @@ def user_config_file(user_dir, user_config_data):
:param user_config_data: Dict of config values
:returns: String of path to config file
"""
config_file = user_dir.join('config')
config_file = user_dir.joinpath('config')

config_text = USER_CONFIG.format(**user_config_data)
config_file.write(config_text)
config_file.write_text(config_text)
return str(config_file)


@pytest.fixture
def output_dir(tmp_path):
"""Fixture to prepare test output directory."""
output_path = tmp_path.joinpath("output")
output_path.mkdir()
return str(output_path)


@pytest.fixture
def clone_dir(tmp_path):
"""Simulate creation of a directory called `clone_dir` inside of `tmp_path`. \
Returns a str to said directory."""
clone_dir = tmp_path.joinpath("clone_dir")
clone_dir.mkdir()
return clone_dir
24 changes: 13 additions & 11 deletions tests/repository/test_determine_repository_should_use_local_repo.py
@@ -1,18 +1,18 @@
"""Tests around using locally cached cookiecutter template repositories."""

import os
from pathlib import Path

import pytest

from cookiecutter import repository, exceptions


def test_finds_local_repo(tmpdir):
def test_finds_local_repo(tmp_path):
"""A valid local repository should be returned."""
project_dir, cleanup = repository.determine_repo_dir(
'tests/fake-repo',
abbreviations={},
clone_to_dir=str(tmpdir),
clone_to_dir=str(tmp_path),
checkout=None,
no_input=True,
)
Expand All @@ -21,15 +21,15 @@ def test_finds_local_repo(tmpdir):
assert not cleanup


def test_local_repo_with_no_context_raises(tmpdir):
def test_local_repo_with_no_context_raises(tmp_path):
"""A local repository without a cookiecutter.json should raise a \
`RepositoryNotFound` exception."""
template_path = os.path.join('tests', 'fake-repo-bad')
template_path = str(Path('tests', 'fake-repo-bad'))
with pytest.raises(exceptions.RepositoryNotFound) as err:
repository.determine_repo_dir(
template_path,
abbreviations={},
clone_to_dir=str(tmpdir),
clone_to_dir=str(tmp_path),
checkout=None,
no_input=True,
)
Expand All @@ -38,20 +38,22 @@ def test_local_repo_with_no_context_raises(tmpdir):
'A valid repository for "{}" could not be found in the following '
'locations:\n{}'.format(
template_path,
'\n'.join([template_path, str(tmpdir / 'tests/fake-repo-bad')]),
'\n'.join(
[template_path, str(tmp_path.joinpath('tests', 'fake-repo-bad'))]
),
)
)


def test_local_repo_typo(tmpdir):
def test_local_repo_typo(tmp_path):
"""An unknown local repository should raise a `RepositoryNotFound` \
exception."""
template_path = os.path.join('tests', 'unknown-repo')
template_path = str(Path('tests', 'unknown-repo'))
with pytest.raises(exceptions.RepositoryNotFound) as err:
repository.determine_repo_dir(
template_path,
abbreviations={},
clone_to_dir=str(tmpdir),
clone_to_dir=str(tmp_path),
checkout=None,
no_input=True,
)
Expand All @@ -60,6 +62,6 @@ def test_local_repo_typo(tmpdir):
'A valid repository for "{}" could not be found in the following '
'locations:\n{}'.format(
template_path,
'\n'.join([template_path, str(tmpdir / 'tests/unknown-repo')]),
'\n'.join([template_path, str(tmp_path.joinpath('tests', 'unknown-repo'))]),
)
)
8 changes: 5 additions & 3 deletions tests/test_abort_generate_on_hook_error.py
Expand Up @@ -16,7 +16,7 @@
ids=("pre_gen_hook_raises_error", "post_gen_hook_raises_error"),
)
@pytest.mark.usefixtures("clean_system")
def test_hooks_raises_errors(tmpdir, abort_pre_gen, abort_post_gen):
def test_hooks_raises_errors(tmp_path, abort_pre_gen, abort_post_gen):
"""Verify pre- and pos-gen errors raises correct error code from script.
This allows developers to make different error codes in their code,
Expand All @@ -32,7 +32,9 @@ def test_hooks_raises_errors(tmpdir, abort_pre_gen, abort_post_gen):

with pytest.raises(exceptions.FailedHookException) as error:
generate.generate_files(
repo_dir="tests/hooks-abort-render", context=context, output_dir=str(tmpdir)
repo_dir="tests/hooks-abort-render",
context=context,
output_dir=str(tmp_path),
)
assert error.value.code == 5
assert not tmpdir.join("foobar").isdir()
assert not tmp_path.joinpath("foobar").is_dir()
24 changes: 8 additions & 16 deletions tests/test_cli.py
Expand Up @@ -238,12 +238,6 @@ def output_dir_flag(request):
return request.param


@pytest.fixture
def output_dir(tmpdir):
"""Pytest fixture return `output_dir` argument as string."""
return str(tmpdir.mkdir('output'))


def test_cli_output_dir(mocker, cli_runner, output_dir_flag, output_dir):
"""Test cli invocation with `output-dir` flag changes output directory."""
mock_cookiecutter = mocker.patch('cookiecutter.cli.cookiecutter')
Expand Down Expand Up @@ -283,9 +277,9 @@ def test_cli_help(cli_runner, help_cli_flag):


@pytest.fixture
def user_config_path(tmpdir):
def user_config_path(tmp_path):
"""Pytest fixture return `user_config` argument as string."""
return str(tmpdir.join('tests/config.yaml'))
return str(tmp_path.joinpath("tests", "config.yaml"))


def test_user_config(mocker, cli_runner, user_config_path):
Expand Down Expand Up @@ -365,9 +359,8 @@ def test_default_user_config(mocker, cli_runner):
)


def test_echo_undefined_variable_error(tmpdir, cli_runner):
def test_echo_undefined_variable_error(output_dir, cli_runner):
"""Cli invocation return error if variable undefined in template."""
output_dir = str(tmpdir.mkdir('output'))
template_path = 'tests/undefined-variable/file-name/'

result = cli_runner(
Expand Down Expand Up @@ -396,9 +389,8 @@ def test_echo_undefined_variable_error(tmpdir, cli_runner):
assert context_str in result.output


def test_echo_unknown_extension_error(tmpdir, cli_runner):
def test_echo_unknown_extension_error(output_dir, cli_runner):
"""Cli return error if extension incorrectly defined in template."""
output_dir = str(tmpdir.mkdir('output'))
template_path = 'tests/test-extensions/unknown/'

result = cli_runner(
Expand Down Expand Up @@ -434,9 +426,9 @@ def test_cli_extra_context_invalid_format(cli_runner):


@pytest.fixture
def debug_file(tmpdir):
def debug_file(tmp_path):
"""Pytest fixture return `debug_file` argument as path object."""
return tmpdir.join('fake-repo.log')
return tmp_path.joinpath('fake-repo.log')


@pytest.mark.usefixtures('remove_fake_project_dir')
Expand All @@ -458,7 +450,7 @@ def test_debug_file_non_verbose(cli_runner, debug_file):
"DEBUG cookiecutter.main: context_file is "
"tests/fake-repo-pre/cookiecutter.json"
)
assert context_log in debug_file.readlines(cr=False)
assert context_log in debug_file.read_text()
assert context_log not in result.output


Expand All @@ -485,7 +477,7 @@ def test_debug_file_verbose(cli_runner, debug_file):
"DEBUG cookiecutter.main: context_file is "
"tests/fake-repo-pre/cookiecutter.json"
)
assert context_log in debug_file.readlines(cr=False)
assert context_log in debug_file.read_text()
assert context_log in result.output


Expand Down
6 changes: 0 additions & 6 deletions tests/test_custom_extensions_in_hooks.py
Expand Up @@ -21,12 +21,6 @@ def template(request):
return 'tests/test-extensions/' + request.param


@pytest.fixture
def output_dir(tmpdir):
"""Fixture. Create and return custom temp directory for test."""
return str(tmpdir.mkdir('hello'))


@pytest.fixture(autouse=True)
def modify_syspath(monkeypatch):
"""Fixture. Make sure that the custom extension can be loaded."""
Expand Down
12 changes: 6 additions & 6 deletions tests/test_default_extensions.py
Expand Up @@ -17,10 +17,10 @@ def freeze():
freezer.stop()


def test_jinja2_time_extension(tmpdir):
def test_jinja2_time_extension(tmp_path):
"""Verify Jinja2 time extension work correctly."""
project_dir = cookiecutter(
'tests/test-extensions/default/', no_input=True, output_dir=str(tmpdir)
'tests/test-extensions/default/', no_input=True, output_dir=str(tmp_path)
)
changelog_file = os.path.join(project_dir, 'HISTORY.rst')
assert os.path.isfile(changelog_file)
Expand All @@ -40,19 +40,19 @@ def test_jinja2_time_extension(tmpdir):
assert expected_lines == changelog_lines


def test_jinja2_slugify_extension(tmpdir):
def test_jinja2_slugify_extension(tmp_path):
"""Verify Jinja2 slugify extension work correctly."""
project_dir = cookiecutter(
'tests/test-extensions/default/', no_input=True, output_dir=str(tmpdir)
'tests/test-extensions/default/', no_input=True, output_dir=str(tmp_path)
)

assert os.path.basename(project_dir) == "it-s-slugified-foobar"


def test_jinja2_uuid_extension(tmpdir):
def test_jinja2_uuid_extension(tmp_path):
"""Verify Jinja2 uuid extension work correctly."""
project_dir = cookiecutter(
'tests/test-extensions/default/', no_input=True, output_dir=str(tmpdir)
'tests/test-extensions/default/', no_input=True, output_dir=str(tmp_path)
)
changelog_file = os.path.join(project_dir, 'id')
assert os.path.isfile(changelog_file)
Expand Down

0 comments on commit a6113ea

Please sign in to comment.