Skip to content

Commit

Permalink
critical/bugfix: handle case where custom-environment-variables.yml
Browse files Browse the repository at this point in the history
… is empty or non-existing, previously tests assumed file was always present
  • Loading branch information
grimen committed Dec 11, 2018
1 parent 2fb7734 commit 470f573
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 8 deletions.
20 changes: 12 additions & 8 deletions config2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,23 @@ def reload(self):
raise error

# load: custom-environment-variables.yml
try:
self.__class__.load_file(env_variables_file)
if env_variables_file:
try:
self.__class__.load_file(env_variables_file)

env_variables_file.data = self.__class__.map_env_variable_config(env_variables_file.data)
env_variables_file.data = self.__class__.map_env_variable_config(env_variables_file.data)

except Exception as error:
if not self.__silent__:
raise error
except Exception as error:
if not self.__silent__:
raise error

config_datas = map((lambda _config_file:
_config_file.data.copy()
_config_file and _config_file.data and _config_file.data.copy()
), config_files)
config_datas = list(config_datas)

config_datas.append(env_variables_file.data.copy())
if env_variables_file:
config_datas.append(env_variables_file.data.copy())

config_data = self.__class__.merge(*config_datas)

Expand All @@ -263,6 +265,8 @@ def reload(self):

@staticmethod
def map_env_variable_config(env_variable_mapping_object):
env_variable_mapping_object = env_variable_mapping_object or {}

env_variable_config = {} # dict(env_variable_mapping_object)

for key, value in env_variable_mapping_object.items():
Expand Down
3 changes: 3 additions & 0 deletions config2/serializers/json_.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def unpack(value):

value = value or NULL

if value is None:
return None

result = json.loads(value)

return result
Expand Down
3 changes: 3 additions & 0 deletions config2/serializers/yaml_.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def unpack(value):

value = value or NULL

if value is None:
return None

result = yaml.load(value)

return result
Expand Down
Empty file.
9 changes: 9 additions & 0 deletions config2/tests/__fixtures__/bar/config/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
a1: DEFAULT 1
a2:
b1: [1, 2, 3]
b2:
- foo
- bar
b3:
c1: 1
c2: "DEFAULT 2"
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
54 changes: 54 additions & 0 deletions config2/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
fixture_foo_root_path = helper.fixture_path('foo')
fixture_foo_src_nested_path = helper.fixture_path('foo', 'src', 'nested')

fixture_bar_root_path = helper.fixture_path('bar')

fixture_baz_root_path = helper.fixture_path('baz')

package_root_path = helper.root_path()

CUSTOM_ENV = {
Expand Down Expand Up @@ -1355,6 +1359,56 @@ def test_config(self):
del env['A2']
del env['C2']

# NOTE: verify case of `custom-environment-variables` empty
config = module.Config('bar', path = fixture_bar_root_path, silent = False)

self.assertDeepEqual(deepdict(config), deepdict({
'__config_data__': config.__config_data__,
'__config_directory_name__': config.__config_directory_name__,
'__config_files__': config.__config_files__,
'__config_path__': config.__config_path__,
'__default_config_file__': config.__default_config_file__,
'__env_config_file__': config.__env_config_file__,
'__env_config_files__': config.__env_config_files__,
'__env_variables_file__': config.__env_variables_file__,
'__env__': config.__env__,
'__files__': config.__files__,
'__logger__': config.__logger__,
'__path__': config.__path__,
'__root_path__': config.__root_path__,
'__silent__': config.__silent__,

'a1': 'DEFAULT 1',
'a2': {
'b1': [1, 2, 3],
'b2': ['foo', 'bar'],
'b3': {
'c1': 1,
'c2': 'DEFAULT 2',
},
},
}))

# NOTE: verify case of `custom-environment-variables` non-existing
config = module.Config('baz', path = fixture_baz_root_path, silent = False)

self.assertDeepEqual(deepdict(config), deepdict({
'__config_data__': config.__config_data__,
'__config_directory_name__': config.__config_directory_name__,
'__config_files__': config.__config_files__,
'__config_path__': config.__config_path__,
'__default_config_file__': config.__default_config_file__,
'__env_config_file__': config.__env_config_file__,
'__env_config_files__': config.__env_config_files__,
'__env_variables_file__': config.__env_variables_file__,
'__env__': config.__env__,
'__files__': config.__files__,
'__logger__': config.__logger__,
'__path__': config.__path__,
'__root_path__': config.__root_path__,
'__silent__': config.__silent__,
}))

def test_get(self):
self.assertTrue(hasattr(module.Config(), 'get'))

Expand Down

0 comments on commit 470f573

Please sign in to comment.