From 364e6b8c981fe7f954799d9ebe025c4d3a7090fb Mon Sep 17 00:00:00 2001 From: N3N Date: Wed, 25 Dec 2024 22:01:35 -0800 Subject: [PATCH] fix: revert test utils and move close to test --- pyproject.toml | 2 +- tests/test_factory.py | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index be4de1c..83ca5a7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -85,4 +85,4 @@ venv = "whispr" [tool.pytest.ini_options] addopts = "--cov=whispr" -pythonpath = ["src", "tests"] +pythonpath = ["src"] diff --git a/tests/test_factory.py b/tests/test_factory.py index 1cebdc2..487ea8a 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -1,3 +1,5 @@ +from functools import wraps +import os import unittest from unittest.mock import MagicMock, patch @@ -9,7 +11,31 @@ from whispr.factory import VaultFactory -from tests.utils import patch_env_var + +def patch_env_var(var_name, var_value): + """ + Test util to patch a given environment variable safely. + + :param var_name: Environment variable to patch (Ex: AWS_DEFAULT_REGION) + :param var_value: Environment variable value for testing (Ex: us-east-1) + """ + + def decorator(func): + @wraps(func) + def wrapper(*args, **kwargs): + original_value = os.environ.get(var_name) + os.environ[var_name] = var_value + try: + return func(*args, **kwargs) + finally: + if original_value is not None: + os.environ[var_name] = original_value + else: + del os.environ[var_name] + + return wrapper + + return decorator class FactoryTestCase(unittest.TestCase):