Skip to content

Commit

Permalink
Warn user about empty namespaces
Browse files Browse the repository at this point in the history
Sometimes extension-related parameters seem to end up not persisted to
`setup.cfg`.

Issue #506, shows that it might happen for the `namespace` extension,
and via a reaction chain cause errors during updates.

The changes implemented in this commit try to prevent this reaction
chain of errors happening, while also instructing the user on how to
workaround in this situation.
  • Loading branch information
abravalheri committed Sep 30, 2021
1 parent 9b561a8 commit b02809a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/pyscaffold/extensions/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ def prepare_namespace(namespace_str: str) -> List[str]:

def enforce_namespace_options(struct: Structure, opts: ScaffoldOpts) -> ActionParams:
"""Make sure options reflect the namespace usage."""
opts.setdefault("namespace", None)
opts.setdefault("namespace", "")

if opts["namespace"]:
opts["ns_list"] = prepare_namespace(opts["namespace"])
opts["root_pkg"] = opts["ns_list"][0]
opts["qual_pkg"] = ".".join([opts["ns_list"][-1], opts["package"]])
opts["namespace"] = opts["namespace"].strip()

return struct, opts

Expand All @@ -99,7 +100,11 @@ def add_namespace(struct: Structure, opts: ScaffoldOpts) -> ActionParams:
Returns:
Directory structure as dictionary of dictionaries and input options
"""
if not opts["namespace"]:
if not opts.get("namespace"):
msg = "Using the `Namespace` extension with an empty namespace string/None. "
msg += "You can try a valid string with the `--namespace` option in `putup` "
msg += "(PyScaffold CLI) the arguments in `create_project` (PyScaffold API)."
logger.warning(msg)
return struct, opts

namespace = opts["ns_list"][-1].split(".")
Expand Down
21 changes: 20 additions & 1 deletion tests/extensions/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ def test_prepare_namespace():
prepare_namespace("com.blue-yonder")


def test_add_namespace():
def test_add_namespace(isolated_logger, caplog):
args = ["project", "-p", "package", "--namespace", "com.blue_yonder"]
opts = parse_args(args)
opts["ns_list"] = prepare_namespace(opts["namespace"])
struct = {"src": {"package": {"file1": "Content"}}}

ns_struct, _ = add_namespace(struct, opts)

ns_pkg_struct = ns_struct["src"]
assert "project" not in set(ns_pkg_struct.keys())
assert "package" not in set(ns_pkg_struct.keys())
Expand All @@ -42,6 +44,23 @@ def test_add_namespace():
submodules = set(ns_pkg_struct["com"]["blue_yonder"].keys())
assert "package" in submodules

# warnings should not be logged
log = caplog.text
unexpected_log = ("empty namespace", "valid string", "the `--namespace` option")
for text in unexpected_log:
assert text not in log


def test_add_namespace_empty(isolated_logger, caplog):
# When trying to add a namespace without actually providing a namespace string
add_namespace({}, {})

# then something should be logged,
log = caplog.text
expected_log = ("empty namespace", "valid string", "the `--namespace` option")
for text in expected_log:
assert text in log


def test_create_project_with_namespace(tmpfolder):
# Given options with the namespace extension,
Expand Down

0 comments on commit b02809a

Please sign in to comment.