mirror of
https://github.com/nix-community/disko.git
synced 2026-11-30 21:13:28 +01:00
disko2: Fix dedenting bug
This commit is contained in:
1 parent
cfcc518b70
commit
db57281c83
2 files changed
+61
-6
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user