config/path: Pass in expanded value in expanded path type
This commit is contained in:
parent
3409ca99d1
commit
ac7edad86d
@ -24,12 +24,11 @@ def encode(value):
|
||||
|
||||
|
||||
class ExpandedPath(bytes):
|
||||
def __new__(self, value):
|
||||
expanded = path.expand_path(value)
|
||||
def __new__(self, original, expanded):
|
||||
return super(ExpandedPath, self).__new__(self, expanded)
|
||||
|
||||
def __init__(self, value):
|
||||
self.original = value
|
||||
def __init__(self, original, expanded):
|
||||
self.original = original
|
||||
|
||||
|
||||
class ConfigValue(object):
|
||||
@ -241,20 +240,18 @@ class Path(ConfigValue):
|
||||
- ``$XDG_DATA_DIR`` according to the XDG spec
|
||||
|
||||
- ``$XDG_MUSIC_DIR`` according to the XDG spec
|
||||
|
||||
Supported kwargs: ``optional``, ``choices``, and ``secret``
|
||||
"""
|
||||
def __init__(self, optional=False, choices=None):
|
||||
def __init__(self, optional=False):
|
||||
self._required = not optional
|
||||
self._choices = choices
|
||||
|
||||
def deserialize(self, value):
|
||||
value = value.strip()
|
||||
expanded = path.expand_path(value)
|
||||
validators.validate_required(value, self._required)
|
||||
validators.validate_choice(value, self._choices)
|
||||
if not value:
|
||||
validators.validate_required(expanded, self._required)
|
||||
if not value or expanded is None:
|
||||
return None
|
||||
return ExpandedPath(value)
|
||||
return ExpandedPath(value, expanded)
|
||||
|
||||
def serialize(self, value, display=False):
|
||||
if isinstance(value, ExpandedPath):
|
||||
|
||||
@ -324,16 +324,19 @@ class PortTest(unittest.TestCase):
|
||||
|
||||
class ExpandedPathTest(unittest.TestCase):
|
||||
def test_is_bytes(self):
|
||||
self.assertIsInstance(types.ExpandedPath(b'/tmp'), bytes)
|
||||
self.assertIsInstance(types.ExpandedPath(b'/tmp', b'foo'), bytes)
|
||||
|
||||
@mock.patch('mopidy.utils.path.expand_path')
|
||||
def test_defaults_to_expanded(self, expand_path_mock):
|
||||
expand_path_mock.return_value = b'expanded_path'
|
||||
self.assertEqual(b'expanded_path', types.ExpandedPath(b'~'))
|
||||
def test_defaults_to_expanded(self,):
|
||||
original = b'~'
|
||||
expanded = b'expanded_path'
|
||||
self.assertEqual(expanded, types.ExpandedPath(original, expanded))
|
||||
|
||||
@mock.patch('mopidy.utils.path.expand_path')
|
||||
def test_orginal_stores_unexpanded(self, expand_path_mock):
|
||||
self.assertEqual('~', types.ExpandedPath(b'~').original)
|
||||
original = b'~'
|
||||
expanded = b'expanded_path'
|
||||
result = types.ExpandedPath(original, expanded)
|
||||
self.assertEqual(original, result.original)
|
||||
|
||||
|
||||
class PathTest(unittest.TestCase):
|
||||
@ -343,11 +346,6 @@ class PathTest(unittest.TestCase):
|
||||
self.assertIsInstance(result, types.ExpandedPath)
|
||||
self.assertIsInstance(result, bytes)
|
||||
|
||||
def test_deserialize_enforces_choices(self):
|
||||
value = types.Path(choices=['/foo', '/bar', '/baz'])
|
||||
self.assertEqual('/foo', value.deserialize(b'/foo'))
|
||||
self.assertRaises(ValueError, value.deserialize, b'/foobar')
|
||||
|
||||
def test_deserialize_enforces_required(self):
|
||||
value = types.Path()
|
||||
self.assertRaises(ValueError, value.deserialize, b'')
|
||||
@ -357,10 +355,8 @@ class PathTest(unittest.TestCase):
|
||||
self.assertIsNone(value.deserialize(b''))
|
||||
self.assertIsNone(value.deserialize(b' '))
|
||||
|
||||
@mock.patch('mopidy.utils.path.expand_path')
|
||||
def test_serialize_uses_original(self, expand_path_mock):
|
||||
expand_path_mock.return_value = b'expanded_path'
|
||||
path = types.ExpandedPath(b'original_path')
|
||||
def test_serialize_uses_original(self):
|
||||
path = types.ExpandedPath(b'original_path', b'expanded_path')
|
||||
value = types.Path()
|
||||
self.assertEqual('expanded_path', path)
|
||||
self.assertEqual('original_path', value.serialize(path))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user