Skip to content

Commit

Permalink
chore: add testing to load offset
Browse files Browse the repository at this point in the history
  • Loading branch information
GabDug committed Jul 26, 2023
1 parent 74873d6 commit 4e082c5
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 17 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
default_language_version:
python: python3.11
repos:
Expand Down
17 changes: 6 additions & 11 deletions src/sync_pre_commit_lock/pre_commit_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,18 @@ def repos_normalized(self) -> set[PreCommitRepo]:

@cached_property
def document_start_offset(self) -> int:
# Use re to split by lines and find the '---'
"""Return the line number where the YAML document starts."""

lines = self.raw_file_contents.split("\n")
for i, line in enumerate(lines):
# Trim leading/trailing whitespaces
line = line.strip()
line = line.rstrip()
# Skip if line is a comment or empty/whitespace
if line.startswith("#") or line == "":
continue
# If line is '---', return line number + 1
if line == "---":
return i + 1
# If line isn't a comment and not '---' or empty/whitespace, it's the start of the YAML doc
# We return the line number (add 1 for 1-based indexing)
else:
return i
return 0

def update_pre_commit_repo_versions(self, new_versions: dict[PreCommitRepo, str]) -> None:
Expand All @@ -112,17 +109,15 @@ def update_pre_commit_repo_versions(self, new_versions: dict[PreCommitRepo, str]
if normalized_repo not in new_versions:
continue

rev_line_number = rev.end_line + self.document_start_offset
rev_line_idx = rev_line_number - 1
rev_line_number: int = rev.end_line + self.document_start_offset
rev_line_idx: int = rev_line_number - 1
original_rev_line: str = updated_lines[rev_line_idx]

updated_lines[rev_line_idx] = original_rev_line.replace(str(rev), new_versions[normalized_repo])

changes = difflib.ndiff(original_lines, updated_lines)
change_count = sum(1 for change in changes if change[0] in ["+", "-"])

if change_count == 0:
# XXX We should probably raise an exception here
return
raise RuntimeError("No changes to write, this should not happen")

Check warning on line 121 in src/sync_pre_commit_lock/pre_commit_config.py

View check run for this annotation

Codecov / codecov/patch

src/sync_pre_commit_lock/pre_commit_config.py#L121

Added line #L121 was not covered by tests
with self.pre_commit_config_file_path.open("w") as stream:
stream.writelines(updated_lines)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
- id: check-toml

- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.2.0
hooks:
- id: black

Expand All @@ -20,3 +20,14 @@ repos:
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

- repo: local
hooks:
- id: mypy
name: mypy
entry: mypy
args: [src, tests, --color-output]
language: system
types: [python]
pass_filenames: false
require_serial: true
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@




default_language_version:
python: python3.11
repos:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

---
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,3 @@ repos:
rev: 23.3.0
hooks:
- id: black

# XXX Fix the issue with documents
1 change: 0 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from sync_pre_commit_lock.db import RepoInfo


# @patch('sync_pre_commit_lock.config.SyncPreCommitLockConfig.__dataclass_fields__', new_callable=MagicMock)
def test_from_toml() -> None:
data = {
"disable-sync-from-lock": True,
Expand Down
27 changes: 27 additions & 0 deletions tests/test_pre_commit_config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,30 @@ def test_repos_property() -> None:
assert config.repos[0].repo == "https://repo1.local:443/test"
assert config.repos[0].rev == "rev1"
assert config.repos_normalized == {PreCommitRepo("https://repo1.local/test", "rev1")}


FIXTURES = Path(__file__).parent / "fixtures" / "sample_pre_commit_config"


@pytest.mark.parametrize(
("path", "offset"),
[
(FIXTURES / "pre-commit-config-document-separator.yaml", 4),
(FIXTURES / "pre-commit-config-start-empty-lines.yaml", 0),
(FIXTURES / "pre-commit-config-with-local.yaml", 2),
(FIXTURES / "pre-commit-config.yaml", 1),
(FIXTURES / "sample-django-stubs.yaml", 0),
],
)
def test_files_offset(path: Path, offset: int) -> None:
config = PreCommitHookConfig.from_yaml_file(path)
assert config.document_start_offset == offset


def test_update_versions():
config = PreCommitHookConfig.from_yaml_file(FIXTURES / "pre-commit-config-document-separator.yaml")
config.pre_commit_config_file_path = MagicMock()

config.update_pre_commit_repo_versions({PreCommitRepo("https://github.com/psf/black", "23.2.0"): "23.3.0"})
# Assert open was called with "w" mode
assert config.pre_commit_config_file_path.open.call_args[0][0] == "w"

0 comments on commit 4e082c5

Please sign in to comment.