diff --git a/mopidy/utils/path.py b/mopidy/utils/path.py index ee8f3c65..7f1b9233 100644 --- a/mopidy/utils/path.py +++ b/mopidy/utils/path.py @@ -62,9 +62,10 @@ def split_path(path): def expand_path(path): + path = string.Template(path).safe_substitute(XDG_DIRS) path = os.path.expanduser(path) path = os.path.abspath(path) - return string.Template(path).safe_substitute(XDG_DIRS) + return path def find_files(path): diff --git a/tests/utils/path_test.py b/tests/utils/path_test.py index 184970ae..d6b2b5a7 100644 --- a/tests/utils/path_test.py +++ b/tests/utils/path_test.py @@ -1,12 +1,13 @@ # encoding: utf-8 +import glib import os import shutil import sys import tempfile from mopidy.utils.path import (get_or_create_folder, mtime, - path_to_uri, uri_to_path, split_path, find_files) + path_to_uri, uri_to_path, expand_path, split_path, find_files) from tests import unittest, path_to_data_dir @@ -135,6 +136,30 @@ class SplitPathTest(unittest.TestCase): self.assertEqual([], split_path('/')) +class ExpandPathTest(unittest.TestCase): + # TODO: test via mocks? + + def test_empty_path(self): + self.assertEqual(os.path.abspath('.'), expand_path('')) + + def test_absolute_path(self): + self.assertEqual('/tmp/foo', expand_path('/tmp/foo')) + + def test_home_dir_expansion(self): + self.assertEqual(os.path.expanduser('~/foo'), expand_path('~/foo')) + + def test_abspath(self): + self.assertEqual(os.path.abspath('./foo'), expand_path('./foo')) + + def test_xdg_subsititution(self): + self.assertEqual(glib.get_user_data_dir() + '/foo', + expand_path('$XDG_DATA_DIR/foo')) + + def test_xdg_subsititution_unknown(self): + self.assertEqual('/tmp/$XDG_INVALID_DIR/foo', + expand_path('/tmp/$XDG_INVALID_DIR/foo')) + + class FindFilesTest(unittest.TestCase): def find(self, path): return list(find_files(path_to_data_dir(path)))