From 8dde5bf633adb1670aeccb68fdaf6cf437823506 Mon Sep 17 00:00:00 2001 From: Gabriel Dugny Date: Sat, 9 Sep 2023 16:46:40 +0200 Subject: [PATCH] chore: coverage --- tests/test_actions/test_install_hooks.py | 12 ++++---- tests/test_pdm/test_pdm_plugin.py | 19 ++++++++++++ tests/test_poetry/test_poetry_plugin.py | 38 +++++++++++++++++++++++- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/tests/test_actions/test_install_hooks.py b/tests/test_actions/test_install_hooks.py index 7472095..b7cada7 100644 --- a/tests/test_actions/test_install_hooks.py +++ b/tests/test_actions/test_install_hooks.py @@ -29,14 +29,16 @@ def test_execute_pre_commit_not_installed(self, printer: Printer, mock_subproces assert printer.debug.call_count == 1 assert printer.debug.call_args == call("pre-commit package is not installed (or detected). Skipping.") - def test_execute_not_in_git_repo(self, printer: MagicMock, mock_subprocess, mocker) -> None: - mock_subprocess.return_value.decode.return_value = "pre-commit" - mocker.patch("subprocess.check_output", side_effect=subprocess.CalledProcessError(1, "cmd")) + def test_execute_not_in_git_repo(self, printer: MagicMock, mocker: MockerFixture) -> None: + mocker.patch( + "subprocess.check_output", side_effect=subprocess.CalledProcessError(1, "git", b"error", b"output") + ) + mocker.patch("subprocess.check_call", return_value=0) + setup = SetupPreCommitHooks(printer, dry_run=False) setup._is_pre_commit_package_installed = MagicMock(return_value=True) - setup._get_git_directory_path = MagicMock(return_value=None) setup.execute() - assert printer.debug.call_count == 1 + assert printer.debug.call_count == 3 assert printer.debug.call_args == call("Not in a git repository - can't install hooks. Skipping.") def test_execute_pre_commit_hooks_already_installed( diff --git a/tests/test_pdm/test_pdm_plugin.py b/tests/test_pdm/test_pdm_plugin.py index 97bd65f..55cafb3 100644 --- a/tests/test_pdm/test_pdm_plugin.py +++ b/tests/test_pdm/test_pdm_plugin.py @@ -14,6 +14,7 @@ PDMPrinter, PDMSetupPreCommitHooks, ) +from sync_pre_commit_lock.pre_commit_config import PreCommitRepo # Create the mock objects @@ -72,3 +73,21 @@ def test_on_pdm_install_setup_pre_commit_success(project: Project) -> None: on_pdm_install_setup_pre_commit(project, hooks=hooks_mock, candidates=candidates_mock, dry_run=False) action_mock.execute.assert_called_once() + + +def test_pdm_printer_list_success(capsys: pytest.CaptureFixture[str]) -> None: + from sync_pre_commit_lock.pdm_plugin import PDMPrinter + + printer = PDMPrinter(UI()) + + printer.list_updated_packages( + { + "package1": ( + PreCommitRepo(repo="https://repo1.local/test", rev="rev1"), + "rev2", + ) + } + ) + captured = capsys.readouterr() + + assert "[sync-pre-commit-lock] ✔ https://repo1.local/test rev1 -> rev2" in captured.out diff --git a/tests/test_poetry/test_poetry_plugin.py b/tests/test_poetry/test_poetry_plugin.py index bf331b6..05e7baa 100644 --- a/tests/test_poetry/test_poetry_plugin.py +++ b/tests/test_poetry/test_poetry_plugin.py @@ -1,3 +1,4 @@ +import re from unittest.mock import MagicMock, patch import pytest @@ -6,7 +7,8 @@ from poetry.console.commands.install import InstallCommand from poetry.console.commands.lock import LockCommand from poetry.console.commands.self.self_command import SelfCommand -from sync_pre_commit_lock.poetry_plugin import SyncPreCommitLockPlugin +from sync_pre_commit_lock.poetry_plugin import SyncPreCommitLockPlugin, SyncPreCommitPoetryCommand +from sync_pre_commit_lock.pre_commit_config import PreCommitRepo def test_activate() -> None: @@ -97,3 +99,37 @@ def test_handle_post_command_application_none() -> None: assert True else: pytest.fail("RuntimeError not raised") + + +def test_poetry_printer_list_success(capsys: pytest.CaptureFixture[str]) -> None: + from cleo.io.inputs.input import Input + from cleo.io.io import IO + from cleo.io.outputs.output import Output + from sync_pre_commit_lock.poetry_plugin import PoetryPrinter + + output = Output() + + def _write(message: str, new_line: bool = False): + print(message) # noqa: T201 + + output._write = _write + printer = PoetryPrinter(IO(input=Input(), output=output, error_output=output)) + + printer.list_updated_packages( + { + "package1": ( + PreCommitRepo(repo="https://repo1.local/test", rev="rev1"), + "rev2", + ) + } + ) + captured = capsys.readouterr() + # Remove all <..> tags, as we don't have the real parser + out = re.sub(r"<[^>]*>", "", captured.out) + + assert "[sync-pre-commit-lock] • https://repo1.local/test rev1 -> rev2" in out + + +def test_direct_command_invocation(): + with pytest.raises(RuntimeError, match="self.application is None"): + SyncPreCommitPoetryCommand().handle()