Skip to content

Commit

Permalink
Merge 0c3a578 into a288bfb
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Jul 9, 2019
2 parents a288bfb + 0c3a578 commit 4db7a11
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
43 changes: 34 additions & 9 deletions src/pyscaffoldext/custom_extension/extension.py
Expand Up @@ -3,9 +3,11 @@
Main logic to create custom extensions
"""
import argparse

from pyscaffold.api import Extension, helpers
from pyscaffold.extensions.no_skeleton import NoSkeleton
from pyscaffold.api.helpers import logger
from pyscaffold.extensions.namespace import Namespace
from pyscaffold.extensions.no_skeleton import NoSkeleton
from pyscaffold.extensions.pre_commit import PreCommit
from pyscaffold.extensions.tox import Tox
from pyscaffold.extensions.travis import Travis
Expand All @@ -18,12 +20,12 @@
EXTENSION_FILE_NAME = "extension"


class InvalidProjectNameException(RuntimeError):
class InvalidProjectNameWarning(RuntimeWarning):
"""Project name does not comply with convention of an extension"""

DEFAULT_MESSAGE = (
"An extension project name should start with "
"``pyscaffoldext-`` or use ``--force`` to overwrite.")
"The prefix ``pyscaffoldext-`` will be added to the project name. "
"If that is not your intention, please use ``--force`` to overwrite.")

def __init__(self, message=DEFAULT_MESSAGE, *args, **kwargs):
super().__init__(message, *args, **kwargs)
Expand Down Expand Up @@ -87,6 +89,12 @@ def activate(self, actions):
Returns:
list: updated list of actions
"""
actions = self.register(
actions,
check_project_name,
before='get_default_options'
)

actions = self.register(
actions,
add_extension_namespace,
Expand All @@ -95,7 +103,7 @@ def activate(self, actions):

actions = self.register(
actions,
check_project_name,
check_package_name,
after='get_default_options'
)

Expand Down Expand Up @@ -346,9 +354,7 @@ def add_test_custom_extension(struct, opts):
def check_project_name(struct, opts):
"""Enforce the naming convention of PyScaffold extensions
The project name must start with 'pyscaffoldext-' and
the package name shouldn't contain the redundant
'pyscaffoldext_' in the beginning of the name.
The project name must start with 'pyscaffoldext-'.
Args:
struct (dict): project representation as (possibly) nested
Expand All @@ -360,8 +366,27 @@ def check_project_name(struct, opts):
struct, opts: updated project representation and options
"""
if not opts["project"].startswith("pyscaffoldext-") and not opts["force"]:
raise InvalidProjectNameException
logger.warning(InvalidProjectNameWarning())
opts["project"] = "pyscaffoldext-" + opts["project"]

return struct, opts


def check_package_name(struct, opts):
"""Enforce the naming convention of PyScaffold extensions
The package name shouldn't contain the redundant
'pyscaffoldext_' in the beginning of the name.
Args:
struct (dict): project representation as (possibly) nested
:obj:`dict`.
opts (dict): given options, see :obj:`create_project` for
an extensive list.
Returns:
struct, opts: updated project representation and options
"""
if opts["package"].startswith("pyscaffoldext_"):
opts["package"] = opts["package"].replace("pyscaffoldext_", "")

Expand Down
Expand Up @@ -7,7 +7,7 @@ ${description}
Usage
=====

Just install this package with ``pip install ${project}`` and note that ``putup -h`` shows a new option ``--${project}``. Use this flag to ...
Just install this package with ``pip install ${project}`` and note that ``putup -h`` shows a new option ``--${package}``. Use this flag to ...


Note
Expand Down
15 changes: 15 additions & 0 deletions tests/test_generated_extension.py
Expand Up @@ -20,3 +20,18 @@ def test_generated_extension(tmpfolder, venv_run):

with chdir("the_actual_project"):
assert '' == venv_run("flake8")


def test_generated_extension_without_prefix(tmpfolder, caplog):
# Ensure prefix is added by default
args = ["--custom-extension", "some_extension"]

opts = parse_args(args)
opts = process_opts(opts)
create_project(opts)
assert path_exists("pyscaffoldext-some_extension")

# Ensure an explanation on how to use
# `--force` to avoid preffixing is given
logs = caplog.text
assert '--force' in logs

0 comments on commit 4db7a11

Please sign in to comment.