Skip to content

Commit

Permalink
moved EchoLevel from logging utility to echo model
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-heist-slalom committed Mar 11, 2024
1 parent 8b1d92b commit 420899c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 55 deletions.
21 changes: 9 additions & 12 deletions secureli/modules/shared/abstractions/echo.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from abc import ABC, abstractmethod
from enum import Enum
from typing import IO, Optional

import sys
import typer
from secureli.modules.shared.models.echo import Color

from secureli.modules.shared.utilities.logging import EchoLevel
from secureli.modules.shared.models.echo import Color, Level


class EchoAbstraction(ABC):
Expand All @@ -18,15 +15,15 @@ class EchoAbstraction(ABC):
"""

def __init__(self, level: str):
self.print_enabled = level != EchoLevel.off
self.debug_enabled = level == EchoLevel.debug
self.info_enabled = level in [EchoLevel.debug, EchoLevel.info]
self.warn_enabled = level in [EchoLevel.debug, EchoLevel.info, EchoLevel.warn]
self.print_enabled = level != Level.off
self.debug_enabled = level == Level.debug
self.info_enabled = level in [Level.debug, Level.info]
self.warn_enabled = level in [Level.debug, Level.info, Level.warn]
self.error_enabled = level in [
EchoLevel.debug,
EchoLevel.info,
EchoLevel.warn,
EchoLevel.error,
Level.debug,
Level.info,
Level.warn,
Level.error,
]

@abstractmethod
Expand Down
14 changes: 14 additions & 0 deletions secureli/modules/shared/models/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ class Color(str, Enum):
MAGENTA = "magenta"
CYAN = "cyan"
WHITE = "white"


class Level(str, Enum):
debug = "DEBUG"
info = "INFO"
warn = "WARN"
error = "ERROR"
off = "OFF"

def __str__(self) -> str:
return self.value

def __repr__(self) -> str:
return self.__str__()
15 changes: 0 additions & 15 deletions secureli/modules/shared/utilities/logging.py

This file was deleted.

6 changes: 3 additions & 3 deletions secureli/repositories/repo_settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from typing import Optional
from pydantic import BaseModel, BaseSettings, Field
from secureli.modules.shared.utilities.logging import EchoLevel
from secureli.modules.shared.models.echo import Level

import yaml

Expand Down Expand Up @@ -74,7 +74,7 @@ class EchoSettings(BaseSettings):
Settings that affect how seCureLI provides information to the user.
"""

level: EchoLevel = Field(default=EchoLevel.warn)
level: Level = Field(default=Level.warn)


class LanguageSupportSettings(BaseSettings):
Expand Down Expand Up @@ -157,7 +157,7 @@ def save(self, settings: SecureliFile):
key: value for (key, value) in settings.dict().items() if value is not None
}

# Converts EchoLevel to string
# Converts echo Level to string
if settings_dict.get("echo"):
settings_dict["echo"]["level"] = "{}".format(settings_dict["echo"]["level"])

Expand Down
3 changes: 2 additions & 1 deletion tests/actions/test_scan_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from secureli.modules.shared.abstractions.pre_commit import RevisionPair
from secureli.actions.action import ActionDependencies
from secureli.actions.scan import ScanAction
from secureli.modules.shared.models.echo import Level
from secureli.modules.shared.models.exit_codes import ExitCode
from secureli.modules.shared.models.install import VerifyOutcome
from secureli.modules.shared.models.language import AnalyzeResult
Expand Down Expand Up @@ -75,7 +76,7 @@ def mock_get_time_far_from_epoch(mocker: MockerFixture) -> MagicMock:

@pytest.fixture()
def mock_default_settings(mock_settings_repository: MagicMock) -> MagicMock:
mock_echo_settings = repo_settings.EchoSettings(level=repo_settings.EchoLevel.info)
mock_echo_settings = repo_settings.EchoSettings(level=Level.info)
mock_settings_file = repo_settings.SecureliFile(echo=mock_echo_settings)
mock_settings_repository.load.return_value = mock_settings_file

Expand Down
36 changes: 17 additions & 19 deletions tests/modules/shared/abstractions/test_typer_echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from unittest.mock import MagicMock, ANY

import pytest
from secureli.modules.shared.models.echo import Color

from secureli.modules.shared.utilities.logging import EchoLevel
from secureli.modules.shared.models.echo import Color, Level

ECHO_SECURELI_PREFIX = "[seCureLI]"

Expand Down Expand Up @@ -48,7 +46,7 @@ def typer_echo(request) -> TyperEcho:

@pytest.mark.parametrize(
"typer_echo",
[EchoLevel.info, EchoLevel.debug, EchoLevel.error, EchoLevel.warn],
[Level.info, Level.debug, Level.error, Level.warn],
indirect=True,
)
def test_that_typer_echo_renders_print_messages_correctly(
Expand All @@ -68,10 +66,10 @@ def test_that_typer_echo_renders_print_messages_correctly(
@pytest.mark.parametrize(
"typer_echo",
[
EchoLevel.debug,
EchoLevel.info,
EchoLevel.warn,
EchoLevel.error,
Level.debug,
Level.info,
Level.warn,
Level.error,
],
indirect=True,
)
Expand All @@ -90,7 +88,7 @@ def test_that_typer_echo_renders_errors_correctly(


@pytest.mark.parametrize(
"typer_echo", [EchoLevel.warn, EchoLevel.info, EchoLevel.debug], indirect=True
"typer_echo", [Level.warn, Level.info, Level.debug], indirect=True
)
def test_that_typer_echo_renders_warnings_correctly(
typer_echo: TyperEcho,
Expand All @@ -106,7 +104,7 @@ def test_that_typer_echo_renders_warnings_correctly(
)


@pytest.mark.parametrize("typer_echo", [EchoLevel.info, EchoLevel.debug], indirect=True)
@pytest.mark.parametrize("typer_echo", [Level.info, Level.debug], indirect=True)
def test_that_typer_echo_renders_info_correctly(
typer_echo: TyperEcho,
mock_echo_text: str,
Expand All @@ -123,7 +121,7 @@ def test_that_typer_echo_renders_info_correctly(

@pytest.mark.parametrize(
"typer_echo",
[EchoLevel.debug],
[Level.debug],
indirect=True,
)
def test_that_typer_echo_renders_debug_messages_correctly(
Expand All @@ -139,7 +137,7 @@ def test_that_typer_echo_renders_debug_messages_correctly(

@pytest.mark.parametrize(
"typer_echo",
[EchoLevel.off],
[Level.off],
indirect=True,
)
def test_that_typer_echo_suppresses_all_messages_when_off(
Expand All @@ -158,7 +156,7 @@ def test_that_typer_echo_suppresses_all_messages_when_off(

@pytest.mark.parametrize(
"typer_echo",
[EchoLevel.off],
[Level.off],
indirect=True,
)
def test_that_typer_echo_suppresses_error_messages(
Expand All @@ -170,7 +168,7 @@ def test_that_typer_echo_suppresses_error_messages(

@pytest.mark.parametrize(
"typer_echo",
[EchoLevel.error, EchoLevel.off],
[Level.error, Level.off],
indirect=True,
)
def test_that_typer_echo_suppresses_warning_messages(
Expand All @@ -182,7 +180,7 @@ def test_that_typer_echo_suppresses_warning_messages(

@pytest.mark.parametrize(
"typer_echo",
[EchoLevel.warn, EchoLevel.error, EchoLevel.off],
[Level.warn, Level.error, Level.off],
indirect=True,
)
def test_that_typer_echo_suppresses_info_messages(
Expand All @@ -194,7 +192,7 @@ def test_that_typer_echo_suppresses_info_messages(

@pytest.mark.parametrize(
"typer_echo",
[EchoLevel.info, EchoLevel.warn, EchoLevel.error, EchoLevel.off],
[Level.info, Level.warn, Level.error, Level.off],
indirect=True,
)
def test_that_typer_echo_suppresses_debug_messages(
Expand All @@ -206,7 +204,7 @@ def test_that_typer_echo_suppresses_debug_messages(

@pytest.mark.parametrize(
"typer_echo",
[EchoLevel.info],
[Level.info],
indirect=True,
)
def test_that_typer_echo_prompts_user_for_confirmation(
Expand All @@ -220,7 +218,7 @@ def test_that_typer_echo_prompts_user_for_confirmation(


def test_that_typer_echo_implements_prompt_with_default(mock_typer_prompt: MagicMock):
typer_echo = TyperEcho(level=EchoLevel.info)
typer_echo = TyperEcho(level=Level.info)
message = "test message"
default_response = "default user response"
typer_echo.prompt(message=message, default_response=default_response)
Expand All @@ -233,7 +231,7 @@ def test_that_typer_echo_implements_prompt_with_default(mock_typer_prompt: Magic
def test_that_typer_echo_implements_prompt_without_default(
mock_typer_prompt: MagicMock,
):
typer_echo = TyperEcho(level=EchoLevel.info)
typer_echo = TyperEcho(level=Level.info)
message = "test message"
typer_echo.prompt(message=message)

Expand Down
6 changes: 3 additions & 3 deletions tests/modules/shared/utilities/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from secureli.modules.shared.utilities.logging import EchoLevel
from secureli.modules.shared.models.echo import Level


def test_that_echo_level_str_returns_enum_val():
level = EchoLevel.info
level = Level.info

assert str(level) == level.value


def test_that_echo_level_repr_returns_str_implementation():
level = EchoLevel.debug
level = Level.debug

assert repr(level) == str(level)
5 changes: 3 additions & 2 deletions tests/repositories/test_settings_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pytest_mock import MockerFixture

from secureli.repositories import repo_settings
from secureli.modules.shared.models.echo import Level


@pytest.fixture()
Expand Down Expand Up @@ -67,7 +68,7 @@ def test_that_settings_file_loads_settings_when_present(
):
secureli_file = settings_repository.load(existent_path)

assert secureli_file.echo.level == repo_settings.EchoLevel.error
assert secureli_file.echo.level == Level.error


def test_that_settings_file_created_when_not_present(
Expand All @@ -84,7 +85,7 @@ def test_that_repo_saves_config(
mock_open: MagicMock,
settings_repository: repo_settings.SecureliRepository,
):
echo_level = repo_settings.EchoSettings(level=repo_settings.EchoLevel.info)
echo_level = repo_settings.EchoSettings(level=Level.info)
settings_file = repo_settings.SecureliFile(echo=echo_level)
settings_repository.save(settings_file)

Expand Down

0 comments on commit 420899c

Please sign in to comment.