From 6e793baed84f277f1ac8525cf9d7463b1fa13b58 Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Wed, 14 Aug 2024 00:38:33 -0500 Subject: [PATCH] removed single use functions --- src/ad_test/test_file_change.py | 41 ++++++++++++--------------------- src/ad_test/utils.py | 8 +++++++ 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/ad_test/test_file_change.py b/src/ad_test/test_file_change.py index 070507f..7db7ff1 100644 --- a/src/ad_test/test_file_change.py +++ b/src/ad_test/test_file_change.py @@ -3,41 +3,29 @@ import logging import re from pathlib import Path -# import child import food.menu import pytest from appdaemon.appdaemon import AppDaemon from git import Repo -from .utils import count_error_lines, get_app_orders, get_load_order, get_loaded_apps +from .utils import ( + count_error_lines, + get_app_orders, + get_load_order, + get_loaded_apps, + reset_file, +) INDENT = ' ' * 4 -def reset_file(repo: Repo, changed: Path): - if not changed.is_absolute(): - changed = Path(repo.working_tree_dir) / changed - repo.git.checkout('HEAD', '--', changed) +def test_file(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo): + module_file = Path(food.menu.__file__) - -def modify_file(path: Path): - file_content = path.read_text() + file_content = module_file.read_text() modified_content = re.sub(r'Ham', r'Spam', file_content, flags=re.MULTILINE) modified_content = re.sub(r'ham', r'spam', modified_content, flags=re.MULTILINE) - path.write_text(modified_content) - - -def insert_import_error(path: Path): - file_content = path.read_text().splitlines() - file_content.insert(0, 'raise ImportError') - path.write_text('\n'.join(file_content)) - - -def test_file(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo): - assert isinstance(ad, AppDaemon) - - module_file = Path(food.menu.__file__) - modify_file(module_file) + module_file.write_text(modified_content) try: with caplog.at_level(logging.DEBUG, logger='AppDaemon._app_management'): @@ -53,10 +41,11 @@ def test_file(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo def test_file_with_error(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo): - assert isinstance(ad, AppDaemon) - module_file = Path(food.menu.__file__) - insert_import_error(module_file) + + file_content = module_file.read_text().splitlines() + file_content.insert(0, 'raise ImportError') + module_file.write_text('\n'.join(file_content)) try: with caplog.at_level(logging.DEBUG): diff --git a/src/ad_test/utils.py b/src/ad_test/utils.py index 468ad82..2a69681 100644 --- a/src/ad_test/utils.py +++ b/src/ad_test/utils.py @@ -1,9 +1,11 @@ import asyncio import re +from pathlib import Path from typing import List import pytest from appdaemon.appdaemon import AppDaemon +from git import Repo async def delayed_stop(ad: AppDaemon, delay: float): @@ -39,3 +41,9 @@ def get_loaded_apps(caplog: pytest.LogCaptureFixture): def count_error_lines(caplog: pytest.LogCaptureFixture) -> int: error_log_lines = [msg for name, lvl, msg in caplog.record_tuples if name.startswith('Error')] return len(error_log_lines) + + +def reset_file(repo: Repo, changed: Path): + if not changed.is_absolute(): + changed = Path(repo.working_tree_dir) / changed + repo.git.checkout('HEAD', '--', changed)