import os

from doit.dependency import Dependency


TESTDB = os.path.join(os.path.dirname(__file__), "testdb")


def remove_db(filename):
    """remove db file from anydbm"""
    extensions = ['', #dbhash #gdbm
                  '.bak', #dumbdb
                  '.dat', #dumbdb
                  '.dir', #dumbdb #dbm
                  '.db', #dbm
                  '.pag', #dbm
                  ]
    for ext in extensions:
        if os.path.exists(filename + ext):
            os.remove(filename + ext)

# fixture for "doit.db". create/remove for every test
def pytest_funcarg__depfile(request):
    def create_depfile():
        if hasattr(request, 'param'):
            dep_class = request.param
        else:
            dep_class = Dependency
        remove_db(TESTDB)
        return dep_class(TESTDB)
    def remove_depfile(depfile):
        if not depfile._closed:
            depfile.close()
        remove_db(TESTDB)
    return request.cached_setup(
        setup=create_depfile,
        teardown=remove_depfile,
        scope="function")


def pytest_funcarg__cwd(request):
    """set cwd to parent folder of this file."""
    def set_cwd():
        cwd = {}
        cwd['previous'] = os.getcwd()
        cwd['current'] = os.path.abspath(os.path.dirname(__file__))
        os.chdir(cwd['current'])
        return cwd
    def restore_cwd(cwd):
        os.chdir(cwd['previous'])
    return request.cached_setup(
        setup=set_cwd,
        teardown=restore_cwd,
        scope="function")

@@ -4,16 +4,27 @@
 ort os%0A%0A
+import py%0A%0A
 from doi
@@ -512,16 +512,17 @@
 + ext)%0A%0A
+%0A
 # fixtur
@@ -756,24 +756,25 @@
 endency%0A
+%0A
         
 remove_d
@@ -769,33 +769,319 @@
     
-remove_db(
+# copied from tempdir plugin%0A        name = request._pyfuncitem.name%0A        name = py.std.re.sub(%22%5B%5CW%5D%22, %22_%22, name)%0A        my_tmpdir = request.config._tmpdirhandler.mktemp(name, numbered=True)%0A        return dep_class(os.path.join(my_tmpdir.strpath, %22testdb%22))%0A        # do not use tempfile use 
 TESTDB
-)
 %0A       
  ret
@@ -1068,32 +1068,34 @@
 e TESTDB%0A       
+ #
  return dep_clas
@@ -1100,24 +1100,25 @@
 ass(TESTDB)%0A
+%0A
     def remo
@@ -1216,23 +1216,30 @@
 move_db(
-TESTDB)
+depfile.name)%0A
 %0A    ret

use tempfile on tests so they can run in paralel.