Skip to content

Commit 99bcd21

Browse files
authored
Merge pull request #153 from moremoban/override-json-data-too
🐛 json data shall override environment data too
2 parents 0ea6326 + 663b1cf commit 99bcd21

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

moban/plugins.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,17 @@ def get_data(self, file_name):
191191
data = utils.open_json(self.context_dirs, file_name)
192192
elif file_extension in [".yml", ".yaml"]:
193193
data = utils.open_yaml(self.context_dirs, file_name)
194-
utils.merge(data, self.__cached_environ_variables)
195194
else:
196195
raise exceptions.IncorrectDataInput
196+
utils.merge(data, self.__cached_environ_variables)
197197
return data
198-
except Exception as exception:
198+
except (IOError, exceptions.IncorrectDataInput) as exception:
199199
# If data file doesn't exist:
200200
# 1. Alert the user of their (potential) mistake
201201
# 2. Attempt to use environment vars as data
202-
reporter.report_info_message(str(exception))
202+
reporter.report_warning_message(str(exception))
203203
reporter.report_using_env_vars()
204-
data = os.environ
205-
return data
204+
return self.__cached_environ_variables
206205

207206

208207
def make_sure_all_pkg_are_loaded():

tests/test_context.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,21 @@ def test_environ_variables():
1818
context = Context(os.path.join("tests", "fixtures"))
1919
data = context.get_data("simple.yaml")
2020
eq_(data[test_var], test_value)
21+
22+
23+
def test_json_data_overrides_environ_variables():
24+
test_var = "TEST_ENVIRONMENT_VARIABLE"
25+
test_value = "am I found"
26+
os.environ[test_var] = test_value
27+
context = Context(os.path.join("tests", "fixtures"))
28+
data = context.get_data("simple.json")
29+
eq_(data[test_var], test_value)
30+
31+
32+
def test_unknown_data_file():
33+
test_var = "TEST_ENVIRONMENT_VARIABLE"
34+
test_value = "am I found"
35+
os.environ[test_var] = test_value
36+
context = Context(os.path.join("tests", "fixtures"))
37+
data = context.get_data("unknown.data")
38+
eq_(data[test_var], test_value)

0 commit comments

Comments
 (0)