mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #1402 from utek/preserve_line_endings
Preserve line ending when running autoupdate
This commit is contained in:
commit
54fdd6ae5f
2 changed files with 41 additions and 2 deletions
|
|
@ -93,7 +93,7 @@ def _original_lines(
|
||||||
retry: bool = False,
|
retry: bool = False,
|
||||||
) -> Tuple[List[str], List[int]]:
|
) -> Tuple[List[str], List[int]]:
|
||||||
"""detect `rev:` lines or reformat the file"""
|
"""detect `rev:` lines or reformat the file"""
|
||||||
with open(path) as f:
|
with open(path, newline='') as f:
|
||||||
original = f.read()
|
original = f.read()
|
||||||
|
|
||||||
lines = original.splitlines(True)
|
lines = original.splitlines(True)
|
||||||
|
|
@ -126,7 +126,7 @@ def _write_new_config(path: str, rev_infos: List[Optional[RevInfo]]) -> None:
|
||||||
comment = match[4]
|
comment = match[4]
|
||||||
lines[idx] = f'{match[1]}rev:{match[2]}{new_rev}{comment}{match[5]}'
|
lines[idx] = f'{match[1]}rev:{match[2]}{new_rev}{comment}{match[5]}'
|
||||||
|
|
||||||
with open(path, 'w') as f:
|
with open(path, 'w', newline='') as f:
|
||||||
f.write(''.join(lines))
|
f.write(''.join(lines))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -263,6 +263,45 @@ def test_does_not_reformat(tmpdir, out_of_date, store):
|
||||||
assert cfg.read() == expected
|
assert cfg.read() == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_does_not_change_mixed_endlines_read(up_to_date, tmpdir, store):
|
||||||
|
fmt = (
|
||||||
|
'repos:\n'
|
||||||
|
'- repo: {}\n'
|
||||||
|
' rev: {} # definitely the version I want!\r\n'
|
||||||
|
' hooks:\r\n'
|
||||||
|
' - id: foo\n'
|
||||||
|
' # These args are because reasons!\r\n'
|
||||||
|
' args: [foo, bar, baz]\r\n'
|
||||||
|
)
|
||||||
|
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||||
|
|
||||||
|
expected = fmt.format(up_to_date, git.head_rev(up_to_date)).encode()
|
||||||
|
cfg.write_binary(expected)
|
||||||
|
|
||||||
|
assert autoupdate(str(cfg), store, freeze=False, tags_only=False) == 0
|
||||||
|
assert cfg.read_binary() == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_does_not_change_mixed_endlines_write(tmpdir, out_of_date, store):
|
||||||
|
fmt = (
|
||||||
|
'repos:\n'
|
||||||
|
'- repo: {}\n'
|
||||||
|
' rev: {} # definitely the version I want!\r\n'
|
||||||
|
' hooks:\r\n'
|
||||||
|
' - id: foo\n'
|
||||||
|
' # These args are because reasons!\r\n'
|
||||||
|
' args: [foo, bar, baz]\r\n'
|
||||||
|
)
|
||||||
|
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||||
|
cfg.write_binary(
|
||||||
|
fmt.format(out_of_date.path, out_of_date.original_rev).encode(),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert autoupdate(str(cfg), store, freeze=False, tags_only=False) == 0
|
||||||
|
expected = fmt.format(out_of_date.path, out_of_date.head_rev).encode()
|
||||||
|
assert cfg.read_binary() == expected
|
||||||
|
|
||||||
|
|
||||||
def test_loses_formatting_when_not_detectable(out_of_date, store, tmpdir):
|
def test_loses_formatting_when_not_detectable(out_of_date, store, tmpdir):
|
||||||
"""A best-effort attempt is made at updating rev without rewriting
|
"""A best-effort attempt is made at updating rev without rewriting
|
||||||
formatting. When the original formatting cannot be detected, this
|
formatting. When the original formatting cannot be detected, this
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue