mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 16:24:40 +04:00
eagerly catch invalid yaml in migrate-config
This commit is contained in:
parent
dc667ab9fb
commit
619f2bf5a9
2 changed files with 22 additions and 0 deletions
|
|
@ -3,8 +3,10 @@ from __future__ import annotations
|
|||
import re
|
||||
import textwrap
|
||||
|
||||
import cfgv
|
||||
import yaml
|
||||
|
||||
from pre_commit.clientlib import InvalidConfigError
|
||||
from pre_commit.yaml import yaml_load
|
||||
|
||||
|
||||
|
|
@ -44,6 +46,13 @@ def migrate_config(config_file: str, quiet: bool = False) -> int:
|
|||
with open(config_file) as f:
|
||||
orig_contents = contents = f.read()
|
||||
|
||||
with cfgv.reraise_as(InvalidConfigError):
|
||||
with cfgv.validate_context(f'File {config_file}'):
|
||||
try:
|
||||
yaml_load(orig_contents)
|
||||
except Exception as e:
|
||||
raise cfgv.ValidationError(str(e))
|
||||
|
||||
contents = _migrate_map(contents)
|
||||
contents = _migrate_sha_to_rev(contents)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.clientlib import InvalidConfigError
|
||||
from pre_commit.commands.migrate_config import migrate_config
|
||||
|
||||
|
||||
|
|
@ -129,3 +132,13 @@ def test_migrate_config_sha_to_rev(tmpdir):
|
|||
' rev: v1.2.0\n'
|
||||
' hooks: []\n'
|
||||
)
|
||||
|
||||
|
||||
def test_migrate_config_invalid_yaml(tmpdir):
|
||||
contents = '['
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
cfg.write(contents)
|
||||
with tmpdir.as_cwd(), pytest.raises(InvalidConfigError) as excinfo:
|
||||
migrate_config(C.CONFIG_FILE)
|
||||
expected = '\n==> File .pre-commit-config.yaml\n=====> '
|
||||
assert str(excinfo.value).startswith(expected)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue