def test_read_conf_stops_after_50_with_exit(self): exc = None calls = [] def _exit(v): calls.append(v) raise Exception('end test') class _SafeConfigParser(ConfigParser.SafeConfigParser): def read(self, files): ConfigParser.SafeConfigParser.readfp(self, StringIO(''' [brim] additional_confs = same_file ''')) return files orig_exit = conf.exit orig_SafeConfigParser = conf.SafeConfigParser try: conf.exit = _exit conf.SafeConfigParser = _SafeConfigParser conf.read_conf(['test.conf']) except Exception, err: exc = err
def test_read_conf_stops_after_50_with_exit(self, mock_exit): def read(slf, files): SafeConfigParser.readfp(slf, StringIO(''' [brim] additional_confs = same_file ''')) return files with patch('ConfigParser.SafeConfigParser.read', read): mock_exit.side_effect = Error() exc = None try: conf.read_conf('test.conf') except Error as err: exc = err self.assertTrue(exc is mock_exit.side_effect) self.assertEqual(len(mock_exit.call_args_list), 1) args, kwargs = mock_exit.call_args_list[0] self.assertEqual(len(args), 1) self.assertEqual(len(kwargs), 0) self.assertTrue(str(args[0]).startswith( 'Tried to read more than 50 conf files.\n' 'Recursion with [brim] additional_confs?\n' 'Files read so far: test.conf same_file same_file'))
def test_read_conf_raises_read_exception_if_asked(self, mock_read): mock_read.side_effect = Error() exc = None try: conf.read_conf('test.conf', exit_on_read_exception=False) except Exception as err: exc = err self.assertTrue(exc is mock_read.side_effect)
def test_read_conf_exits_on_read_exception(self, mock_exit, mock_read): mock_read.side_effect = Error() conf.read_conf('test.conf') self.assertEqual(len(mock_exit.call_args_list), 1) args, kwargs = mock_exit.call_args_list[0] self.assertEqual(len(args), 1) self.assertEqual(len(kwargs), 0) self.assertTrue(args[0] is mock_read.side_effect)
def test_read_conf_raises_read_exception_if_asked(self): exc = None class _SafeConfigParser(object): def read(self, files): raise ConfigParser.Error('SafeConfigParser error') orig_SafeConfigParser = conf.SafeConfigParser try: conf.SafeConfigParser = _SafeConfigParser conf.read_conf(['test.conf'], exit_on_read_exception=False) except Exception, err: exc = err
def test_read_conf(self): class _SafeConfigParser(ConfigParser.SafeConfigParser): def read(self, files): ConfigParser.SafeConfigParser.readfp(self, StringIO(''' [DEFAULT] default1 = 1 [section1] option1 = 1.1 option2 = 1.2 [section2] option1 = 2.1 option2 = 2.2 ''')) return files orig_SafeConfigParser = conf.SafeConfigParser try: conf.SafeConfigParser = _SafeConfigParser c = conf.read_conf(['test1.conf', 'test2.conf'], exit_on_read_exception=False) self.assertEquals( c.store, {'section1': {'default1': '1', 'option1': '1.1', 'option2': '1.2'}, 'section2': {'default1': '1', 'option1': '2.1', 'option2': '2.2'}}) self.assertEquals(c.files, ['test1.conf', 'test2.conf']) finally: conf.SafeConfigParser = orig_SafeConfigParser
def test_read_conf_calls_expanduser(self): calls = [] def _expanduser(path): calls.append(path) return path u1 = uuid4().hex u2 = uuid4().hex orig_expanduser = conf.expanduser try: conf.expanduser = _expanduser conf.read_conf([u1, u2]) finally: conf.expanduser = orig_expanduser self.assertEquals(calls, [u1, u2])
def test_read_conf(self): def read(slf, files): SafeConfigParser.readfp(slf, StringIO(''' [DEFAULT] default1 = 1 [section1] option1 = 1.1 option2 = 1.2 [section2] option1 = 2.1 option2 = 2.2 ''')) return files with patch('ConfigParser.SafeConfigParser.read', read): c = conf.read_conf( ['test1.conf', 'test2.conf'], exit_on_read_exception=False) self.assertEqual( c.store, { 'section1': { 'default1': '1', 'option1': '1.1', 'option2': '1.2'}, 'section2': { 'default1': '1', 'option1': '2.1', 'option2': '2.2'}}) self.assertEqual(c.files, ['test1.conf', 'test2.conf'])
def test_read_conf_stops_after_50(self): class _SafeConfigParser(ConfigParser.SafeConfigParser): def read(self, files): ConfigParser.SafeConfigParser.readfp(self, StringIO(''' [brim] additional_confs = same_file ''')) return files exc = None orig_SafeConfigParser = conf.SafeConfigParser try: conf.SafeConfigParser = _SafeConfigParser conf.read_conf(['test.conf'], exit_on_read_exception=False) except Exception, err: exc = err
def test_read_conf_stops_after_50(self): def read(slf, files): SafeConfigParser.readfp(slf, StringIO(''' [brim] additional_confs = same_file ''')) return files with patch('ConfigParser.SafeConfigParser.read', read): exc = None try: conf.read_conf('test.conf', exit_on_read_exception=False) except Error as err: exc = err self.assertTrue(str(exc).startswith( 'Tried to read more than 50 conf files.\n' 'Recursion with [brim] additional_confs?\n' 'Files read so far: test.conf same_file same_file'))
def test_read_conf_exits_on_read_exception(self): exc = None calls = [] def _exit(v): calls.append(v) raise Exception('end test') class _SafeConfigParser(object): def read(self, files): raise ConfigParser.Error('SafeConfigParser error') orig_exit = conf.exit orig_SafeConfigParser = conf.SafeConfigParser try: conf.exit = _exit conf.SafeConfigParser = _SafeConfigParser conf.read_conf(['test.conf']) except Exception, err: exc = err
def test_read_conf_calls_expanduser(self, mock_expanduser): u1 = uuid4().hex u2 = uuid4().hex mock_expanduser.return_value = 'mock' conf.read_conf([u1, u2]) mock_expanduser.assert_has_calls([call(u1), call(u2)])