Skip to content

Commit

Permalink
♻️ refactor: Autotest class and update tests for improved type safety…
Browse files Browse the repository at this point in the history
… and readability
  • Loading branch information
davidjrice committed Nov 5, 2023
1 parent b5b1bc5 commit 1610f2c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 5 additions & 4 deletions autopytest/autotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import sys
import time
from pathlib import Path
from typing import Any

from watchdog.events import FileSystemEvent, FileSystemEventHandler
from watchdog.observers import Observer
from watchdog.observers.api import BaseObserver

from .config import Config
from .file import File
Expand All @@ -19,9 +19,10 @@
level=logging.INFO,
)

class Autotest(FileSystemEventHandler):

class Autotest(FileSystemEventHandler):
_log: logging.Logger = logging.getLogger("autopytest")
_observer: BaseObserver

def __init__(self, path: str) -> None:
self.config = Config.parse(path)
Expand Down Expand Up @@ -88,5 +89,5 @@ def log(self) -> logging.Logger:
return self._log

@property
def observer(self) -> Any:
return self._observer
def observer(self) -> BaseObserver:
return self._observer
9 changes: 7 additions & 2 deletions tests/test_autotest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import contextlib
from pathlib import Path
from unittest.mock import MagicMock, call, patch

import pytest
from watchdog.events import FileModifiedEvent

from autopytest.autotest import Autotest
import contextlib

pytestmark = pytest.mark.filterwarnings(
"ignore:cannot collect test class 'TestFileStrategy'",
Expand Down Expand Up @@ -69,10 +69,15 @@ def test_on_modified(mock_match_strategy: MagicMock) -> None:

mock_match_strategy.assert_called_once_with(path)


@patch("autopytest.autotest.Autotest.observer")
@patch("autopytest.autotest.Autotest.log")
@patch("time.sleep", side_effect=KeyboardInterrupt)
def test_start(_mock_sleep: MagicMock, mock_log: MagicMock, mock_observer: MagicMock) -> None:
def test_start(
_mock_sleep: MagicMock,
mock_log: MagicMock,
mock_observer: MagicMock,
) -> None:
autotest_instance = Autotest("fixtures/application")

with contextlib.suppress(SystemExit):
Expand Down

0 comments on commit 1610f2c

Please sign in to comment.