changeset 5465:6db421a8cd9a

tests: fix generation of a unique temporary directory path for VCS testing The old code was complicated and failed if directories were created quickly in succession. Re-implement vcs get_new_dir without get_normalized_path and VCSTestError. The top level get_new_dir was unused.
author Branko Majic <branko@majic.rs>
date Thu, 03 Sep 2015 23:04:13 +0200
parents dde94c758087
children 28d9e9f0ae4e
files kallithea/tests/__init__.py kallithea/tests/vcs/__init__.py kallithea/tests/vcs/conf.py kallithea/tests/vcs/utils.py
diffstat 4 files changed, 37 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/tests/__init__.py	Thu Sep 03 21:43:20 2015 +0200
+++ b/kallithea/tests/__init__.py	Thu Sep 03 23:04:13 2015 +0200
@@ -68,7 +68,7 @@
 log = logging.getLogger(__name__)
 
 __all__ = [
-    'parameterized', 'environ', 'url', 'get_new_dir', 'TestController',
+    'parameterized', 'environ', 'url', 'TestController',
     'SkipTest', 'ldap_lib_installed', 'pam_lib_installed', 'BaseTestCase', 'init_stack',
     'TESTS_TMP_PATH', 'HG_REPO', 'GIT_REPO', 'NEW_HG_REPO', 'NEW_GIT_REPO',
     'HG_FORK', 'GIT_FORK', 'TEST_USER_ADMIN_LOGIN', 'TEST_USER_ADMIN_PASS',
@@ -151,20 +151,6 @@
 except ImportError:
     pam_lib_installed = False
 
-def get_new_dir(title):
-    """
-    Returns always new directory path.
-    """
-    from kallithea.tests.vcs.utils import get_normalized_path
-    name = TEST_REPO_PREFIX
-    if title:
-        name = '-'.join((name, title))
-    hex = hashlib.sha1(str(time.time())).hexdigest()
-    name = '-'.join((name, hex))
-    path = os.path.join(TEST_DIR, name)
-    return get_normalized_path(path)
-
-
 import logging
 
 class NullHandler(logging.Handler):
--- a/kallithea/tests/vcs/__init__.py	Thu Sep 03 21:43:20 2015 +0200
+++ b/kallithea/tests/vcs/__init__.py	Thu Sep 03 23:04:13 2015 +0200
@@ -21,7 +21,7 @@
 """
 from kallithea.lib.vcs.utils.compat import unittest
 from kallithea.tests.vcs.conf import *
-from kallithea.tests.vcs.utils import VCSTestError, SCMFetcher
+from kallithea.tests.vcs.utils import SCMFetcher
 
 from kallithea.tests import *
 
@@ -45,12 +45,10 @@
             'clone_cmd': 'git clone --bare',
         },
     }
-    try:
-        for scm, fetcher_info in fetchers.items():
-            fetcher = SCMFetcher(**fetcher_info)
-            fetcher.setup()
-    except VCSTestError as err:
-        raise RuntimeError(str(err))
+
+    for scm, fetcher_info in fetchers.items():
+        fetcher = SCMFetcher(**fetcher_info)
+        fetcher.setup()
 
 
 def collector():
--- a/kallithea/tests/vcs/conf.py	Thu Sep 03 21:43:20 2015 +0200
+++ b/kallithea/tests/vcs/conf.py	Thu Sep 03 23:04:13 2015 +0200
@@ -7,7 +7,7 @@
 import tempfile
 import datetime
 import shutil
-from utils import get_normalized_path
+import uuid
 from os.path import join as jn
 
 __all__ = (
@@ -42,17 +42,40 @@
 TEST_REPO_PREFIX = 'vcs-test'
 
 
-def get_new_dir(title):
+def get_new_dir(title=None):
     """
-    Returns always new directory path.
+    Calculates a path for a new, non-existant, unique sub-directory in TEST_DIR.
+
+    Resulting directory name will have format:
+
+    prefix-[title-]hexuuid
+
+    Prefix is equal to value of variable TEST_REPO_PREFIX. The "hexuuid" is a
+    hexadecimal value of a randomly generated UUID. Title will be added if
+    specified.
+
+    Args:
+        title: Custom title to include as part of the resulting sub-directory
+            name. Can be useful for debugging to identify destination. Defaults
+            to None.
+
+    Returns:
+        Path to the new directory as a string.
     """
-    name = TEST_REPO_PREFIX
+
     if title:
-        name = '-'.join((name, title))
-    hex = hashlib.sha1(str(time.time())).hexdigest()
-    name = '-'.join((name, hex))
+        name = "%s-%s" % (TEST_REPO_PREFIX, title)
+    else:
+        name = TEST_REPO_PREFIX
+
     path = os.path.join(TEST_DIR, name)
-    return get_normalized_path(path)
+
+    # Generate new hexes until we get a unique name (just in case).
+    hex_uuid = uuid.uuid4().hex
+    while os.path.exists("%s-%s" % (path, hex_uuid)):
+        hex_uuid = uuid.uuid4().hex
+
+    return "%s-%s" % (path, hex_uuid)
 
 
 PACKAGE_DIR = os.path.abspath(os.path.join(
--- a/kallithea/tests/vcs/utils.py	Thu Sep 03 21:43:20 2015 +0200
+++ b/kallithea/tests/vcs/utils.py	Thu Sep 03 23:04:13 2015 +0200
@@ -3,16 +3,11 @@
 functions here are crafted as we don't want to use ``vcs`` to verify tests.
 """
 import os
-import re
 import sys
 
 from subprocess import Popen
 
 
-class VCSTestError(Exception):
-    pass
-
-
 def run_command(cmd, args):
     """
     Runs command on the system with given ``args``.
@@ -58,40 +53,3 @@
         remote = self.remote_repo
         eprint("Fetching repository %s into %s" % (remote, self.test_repo_path))
         run_command(self.clone_cmd,  '%s %s' % (remote, self.test_repo_path))
-
-
-def get_normalized_path(path):
-    """
-    If given path exists, new path would be generated and returned. Otherwise
-    same whats given is returned. Assumes that there would be no more than
-    10000 same named files.
-    """
-    if os.path.exists(path):
-        dir, basename = os.path.split(path)
-        splitted_name = basename.split('.')
-        if len(splitted_name) > 1:
-            ext = splitted_name[-1]
-        else:
-            ext = None
-        name = '.'.join(splitted_name[:-1])
-        matcher = re.compile(r'^.*-(\d{5})$')
-        start = 0
-        m = matcher.match(name)
-        if not m:
-            # Haven't append number yet so return first
-            newname = '%s-00000' % name
-            newpath = os.path.join(dir, newname)
-            if ext:
-                newpath = '.'.join((newpath, ext))
-            return get_normalized_path(newpath)
-        else:
-            start = int(m.group(1)[-5:]) + 1
-            for x in xrange(start, 10000):
-                newname = name[:-5] + str(x).rjust(5, '0')
-                newpath = os.path.join(dir, newname)
-                if ext:
-                    newpath = '.'.join((newpath, ext))
-                if not os.path.exists(newpath):
-                    return newpath
-        raise VCSTestError("Couldn't compute new path for %s" % path)
-    return path