Skip to content

Commit 0c6763c

Browse files
committed
get rid of multi-line inputs; use textwrap.wrap automatically
1 parent 3b1e86b commit 0c6763c

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/dotenv/main.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import sys
77
import tempfile
8+
import textwrap
89
from collections import OrderedDict
910
from contextlib import contextmanager
1011
from typing import IO, Dict, Iterable, Iterator, Mapping, Optional, Tuple, Union
@@ -419,13 +420,14 @@ def set_header(
419420
logger.info("Ignoring empty header.")
420421
return False, header
421422

422-
header = header.strip()
423-
lines = header.split("\n")
423+
header = header.replace("\n", " ")
424+
lines = textwrap.wrap(header, width=60)
424425
for i, line in enumerate(lines):
425-
if not line.startswith("# "):
426-
lines[i] = f"# {line}"
427-
header = "\n".join(lines)
426+
lines[i] = f"# {line}\n"
427+
header = "".join(lines)
428+
dest.write(header)
429+
428430
text = "".join(atom for atom in source.readlines() if not atom.startswith("#"))
429-
dest.write(f"{header}\n{text}\n")
431+
dest.write(f"{text}\n")
430432

431433
return True, header

tests/test_main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,17 +419,17 @@ def test_set_header_empty(dotenv_path, header):
419419
@pytest.mark.parametrize(
420420
"new_header, expected, expected_header, old_header, content",
421421
[
422-
("# header only", True, "# header only", "", ""),
423-
("# single-line header", True, "# single-line header", "", "a=b\nc=d"),
424-
("# multi-line\n# header", True, "# multi-line\n# header", "", "a=b\nc=d"),
422+
("single-line input", True, "# single-line input\n", "", "a=b\nc=d"),
423+
("multi-line\ninput", True, "# multi-line input\n", "", "a=b\nc=d"),
424+
("new header", True, "# new header\n", "# old header", "a=b\nc=d"),
425425
(
426-
"Single-line, no comment header",
426+
" ".join("x" * 57 for _ in range(2)),
427427
True,
428-
"# Single-line, no comment header",
428+
"".join(f"# {'x' * 57}\n" for _ in range(2)),
429429
"",
430430
"a=b",
431431
),
432-
("# New header", True, "# New header", "# Old header", "a=b\nc=d"),
432+
# ("# New header", True, "# New header", "# Old header", "a=b\nc=d"),
433433
],
434434
)
435435
def test_set_header(

0 commit comments

Comments
 (0)