From 4e082c5045ad6f681bb02a03688f24c7f96e3cab Mon Sep 17 00:00:00 2001 From: Gabriel Dugny Date: Wed, 26 Jul 2023 21:42:30 +0200 Subject: [PATCH] chore: add testing to load offset --- .pre-commit-config.yaml | 1 - src/sync_pre_commit_lock/pre_commit_config.py | 17 +++++------- .../pre-commit-config-document-separator.yaml | 13 ++++++++- .../pre-commit-config-start-empty-lines.yaml | 1 - .../pre-commit-config-with-local.yaml | 1 + .../pre-commit-config.yaml | 2 -- tests/test_config.py | 1 - tests/test_pre_commit_config_file.py | 27 +++++++++++++++++++ 8 files changed, 46 insertions(+), 17 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a333743..3582980 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,4 +1,3 @@ ---- default_language_version: python: python3.11 repos: diff --git a/src/sync_pre_commit_lock/pre_commit_config.py b/src/sync_pre_commit_lock/pre_commit_config.py index 6e20840..89938ec 100644 --- a/src/sync_pre_commit_lock/pre_commit_config.py +++ b/src/sync_pre_commit_lock/pre_commit_config.py @@ -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: @@ -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") with self.pre_commit_config_file_path.open("w") as stream: stream.writelines(updated_lines) diff --git a/tests/fixtures/sample_pre_commit_config/pre-commit-config-document-separator.yaml b/tests/fixtures/sample_pre_commit_config/pre-commit-config-document-separator.yaml index ffe964a..8de34c1 100644 --- a/tests/fixtures/sample_pre_commit_config/pre-commit-config-document-separator.yaml +++ b/tests/fixtures/sample_pre_commit_config/pre-commit-config-document-separator.yaml @@ -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 @@ -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 diff --git a/tests/fixtures/sample_pre_commit_config/pre-commit-config-start-empty-lines.yaml b/tests/fixtures/sample_pre_commit_config/pre-commit-config-start-empty-lines.yaml index 970dc10..c476fcd 100644 --- a/tests/fixtures/sample_pre_commit_config/pre-commit-config-start-empty-lines.yaml +++ b/tests/fixtures/sample_pre_commit_config/pre-commit-config-start-empty-lines.yaml @@ -1,7 +1,6 @@ - default_language_version: python: python3.11 repos: diff --git a/tests/fixtures/sample_pre_commit_config/pre-commit-config-with-local.yaml b/tests/fixtures/sample_pre_commit_config/pre-commit-config-with-local.yaml index 332a627..f3097d6 100644 --- a/tests/fixtures/sample_pre_commit_config/pre-commit-config-with-local.yaml +++ b/tests/fixtures/sample_pre_commit_config/pre-commit-config-with-local.yaml @@ -1,3 +1,4 @@ + --- repos: - repo: https://github.com/astral-sh/ruff-pre-commit diff --git a/tests/fixtures/sample_pre_commit_config/pre-commit-config.yaml b/tests/fixtures/sample_pre_commit_config/pre-commit-config.yaml index bc00dd0..f6d8524 100644 --- a/tests/fixtures/sample_pre_commit_config/pre-commit-config.yaml +++ b/tests/fixtures/sample_pre_commit_config/pre-commit-config.yaml @@ -24,5 +24,3 @@ repos: rev: 23.3.0 hooks: - id: black - -# XXX Fix the issue with documents diff --git a/tests/test_config.py b/tests/test_config.py index 668c46c..8c7cbc1 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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, diff --git a/tests/test_pre_commit_config_file.py b/tests/test_pre_commit_config_file.py index d394253..2ab1c6b 100644 --- a/tests/test_pre_commit_config_file.py +++ b/tests/test_pre_commit_config_file.py @@ -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"