Bug 1608783 - [mozlint] Fix bug in 'collapse' when two paths are a prefix of one another, r=Standard8

This was happening because we had two exclude paths that were run through the 'collapse' algorithm:
editor/libeditor/tests/browserscope/lib/richtext
editor/libeditor/tests/browserscope/lib/richtext2

The problem was that in order to determine the base directory, the algorithm called
'os.path.commonprefix'. This function just returns the common string prefix, which
is the '.../lib/richtext' path. Even though in the above, the base *should* have
been '.../lib'.

To fix the problem, we add a check to ensure the computed base doesn't have any
sibling directories with the same prefix. If there are, it means 'commonprefix' was
too greedy.

Differential Revision: https://phabricator.services.mozilla.com/D60173

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Andrew Halberstadt 2020-01-17 16:18:59 +00:00
Родитель f58b2ca786
Коммит 0f4437d7d5
4 изменённых файлов: 8 добавлений и 3 удалений

Просмотреть файл

@ -93,10 +93,14 @@ def collapse(paths, base=None, dotfiles=False):
if not base:
paths = list(map(mozpath.abspath, paths))
base = mozpath.commonprefix(paths)
base = mozpath.commonprefix(paths).rstrip('/')
if not os.path.isdir(base):
base = os.path.dirname(base)
# Make sure `commonprefix` factors in sibling directories that have the
# same prefix in their basenames.
parent = mozpath.dirname(base)
same_prefix = [p for p in os.listdir(parent) if p.startswith(mozpath.basename(base))]
if not os.path.isdir(base) or len(same_prefix) > 1:
base = parent
if base in paths:
return [base]

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -86,6 +86,7 @@ def test_filterpaths(test):
(['subdir1/b.py', 'subdir1/subdir3'], ['subdir1/b.py', 'subdir1/subdir3']),
(['subdir1/b.py', 'subdir1/b.js'], ['subdir1/b.py', 'subdir1/b.js']),
(['subdir1/subdir3'], ['subdir1/subdir3']),
(['foo', 'foobar', ], ['foo', 'foobar']),
])
def test_collapse(paths, expected):
os.chdir(root)