mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
change migrate-config to use yaml parse tree instead
This commit is contained in:
parent
504149d2ca
commit
364e6d77f0
5 changed files with 194 additions and 10 deletions
|
|
@ -134,6 +134,27 @@ def test_migrate_config_sha_to_rev(tmpdir):
|
|||
)
|
||||
|
||||
|
||||
def test_migrate_config_sha_to_rev_json(tmp_path):
|
||||
contents = """\
|
||||
{"repos": [{
|
||||
"repo": "https://github.com/pre-commit/pre-commit-hooks",
|
||||
"sha": "v1.2.0",
|
||||
"hooks": []
|
||||
}]}
|
||||
"""
|
||||
expected = """\
|
||||
{"repos": [{
|
||||
"repo": "https://github.com/pre-commit/pre-commit-hooks",
|
||||
"rev": "v1.2.0",
|
||||
"hooks": []
|
||||
}]}
|
||||
"""
|
||||
cfg = tmp_path.joinpath('cfg.yaml')
|
||||
cfg.write_text(contents)
|
||||
assert not migrate_config(str(cfg))
|
||||
assert cfg.read_text() == expected
|
||||
|
||||
|
||||
def test_migrate_config_language_python_venv(tmp_path):
|
||||
src = '''\
|
||||
repos:
|
||||
|
|
@ -167,6 +188,31 @@ repos:
|
|||
assert cfg.read_text() == expected
|
||||
|
||||
|
||||
def test_migrate_config_quoted_python_venv(tmp_path):
|
||||
src = '''\
|
||||
repos:
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: example
|
||||
name: example
|
||||
entry: example
|
||||
language: "python_venv"
|
||||
'''
|
||||
expected = '''\
|
||||
repos:
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: example
|
||||
name: example
|
||||
entry: example
|
||||
language: "python"
|
||||
'''
|
||||
cfg = tmp_path.joinpath('cfg.yaml')
|
||||
cfg.write_text(src)
|
||||
assert migrate_config(str(cfg)) == 0
|
||||
assert cfg.read_text() == expected
|
||||
|
||||
|
||||
def test_migrate_config_invalid_yaml(tmpdir):
|
||||
contents = '['
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
|
|
|
|||
47
tests/yaml_rewrite_test.py
Normal file
47
tests/yaml_rewrite_test.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit.yaml import yaml_compose
|
||||
from pre_commit.yaml_rewrite import MappingKey
|
||||
from pre_commit.yaml_rewrite import MappingValue
|
||||
from pre_commit.yaml_rewrite import match
|
||||
from pre_commit.yaml_rewrite import SequenceItem
|
||||
|
||||
|
||||
def test_match_produces_scalar_values_only():
|
||||
src = '''\
|
||||
- name: foo
|
||||
- name: [not, foo] # not a scalar: should be skipped!
|
||||
- name: bar
|
||||
'''
|
||||
matcher = (SequenceItem(), MappingValue('name'))
|
||||
ret = [n.value for n in match(yaml_compose(src), matcher)]
|
||||
assert ret == ['foo', 'bar']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cls', (MappingKey, MappingValue))
|
||||
def test_mapping_not_a_map(cls):
|
||||
m = cls('s')
|
||||
assert list(m.match(yaml_compose('[foo]'))) == []
|
||||
|
||||
|
||||
def test_sequence_item_not_a_sequence():
|
||||
assert list(SequenceItem().match(yaml_compose('s: val'))) == []
|
||||
|
||||
|
||||
def test_mapping_key():
|
||||
m = MappingKey('s')
|
||||
ret = [n.value for n in m.match(yaml_compose('s: val\nt: val2'))]
|
||||
assert ret == ['s']
|
||||
|
||||
|
||||
def test_mapping_value():
|
||||
m = MappingValue('s')
|
||||
ret = [n.value for n in m.match(yaml_compose('s: val\nt: val2'))]
|
||||
assert ret == ['val']
|
||||
|
||||
|
||||
def test_sequence_item():
|
||||
ret = [n.value for n in SequenceItem().match(yaml_compose('[a, b, c]'))]
|
||||
assert ret == ['a', 'b', 'c']
|
||||
Loading…
Add table
Add a link
Reference in a new issue