Skip to content

Commit

Permalink
Merge pull request #1683 from pre-commit/textwrap_indent
Browse files Browse the repository at this point in the history
use textwrap.indent instead of _indent
  • Loading branch information
asottile committed Nov 6, 2020
2 parents 392a1fe + 6487669 commit 1975c05
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 21 deletions.
8 changes: 2 additions & 6 deletions pre_commit/commands/migrate_config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import re
import textwrap

import yaml

from pre_commit.clientlib import load_config
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:
return line.startswith(('#', '---')) or not line.strip()

Expand All @@ -34,7 +30,7 @@ def _migrate_map(contents: str) -> str:
yaml_load(trial_contents)
contents = trial_contents
except yaml.YAMLError:
contents = f'{header}repos:\n{_indent(rest)}'
contents = f'{header}repos:\n{textwrap.indent(rest, " " * 4)}'

return contents

Expand Down
15 changes: 0 additions & 15 deletions tests/commands/migrate_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,9 @@

import pre_commit.constants as C
from pre_commit.clientlib import InvalidConfigError
from pre_commit.commands.migrate_config import _indent
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):
cfg = tmpdir.join(C.CONFIG_FILE)
cfg.write(
Expand Down

0 comments on commit 1975c05

Please sign in to comment.