diff --git a/mopidy/config/types.py b/mopidy/config/types.py index 29c41ae2..726d81f7 100644 --- a/mopidy/config/types.py +++ b/mopidy/config/types.py @@ -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): diff --git a/tests/config/types_test.py b/tests/config/types_test.py index 6351a0b2..ea07d5f5 100644 --- a/tests/config/types_test.py +++ b/tests/config/types_test.py @@ -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))