def get_udf_suggested_path(path): """Return the suggested_path, name for 'path'. 'path' must be a path inside the user home directory, if it's not a ValueError is raised. """ if not path: raise ValueError("no path specified") assert isinstance(path, str) path = path.decode("utf8") user_home = expand_user("~").decode("utf-8") start_list = os.path.abspath(user_home).split(os.path.sep) path_list = os.path.abspath(path).split(os.path.sep) # Work out how much of the filepath is shared by user_home and path. common_prefix = os.path.commonprefix([start_list, path_list]) if os.path.sep.join(common_prefix) != user_home: raise ValueError("path isn't inside user home: %r" % path) # suggested_path is always unicode, because the suggested path is a # server-side metadata, and we will always use the unix path separator '/' suggested_path = path.replace(user_home, u"~") suggested_path = suggested_path.replace(os.path.sep, u"/") assert isinstance(suggested_path, unicode) return suggested_path
def _test_get_udf_path(self, suggested_path): """Assert that the resulting udf path is correct.""" assert isinstance(suggested_path, unicode) assert suggested_path.startswith(u'~') path = get_udf_path(suggested_path) expected = suggested_path.replace(u'/', os.path.sep) expected = expand_user(expected.encode('utf8')) self.assertEqual(path, expected)
def home_dir_parser(value): """Parser for the root_dir and shares_dir options. Return the path using user home + value. """ path = path_from_unix(value) result = expand_user(path) assert isinstance(result, str) return result
def get_udf_path(suggested_path): """Build the udf path using the suggested_path. 'suggested_path' is a non-local path, with unix-like slashes since is send to and received from the server. """ assert isinstance(suggested_path, unicode) # Unicode boundary! the suggested_path is Unicode in protocol and server, # but here we use bytes for paths path = suggested_path.replace(u"/", os.path.sep).encode("utf-8") return expand_user(path)
def test_expand_user_only_tilde(self): """Test the expand_user function returns with only tilde input.""" tilde = '~' result = expand_user(tilde) self.assertEqual(self.home_dir, result) self.assertFalse(result.endswith(os.path.sep))
def test_expand_user_tilde_and_backslash(self): """Test the expand_user function with tilde and backslash.""" tilde = '~' + os.path.sep result = expand_user(tilde) expected = self.home_dir + os.path.sep self.assertEqual(expected, result)
def test_expand_user_start_with_tilde(self): """Test the expand_user function with a path like: ~/userpath.""" path = os.path.join('~', 'userpath') result = expand_user(path) expected = os.path.join(self.home_dir, 'userpath') self.assertEqual(expected, result)
def test_expand_user_double_backslash(self): """Test the expand_user function with double backslash.""" path = '~~userpath' result = expand_user(path) self.assertEqual(path, result)
def test_expand_user_start_with_tilde_no_backslash(self): """Test the expand_user function with tilde an ordinary path.""" path = '~userpath' result = expand_user(path) self.assertEqual(path, result)
def test_expand_user_not_start_with_tilde(self): """Test the expand_user function with an ordinary path.""" path = 'userpath' result = expand_user(path) self.assertEqual(path, result)
def test_get_sharesdir(self): """The get_sharesdir returns the shares dir.""" expected = expand_user(os.path.join('~', 'Share it to Me')) main = self.build_main(shares_dir=expected) self.assertEqual(main.get_sharesdir(), expected)
def test_get_rootdir(self): """The get_rootdir returns the root dir.""" expected = expand_user(os.path.join('~', 'Ubuntu Test One')) main = self.build_main(root_dir=expected) self.assertEqual(main.get_rootdir(), expected)
def test_get_homedir(self): """The get_homedir returns the root dir.""" self.patch(main_mod, "user_home", self.home_dir) expected = expand_user('~') main = self.build_main() self.assertEqual(main.get_homedir(), expected)