Skip to content

Commit

Permalink
fix: correctly represent yaml dumped multiline strings
Browse files Browse the repository at this point in the history
Add a representer as we do for other craft applications to correctly
dump multi line strings.
  • Loading branch information
sergiusens committed Sep 6, 2023
1 parent 0d5ef51 commit 51346d9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
16 changes: 15 additions & 1 deletion craft_application/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""Base pydantic model for *craft applications."""
from __future__ import annotations

from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Type, cast

import pydantic
import yaml
Expand All @@ -34,6 +34,17 @@ def _alias_generator(s: str) -> str:
return s.replace("_", "-")


# pyright: reportUnknownMemberType=false
# Type of "represent_scalar" is "(tag: str, value: Unknown, style: str | None = None)
# ->
# ScalarNode" (reportUnknownMemberType)
def _repr_str(dumper: yaml.Dumper, data: str) -> yaml.ScalarNode:
"""Multi-line string representer for the YAML dumper."""
if "\n" in data:
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
return dumper.represent_scalar("tag:yaml.org,2002:str", data)


class CraftBaseConfig(pydantic.BaseConfig): # pylint: disable=too-few-public-methods
"""Pydantic model configuration."""

Expand Down Expand Up @@ -83,4 +94,7 @@ def from_yaml_file(cls, path: pathlib.Path) -> Self:
def to_yaml_file(self, path: pathlib.Path) -> None:
"""Write this model to a YAML file."""
with path.open("wt") as file:
yaml.add_representer(
str, _repr_str, Dumper=cast(Type[yaml.Dumper], yaml.SafeDumper)
)
yaml.safe_dump(self.marshal(), file)
4 changes: 3 additions & 1 deletion tests/unit/models/project_models/full_project.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
base: core24
contact: [email protected]
description: A fully-defined craft-application project. (description)
description: |
A fully-defined craft-application project.
With more than one line.
issues: https://github.com/canonical/craft-application/issues
license: LGPLv3
name: full-project
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/models/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Tests for BaseProject"""
import pathlib
from textwrap import dedent
from typing import Optional

import pytest
Expand Down Expand Up @@ -44,14 +45,19 @@
issues="https://github.com/canonical/craft-application/issues",
source_code="https://github.com/canonical/craft-application", # pyright: ignore[reportGeneralTypeIssues]
summary="A fully-defined craft-application project.", # pyright: ignore[reportGeneralTypeIssues]
description="A fully-defined craft-application project. (description)",
description="A fully-defined craft-application project.\nWith more than one line.\n",
license="LGPLv3",
parts=PARTS_DICT,
)
FULL_PROJECT_DICT = {
"base": "core24",
"contact": "[email protected]",
"description": "A fully-defined craft-application project. (description)",
"description": dedent(
"""\
A fully-defined craft-application project.
With more than one line.
"""
),
"issues": "https://github.com/canonical/craft-application/issues",
"license": "LGPLv3",
"name": "full-project",
Expand Down

0 comments on commit 51346d9

Please sign in to comment.