Skip to content

Commit

Permalink
Fix error adding hook when updating revision in 'add-pre-commit-hook'
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Jul 1, 2021
1 parent a73aa8e commit 0c0893d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.5.1
current_version = 1.5.2

[bumpversion:file:setup.cfg]

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

```yaml
- repo: https://github.com/mondeja/pre-commit-hooks
rev: v1.5.1
rev: v1.5.2
hooks:
- id: dev-extras-required
- id: root-editorconfig-required
Expand Down
18 changes: 10 additions & 8 deletions hooks/add_pre_commit_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def add_pre_commit_hook(repo, rev, hook_id, quiet=False, dry_run=False):
if not repo_in_config["hook_found"]:
config_lines = config_content.splitlines(keepends=True)

_inside_repo, _inside_hooks, _hooks_indent = (False, False, None)
_inside_repo, _hooks_line, _hooks_indent = (False, None, None)
_rev_line = None
for i, line in enumerate(config_lines):
if not _inside_repo:
Expand All @@ -156,15 +156,15 @@ def add_pre_commit_hook(repo, rev, hook_id, quiet=False, dry_run=False):
):
_inside_repo = True
else:
if _inside_hooks:
if _hooks_line is not None:
if _hooks_indent is None:
_hooks_indent = line.index("-") if "-" in line else None

if line.lstrip().startswith("- repo:"):
break
else:
if line.lstrip().startswith("hooks:"):
_inside_hooks = True
_hooks_line = i
elif line.lstrip().startswith("rev:"):
_rev_line = i

Expand All @@ -173,11 +173,13 @@ def add_pre_commit_hook(repo, rev, hook_id, quiet=False, dry_run=False):
if n == _rev_line and not repo_in_config["same_rev"]:
new_lines.append(line.split(":")[0] + f": {rev}\n")
else:
new_lines.append(line)
if n == i:
if not new_lines[-1].strip():
new_lines = new_lines[:-1]
new_lines.append(" " * _hooks_indent + f"- id: {hook_id}\n")
if n == _hooks_line:
if not new_lines[-1].strip():
new_lines = new_lines[:-1]
new_lines.append(line)
new_lines.append(" " * _hooks_indent + f"- id: {hook_id}\n")
else:
new_lines.append(line)

if dry_run:
if not quiet:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = mondeja_pre_commit_hooks
version = 1.5.1
version = 1.5.2
description = My own useful pre-commit hooks
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
31 changes: 30 additions & 1 deletion tests/test_add_pre_commit_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
- repo: https://github.com/mondeja/pre-commit-hooks
rev: v1.0.0
hooks:
- id: wavelint
- id: dev-extras-required
- id: wavelint
""",
1,
None,
Expand All @@ -91,6 +91,35 @@
None,
id="dont-add-hook",
),
pytest.param(
"https://github.com/mondeja/pre-commit-hooks",
"v1.5.1",
"dev-extras-required",
"""repos:
- repo: https://github.com/mondeja/pre-commit-hooks
rev: v1.3.0
hooks:
- id: wavelint
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v1.17.0
hooks:
- id: setup-cfg-fmt
""",
"""repos:
- repo: https://github.com/mondeja/pre-commit-hooks
rev: v1.5.1
hooks:
- id: dev-extras-required
- id: wavelint
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v1.17.0
hooks:
- id: setup-cfg-fmt
""",
1,
None,
id="add-hook-change-ref",
),
pytest.param(
"https://github.com/mondeja/pre-commit-hooks",
"v1.1.2",
Expand Down
8 changes: 8 additions & 0 deletions tests/test_cf_gh_pages_dns_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

import contextlib
import io
import os

import pytest

from hooks.cf_gh_pages_dns_records import check_cloudflare_gh_pages_dns_records


@pytest.mark.skipif(
not os.environ.get("CF_API_KEY"),
reason=(
"Cloudflare user API key defined in 'CF_API_KEY' environment variable"
" needed."
),
)
@pytest.mark.parametrize("quiet", (True, False), ids=("quiet=True", "quiet=False"))
@pytest.mark.parametrize(
("domain", "username", "expected_result", "expected_stderr"),
Expand Down

0 comments on commit 0c0893d

Please sign in to comment.