Changeset - 6db421a8cd9a
[Not reviewed]
default
0 4 0
Branko Majic (branko) - 10 years ago 2015-09-03 23:04:13
branko@majic.rs
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.
4 files changed with 37 insertions and 72 deletions:
0 comments (0 inline, 0 general)
kallithea/tests/__init__.py
Show inline comments
 
@@ -68,7 +68,7 @@ if not is_windows:
 
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 @@ try:
 
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):
kallithea/tests/vcs/__init__.py
Show inline comments
 
@@ -21,7 +21,7 @@ function at ``tests/__init__.py``.
 
"""
 
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 @@ def setup_package():
 
            '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():
kallithea/tests/vcs/conf.py
Show inline comments
 
@@ -7,7 +7,7 @@ import hashlib
 
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_DIR = os.environ.get('VCS_TEST_ROOT
 
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(
kallithea/tests/vcs/utils.py
Show inline comments
 
@@ -3,16 +3,11 @@ Utilities for tests only. These are not 
 
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 @@ class SCMFetcher(object):
 
        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
0 comments (0 inline, 0 general)