From 01c08c7387dcf21b8540dbd19c5cedf7769c0492 Mon Sep 17 00:00:00 2001 From: milde Date: Fri, 3 Feb 2012 12:22:14 +0000 Subject: [PATCH] Fix relative_path() with source=None and `unicode` target. git-svn-id: https://docutils.svn.sourceforge.net/svnroot/docutils/trunk@7338 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/docutils/utils/__init__.py | 3 ++- docutils/test/test_utils.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docutils/docutils/utils/__init__.py b/docutils/docutils/utils/__init__.py index aaed6dd0c..67bb86d00 100644 --- a/docutils/docutils/utils/__init__.py +++ b/docutils/docutils/utils/__init__.py @@ -457,7 +457,8 @@ def relative_path(source, target): If there is no common prefix, return the absolute path to `target`. """ - source_parts = os.path.abspath(source or 'dummy_file').split(os.sep) + source_parts = os.path.abspath(source or type(target)('dummy_file') + ).split(os.sep) target_parts = os.path.abspath(target).split(os.sep) # Check first 2 parts because '/dir'.split('/') == ['', 'dir']: if source_parts[:2] != target_parts[:2]: diff --git a/docutils/test/test_utils.py b/docutils/test/test_utils.py index 594b65321..de5154ca2 100755 --- a/docutils/test/test_utils.py +++ b/docutils/test/test_utils.py @@ -253,5 +253,37 @@ class HelperFunctionsTests(unittest.TestCase): self.assertEqual(utils.column_width(u'dà‚'), 2) # combining + def test_relative_path(self): + # Build and return a path to `target`, relative to `source`: + # Use '/' as path sep in result. + self.assertEqual(utils.relative_path('spam', 'spam'), '') + source = os.path.join('h\xE4m', 'spam', 'fileA') + target = os.path.join('h\xE4m', 'spam', 'fileB') + self.assertEqual(utils.relative_path(source, target), 'fileB') + source = os.path.join('h\xE4m', 'spam', 'fileA') + target = os.path.join('h\xE4m', 'fileB') + self.assertEqual(utils.relative_path(source, target), '../fileB') + # if source is None, default to the cwd: + target = os.path.join('eggs', 'fileB') + self.assertEqual(utils.relative_path(None, target), 'eggs/fileB') + # If there is no common prefix, return the absolute path to `target`: + # source = '/foo/bar/fileA' # POSIX + # TODO: how to specify an absolute path independent of the OS? + # target = os.path.join('eggs', 'fileB') + # self.assertEqual(utils.relative_path(source, target), + # os.path.abspath('fileB')) + # Correctly process unicode instances: + self.assertEqual(utils.relative_path(u'spam', u'spam'), u'') + source = os.path.join(u'h\xE4m', u'spam', u'fileA') + target = os.path.join(u'h\xE4m', u'spam', u'fileB') + self.assertEqual(utils.relative_path(source, target), u'fileB') + source = os.path.join(u'h\xE4m', u'spam', u'fileA') + target = os.path.join(u'h\xE4m', u'fileB') + self.assertEqual(utils.relative_path(source, target), u'../fileB') + # if source is None, default to the cwd: + target = os.path.join(u'eggs', u'fileB') + self.assertEqual(utils.relative_path(None, target), u'eggs/fileB') + + if __name__ == '__main__': unittest.main() -- 2.11.4.GIT