Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e2ad21

Browse files
authored
Merge pull request #933 from jessebona/patch-1
Fix crash on `pre-commit migrate-config` when configuration is empty
2 parents 35a78c0 + 1a3d296 commit 7e2ad21

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

pre_commit/commands/migrate_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def _migrate_map(contents):
2121
# Find the first non-header line
2222
lines = contents.splitlines(True)
2323
i = 0
24-
while _is_header_line(lines[i]):
24+
# Only loop on non empty configuration file
25+
while i < len(lines) and _is_header_line(lines[i]):
2526
i += 1
2627

2728
header = ''.join(lines[:i])

tests/commands/migrate_config_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,13 @@ def test_migrate_config_sha_to_rev(tmpdir):
147147
' rev: v1.2.0\n'
148148
' hooks: []\n'
149149
)
150+
151+
152+
@pytest.mark.parametrize('contents', ('', '\n'))
153+
def test_empty_configuration_file_user_error(tmpdir, contents):
154+
cfg = tmpdir.join(C.CONFIG_FILE)
155+
cfg.write(contents)
156+
with tmpdir.as_cwd():
157+
assert not migrate_config(C.CONFIG_FILE)
158+
# even though the config is invalid, this should be a noop
159+
assert cfg.read() == contents

0 commit comments

Comments
 (0)