Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check charmcraft.yaml for charm name #1974

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions charmcraft/application/commands/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ def run(self, parsed_args):
"characters and underscore, starting with alpha."
)

charm_name = self._services.project.name or utils.get_name_from_metadata()
charm_name = self._services.project.name or utils.get_name_from_yaml()
if charm_name is None:
raise CraftError(
"Cannot find a valid charm name in charm definition. "
Expand Down Expand Up @@ -1460,7 +1460,7 @@ def fill_parser(self, parser):

def run(self, parsed_args):
"""Run the command."""
charm_name = self._services.project.name or utils.get_name_from_metadata()
charm_name = self._services.project.name or utils.get_name_from_yaml()
if charm_name is None:
raise CraftError(
"Cannot find a valid charm name in charm definition. "
Expand Down Expand Up @@ -1873,7 +1873,7 @@ def run(self, parsed_args):
if parsed_args.name:
charm_name = parsed_args.name
else:
charm_name = utils.get_name_from_metadata()
charm_name = utils.get_name_from_yaml()
if charm_name is None:
raise CraftError(
"Can't access name in 'metadata.yaml' file. The 'list-lib' command must "
Expand Down
4 changes: 2 additions & 2 deletions charmcraft/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
LibData,
LibInternals,
QualifiedLibraryName,
get_name_from_metadata,
get_name_from_yaml,
create_charm_name_from_importable,
create_importable_name,
get_lib_internals,
Expand Down Expand Up @@ -80,7 +80,7 @@
"LibData",
"LibInternals",
"QualifiedLibraryName",
"get_name_from_metadata",
"get_name_from_yaml",
"create_charm_name_from_importable",
"create_importable_name",
"get_lib_internals",
Expand Down
18 changes: 9 additions & 9 deletions charmcraft/utils/charmlibs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
from dataclasses import dataclass
from typing import overload

import yaml
from craft_cli import CraftError
from typing_extensions import Self

from charmcraft import const, errors
from charmcraft.utils.yaml import load_yaml


@dataclass(frozen=True)
Expand Down Expand Up @@ -76,15 +76,15 @@ def __str__(self) -> str:
return f"{create_charm_name_from_importable(self.charm_name)}.{self.lib_name}"


def get_name_from_metadata() -> str | None:
def get_name_from_yaml() -> str | None:
"""Return the name if present and plausible in metadata.yaml."""
try:
with open(const.METADATA_FILENAME, "rb") as fh:
metadata = yaml.safe_load(fh)
charm_name = metadata["name"]
except (yaml.error.YAMLError, OSError, KeyError):
return None
return charm_name
charmcraft_yaml = load_yaml(pathlib.Path(const.CHARMCRAFT_FILENAME))
if charmcraft_yaml and "name" in charmcraft_yaml:
return charmcraft_yaml.get("name")
metadata_yaml = load_yaml(pathlib.Path(const.METADATA_FILENAME))
if metadata_yaml:
return metadata_yaml.get("name")
return None


def create_importable_name(charm_name: str) -> str:
Expand Down
56 changes: 42 additions & 14 deletions tests/unit/utils/test_charmlibs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
get_lib_module_name,
get_lib_path,
get_libs_from_tree,
get_name_from_metadata,
get_name_from_yaml,
)


Expand Down Expand Up @@ -64,49 +64,77 @@ def test_qualified_library_name_from_string_error(value: str):


# region Name-related tests
def test_get_name_from_metadata_ok(tmp_path, monkeypatch):
@pytest.mark.parametrize(
("file_name"),
[const.METADATA_FILENAME, const.CHARMCRAFT_FILENAME],
)
def test_get_name_from_yaml_ok(tmp_path, monkeypatch, file_name):
"""The metadata file is valid yaml, but there is no name."""
monkeypatch.chdir(tmp_path)

# put a valid yaml
yaml_file = tmp_path / file_name
with yaml_file.open("wb") as fh:
fh.write(b"name: test-name")

result = get_name_from_yaml()
assert result == "test-name"


def test_get_name_from_yaml_both_exist_metadata_has_name(tmp_path, monkeypatch):
"""The metadata file is valid yaml, but there is no name."""
monkeypatch.chdir(tmp_path)

# put a valid metadata
# put a valid yaml, but name is in metadata.yaml
charmcraft_file = tmp_path / const.CHARMCRAFT_FILENAME
with charmcraft_file.open("wb") as fh:
fh.write(b"notname: test-name")
metadata_file = tmp_path / const.METADATA_FILENAME
with metadata_file.open("wb") as fh:
fh.write(b"name: test-name")

result = get_name_from_metadata()
result = get_name_from_yaml()
assert result == "test-name"


def test_get_name_from_metadata_no_file(tmp_path, monkeypatch):
def test_get_name_from_yaml_no_file(tmp_path, monkeypatch):
"""No metadata file to get info."""
monkeypatch.chdir(tmp_path)
result = get_name_from_metadata()
result = get_name_from_yaml()
assert result is None


def test_get_name_from_metadata_bad_content_garbage(tmp_path, monkeypatch):
@pytest.mark.parametrize(
("file_name"),
[const.METADATA_FILENAME, const.CHARMCRAFT_FILENAME],
)
def test_get_name_from_yaml_bad_content_garbage(tmp_path, monkeypatch, file_name):
"""The metadata file is broken."""
monkeypatch.chdir(tmp_path)

# put a broken metadata
metadata_file = tmp_path / const.METADATA_FILENAME
# put a broken yaml
metadata_file = tmp_path / file_name
with metadata_file.open("wb") as fh:
fh.write(b"\b00\bff -- not a really yaml stuff")

result = get_name_from_metadata()
result = get_name_from_yaml()
assert result is None


def test_get_name_from_metadata_bad_content_no_name(tmp_path, monkeypatch):
@pytest.mark.parametrize(
("file_name"),
[const.METADATA_FILENAME, const.CHARMCRAFT_FILENAME],
)
def test_get_name_from_yaml_bad_content_no_name(tmp_path, monkeypatch, file_name):
"""The metadata file is valid yaml, but there is no name."""
monkeypatch.chdir(tmp_path)

# put a broken metadata
metadata_file = tmp_path / const.METADATA_FILENAME
# put a broken yaml
metadata_file = tmp_path / file_name
with metadata_file.open("wb") as fh:
fh.write(b"{}")

result = get_name_from_metadata()
result = get_name_from_yaml()
assert result is None


Expand Down
Loading