diff --git a/src/ops2deb/formatter.py b/src/ops2deb/formatter.py index e618fda..a8770d2 100644 --- a/src/ops2deb/formatter.py +++ b/src/ops2deb/formatter.py @@ -28,8 +28,9 @@ def sort_blueprints(blueprints: List[Blueprint]) -> List[Blueprint]: def format_description(description: str) -> str: lines: List[str] = [] + description = description.strip("\n ") for line in description.split("\n"): - lines.extend(wrap(line, width=79)) + lines.extend(wrap(line, width=79) or [""]) return "\n".join(lines) diff --git a/tests/test_formatter.py b/tests/test_formatter.py index 85accd9..173fe01 100644 --- a/tests/test_formatter.py +++ b/tests/test_formatter.py @@ -1,6 +1,8 @@ +import pytest + from ops2deb.formatter import format_description -description_with_empty_line = """\ +description_with_empty_line = """ This thing does the following: - It does this @@ -12,16 +14,30 @@ """ -def test_format_description_should_remove_empty_lines(): +def test_format_description_should_only_remove_empty_lines_at_start_or_end(): result = format_description(description_with_empty_line) - assert "" not in result.split("\n") + assert result[0] != "\n" + assert result[-1] != "\n" + assert "\n" in result -def test_format_description_should_be_idempotent(): - result = format_description(description_with_empty_line) - assert format_description(result) == result +def test_format_description_should_remove_trailing_spaces(): + lines = description_with_empty_line.split("\n") + lines = [line + " " for line in lines] + description_with_trailing_spaces = "\n".join(lines) + result = format_description(description_with_trailing_spaces).split("\n") + assert result[0][-1] == ":" + assert result[1] == "" def test_format_description_should_wrap_long_lines(): result = format_description(description_with_long_line) assert len(result.split("\n")) == 2 + + +@pytest.mark.parametrize( + "description", [description_with_empty_line, description_with_long_line] +) +def test_format_description_should_be_idempotent(description): + result = format_description(description) + assert format_description(result) == result