Skip to content

Commit 4393f9b

Browse files
cliffhallclaude
andcommitted
test(git): write LF fixtures with newline="\n" so Windows keeps LF (#1149)
core.autocrlf=false was not enough: on Windows, Path.write_text translates "\n" to "\r\n" before git ever sees the file, so the committed blobs already carried CRLF and diff/show output did too. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 0dbc48f commit 4393f9b

2 files changed

Lines changed: 42 additions & 26 deletions

File tree

‎src/git/tests/conftest.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def make_repo(path: Path) -> git.Repo:
8080
with repo.config_writer() as config:
8181
config.set_value("user", "name", "Test User")
8282
config.set_value("user", "email", "[email protected]")
83-
(path / "test.txt").write_text("line 1\nline 2\nline 3\nline 4\nline 5\n")
83+
(path / "test.txt").write_text(
84+
"line 1\nline 2\nline 3\nline 4\nline 5\n", newline="\n"
85+
)
8486
repo.index.add(["test.txt"])
8587
repo.index.commit("initial commit")
8688
return repo

‎src/git/tests/test_protocol.py‎

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ async def test_git_status_clean(repo: git.Repo):
164164

165165
async def test_git_status_reports_untracked_and_modified(repo: git.Repo):
166166
root = root_of(repo)
167-
(root / "test.txt").write_text("changed\n")
168-
(root / "new.txt").write_text("new\n")
167+
(root / "test.txt").write_text("changed\n", newline="\n")
168+
(root / "new.txt").write_text("new\n", newline="\n")
169169
result = await call(None, "git_status", {"repo_path": str(root)})
170170
assert result == text_result(f"Repository status:\n{repo.git.status()}")
171171
text = result["content"][0]["text"]
@@ -180,7 +180,9 @@ async def test_git_status_reports_untracked_and_modified(repo: git.Repo):
180180

181181
async def test_git_diff_unstaged(repo: git.Repo):
182182
root = root_of(repo)
183-
(root / "test.txt").write_text("line 1\nline 2\nCHANGED\nline 4\nline 5\n")
183+
(root / "test.txt").write_text(
184+
"line 1\nline 2\nCHANGED\nline 4\nline 5\n", newline="\n"
185+
)
184186
result = await call(None, "git_diff_unstaged", {"repo_path": str(root)})
185187
assert result == text_result("Unstaged changes:\n" + repo.git.diff("--unified=3"))
186188
assert "-line 3\n+CHANGED" in result["content"][0]["text"]
@@ -189,7 +191,9 @@ async def test_git_diff_unstaged(repo: git.Repo):
189191

190192
async def test_git_diff_unstaged_honors_context_lines(repo: git.Repo):
191193
root = root_of(repo)
192-
(root / "test.txt").write_text("line 1\nline 2\nCHANGED\nline 4\nline 5\n")
194+
(root / "test.txt").write_text(
195+
"line 1\nline 2\nCHANGED\nline 4\nline 5\n", newline="\n"
196+
)
193197
result = await call(
194198
None, "git_diff_unstaged", {"repo_path": str(root), "context_lines": 0}
195199
)
@@ -206,7 +210,9 @@ async def test_git_diff_unstaged_empty(repo: git.Repo):
206210

207211
async def test_git_diff_staged(repo: git.Repo):
208212
root = root_of(repo)
209-
(root / "test.txt").write_text("line 1\nline 2\nCHANGED\nline 4\nline 5\n")
213+
(root / "test.txt").write_text(
214+
"line 1\nline 2\nCHANGED\nline 4\nline 5\n", newline="\n"
215+
)
210216
repo.index.add(["test.txt"])
211217
result = await call(None, "git_diff_staged", {"repo_path": str(root)})
212218
assert result == text_result(
@@ -217,7 +223,9 @@ async def test_git_diff_staged(repo: git.Repo):
217223

218224
async def test_git_diff_staged_honors_context_lines(repo: git.Repo):
219225
root = root_of(repo)
220-
(root / "test.txt").write_text("line 1\nline 2\nCHANGED\nline 4\nline 5\n")
226+
(root / "test.txt").write_text(
227+
"line 1\nline 2\nCHANGED\nline 4\nline 5\n", newline="\n"
228+
)
221229
repo.index.add(["test.txt"])
222230
result = await call(
223231
None, "git_diff_staged", {"repo_path": str(root), "context_lines": 0}
@@ -241,7 +249,9 @@ async def test_git_diff_staged_empty(repo: git.Repo):
241249
async def test_git_diff_against_branch(repo: git.Repo):
242250
root = root_of(repo)
243251
repo.git.checkout("-b", "feature")
244-
(root / "test.txt").write_text("line 1\nline 2\nFEATURE\nline 4\nline 5\n")
252+
(root / "test.txt").write_text(
253+
"line 1\nline 2\nFEATURE\nline 4\nline 5\n", newline="\n"
254+
)
245255
repo.index.add(["test.txt"])
246256
repo.index.commit("feature commit")
247257
result = await call(None, "git_diff", {"repo_path": str(root), "target": "main"})
@@ -253,7 +263,9 @@ async def test_git_diff_against_branch(repo: git.Repo):
253263

254264
async def test_git_diff_honors_context_lines(repo: git.Repo):
255265
root = root_of(repo)
256-
(root / "test.txt").write_text("line 1\nline 2\nCHANGED\nline 4\nline 5\n")
266+
(root / "test.txt").write_text(
267+
"line 1\nline 2\nCHANGED\nline 4\nline 5\n", newline="\n"
268+
)
257269
result = await call(
258270
None,
259271
"git_diff",
@@ -286,7 +298,7 @@ async def test_git_diff_rejects_flag_injection(repo: git.Repo):
286298

287299
async def test_git_commit_records_staged_changes(repo: git.Repo):
288300
root = root_of(repo)
289-
(root / "new.txt").write_text("new\n")
301+
(root / "new.txt").write_text("new\n", newline="\n")
290302
repo.index.add(["new.txt"])
291303
result = await call(
292304
None, "git_commit", {"repo_path": str(root), "message": "add new"}
@@ -305,7 +317,7 @@ async def test_git_commit_with_nothing_staged_creates_empty_commit(repo: git.Rep
305317
# tree equals its parent's) and reports success. `git commit` would refuse
306318
# with "no changes added to commit". Fixing #4762 changes this test.
307319
root = root_of(repo)
308-
(root / "test.txt").write_text("edited but not staged\n")
320+
(root / "test.txt").write_text("edited but not staged\n", newline="\n")
309321
before = repo.head.commit
310322
result = await call(
311323
None, "git_commit", {"repo_path": str(root), "message": "claims a fix"}
@@ -326,17 +338,17 @@ async def test_git_commit_with_nothing_staged_creates_empty_commit(repo: git.Rep
326338

327339
async def test_git_add_specific_files(repo: git.Repo):
328340
root = root_of(repo)
329-
(root / "a.txt").write_text("a\n")
330-
(root / "b.txt").write_text("b\n")
341+
(root / "a.txt").write_text("a\n", newline="\n")
342+
(root / "b.txt").write_text("b\n", newline="\n")
331343
result = await call(None, "git_add", {"repo_path": str(root), "files": ["a.txt"]})
332344
assert result == text_result("Files staged successfully")
333345
assert staged_paths(repo) == {"a.txt"}
334346

335347

336348
async def test_git_add_dot_stages_everything(repo: git.Repo):
337349
root = root_of(repo)
338-
(root / "a.txt").write_text("a\n")
339-
(root / "b.txt").write_text("b\n")
350+
(root / "a.txt").write_text("a\n", newline="\n")
351+
(root / "b.txt").write_text("b\n", newline="\n")
340352
result = await call(None, "git_add", {"repo_path": str(root), "files": ["."]})
341353
assert result == text_result("Files staged successfully")
342354
assert staged_paths(repo) == {"a.txt", "b.txt"}
@@ -345,7 +357,7 @@ async def test_git_add_dot_stages_everything(repo: git.Repo):
345357
async def test_git_add_dot_never_stages_git_dir(repo: git.Repo):
346358
# Regression guard for #628: `git add .` must not stage `.git` itself.
347359
root = root_of(repo)
348-
(root / "a.txt").write_text("a\n")
360+
(root / "a.txt").write_text("a\n", newline="\n")
349361
await call(None, "git_add", {"repo_path": str(root), "files": ["."]})
350362
entries = [str(path) for path, _stage in repo.index.entries]
351363
assert not any(p == ".git" or p.startswith(".git/") for p in entries)
@@ -354,7 +366,7 @@ async def test_git_add_dot_never_stages_git_dir(repo: git.Repo):
354366

355367
async def test_git_add_absolute_path_inside_repository(repo: git.Repo):
356368
root = root_of(repo)
357-
(root / "a.txt").write_text("a\n")
369+
(root / "a.txt").write_text("a\n", newline="\n")
358370
result = await call(
359371
None, "git_add", {"repo_path": str(root), "files": [str(root / "a.txt")]}
360372
)
@@ -366,7 +378,7 @@ async def test_git_add_file_starting_with_dash_is_a_path(repo: git.Repo):
366378
# `--` separates the pathspec, so a dash-named file is staged, not parsed
367379
# as an option.
368380
root = root_of(repo)
369-
(root / "-n").write_text("dash\n")
381+
(root / "-n").write_text("dash\n", newline="\n")
370382
result = await call(None, "git_add", {"repo_path": str(root), "files": ["-n"]})
371383
assert result == text_result("Files staged successfully")
372384
assert staged_paths(repo) == {"-n"}
@@ -379,7 +391,7 @@ async def test_git_add_empty_list_reports_success_but_stages_nothing(
379391
# Pins #4763: `files: []` runs `git add --`, a no-op, and still reports
380392
# success. Fixing #4763 changes this test.
381393
root = root_of(repo)
382-
(root / "test.txt").write_text("edited\n")
394+
(root / "test.txt").write_text("edited\n", newline="\n")
383395
result = await call(None, "git_add", {"repo_path": str(root), "files": []})
384396
assert result == text_result("Files staged successfully")
385397
assert staged_paths(repo) == set()
@@ -409,7 +421,7 @@ async def test_git_add_rejects_relative_traversal(repo: git.Repo):
409421
# CVE-2026-27735: a path escaping the working tree is refused before git
410422
# runs.
411423
root = root_of(repo)
412-
(root.parent / "outside.txt").write_text("secret\n")
424+
(root.parent / "outside.txt").write_text("secret\n", newline="\n")
413425
result = await call(
414426
None, "git_add", {"repo_path": str(root), "files": ["../outside.txt"]}
415427
)
@@ -423,7 +435,7 @@ async def test_git_add_rejects_relative_traversal(repo: git.Repo):
423435
async def test_git_add_rejects_absolute_path_outside(repo: git.Repo):
424436
root = root_of(repo)
425437
outside = root.parent / "outside.txt"
426-
outside.write_text("secret\n")
438+
outside.write_text("secret\n", newline="\n")
427439
result = await call(
428440
None, "git_add", {"repo_path": str(root), "files": [str(outside)]}
429441
)
@@ -436,7 +448,7 @@ async def test_git_add_rejects_absolute_path_outside(repo: git.Repo):
436448
async def test_git_add_rejects_symlink_out_of_repository(repo: git.Repo):
437449
root = root_of(repo)
438450
outside = root.parent / "outside.txt"
439-
outside.write_text("secret\n")
451+
outside.write_text("secret\n", newline="\n")
440452
(root / "link.txt").symlink_to(outside)
441453
result = await call(
442454
None, "git_add", {"repo_path": str(root), "files": ["link.txt"]}
@@ -473,8 +485,8 @@ def resolve(self: Path, strict: bool = False) -> Path:
473485

474486
async def test_git_reset_unstages_everything(repo: git.Repo):
475487
root = root_of(repo)
476-
(root / "a.txt").write_text("a\n")
477-
(root / "test.txt").write_text("edited\n")
488+
(root / "a.txt").write_text("a\n", newline="\n")
489+
(root / "test.txt").write_text("edited\n", newline="\n")
478490
repo.index.add(["a.txt", "test.txt"])
479491
assert staged_paths(repo) == {"a.txt", "test.txt"}
480492
result = await call(None, "git_reset", {"repo_path": str(root)})
@@ -502,7 +514,7 @@ def add_commits(repo: git.Repo, n: int) -> list[git.Commit]:
502514
root = root_of(repo)
503515
commits = []
504516
for i in range(n):
505-
(root / f"f{i}.txt").write_text(f"{i}\n")
517+
(root / f"f{i}.txt").write_text(f"{i}\n", newline="\n")
506518
repo.index.add([f"f{i}.txt"])
507519
commits.append(repo.index.commit(f"commit {i}\n\nbody {i}"))
508520
return commits
@@ -731,7 +743,9 @@ def show_header(commit: git.Commit) -> str:
731743
# KNOWN BUG #4998: git_show prints Python reprs (quoted sha, <git.Actor>, datetime with a memory address) instead of git's format; the fix changes this assertion.
732744
async def test_git_show_commit_with_parent(repo: git.Repo):
733745
root = root_of(repo)
734-
(root / "test.txt").write_text("line 1\nline 2\nSHOWN\nline 4\nline 5\n")
746+
(root / "test.txt").write_text(
747+
"line 1\nline 2\nSHOWN\nline 4\nline 5\n", newline="\n"
748+
)
735749
repo.index.add(["test.txt"])
736750
commit = repo.index.commit("show me")
737751
result = await call(

0 commit comments

Comments
 (0)