fix(install): preserve line endings of CLAUDE.md, AGENTS.md and siblings (#4360)
#3741 stopped the always-on and Gemini writers from translating line endings
on write (newline=""), but every one of these paths still READ through
Path.read_text, which opens in universal-newline mode and folds CRLF to LF
before the block is merged. A later write then re-emitted the platform
default, so a read-modify-write normalised the WHOLE file: on Windows an LF
file gained CRLF on every line, on Linux a CRLF file lost its CR.
Install and uninstall now go through one shared read/write pair
(_read_keep_newline / _write_keep_newline): the file's own dominant ending is
detected on read, the section helpers keep working on bare-LF text, and the
ending is restored on write with newline="". A file with no prior convention
is written LF, matching the other writers. This covers claude_install,
_agents_install, codebuddy_install, gemini_install/_uninstall,
vscode_install/_uninstall, _strip_graphify_md_section, _agents_uninstall,
codebuddy_uninstall and _remove_claude_skill_registration, plus the two
#3741 paths so the read side can no longer drift from the write side.
Adds a round-trip test per writer: an LF file and a CRLF file in, same
endings out, and install-then-uninstall returns the exact original bytes.
(cherry picked from commit df5f032a62)
This commit is contained in:
1 parent
c3d537ae4a
commit
d2c1592eab
2 files changed
+264
-39
No files matched your search
+76
-39
@@ -411,7 +411,7 @@ def _remove_claude_skill_registration(project_dir: Path) -> None:
|
||||
claude_md = project_dir / ".claude" / "CLAUDE.md"
|
||||
if not claude_md.exists():
|
||||
return
|
||||
content = claude_md.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(claude_md)
|
||||
# Match the exact H1 `# graphify` registration heading, never a substring of a
|
||||
# user's `## graphify`/`### graphify` (#2062). Section runs to the next H1.
|
||||
# _SKILL_REGISTRATION_MARKER is the single source of truth for this heading,
|
||||
@@ -422,9 +422,9 @@ def _remove_claude_skill_registration(project_dir: Path) -> None:
|
||||
return
|
||||
# A symlink stays: writing through it cleans the file it points to.
|
||||
if cleaned or claude_md.is_symlink():
|
||||
# newline="" so the rest of the file's own line endings are never
|
||||
# translated on write (#3668, same CRLF issue as the insert side).
|
||||
claude_md.write_text(cleaned + "\n", encoding="utf-8", newline="")
|
||||
# The file's own line ending is restored, so the rest of its content is
|
||||
# never re-translated on write (#3668/#4360).
|
||||
_write_keep_newline(claude_md, cleaned + "\n", newline)
|
||||
print(f" CLAUDE.md -> graphify skill registration removed from {_shown_path(claude_md)}")
|
||||
else:
|
||||
claude_md.unlink()
|
||||
@@ -571,10 +571,10 @@ def _register_always_on_block(target: Path, prefix: str, registration: str) -> N
|
||||
of the bare "graphify" substring check silently treating any unrelated
|
||||
mention of the word as already-registered and skipping every re-run (#3668).
|
||||
|
||||
Written with newline="" so an existing file's own line endings are never
|
||||
translated -- Path.write_text otherwise opens in text mode, which on Windows
|
||||
turns the WHOLE file's pre-existing bare-LF content into CRLF just to append a
|
||||
few lines (#3668).
|
||||
Written through the shared _read_keep_newline/_write_keep_newline pair, so an
|
||||
existing file's own line endings survive the read-modify-write: a plain
|
||||
read_text/write_text round-trip turns a bare-LF file into CRLF on Windows (and
|
||||
a CRLF file into LF elsewhere) just to append a few lines (#3668/#4360).
|
||||
|
||||
The skill files are copied before this runs, so a *target* that cannot be
|
||||
read or written must not abort an otherwise-complete install (#3474). That
|
||||
@@ -584,18 +584,18 @@ def _register_always_on_block(target: Path, prefix: str, registration: str) -> N
|
||||
"""
|
||||
try:
|
||||
existed = target.exists()
|
||||
content = target.read_text(encoding="utf-8") if existed else ""
|
||||
content, newline = _read_keep_newline(target) if existed else ("", "\n")
|
||||
new_content = _replace_or_append_section(
|
||||
content, _SKILL_REGISTRATION_MARKER, registration, boundary_prefix="# "
|
||||
)
|
||||
if existed and new_content == content:
|
||||
print(f"{prefix}already registered (no change)")
|
||||
elif existed:
|
||||
target.write_text(new_content, encoding="utf-8", newline="")
|
||||
_write_keep_newline(target, new_content, newline)
|
||||
print(f"{prefix}skill registered in {_shown_path(target)}")
|
||||
else:
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_text(new_content, encoding="utf-8", newline="")
|
||||
_write_keep_newline(target, new_content, newline)
|
||||
print(f"{prefix}created at {_shown_path(target)}")
|
||||
except OSError as exc:
|
||||
print(f"{prefix}skipped: {exc.__class__.__name__}: {exc}", file=sys.stderr)
|
||||
@@ -848,6 +848,40 @@ def _remove_marker_section(content: str, marker: str, boundary_prefix: str = "##
|
||||
return "\n".join(lines).rstrip()
|
||||
|
||||
|
||||
def _read_keep_newline(path: Path) -> "tuple[str, str]":
|
||||
"""Read *path* as UTF-8 and return ``(text, newline)``.
|
||||
|
||||
``text`` uses bare ``\n`` internally, so the section helpers
|
||||
(``_replace_or_append_section`` / ``_remove_marker_section``) work on it
|
||||
unchanged, and ``newline`` is the file's own dominant line ending (``"\r\n"``
|
||||
or ``"\n"``) so a later write can put it back.
|
||||
|
||||
``Path.read_text`` opens in universal-newline mode and folds every ``\r\n``
|
||||
to ``\n``, so the file's convention is lost before the write side ever sees
|
||||
it; a plain ``write_text`` then re-emits the platform default and rewrites
|
||||
the WHOLE file's endings, turning an LF file into CRLF on Windows (or a CRLF
|
||||
file into LF on Linux) just to merge in a few lines (#4360). Reading the bytes
|
||||
and normalising here keeps the original ending to restore.
|
||||
"""
|
||||
raw = path.read_bytes().decode("utf-8")
|
||||
newline = "\r\n" if "\r\n" in raw else "\n"
|
||||
return raw.replace("\r\n", "\n").replace("\r", "\n"), newline
|
||||
|
||||
|
||||
def _write_keep_newline(path: Path, text: str, newline: str = "\n") -> None:
|
||||
"""Write bare-``\n`` *text* using *newline* endings, without translation.
|
||||
|
||||
``newline=""`` stops ``write_text`` from translating on the way out; the one
|
||||
conversion from bare ``\n`` to the file's own ending happens here, so every
|
||||
install/uninstall writer shares a single line-ending contract (#4360). A file
|
||||
with no prior convention (a fresh install) is written with plain LF, matching
|
||||
what the other install writers already emit.
|
||||
"""
|
||||
if newline != "\n":
|
||||
text = text.replace("\n", newline)
|
||||
path.write_text(text, encoding="utf-8", newline="")
|
||||
|
||||
|
||||
_BANNER_LOGO = """\
|
||||
▄▄▀█▀▄▄
|
||||
▄▄▀▀ ▄█▄ ▀▀▄▄
|
||||
@@ -997,20 +1031,20 @@ def gemini_install(project_dir: Path | None = None, *, project: bool = False) ->
|
||||
target = project_dir / "GEMINI.md"
|
||||
|
||||
if target.exists():
|
||||
content = target.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(target)
|
||||
new_content = _replace_or_append_section(
|
||||
content, _GEMINI_MD_MARKER, _always_on("gemini-md")
|
||||
)
|
||||
else:
|
||||
content, newline = "", "\n"
|
||||
new_content = _always_on("gemini-md")
|
||||
|
||||
if target.exists() and new_content == target.read_text(encoding="utf-8"):
|
||||
if target.exists() and new_content == content:
|
||||
print(f"graphify already configured in {target.resolve()} (no change)")
|
||||
else:
|
||||
# newline="" so an existing file's own line endings are never translated
|
||||
# (Path.write_text otherwise opens in text mode, which on Windows turns
|
||||
# the WHOLE file's pre-existing bare-LF content into CRLF, #3668).
|
||||
target.write_text(new_content, encoding="utf-8", newline="")
|
||||
# The file's own line ending is restored on write, so its existing
|
||||
# content is never re-translated (#3668/#4360).
|
||||
_write_keep_newline(target, new_content, newline)
|
||||
print(f"graphify section written to {target.resolve()}")
|
||||
|
||||
# Always re-install the Gemini hook so an older payload (e.g. pre-issue-#580
|
||||
@@ -1115,14 +1149,14 @@ def gemini_uninstall(project_dir: Path | None = None, *, project: bool = False,
|
||||
if not target.exists():
|
||||
print("No GEMINI.md found in current directory - nothing to do")
|
||||
return
|
||||
content = target.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(target)
|
||||
cleaned = _remove_marker_section(content, _GEMINI_MD_MARKER)
|
||||
if cleaned is None:
|
||||
print("graphify section not found in GEMINI.md - nothing to do")
|
||||
return
|
||||
# A symlink stays: writing through it cleans the file it points to.
|
||||
if cleaned or target.is_symlink():
|
||||
target.write_text(cleaned + "\n", encoding="utf-8")
|
||||
_write_keep_newline(target, cleaned + "\n", newline)
|
||||
print(f"graphify section removed from {target.resolve()}")
|
||||
else:
|
||||
target.unlink()
|
||||
@@ -1172,17 +1206,17 @@ def vscode_install(project_dir: Path | None = None) -> None:
|
||||
instructions = (project_dir or Path(".")) / ".github" / "copilot-instructions.md"
|
||||
instructions.parent.mkdir(parents=True, exist_ok=True)
|
||||
if instructions.exists():
|
||||
content = instructions.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(instructions)
|
||||
new_content = _replace_or_append_section(
|
||||
content, _VSCODE_INSTRUCTIONS_MARKER, _always_on("vscode-instructions")
|
||||
)
|
||||
if new_content == content:
|
||||
print(f" {instructions} -> already configured (no change)")
|
||||
else:
|
||||
instructions.write_text(new_content, encoding="utf-8")
|
||||
_write_keep_newline(instructions, new_content, newline)
|
||||
print(f" {instructions} -> graphify section {'updated' if _VSCODE_INSTRUCTIONS_MARKER in content else 'added'}")
|
||||
else:
|
||||
instructions.write_text(_always_on("vscode-instructions"), encoding="utf-8")
|
||||
_write_keep_newline(instructions, _always_on("vscode-instructions"))
|
||||
print(f" {instructions} -> created")
|
||||
|
||||
print()
|
||||
@@ -1216,13 +1250,13 @@ def vscode_uninstall(project_dir: Path | None = None) -> None:
|
||||
instructions = (project_dir or Path(".")) / ".github" / "copilot-instructions.md"
|
||||
if not instructions.exists():
|
||||
return
|
||||
content = instructions.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(instructions)
|
||||
cleaned = _remove_marker_section(content, _VSCODE_INSTRUCTIONS_MARKER)
|
||||
if cleaned is None:
|
||||
return
|
||||
# A symlink stays: writing through it cleans the file it points to.
|
||||
if cleaned or instructions.is_symlink():
|
||||
instructions.write_text(cleaned + "\n", encoding="utf-8")
|
||||
_write_keep_newline(instructions, cleaned + "\n", newline)
|
||||
print(f" graphify section removed from {instructions}")
|
||||
else:
|
||||
instructions.unlink()
|
||||
@@ -1865,17 +1899,18 @@ def _agents_install(project_dir: Path, platform: str, project: bool = False) ->
|
||||
target = (project_dir or Path(".")) / "AGENTS.md"
|
||||
|
||||
if target.exists():
|
||||
content = target.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(target)
|
||||
new_content = _replace_or_append_section(
|
||||
content, _AGENTS_MD_MARKER, _always_on("agents-md")
|
||||
)
|
||||
else:
|
||||
content, newline = "", "\n"
|
||||
new_content = _always_on("agents-md")
|
||||
|
||||
if target.exists() and new_content == target.read_text(encoding="utf-8"):
|
||||
if target.exists() and new_content == content:
|
||||
print(f"graphify already configured in {target.resolve()} (no change)")
|
||||
else:
|
||||
target.write_text(new_content, encoding="utf-8")
|
||||
_write_keep_newline(target, new_content, newline)
|
||||
print(f"graphify section written to {target.resolve()}")
|
||||
|
||||
if platform == "codex":
|
||||
@@ -2037,7 +2072,7 @@ def _agents_uninstall(project_dir: Path, platform: str = "") -> None:
|
||||
_uninstall_kilo_plugin(project_dir or Path("."))
|
||||
return
|
||||
|
||||
content = target.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(target)
|
||||
cleaned = _remove_marker_section(content, _AGENTS_MD_MARKER)
|
||||
if cleaned is None:
|
||||
print("graphify section not found in AGENTS.md - nothing to do")
|
||||
@@ -2049,7 +2084,7 @@ def _agents_uninstall(project_dir: Path, platform: str = "") -> None:
|
||||
|
||||
# A symlink stays: writing through it cleans the file it points to.
|
||||
if cleaned or target.is_symlink():
|
||||
target.write_text(cleaned + "\n", encoding="utf-8")
|
||||
_write_keep_newline(target, cleaned + "\n", newline)
|
||||
print(f"graphify section removed from {target.resolve()}")
|
||||
else:
|
||||
target.unlink()
|
||||
@@ -2103,17 +2138,18 @@ def claude_install(project_dir: Path | None = None, strict: bool = False, projec
|
||||
target = (project_dir or Path(".")) / "CLAUDE.md"
|
||||
|
||||
if target.exists():
|
||||
content = target.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(target)
|
||||
new_content = _replace_or_append_section(
|
||||
content, _CLAUDE_MD_MARKER, _always_on("claude-md")
|
||||
)
|
||||
else:
|
||||
content, newline = "", "\n"
|
||||
new_content = _always_on("claude-md")
|
||||
|
||||
if target.exists() and new_content == target.read_text(encoding="utf-8"):
|
||||
if target.exists() and new_content == content:
|
||||
print(f"graphify already configured in {target.resolve()} (no change)")
|
||||
else:
|
||||
target.write_text(new_content, encoding="utf-8")
|
||||
_write_keep_newline(target, new_content, newline)
|
||||
print(f"graphify section written to {target.resolve()}")
|
||||
|
||||
# Always re-install the Claude Code PreToolUse hook so an old hook
|
||||
@@ -2303,7 +2339,7 @@ def _strip_graphify_md_section(target: Path) -> bool:
|
||||
remains after removal.
|
||||
"""
|
||||
try:
|
||||
content = target.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(target)
|
||||
except (OSError, UnicodeDecodeError):
|
||||
# An unreadable/undecodable CLAUDE.md-style file (e.g. non-UTF-8, or a
|
||||
# directory of that name) must not abort uninstall - nothing to strip.
|
||||
@@ -2315,7 +2351,7 @@ def _strip_graphify_md_section(target: Path) -> bool:
|
||||
return False
|
||||
# A symlink stays: writing through it cleans the file it points to.
|
||||
if cleaned or target.is_symlink():
|
||||
target.write_text(cleaned + "\n", encoding="utf-8")
|
||||
_write_keep_newline(target, cleaned + "\n", newline)
|
||||
print(f"graphify section removed from {target.resolve()}")
|
||||
else:
|
||||
target.unlink()
|
||||
@@ -2327,17 +2363,18 @@ def codebuddy_install(project_dir: Path | None = None) -> None:
|
||||
target = (project_dir or Path(".")) / "CODEBUDDY.md"
|
||||
|
||||
if target.exists():
|
||||
content = target.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(target)
|
||||
new_content = _replace_or_append_section(
|
||||
content, _CODEBUDDY_MD_MARKER, _always_on("claude-md")
|
||||
)
|
||||
else:
|
||||
content, newline = "", "\n"
|
||||
new_content = _always_on("claude-md")
|
||||
|
||||
if target.exists() and new_content == target.read_text(encoding="utf-8"):
|
||||
if target.exists() and new_content == content:
|
||||
print(f"graphify already configured in {target.resolve()} (no change)")
|
||||
else:
|
||||
target.write_text(new_content, encoding="utf-8")
|
||||
_write_keep_newline(target, new_content, newline)
|
||||
print(f"graphify section written to {target.resolve()}")
|
||||
|
||||
# Also write CodeBuddy PreToolUse hook to .codebuddy/settings.json
|
||||
@@ -2404,7 +2441,7 @@ def codebuddy_uninstall(project_dir: Path | None = None, *, project: bool = Fals
|
||||
print("No CODEBUDDY.md found in current directory - nothing to do")
|
||||
return
|
||||
|
||||
content = target.read_text(encoding="utf-8")
|
||||
content, newline = _read_keep_newline(target)
|
||||
cleaned = _remove_marker_section(content, _CODEBUDDY_MD_MARKER)
|
||||
if cleaned is None:
|
||||
print("graphify section not found in CODEBUDDY.md - nothing to do")
|
||||
@@ -2412,7 +2449,7 @@ def codebuddy_uninstall(project_dir: Path | None = None, *, project: bool = Fals
|
||||
|
||||
# A symlink stays: writing through it cleans the file it points to.
|
||||
if cleaned or target.is_symlink():
|
||||
target.write_text(cleaned + "\n", encoding="utf-8")
|
||||
_write_keep_newline(target, cleaned + "\n", newline)
|
||||
print(f"graphify section removed from {target.resolve()}")
|
||||
else:
|
||||
target.unlink()
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
"""Line-ending preservation for the install/uninstall markdown writers (#4360).
|
||||
|
||||
#3741 stopped the always-on registration and the Gemini writer from translating
|
||||
line endings on *write* by passing ``newline=""``. But those paths, and every
|
||||
other md writer listed in #4360, still read through ``Path.read_text``, which
|
||||
opens in universal-newline mode and folds every ``\\r\\n`` to ``\\n`` before the
|
||||
merge happens. A later plain ``write_text`` then re-emitted the platform
|
||||
default, so a read-modify-write normalised the WHOLE file: on Windows an LF file
|
||||
gained CRLF on every line, and on Linux a CRLF file lost its ``\\r``. For
|
||||
``CLAUDE.md``/``AGENTS.md``, which are shared and reviewed, that buries the real
|
||||
one-block change in an every-line diff and can trip line-ending checks.
|
||||
|
||||
These tests pin the contract for every writer: an LF file stays LF, a CRLF file
|
||||
stays CRLF, and install-then-uninstall returns the exact original bytes. The bug
|
||||
is observable in the file bytes on every host OS (LF input fails on Windows,
|
||||
CRLF input fails on Linux), so these do not depend on the platform running them.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from graphify import install as install_mod
|
||||
|
||||
LF = "\n"
|
||||
CRLF = "\r\n"
|
||||
USER_BODY = ["# My rules", "", "some notes"]
|
||||
NEWLINES = [pytest.param(LF, id="lf"), pytest.param(CRLF, id="crlf")]
|
||||
|
||||
|
||||
def _seed(path: Path, newline: str) -> bytes:
|
||||
"""Write a small user-owned file with *newline* endings; return its bytes."""
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_bytes((newline.join(USER_BODY) + newline).encode("utf-8"))
|
||||
return path.read_bytes()
|
||||
|
||||
|
||||
def _assert_single_ending(data: bytes, newline: str) -> None:
|
||||
"""Assert every line in *data* uses *newline* (no normalisation, no mixing)."""
|
||||
lf = data.count(b"\n")
|
||||
crlf = data.count(b"\r\n")
|
||||
if newline == CRLF:
|
||||
assert lf == crlf, f"file is not wholly CRLF: {lf} LF vs {crlf} CRLF"
|
||||
else:
|
||||
assert crlf == 0, f"an LF file gained {crlf} CRLF endings"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patched_home(tmp_path, monkeypatch):
|
||||
"""Keep user-scope skill writes inside tmp_path."""
|
||||
home = tmp_path / "home"
|
||||
home.mkdir()
|
||||
with patch("graphify.install.Path.home", return_value=home):
|
||||
yield home
|
||||
|
||||
|
||||
# (id, instructions file relative to the project, install, uninstall)
|
||||
_WRITERS = [
|
||||
(
|
||||
"claude",
|
||||
"CLAUDE.md",
|
||||
lambda project: install_mod.claude_install(project),
|
||||
lambda project: install_mod.claude_uninstall(project, project=True),
|
||||
install_mod._CLAUDE_MD_MARKER,
|
||||
),
|
||||
(
|
||||
"agents",
|
||||
"AGENTS.md",
|
||||
lambda project: install_mod._agents_install(project, "codex"),
|
||||
lambda project: install_mod._agents_uninstall(project, platform="codex"),
|
||||
install_mod._AGENTS_MD_MARKER,
|
||||
),
|
||||
(
|
||||
"gemini",
|
||||
"GEMINI.md",
|
||||
lambda project: install_mod.gemini_install(project, project=True),
|
||||
lambda project: install_mod.gemini_uninstall(project, project=True),
|
||||
install_mod._GEMINI_MD_MARKER,
|
||||
),
|
||||
(
|
||||
"codebuddy",
|
||||
"CODEBUDDY.md",
|
||||
lambda project: install_mod.codebuddy_install(project),
|
||||
lambda project: install_mod.codebuddy_uninstall(project, project=True),
|
||||
install_mod._CODEBUDDY_MD_MARKER,
|
||||
),
|
||||
(
|
||||
"vscode",
|
||||
".github/copilot-instructions.md",
|
||||
lambda project: install_mod.vscode_install(project),
|
||||
lambda project: install_mod.vscode_uninstall(project),
|
||||
install_mod._VSCODE_INSTRUCTIONS_MARKER,
|
||||
),
|
||||
]
|
||||
_WRITER_IDS = [w[0] for w in _WRITERS]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("newline", NEWLINES)
|
||||
@pytest.mark.parametrize("name,rel,install,uninstall,marker", _WRITERS, ids=_WRITER_IDS)
|
||||
def test_md_writer_round_trip_preserves_line_endings(
|
||||
tmp_path, patched_home, newline, name, rel, install, uninstall, marker
|
||||
):
|
||||
"""Install then uninstall leaves the file byte-identical to the original."""
|
||||
project = tmp_path / "proj"
|
||||
project.mkdir()
|
||||
target = project / rel
|
||||
original = _seed(target, newline)
|
||||
|
||||
install(project)
|
||||
after_install = target.read_bytes()
|
||||
assert marker in after_install.decode("utf-8"), "graphify block was not written"
|
||||
_assert_single_ending(after_install, newline)
|
||||
|
||||
uninstall(project)
|
||||
assert target.read_bytes() == original
|
||||
|
||||
|
||||
@pytest.mark.parametrize("newline", NEWLINES)
|
||||
def test_always_on_registration_round_trip_preserves_line_endings(tmp_path, newline):
|
||||
"""_register_always_on_block / _remove_claude_skill_registration (#4360)."""
|
||||
project = tmp_path / "proj"
|
||||
md = project / ".claude" / "CLAUDE.md"
|
||||
original = _seed(md, newline)
|
||||
|
||||
install_mod._register_always_on_block(
|
||||
md, " CLAUDE.md -> ", install_mod._skill_registration()
|
||||
)
|
||||
_assert_single_ending(md.read_bytes(), newline)
|
||||
assert install_mod._SKILL_REGISTRATION_MARKER in md.read_text(encoding="utf-8")
|
||||
|
||||
install_mod._remove_claude_skill_registration(project)
|
||||
assert md.read_bytes() == original
|
||||
|
||||
|
||||
@pytest.mark.parametrize("newline", NEWLINES)
|
||||
def test_strip_graphify_md_section_preserves_line_endings(tmp_path, newline):
|
||||
"""The CLAUDE.md-variant uninstall writer strips only graphify's section."""
|
||||
target = tmp_path / "CLAUDE.local.md"
|
||||
text = newline.join(
|
||||
["# local", "", "## graphify", "- managed line", "", "## other", "keep me"]
|
||||
) + newline
|
||||
target.write_bytes(text.encode("utf-8"))
|
||||
|
||||
assert install_mod._strip_graphify_md_section(target) is True
|
||||
|
||||
data = target.read_bytes()
|
||||
_assert_single_ending(data, newline)
|
||||
remaining = data.decode("utf-8")
|
||||
assert "## graphify" not in remaining
|
||||
assert "keep me" in remaining and "local" in remaining
|
||||
|
||||
|
||||
def test_new_instructions_file_is_written_lf(tmp_path, patched_home):
|
||||
"""A fresh file has no convention to preserve, so graphify writes LF on every
|
||||
host -- matching the other install writers (#4360/#3668)."""
|
||||
project = tmp_path / "proj"
|
||||
project.mkdir()
|
||||
|
||||
install_mod.vscode_install(project)
|
||||
|
||||
data = (project / ".github" / "copilot-instructions.md").read_bytes()
|
||||
assert data, "the instructions file was not created"
|
||||
assert b"\r\n" not in data, "a new file must not be written with platform CRLF"
|
||||
|
||||
|
||||
def test_read_keep_newline_reports_and_normalises(tmp_path):
|
||||
path = tmp_path / "f.md"
|
||||
path.write_bytes(b"a\r\nb\r\n")
|
||||
text, newline = install_mod._read_keep_newline(path)
|
||||
assert text == "a\nb\n"
|
||||
assert newline == CRLF
|
||||
|
||||
path.write_bytes(b"a\nb\n")
|
||||
text, newline = install_mod._read_keep_newline(path)
|
||||
assert text == "a\nb\n"
|
||||
assert newline == LF
|
||||
|
||||
|
||||
def test_write_keep_newline_never_translates(tmp_path):
|
||||
path = tmp_path / "f.md"
|
||||
install_mod._write_keep_newline(path, "a\nb\n", CRLF)
|
||||
assert path.read_bytes() == b"a\r\nb\r\n"
|
||||
install_mod._write_keep_newline(path, "a\nb\n", LF)
|
||||
assert path.read_bytes() == b"a\nb\n"
|
||||
install_mod._write_keep_newline(path, "x\n") # default is LF
|
||||
assert path.read_bytes() == b"x\n"
|
||||
Reference in new issue
Block a user