mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #1683 from pre-commit/textwrap_indent
use textwrap.indent instead of _indent
This commit is contained in:
commit
1975c056bc
2 changed files with 2 additions and 21 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import re
|
import re
|
||||||
|
import textwrap
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
|
@ -6,11 +7,6 @@ from pre_commit.clientlib import load_config
|
||||||
from pre_commit.util import yaml_load
|
from pre_commit.util import yaml_load
|
||||||
|
|
||||||
|
|
||||||
def _indent(s: str) -> str:
|
|
||||||
lines = s.splitlines(True)
|
|
||||||
return ''.join(' ' * 4 + line if line.strip() else line for line in lines)
|
|
||||||
|
|
||||||
|
|
||||||
def _is_header_line(line: str) -> bool:
|
def _is_header_line(line: str) -> bool:
|
||||||
return line.startswith(('#', '---')) or not line.strip()
|
return line.startswith(('#', '---')) or not line.strip()
|
||||||
|
|
||||||
|
|
@ -34,7 +30,7 @@ def _migrate_map(contents: str) -> str:
|
||||||
yaml_load(trial_contents)
|
yaml_load(trial_contents)
|
||||||
contents = trial_contents
|
contents = trial_contents
|
||||||
except yaml.YAMLError:
|
except yaml.YAMLError:
|
||||||
contents = f'{header}repos:\n{_indent(rest)}'
|
contents = f'{header}repos:\n{textwrap.indent(rest, " " * 4)}'
|
||||||
|
|
||||||
return contents
|
return contents
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,24 +2,9 @@ import pytest
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit.clientlib import InvalidConfigError
|
from pre_commit.clientlib import InvalidConfigError
|
||||||
from pre_commit.commands.migrate_config import _indent
|
|
||||||
from pre_commit.commands.migrate_config import migrate_config
|
from pre_commit.commands.migrate_config import migrate_config
|
||||||
|
|
||||||
|
|
||||||
@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):
|
def test_migrate_config_normal_format(tmpdir, capsys):
|
||||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||||
cfg.write(
|
cfg.write(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue