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

fix(reactive): don't overwrite actions.yaml for reactive charms #1829

Merged
merged 2 commits into from
Aug 26, 2024
Merged
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
11 changes: 8 additions & 3 deletions charmcraft/services/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,17 @@ def write_metadata(self, path: pathlib.Path) -> None:

# If there is a reactive part, defer to it for the existence of metadata.yaml.
plugins = {part.get("plugin") or name for name, part in self._project.parts.items()}
is_reactive = "reactive" in plugins
stage_dir = self._services.lifecycle.project_info.dirs.stage_dir
if "reactive" in plugins and (stage_dir / const.METADATA_FILENAME).exists():
emit.debug("'metadata.yaml' generated by charm, not using original project metadata.")
if is_reactive and (stage_dir / const.METADATA_FILENAME).exists():
emit.debug(
f"{const.METADATA_FILENAME!r} generated by charm. Not using original project metadata."
)
else:
self._write_file_or_object(self.metadata.marshal(), const.METADATA_FILENAME, path)
if actions := cast(dict | None, project_dict.get("actions")):
if is_reactive and (stage_dir / const.JUJU_ACTIONS_FILENAME).exists():
emit.debug(f"{const.JUJU_ACTIONS_FILENAME!r} generated by charm. Skipping generation.")
elif actions := cast(dict | None, project_dict.get("actions")):
self._write_file_or_object(actions, "actions.yaml", path)
if config := cast(dict | None, project_dict.get("config")):
self._write_file_or_object(config, "config.yaml", path)
23 changes: 21 additions & 2 deletions tests/unit/services/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,36 @@ def test_do_not_overwrite_metadata_yaml(
fake_prime_dir.mkdir()
fake_stage_dir = fake_path / "stage"
fake_stage_dir.mkdir()
fake_staged_metadata = fake_stage_dir / "metadata.yaml"
fake_staged_metadata = fake_stage_dir / const.METADATA_FILENAME
fake_staged_metadata.touch()
package_service._project.parts["reactive"] = {"source": "."}

package_service.write_metadata(fake_prime_dir)

emitter.assert_debug(
"'metadata.yaml' generated by charm, not using original project metadata."
"'metadata.yaml' generated by charm. Not using original project metadata."
)


def test_do_not_overwrite_actions_yaml(
emitter: craft_cli.pytest_plugin.RecordingEmitter,
fake_path,
package_service,
simple_charm,
):
fake_prime_dir = fake_path / "prime"
fake_prime_dir.mkdir()
fake_stage_dir = fake_path / "stage"
fake_stage_dir.mkdir()
fake_staged_actions = fake_stage_dir / const.JUJU_ACTIONS_FILENAME
fake_staged_actions.touch()
package_service._project.parts["reactive"] = {"source": "."}

package_service.write_metadata(fake_prime_dir)

emitter.assert_debug("'actions.yaml' generated by charm. Skipping generation.")


# region Tests for getting bases for manifest.yaml
@pytest.mark.parametrize(
("bases", "build_item", "expected"),
Expand Down
Loading