Skip to content

Commit

Permalink
Merge pull request #425 from boeddeker/patch-5
Browse files Browse the repository at this point in the history
Improve get_handler exception msg
  • Loading branch information
Qwlouse authored Mar 5, 2019
2 parents ebccefd + 773510f commit 99dccd1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sacred/config/config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ def __init__(self, load, dump, mode):

def get_handler(filename):
_, extension = os.path.splitext(filename)
return HANDLER_BY_EXT[extension]
try:
return HANDLER_BY_EXT[extension]
except KeyError:
raise ValueError(
'Configuration file "{}" has invalid or unsupported extension '
'"{}".'.format(filename, extension)
)


def load_config_file(filename):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_config/test_config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import division, print_function, unicode_literals

import os
import re
import tempfile
import pytest

Expand Down Expand Up @@ -35,3 +36,19 @@ def test_load_config_file(ext, handler):
d = load_config_file(f_name)
assert d == data
os.remove(f_name)


def test_load_config_file_exception_msg_invalid_ext():
handle, f_name = tempfile.mkstemp(suffix='.invalid')
f = os.fdopen(handle, "w") # necessary for windows
f.close()
try:
exception_msg = re.compile(
'Configuration file ".*.invalid" has invalid or '
'unsupported extension ".invalid".'
)
with pytest.raises(ValueError) as excinfo:
load_config_file(f_name)
assert exception_msg.match(excinfo.value.args[0])
finally:
os.remove(f_name)

0 comments on commit 99dccd1

Please sign in to comment.