Skip to content

Commit

Permalink
some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Heffernan committed Feb 29, 2024
1 parent 47d1fd0 commit 03374a3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
1 change: 1 addition & 0 deletions secureli/actions/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def verify_install(
if not self.action_deps.scanner.pre_commit.get_preferred_pre_commit_config_path(
folder_path
).exists():
self.action_deps.echo.print("bingo")
update_result: VerifyResult = (
self._update_secureli_pre_commit_config_location(
folder_path, always_yes
Expand Down
7 changes: 4 additions & 3 deletions tests/abstractions/test_pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
InstallResult,
PreCommitAbstraction,
)
from secureli.abstractions.echo import EchoAbstraction
from secureli.repositories.settings import (
PreCommitSettings,
PreCommitRepo,
Expand Down Expand Up @@ -81,12 +82,12 @@ def mock_subprocess(mocker: MockerFixture) -> MagicMock:
@pytest.fixture()
def mock_echo(mocker: MockerFixture) -> MagicMock:
mock_echo = MagicMock()
mocker.patch("secureli.abstractions.echo", mock_echo)
return mock_echo


@pytest.fixture()
def pre_commit(
mock_echo: mock_echo,
mock_hashlib: MagicMock,
mock_open: MagicMock,
mock_subprocess: MagicMock,
Expand Down Expand Up @@ -558,9 +559,9 @@ def test_migrate_config_file_moves_pre_commit_conig(
um.patch.object(Path, "exists", return_value=True),
):
pre_commit.migrate_config_file(test_folder_path)
old_location = test_folder_path / ".pre-commit-config.yaml"
old_location = test_folder_path / ".secureli" / ".pre-commit-config.yaml"
new_location = test_folder_path / ".secureli" / ".pre-commit-config.yaml"
mock_echo.print.assert_called_with(
mock_echo.print.assert_called_once_with(
f"Moving {old_location} to {new_location}..."
)
mock_move.assert_called_once()
49 changes: 20 additions & 29 deletions tests/actions/test_action.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path
from unittest.mock import MagicMock
import unittest.mock as um

import pytest
from secureli.abstractions.pre_commit import InstallResult
Expand Down Expand Up @@ -407,31 +408,23 @@ def test_that_verify_install_returns_success_result_newly_detected_language_inst
assert verify_result.outcome == VerifyOutcome.INSTALL_SUCCEEDED


def my_side_effect():
raise Exception("Test")


def test_that_verify_install_returns_failure_result(
def test_that_verify_install_returns_failure_result_without_file_path(
action: Action,
mock_scanner: MagicMock,
mock_echo: MagicMock,
):
mock_scanner.pre_commit.get_preferred_pre_commit_config_path.return_value = (
test_folder_path / ".secureli" / ".pre-commit-config.yaml"
)
# not properly mocking this?
mock_scanner.pre_commit.migrate_config_file.return_value.raiseError = my_side_effect
update_result = action._update_secureli_pre_commit_config_location(
test_folder_path, True
)
mock_echo.print.assert_called_once_with(
"seCureLI's .pre-commit-config.yaml is in a deprecated location."
)
assert update_result.outcome == VerifyOutcome.UPDATE_FAILED
with (um.patch.object(Path, "exists", return_value=False),):
mock_scanner.pre_commit.get_preferred_pre_commit_config_path.return_value = (
Path(None)
)

update_result = action.verify_install(
test_folder_path, reset=False, always_yes=True
)

# BINGO BONGO BINGO BONGO BINGO BONGO BINGO BONGO BINGO BONGO BINGO BONGO BINGO BONGO
# BINGO BONGO BINGO BONGO BINGO BONGO BINGO BONGO BINGO BONGO BINGO BONGO BINGO BONGO
mock_echo.print.assert_called_with("bingo")
assert mock_scanner.pre_commit.migrate_config_file.assert_called_once()
assert update_result.outcome == VerifyOutcome.UPDATE_FAILED


def test_that_update_secureli_pre_commit_config_location_moves_file(
Expand All @@ -453,17 +446,15 @@ def test_that_update_secureli_pre_commit_config_location_moves_file(
def test_that_update_secureli_pre_commit_config_fails_on_exception(
action: Action,
mock_scanner: MagicMock,
mock_echo: MagicMock,
):
update_file_location = test_folder_path / ".secureli" / ".pre-commit-config.yaml"
update_result = action._update_secureli_pre_commit_config_location(
update_file_location, True
)
mock_echo.print.assert_called_once_with(
"seCureLI's .pre-commit-config.yaml is in a deprecated location."
)
mock_scanner.pre_commit.migrate_config_file.raiseError.side_effect = my_side_effect
assert update_result.outcome == VerifyOutcome.UPDATE_FAILED
with pytest.raises(Exception):
mock_scanner.pre_commit.get_preferred_pre_commit_config_path.return_value = (
test_folder_path / ".secureli" / ".pre-commit-config.yaml"
)
update_result = action._update_secureli_pre_commit_config_location(
test_folder_path, True
)
assert update_result.outcome == VerifyOutcome.UPDATE_FAILED


def test_that_update_secureli_pre_commit_config_location_does_not_move_file(
Expand Down
3 changes: 0 additions & 3 deletions tests/actions/test_initializer_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@

from secureli.actions.action import ActionDependencies
from secureli.actions.initializer import InitializerAction
from secureli.repositories.secureli_config import SecureliConfig
from secureli.repositories.settings import SecureliFile, TelemetrySettings
from secureli.services.language_config import LanguageNotSupportedError
from secureli.services.logging import LogAction
from secureli.settings import Settings

test_folder_path = Path("does-not-matter")

Expand Down

0 comments on commit 03374a3

Please sign in to comment.