Monkey patching sucks.
But this makes this stuff at least testable.
This commit is contained in:
parent
7b0545ffd3
commit
d52d36d4c3
|
@ -1,6 +1,6 @@
|
|||
import configparser
|
||||
from os import getenv
|
||||
from os.path import join, exists, isfile
|
||||
import os.path
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
|
@ -17,19 +17,19 @@ class ConfigProvider(object):
|
|||
|
||||
def discover_config_file_by_name(filename: str, script_dir: str=None, env_var: str='RS500_CONFIG_PATH') -> str:
|
||||
if script_dir is not None:
|
||||
candidate = join(script_dir, filename)
|
||||
if exists(candidate) and isfile(candidate):
|
||||
candidate = os.path.join(script_dir, filename)
|
||||
if os.path.exists(candidate) and os.path.isfile(candidate):
|
||||
return candidate
|
||||
if env_var is not None:
|
||||
env_var_value = getenv(env_var, None)
|
||||
if env_var_value is not None:
|
||||
candidate = join(env_var_value, filename)
|
||||
if exists(candidate) and isfile(candidate):
|
||||
candidate = os.path.join(env_var_value, filename)
|
||||
if os.path.exists(candidate) and os.path.isfile(candidate):
|
||||
return candidate
|
||||
candidate = join(str(Path.home().absolute()), '.rs500', filename)
|
||||
if exists(candidate) and isfile(candidate):
|
||||
candidate = os.path.join(str(Path.home().absolute()), '.rs500', filename)
|
||||
if os.path.exists(candidate) and os.path.isfile(candidate):
|
||||
return candidate
|
||||
candidate = join('/etc', filename)
|
||||
if exists(candidate) and isfile(candidate):
|
||||
candidate = os.path.join('/etc', filename)
|
||||
if os.path.exists(candidate) and os.path.isfile(candidate):
|
||||
return candidate
|
||||
raise FileNotFoundError('Unable to find configuration file "{}"'.format(filename))
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import os
|
||||
import os.path
|
||||
|
||||
import pytest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
|
@ -7,11 +7,9 @@ from rs500common.configuration import discover_config_file_by_name
|
|||
|
||||
|
||||
def test_discovery(monkeypatch: MonkeyPatch):
|
||||
def mock(path):
|
||||
return True
|
||||
with monkeypatch.context() as m:
|
||||
m.setattr(os.path, 'isfile', mock)
|
||||
m.setattr(os.path, 'exists', mock)
|
||||
m.setattr('os.path.isfile', lambda path: True)
|
||||
m.setattr('os.path.exists', lambda path: True)
|
||||
result = discover_config_file_by_name('test.ini', '/foo/bar')
|
||||
assert result == os.path.join('/foo/bar', 'test.ini')
|
||||
|
||||
|
|
Loading…
Reference in New Issue