Adds test for AdjacentTempDirectory._generate_names

This commit is contained in:
Steve Dower 2018-11-21 13:53:49 -08:00
parent 85f9cdf866
commit 1bf93a78cb
3 changed files with 36 additions and 7 deletions

View File

@ -17,7 +17,7 @@ from pip._internal.utils.misc import (
FakeFile, ask, dist_in_usersite, dist_is_local, egg_link_path, is_local,
normalize_path, renames,
)
from pip._internal.utils.temp_dir import TempDirectory, AdjacentTempDirectory
from pip._internal.utils.temp_dir import AdjacentTempDirectory
logger = logging.getLogger(__name__)
@ -105,7 +105,7 @@ def compress_for_rename(paths):
remaining = set(paths)
unchecked = sorted(set(os.path.split(p)[0] for p in remaining), key=len)
wildcards = set()
def norm_join(*a):
return os.path.normcase(os.path.join(*a))

View File

@ -3,7 +3,6 @@ from __future__ import absolute_import
import itertools
import logging
import os.path
import shutil
import tempfile
from pip._internal.utils.misc import rmtree
@ -115,8 +114,12 @@ class AdjacentTempDirectory(TempDirectory):
package).
"""
for i in range(1, len(name)):
for candidate in itertools.permutations(cls.LEADING_CHARS, i):
yield ''.join(candidate) + name[i:]
if name[i] in cls.LEADING_CHARS:
continue
for candidate in itertools.combinations_with_replacement(cls.LEADING_CHARS, i):
new_name = ''.join(candidate) + name[i:]
if new_name != name:
yield new_name
def create(self):
root, name = os.path.split(self.original)
@ -136,4 +139,3 @@ class AdjacentTempDirectory(TempDirectory):
tempfile.mkdtemp(prefix="pip-{}-".format(self.kind))
)
logger.debug("Created temporary directory: {}".format(self.path))

View File

@ -4,6 +4,7 @@
util tests
"""
import itertools
import os
import shutil
import stat
@ -29,7 +30,7 @@ from pip._internal.utils.misc import (
split_auth_from_netloc, untar_file, unzip_file,
)
from pip._internal.utils.packaging import check_dist_requires_python
from pip._internal.utils.temp_dir import TempDirectory
from pip._internal.utils.temp_dir import TempDirectory, AdjacentTempDirectory
class Tests_EgglinkPath:
@ -547,6 +548,32 @@ class TestTempDirectory(object):
assert tmp_dir.path is None
assert not os.path.exists(created_path)
@pytest.mark.parametrize("name",
[
"ABC",
"ABC.dist-info",
"_+-",
"_package",
])
def test_adjacent_directory_names(self, name):
names = lambda: AdjacentTempDirectory._generate_names(name)
chars = AdjacentTempDirectory.LEADING_CHARS
# Ensure many names are unique
# (For long *name*, this sequence can be extremely long.
# However, since we're only ever going to take the first
# result that works, provided there are many of those
# and that shorter names result in totally unique sets,
# it's okay to skip part of the test.)
some_names = list(itertools.islice(names(), 10000))
assert len(some_names) == len(set(some_names))
# Ensure original name does not appear
assert not any(n == name for n in names())
# Check the first group are correct
assert all(x == y for x, y in zip(some_names, [c + name[1:] for c in chars]))
class TestGlibc(object):
def test_manylinux1_check_glibc_version(self):