mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Implement pre-commit migrate-config
This commit is contained in:
parent
3e76cdaf25
commit
8f5675d813
6 changed files with 226 additions and 12 deletions
|
|
@ -128,6 +128,7 @@ def test_does_not_reformat(
|
|||
out_of_date_repo, mock_out_store_directory, in_tmpdir,
|
||||
):
|
||||
fmt = (
|
||||
'repos:\n'
|
||||
'- repo: {}\n'
|
||||
' sha: {} # definitely the version I want!\n'
|
||||
' hooks:\n'
|
||||
|
|
@ -153,7 +154,7 @@ def test_loses_formatting_when_not_detectable(
|
|||
is abandoned.
|
||||
"""
|
||||
config = (
|
||||
'[\n'
|
||||
'repos: [\n'
|
||||
' {{\n'
|
||||
' repo: {}, sha: {},\n'
|
||||
' hooks: [\n'
|
||||
|
|
@ -171,6 +172,7 @@ def test_loses_formatting_when_not_detectable(
|
|||
autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
|
||||
after = open(C.CONFIG_FILE).read()
|
||||
expected = (
|
||||
'repos:\n'
|
||||
'- repo: {}\n'
|
||||
' sha: {}\n'
|
||||
' hooks:\n'
|
||||
|
|
@ -284,10 +286,36 @@ def test_autoupdate_local_hooks_with_out_of_date_repo(
|
|||
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
|
||||
)
|
||||
local_config = config_with_local_hooks()
|
||||
config = [local_config, stale_config]
|
||||
config = {'repos': [local_config, stale_config]}
|
||||
write_config('.', config)
|
||||
runner = Runner('.', C.CONFIG_FILE)
|
||||
assert autoupdate(runner, tags_only=False) == 0
|
||||
new_config_writen = load_config(runner.config_file_path)
|
||||
assert len(new_config_writen['repos']) == 2
|
||||
assert new_config_writen['repos'][0] == local_config
|
||||
|
||||
|
||||
def test_updates_old_format_to_new_format(tmpdir, capsys):
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
cfg.write(
|
||||
'- repo: local\n'
|
||||
' hooks:\n'
|
||||
' - id: foo\n'
|
||||
' name: foo\n'
|
||||
' entry: ./bin/foo.sh\n'
|
||||
' language: script\n',
|
||||
)
|
||||
ret = autoupdate(Runner(tmpdir.strpath, C.CONFIG_FILE), tags_only=True)
|
||||
assert ret == 1
|
||||
contents = cfg.read()
|
||||
assert contents == (
|
||||
'repos:\n'
|
||||
'- repo: local\n'
|
||||
' hooks:\n'
|
||||
' - id: foo\n'
|
||||
' name: foo\n'
|
||||
' entry: ./bin/foo.sh\n'
|
||||
' language: script\n'
|
||||
)
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == 'Configuration has been migrated.\n'
|
||||
|
|
|
|||
120
tests/commands/migrate_config_test.py
Normal file
120
tests/commands/migrate_config_test.py
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import pytest
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.commands.migrate_config import _indent
|
||||
from pre_commit.commands.migrate_config import migrate_config
|
||||
from pre_commit.runner import Runner
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('s', 'expected'),
|
||||
(
|
||||
('', ''),
|
||||
('a', ' a'),
|
||||
('foo\nbar', ' foo\n bar'),
|
||||
('foo\n\nbar\n', ' foo\n\n bar\n'),
|
||||
('\n\n\n', '\n\n\n'),
|
||||
),
|
||||
)
|
||||
def test_indent(s, expected):
|
||||
assert _indent(s) == expected
|
||||
|
||||
|
||||
def test_migrate_config_normal_format(tmpdir, capsys):
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
cfg.write(
|
||||
'- repo: local\n'
|
||||
' hooks:\n'
|
||||
' - id: foo\n'
|
||||
' name: foo\n'
|
||||
' entry: ./bin/foo.sh\n'
|
||||
' language: script\n',
|
||||
)
|
||||
assert migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE)) == 1
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == 'Configuration has been migrated.\n'
|
||||
contents = cfg.read()
|
||||
assert contents == (
|
||||
'repos:\n'
|
||||
'- repo: local\n'
|
||||
' hooks:\n'
|
||||
' - id: foo\n'
|
||||
' name: foo\n'
|
||||
' entry: ./bin/foo.sh\n'
|
||||
' language: script\n'
|
||||
)
|
||||
|
||||
|
||||
def test_migrate_config_document_marker(tmpdir):
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
cfg.write(
|
||||
'# comment\n'
|
||||
'\n'
|
||||
'---\n'
|
||||
'- repo: local\n'
|
||||
' hooks:\n'
|
||||
' - id: foo\n'
|
||||
' name: foo\n'
|
||||
' entry: ./bin/foo.sh\n'
|
||||
' language: script\n',
|
||||
)
|
||||
assert migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE)) == 1
|
||||
contents = cfg.read()
|
||||
assert contents == (
|
||||
'# comment\n'
|
||||
'\n'
|
||||
'---\n'
|
||||
'repos:\n'
|
||||
'- repo: local\n'
|
||||
' hooks:\n'
|
||||
' - id: foo\n'
|
||||
' name: foo\n'
|
||||
' entry: ./bin/foo.sh\n'
|
||||
' language: script\n'
|
||||
)
|
||||
|
||||
|
||||
def test_migrate_config_list_literal(tmpdir):
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
cfg.write(
|
||||
'[{\n'
|
||||
' repo: local,\n'
|
||||
' hooks: [{\n'
|
||||
' id: foo, name: foo, entry: ./bin/foo.sh,\n'
|
||||
' language: script,\n'
|
||||
' }]\n'
|
||||
'}]',
|
||||
)
|
||||
assert migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE)) == 1
|
||||
contents = cfg.read()
|
||||
assert contents == (
|
||||
'repos:\n'
|
||||
' [{\n'
|
||||
' repo: local,\n'
|
||||
' hooks: [{\n'
|
||||
' id: foo, name: foo, entry: ./bin/foo.sh,\n'
|
||||
' language: script,\n'
|
||||
' }]\n'
|
||||
' }]'
|
||||
)
|
||||
|
||||
|
||||
def test_already_migrated_configuration_noop(tmpdir, capsys):
|
||||
contents = (
|
||||
'repos:\n'
|
||||
'- repo: local\n'
|
||||
' hooks:\n'
|
||||
' - id: foo\n'
|
||||
' name: foo\n'
|
||||
' entry: ./bin/foo.sh\n'
|
||||
' language: script\n'
|
||||
)
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
cfg.write(contents)
|
||||
assert not migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE))
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == 'Configuration is already migrated.\n'
|
||||
assert cfg.read() == contents
|
||||
Loading…
Add table
Add a link
Reference in a new issue