Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: dump to file when leaving context managers #322

Merged
merged 7 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/compwa_policy/utilities/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,19 @@ def __exit__(
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
tb: TracebackType | None,
) -> None:
) -> bool:
if exc_type is not None and not issubclass(exc_type, PrecommitError):
return False
if isinstance(exc_value, PrecommitError):
self.__error_messages.append(str("\n".join(exc_value.args)))
error_msg = self.merge_messages()
if error_msg:
if self.__raise_exception:
raise PrecommitError(error_msg) from exc_value
raise PrecommitError(error_msg)
print(error_msg) # noqa: T201
if os.getenv("COMPWA_POLICY_DEBUG") is not None:
self.print_execution_times()
return True

def merge_messages(self) -> str:
stripped_messages = (s.strip() for s in self.__error_messages)
Expand Down
11 changes: 7 additions & 4 deletions src/compwa_policy/utilities/precommit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def load(cls: type[T], source: IO | Path | str = CONFIG_PATH.precommit) -> T:

def dumps(self) -> str:
with io.StringIO() as stream:
return self.parser.dump(self.document, stream)
self.parser.dump(self.document, stream)
return stream.getvalue()

def find_repo(self, search_pattern: str) -> Repo | None:
"""Find pre-commit repo definition in pre-commit config."""
Expand Down Expand Up @@ -88,10 +89,12 @@ def __exit__(
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
tb: TracebackType | None,
) -> None:
) -> bool:
if exc_type is not None and not issubclass(exc_type, PrecommitError):
return False
if not self.__changelog:
return
if self.parser is None:
return True
if self.parser is not None:
self.dump(self.source)
msg = "The following modifications were made"
if isinstance(self.source, Path):
Expand Down
8 changes: 5 additions & 3 deletions src/compwa_policy/utilities/pyproject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,12 @@ def __exit__(
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
tb: TracebackType | None,
) -> None:
) -> bool:
if exc_type is not None and not issubclass(exc_type, PrecommitError):
return False
if not self._changelog:
return
if self._source is None:
return True
if self._source is not None:
self.dump(self._source)
msg = "The following modifications were made"
if isinstance(self._source, (Path, str)):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: https://github.com/editorconfig-checker/editorconfig-checker.python
rev: 2.7.3
hooks:
- id: editorconfig-checker
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/editorconfig-checker/editorconfig-checker.python
rev: 2.7.3
hooks:
- id: editorconfig-checker
name: editorconfig
alias: ec
11 changes: 11 additions & 0 deletions tests/check_dev_files/editorconfig/.pre-commit-config-good.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repos:
- repo: https://github.com/editorconfig-checker/editorconfig-checker.python
rev: 2.7.3
hooks:
- id: editorconfig-checker
name: editorconfig
alias: ec
exclude: >-
(?x)^(
.*\.py
)$
Empty file.
30 changes: 30 additions & 0 deletions tests/check_dev_files/editorconfig/test_editorconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import io
from pathlib import Path

import pytest

from compwa_policy.check_dev_files.editorconfig import _update_precommit_config
from compwa_policy.errors import PrecommitError
from compwa_policy.utilities.precommit import ModifiablePrecommit


@pytest.mark.parametrize("no_python", [True, False])
def test_update_precommit_config(no_python: bool):
this_dir = Path(__file__).parent
with open(this_dir / ".pre-commit-config-bad.yaml") as file:
src = file.read()

stream = io.StringIO(src)
with pytest.raises(
PrecommitError, match=r"Updated editorconfig-checker hook"
), ModifiablePrecommit.load(stream) as precommit:
_update_precommit_config(precommit, no_python)

result = precommit.dumps()
if no_python:
expected_file = this_dir / ".pre-commit-config-good-no-python.yaml"
else:
expected_file = this_dir / ".pre-commit-config-good.yaml"
with open(expected_file) as file:
expected = file.read()
assert result == expected
55 changes: 55 additions & 0 deletions tests/utilities/precommit/test_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import io
from pathlib import Path

import pytest

from compwa_policy.errors import PrecommitError
from compwa_policy.utilities.precommit import ModifiablePrecommit, Precommit


@pytest.fixture
def this_dir() -> Path:
return Path(__file__).parent


@pytest.fixture
def example_config(this_dir: Path) -> str:
with open(this_dir / ".pre-commit-config.yaml") as file:
return file.read()


class TestModifiablePrecommit:
def test_no_context_manager(self, example_config: str):
with pytest.raises(
expected_exception=RuntimeError,
match=r"^Modifications can only be made within a context$",
):
precommit = ModifiablePrecommit.load(example_config)
precommit.document["fail_fast"] = True
precommit.append_to_changelog("Fake modification")

def test_context_manager_path(self, this_dir: Path, example_config: str):
with pytest.raises(
PrecommitError,
match=r"Fake modification$",
), ModifiablePrecommit.load(this_dir / ".pre-commit-config.yaml") as precommit:
precommit.append_to_changelog("Fake modification")
yaml = precommit.dumps()
assert yaml == example_config

def test_context_manager_string_stream(self, example_config: str):
stream = io.StringIO(example_config)
with pytest.raises(
PrecommitError, match=r"Fake modification$"
), ModifiablePrecommit.load(stream) as precommit:
precommit.append_to_changelog("Fake modification")
stream.seek(0)
yaml = stream.read()
assert yaml == example_config


class TestPrecommit:
def test_dumps(self, this_dir: Path, example_config: str):
precommit = Precommit.load(this_dir / ".pre-commit-config.yaml")
yaml = precommit.dumps()
assert yaml == example_config
Loading