Skip to content

Commit 9b8e3d0

Browse files
committed
refuse to migrate an invalid configuration
1 parent 46f5cc9 commit 9b8e3d0

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

pre_commit/commands/migrate_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import yaml
44

5+
from pre_commit.clientlib import load_config
56
from pre_commit.util import yaml_load
67

78

@@ -43,6 +44,9 @@ def _migrate_sha_to_rev(contents: str) -> str:
4344

4445

4546
def migrate_config(config_file: str, quiet: bool = False) -> int:
47+
# ensure that the configuration is a valid pre-commit configuration
48+
load_config(config_file)
49+
4650
with open(config_file) as f:
4751
orig_contents = contents = f.read()
4852

tests/commands/migrate_config_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
import pre_commit.constants as C
4+
from pre_commit.clientlib import InvalidConfigError
45
from pre_commit.commands.migrate_config import _indent
56
from pre_commit.commands.migrate_config import migrate_config
67

@@ -147,10 +148,10 @@ def test_migrate_config_sha_to_rev(tmpdir):
147148

148149

149150
@pytest.mark.parametrize('contents', ('', '\n'))
150-
def test_empty_configuration_file_user_error(tmpdir, contents):
151+
def test_migrate_config_invalid_configuration(tmpdir, contents):
151152
cfg = tmpdir.join(C.CONFIG_FILE)
152153
cfg.write(contents)
153-
with tmpdir.as_cwd():
154-
assert not migrate_config(C.CONFIG_FILE)
154+
with tmpdir.as_cwd(), pytest.raises(InvalidConfigError):
155+
migrate_config(C.CONFIG_FILE)
155156
# even though the config is invalid, this should be a noop
156157
assert cfg.read() == contents

0 commit comments

Comments
 (0)