Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoTian committed Aug 19, 2016
1 parent 2d9c395 commit c6c2b3d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions conda/cli/main_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class BY_HANDLE_FILE_INFORMATION(ctypes.Structure):
cls._st_nlink = cls._windows_st_nlink


def find_lock(file_ending=LOCK_EXTENSION):
def find_lock(file_ending=LOCK_EXTENSION, extra_path=None):
from os.path import join
lock_dirs = context.pkgs_dirs[:]
lock_dirs += [context.root_dir]
Expand All @@ -176,7 +176,7 @@ def find_lock(file_ending=LOCK_EXTENSION):
except ImportError:
pass

for dir in lock_dirs:
for dir in lock_dirs + list(extra_path):
if not os.path.exists(dir):
continue
for dn in os.listdir(dir):
Expand Down
12 changes: 8 additions & 4 deletions conda/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from traceback import format_exc
from . import CondaError, text_type
from .compat import iteritems, iterkeys
from conda.cli.main_clean import find_lock
log = logging.getLogger(__name__)

class LockError(CondaError, RuntimeError):
Expand Down Expand Up @@ -438,19 +437,24 @@ def print_unexpected_error_message(e):
stderrlogger.info('\n'.join(' ' + line for line in traceback.splitlines()))


def delete_lock():
def delete_lock(extra_path=None):
"""
Delete lock on exception accoding to pid
log warning when delete fails
Args:
extra_path : The extra path that you want to search and
delete locks
"""
from conda.cli.main_clean import find_lock
from .lock import LOCK_EXTENSION
import os
pid = os.getpid()
file_end = text_type(pid) + "." + LOCK_EXTENSION
locks = list(find_lock(file_ending=file_end))
locks = list(find_lock(file_ending=file_end, extra_path=extra_path))
failed_delete = []
for path in locks:
assert os.path.exists(path)
assert os.path.exists(path), path
try:
os.unlink(path)
except (OSError, IOError) as e:
Expand Down
18 changes: 13 additions & 5 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from os.path import basename, join
from conda.lock import FileLock, LOCKSTR, LOCK_EXTENSION, LockError
from os.path import basename, join, exists, isfile
from conda.lock import FileLock, LockError, DirectoryLock
from conda.install import on_win

def test_filelock_passes(tmpdir):
Expand Down Expand Up @@ -107,8 +107,16 @@ def test_lock_retries(tmpdir):
# lock should clean up after itself
assert not tmpdir.join(path).exists()

def test_delete_lock(tmpdir):

def test_delete_lock():
from .test_create import make_temp_env
from conda.base.context import context
from conda.exceptions import delete_lock
with make_temp_env() as prefix:
assert False, context.pkgs_dirs
try:
with DirectoryLock(prefix) as lock:
path = basename(lock.lock_file_path)
assert isfile(join(prefix,path))
raise TypeError
except TypeError:
delete_lock(extra_path=prefix)
assert not exists(join(prefix, path))

0 comments on commit c6c2b3d

Please sign in to comment.