[meld] misc: Fix new name-shortening for relative and non-path cases



commit 8ef208ce85fb2863d4be8704e80d55551e043146
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Jul 22 07:22:37 2018 +1000

    misc: Fix new name-shortening for relative and non-path cases
    
    This situation was being hit when starting a new blank comparison (so no
    actual file names) and then loading a file into one pane. The pane with
    a file ended up with valid parent components, but nothing in common with
    the other pane.

 meld/misc.py      | 5 ++++-
 test/test_misc.py | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
---
diff --git a/meld/misc.py b/meld/misc.py
index 1d700d09..4c6091e0 100644
--- a/meld/misc.py
+++ b/meld/misc.py
@@ -26,6 +26,7 @@ import re
 import shutil
 import subprocess
 from pathlib import PurePath
+from typing import List
 
 from gi.repository import Gdk
 from gi.repository import GLib
@@ -293,7 +294,7 @@ def all_same(iterable):
     return True
 
 
-def shorten_names(*names):
+def shorten_names(*names) -> List[str]:
     """Remove common parts of a list of paths
 
     For example, `('/tmp/foo1', '/tmp/foo2')` would be summarised as
@@ -307,6 +308,8 @@ def shorten_names(*names):
     # Identify the longest common path among the list of path
     common = set(paths[0].parents)
     common = common.intersection(*(p.parents for p in paths))
+    if not common:
+        return list(names)
     common_parent = sorted(common, key=lambda p: -len(p.parts))[0]
 
     paths = [p.relative_to(common_parent) for p in paths]
diff --git a/test/test_misc.py b/test/test_misc.py
index 47cc900a..7ab6ff45 100644
--- a/test/test_misc.py
+++ b/test/test_misc.py
@@ -90,6 +90,9 @@ def test_all_same(lst, expected):
     ('posix', ['/tmp/bar/subdir/subsub', '/tmp/bar/'], ['subsub', 'bar']),
     ('nt', ['C:\\Users\\hmm\\bar', 'C:\\Users\\hmm\\foo'], ['bar', 'foo']),
     ('nt', ['C:\\Users\\bar\\hmm', 'C:\\Users\\foo\\hmm'], ['[bar] hmm', '[foo] hmm']),
+    # Check that paths with no commonality are handled
+    ('posix', ['nothing in', 'common'], ['nothing in', 'common']),
+    ('posix', ['<unnamed>', '/tmp/real/path'], ['<unnamed>', '/tmp/real/path']),
 ])
 def test_shorten_names(os_name, paths, expected):
     from meld.misc import shorten_names


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]