Skip to content

Commit 33ebca1

Browse files
committed
raise better exception for an empty config file
The yaml.safe_load returns None for an empty YAML file and this caused problems updating properties. Fixes #316
1 parent 6df1eb9 commit 33ebca1

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

ddsc/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ def add_properties(self, filename):
8888
filename = os.path.expanduser(filename)
8989
if os.path.exists(filename):
9090
with open(filename, 'r') as yaml_file:
91-
self.update_properties(yaml.safe_load(yaml_file))
91+
config_data = yaml.safe_load(yaml_file)
92+
if config_data:
93+
self.update_properties(config_data)
94+
else:
95+
raise ValueError("Error: Empty config file {}".format(filename))
9296

9397
def update_properties(self, new_values):
9498
"""

ddsc/tests/test_config.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import math
33
import ddsc.config
44
import multiprocessing
5-
from mock.mock import patch
5+
from mock.mock import patch, mock_open
66

77

88
class TestConfig(TestCase):
@@ -159,3 +159,13 @@ def test_storage_provider_id(self, mock_os):
159159
}
160160
config.update_properties(some_config)
161161
self.assertEqual(config.storage_provider_id, '123456')
162+
163+
@patch('ddsc.config.os')
164+
def test_add_properties_empty_file(self, mock_os):
165+
mock_os.path.expanduser.return_value = '/home/user/.ddsclient'
166+
mock_os.path.exists.return_value = True
167+
config = ddsc.config.Config()
168+
with self.assertRaises(ValueError) as raised_exception:
169+
with patch('builtins.open', mock_open(read_data='')):
170+
config.add_properties('~/.ddsclient')
171+
self.assertEqual(str(raised_exception.exception), 'Error: Empty config file /home/user/.ddsclient')

0 commit comments

Comments
 (0)