Coverage for pydaconf/plugins/env.py: 85.00%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.11, created at 2025-02-16 17:46 +0000

1import os 

2from collections.abc import Callable 

3 

4from dotenv import load_dotenv 

5 

6from pydaconf.plugins.base import PluginBase 

7 

8# TODO: Remove the cache since the provider implements its own cache 

9 

10class EnvPlugin(PluginBase): 

11 """Environment variables plugin. Loads environment variable based on prefix ENV:///""" 

12 

13 PREFIX='ENV' 

14 

15 def __init__(self) -> None: 

16 self.cache: dict[str, str] = {} 

17 

18 def run(self, value: str, on_update_callback: Callable[[str], None]) -> str: 

19 load_dotenv() 

20 if value not in self.cache: 20 ↛ 27line 20 didn't jump to line 27 because the condition on line 20 was always true

21 env_var = os.environ.get(value) 

22 if not env_var: 22 ↛ 23line 22 didn't jump to line 23 because the condition on line 22 was never true

23 raise Exception(f"Secret cannot be loaded for '{value}'. Environment '{value}' is empty.") 

24 

25 self.cache[value] = env_var 

26 

27 return self.cache[value]