Skip to content

Commit

Permalink
[1518] Directory Name Render and Create Fix (#1991)
Browse files Browse the repository at this point in the history
* Raise warning when directory name is empty

* Adding EmptyDirNameException for situation in render_and_create_dir where dirname is empty

* Include new exception type in cli.py

* Fixing linting issues and adding in test case for render_and_create_dir

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Rafya Khan <rafya.khan@mail.utoronto.ca>
Co-authored-by: Jens W. Klein <jk@kleinundpartner.at>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Fábio C. Barrionuevo da Luz <bnafta@gmail.com>
  • Loading branch information
5 people committed Mar 20, 2024
1 parent b749744 commit 9f94bce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cookiecutter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cookiecutter.config import get_user_config
from cookiecutter.exceptions import (
ContextDecodingException,
EmptyDirNameException,
FailedHookException,
InvalidModeException,
InvalidZipRepository,
Expand Down Expand Up @@ -219,6 +220,7 @@ def main(
except (
ContextDecodingException,
OutputDirExistsException,
EmptyDirNameException,
InvalidModeException,
FailedHookException,
UnknownExtension,
Expand Down
8 changes: 8 additions & 0 deletions cookiecutter/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ class OutputDirExistsException(CookiecutterException):
"""


class EmptyDirNameException(CookiecutterException):
"""
Exception for a empty directory name.
Raised when the directory name provided is empty.
"""


class InvalidModeException(CookiecutterException):
"""
Exception for incompatible modes.
Expand Down
5 changes: 5 additions & 0 deletions cookiecutter/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from cookiecutter.exceptions import (
ContextDecodingException,
EmptyDirNameException,
OutputDirExistsException,
UndefinedVariableInTemplate,
)
Expand Down Expand Up @@ -252,6 +253,10 @@ def render_and_create_dir(
overwrite_if_exists: bool = False,
) -> tuple[Path, bool]:
"""Render name of a directory, create the directory, return its path."""
if not dirname or dirname == "":
msg = 'Error: directory name is empty'
raise EmptyDirNameException(msg)

name_tmpl = environment.from_string(dirname)
rendered_dirname = name_tmpl.render(**context)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_generate_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,15 @@ def test_raise_undefined_variable_project_dir(tmp_path) -> None:
assert error.context == {}

assert not Path(tmp_path, 'testproject').exists()


def test_raise_empty_dir_name(output_dir, undefined_context):
"""Verify correct error raised when directory name is empty."""
with pytest.raises(exceptions.EmptyDirNameException):
generate.render_and_create_dir(
dirname='',
output_dir=output_dir,
context=undefined_context,
environment=None,
)
assert not Path(output_dir).joinpath('testproject').exists()

0 comments on commit 9f94bce

Please sign in to comment.