From 290fb05e0d9d2906ccd63e008325c59be324c1c5 Mon Sep 17 00:00:00 2001 From: Chinedum Echeta <60179183+cecheta@users.noreply.github.com> Date: Tue, 21 May 2024 09:45:51 +0100 Subject: [PATCH] fix: EnvHelper unusable if error occurs during initialisation (#954) --- code/backend/batch/utilities/helpers/env_helper.py | 5 +++-- code/tests/utilities/helpers/test_env_helper.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/code/backend/batch/utilities/helpers/env_helper.py b/code/backend/batch/utilities/helpers/env_helper.py index 7319c0499..96819b0c6 100644 --- a/code/backend/batch/utilities/helpers/env_helper.py +++ b/code/backend/batch/utilities/helpers/env_helper.py @@ -15,8 +15,9 @@ class EnvHelper: def __new__(cls): with cls._lock: if cls._instance is None: - cls._instance = super(EnvHelper, cls).__new__(cls) - cls._instance.__load_config() + instance = super(EnvHelper, cls).__new__(cls) + instance.__load_config() + cls._instance = instance return cls._instance def __load_config(self, **kwargs) -> None: diff --git a/code/tests/utilities/helpers/test_env_helper.py b/code/tests/utilities/helpers/test_env_helper.py index aff7090bc..10e1de308 100644 --- a/code/tests/utilities/helpers/test_env_helper.py +++ b/code/tests/utilities/helpers/test_env_helper.py @@ -1,3 +1,4 @@ +from unittest.mock import patch from pytest import MonkeyPatch import pytest from backend.batch.utilities.helpers.env_helper import EnvHelper @@ -140,3 +141,16 @@ def test_use_advanced_image_processing(monkeypatch: MonkeyPatch, value, expected # then assert actual_use_advanced_image_processing == expected + + +@patch( + "backend.batch.utilities.helpers.env_helper.os.getenv", + side_effect=Exception("Some error"), +) +def test_env_helper_not_created_if_error_occurs(_): + # when + with pytest.raises(Exception): + EnvHelper() + + # then + assert EnvHelper._instance is None