From db57281c83c91345a829c60a8e81780af4b78eb8 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Wed, 27 Nov 2024 23:06:19 +0100 Subject: [PATCH] disko2: Fix dedenting bug --- src/disko_lib/logging.py | 12 +++---- tests/disko_lib/test_logging.py | 55 +++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 tests/disko_lib/test_logging.py diff --git a/src/disko_lib/logging.py b/src/disko_lib/logging.py index ac25477..c2479a8 100644 --- a/src/disko_lib/logging.py +++ b/src/disko_lib/logging.py @@ -12,13 +12,13 @@ from typing import ( ) from .ansi import Colors -from .messages.colors import RESET logging.basicConfig(format="%(message)s", level=logging.INFO) LOGGER = logging.getLogger("disko_logger") MessageTypes = Literal["bug", "error", "warning", "info", "help", "debug"] +RESET = Colors.RESET BG_COLOR_MAP = { "bug": Colors.BG_RED, "error": Colors.BG_RED, @@ -101,19 +101,19 @@ def dedent_start_lines(lines: list[str]) -> list[str]: if dedent_width == 0: return lines - match_indent_regex = re.compile(f"^ {{{dedent_width}}}") + indent_string = " " * dedent_width dedented_lines = [] stop_dedenting = False for line in lines: - if not line.startswith(" "): - stop_dedenting = True - if stop_dedenting: dedented_lines.append(line) continue - dedented_line = re.sub(match_indent_regex, "", line) + if not line.startswith(indent_string): + stop_dedenting = True + + dedented_line = re.sub(indent_string, "", line) dedented_lines.append(dedented_line) return dedented_lines diff --git a/tests/disko_lib/test_logging.py b/tests/disko_lib/test_logging.py new file mode 100644 index 0000000..afc0bf8 --- /dev/null +++ b/tests/disko_lib/test_logging.py @@ -0,0 +1,55 @@ +from disko_lib.logging import dedent_start_lines + + +def test_dedent_start_lines() -> None: + # Some later lines are indented as much or more than the first line, + # but they should NOT be dedented! + raw_lines = """ + Successfully generated config for some devices. + Errors are printed above. The generated partial config is: + { + "disko": { + "devices": { + "disk": { + "MODEL:SanDisk SD8TB8U256G1001,SN:171887425854": { + "device": "/dev/sdb", + "type": "disk", + "content": { + "type": "gpt", + "partitions": { + "UUID:01D60069CEED69C0": { + "_index": 1, + "size": "523730944", + "content": { + "type": "filesystem", + "format": "ntfs", + "mountpoint": null + } + """ + + expected_output = """Successfully generated config for some devices. +Errors are printed above. The generated partial config is: +{ + "disko": { + "devices": { + "disk": { + "MODEL:SanDisk SD8TB8U256G1001,SN:171887425854": { + "device": "/dev/sdb", + "type": "disk", + "content": { + "type": "gpt", + "partitions": { + "UUID:01D60069CEED69C0": { + "_index": 1, + "size": "523730944", + "content": { + "type": "filesystem", + "format": "ntfs", + "mountpoint": null + } + """ + + lines = raw_lines.splitlines()[1:] + result = "\n".join(dedent_start_lines(lines)) + + assert result == expected_output