From 6057b2e645dd3b7a262553d5a547ef45d3fc5d93 Mon Sep 17 00:00:00 2001 From: Neal Norwitz Date: Sun, 24 Feb 2008 18:47:03 +0000 Subject: [PATCH] Create a db_home directory with a unique name so multiple users can run the test simultaneously. The simplest thing I found that worked on both Windows and Unix was to use the PID. It's unique so should be sufficient. This should prevent many of the spurious failures of the automated tests since they run as different users. Also cleanup the directory consistenly in the tearDown methods. It would be nice if someone ensured that the directories are always created with a consistent name. --- Lib/bsddb/test/test_associate.py | 11 ++++------- Lib/bsddb/test/test_basics.py | 14 +++++--------- Lib/bsddb/test/test_compare.py | 6 +++--- Lib/bsddb/test/test_cursor_pget_bug.py | 7 +++---- Lib/bsddb/test/test_dbobj.py | 8 +++----- Lib/bsddb/test/test_dbshelve.py | 9 +++------ Lib/bsddb/test/test_dbtables.py | 4 ++-- Lib/bsddb/test/test_env_close.py | 9 +++------ Lib/bsddb/test/test_join.py | 8 +++----- Lib/bsddb/test/test_lock.py | 4 ++-- Lib/bsddb/test/test_misc.py | 11 ++++------- Lib/bsddb/test/test_pickle.py | 8 +++----- Lib/bsddb/test/test_recno.py | 12 +++++++----- Lib/bsddb/test/test_sequence.py | 8 +++----- Lib/bsddb/test/test_thread.py | 9 +++------ Lib/test/test_bsddb3.py | 13 ++++++++++++- Lib/test/test_support.py | 9 +++++++++ 17 files changed, 72 insertions(+), 78 deletions(-) diff --git a/Lib/bsddb/test/test_associate.py b/Lib/bsddb/test/test_associate.py index fd70c5d4a0..88c1f460ca 100644 --- a/Lib/bsddb/test/test_associate.py +++ b/Lib/bsddb/test/test_associate.py @@ -91,7 +91,7 @@ musicdata = { class AssociateErrorTestCase(unittest.TestCase): def setUp(self): self.filename = self.__class__.__name__ + '.db' - homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) self.homeDir = homeDir try: os.mkdir(homeDir) @@ -106,11 +106,8 @@ class AssociateErrorTestCase(unittest.TestCase): def tearDown(self): self.env.close() self.env = None - import glob - files = glob.glob(os.path.join(self.homeDir, '*')) - for file in files: - os.remove(file) - + from test import test_support + test_support.rmtree(self.homeDir) def test00_associateDBError(self): if verbose: @@ -151,7 +148,7 @@ class AssociateTestCase(unittest.TestCase): def setUp(self): self.filename = self.__class__.__name__ + '.db' - homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) self.homeDir = homeDir try: os.mkdir(homeDir) diff --git a/Lib/bsddb/test/test_basics.py b/Lib/bsddb/test/test_basics.py index 6d0fff15e4..af82cd1b6e 100644 --- a/Lib/bsddb/test/test_basics.py +++ b/Lib/bsddb/test/test_basics.py @@ -5,10 +5,10 @@ various DB flags, etc. import os import errno -import shutil import string import tempfile from pprint import pprint +from test import test_support import unittest import time @@ -53,13 +53,9 @@ class BasicTestCase(unittest.TestCase): def setUp(self): if self.useEnv: - homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) self.homeDir = homeDir - try: - shutil.rmtree(homeDir) - except OSError, e: - # unix returns ENOENT, windows returns ESRCH - if e.errno not in (errno.ENOENT, errno.ESRCH): raise + test_support.rmtree(homeDir) os.mkdir(homeDir) try: self.env = db.DBEnv() @@ -73,7 +69,7 @@ class BasicTestCase(unittest.TestCase): tempfile.tempdir = None # Yes, a bare except is intended, since we're re-raising the exc. except: - shutil.rmtree(homeDir) + test_support.rmtree(homeDir) raise else: self.env = None @@ -97,8 +93,8 @@ class BasicTestCase(unittest.TestCase): def tearDown(self): self.d.close() if self.env is not None: + test_support.rmtree(self.homeDir) self.env.close() - shutil.rmtree(self.homeDir) ## Make a new DBEnv to remove the env files from the home dir. ## (It can't be done while the env is open, nor after it has been ## closed, so we make a new one to do it.) diff --git a/Lib/bsddb/test/test_compare.py b/Lib/bsddb/test/test_compare.py index 5aa830869b..2782e8ccc3 100644 --- a/Lib/bsddb/test/test_compare.py +++ b/Lib/bsddb/test/test_compare.py @@ -52,7 +52,7 @@ class AbstractBtreeKeyCompareTestCase (unittest.TestCase): def setUp (self): self.filename = self.__class__.__name__ + '.db' - homeDir = os.path.join (tempfile.gettempdir(), 'db_home') + homeDir = os.path.join (tempfile.gettempdir(), 'db_home%d'%os.getpid()) self.homeDir = homeDir try: os.mkdir (homeDir) @@ -70,8 +70,8 @@ class AbstractBtreeKeyCompareTestCase (unittest.TestCase): if self.env is not None: self.env.close () self.env = None - import glob - map (os.remove, glob.glob (os.path.join (self.homeDir, '*'))) + from test import test_support + test_support.rmtree(self.homeDir) def addDataToDB (self, data): i = 0 diff --git a/Lib/bsddb/test/test_cursor_pget_bug.py b/Lib/bsddb/test/test_cursor_pget_bug.py index 2e83a6cb1c..95bbe1be1c 100644 --- a/Lib/bsddb/test/test_cursor_pget_bug.py +++ b/Lib/bsddb/test/test_cursor_pget_bug.py @@ -17,7 +17,7 @@ class pget_bugTestCase(unittest.TestCase): db_name = 'test-cursor_pget.db' def setUp(self): - self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) try: os.mkdir(self.homeDir) except os.error: @@ -42,9 +42,8 @@ class pget_bugTestCase(unittest.TestCase): del self.secondary_db del self.primary_db del self.env - for file in glob.glob(os.path.join(self.homeDir, '*')): - os.remove(file) - os.removedirs(self.homeDir) + from test import test_support + test_support.rmtree(self.homeDir) def test_pget(self): cursor = self.secondary_db.cursor() diff --git a/Lib/bsddb/test/test_dbobj.py b/Lib/bsddb/test/test_dbobj.py index dd9909d6d2..071b54724a 100644 --- a/Lib/bsddb/test/test_dbobj.py +++ b/Lib/bsddb/test/test_dbobj.py @@ -1,7 +1,6 @@ import os, string import unittest -import glob import tempfile try: @@ -20,7 +19,7 @@ class dbobjTestCase(unittest.TestCase): db_name = 'test-dbobj.db' def setUp(self): - homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) self.homeDir = homeDir try: os.mkdir(homeDir) except os.error: pass @@ -30,9 +29,8 @@ class dbobjTestCase(unittest.TestCase): del self.db if hasattr(self, 'env'): del self.env - files = glob.glob(os.path.join(self.homeDir, '*')) - for file in files: - os.remove(file) + from test import test_support + test_support.rmtree(self.homeDir) def test01_both(self): class TestDBEnv(dbobj.DBEnv): pass diff --git a/Lib/bsddb/test/test_dbshelve.py b/Lib/bsddb/test/test_dbshelve.py index bd399a431a..d6c997fd20 100644 --- a/Lib/bsddb/test/test_dbshelve.py +++ b/Lib/bsddb/test/test_dbshelve.py @@ -245,7 +245,7 @@ class ThreadHashShelveTestCase(BasicShelveTestCase): class BasicEnvShelveTestCase(DBShelveTestCase): def do_open(self): self.homeDir = homeDir = os.path.join( - tempfile.gettempdir(), 'db_home') + tempfile.gettempdir(), 'db_home%d'%os.getpid()) try: os.mkdir(homeDir) except os.error: pass self.env = db.DBEnv() @@ -262,12 +262,9 @@ class BasicEnvShelveTestCase(DBShelveTestCase): def tearDown(self): + from test import test_support + test_support.rmtree(self.homeDir) self.do_close() - import glob - files = glob.glob(os.path.join(self.homeDir, '*')) - for file in files: - os.remove(file) - class EnvBTreeShelveTestCase(BasicEnvShelveTestCase): diff --git a/Lib/bsddb/test/test_dbtables.py b/Lib/bsddb/test/test_dbtables.py index 0c9cf5fffa..7acfdd12ed 100644 --- a/Lib/bsddb/test/test_dbtables.py +++ b/Lib/bsddb/test/test_dbtables.py @@ -22,7 +22,6 @@ import os, re import tempfile -import shutil try: import cPickle pickle = cPickle @@ -58,7 +57,8 @@ class TableDBTestCase(unittest.TestCase): def tearDown(self): self.tdb.close() - shutil.rmtree(self.testHomeDir) + from test import test_support + test_support.rmtree(self.testHomeDir) def test01(self): tabname = "test01" diff --git a/Lib/bsddb/test/test_env_close.py b/Lib/bsddb/test/test_env_close.py index efc3460f79..409f3dc5c0 100644 --- a/Lib/bsddb/test/test_env_close.py +++ b/Lib/bsddb/test/test_env_close.py @@ -4,7 +4,6 @@ is closed before its DB objects. import os import tempfile -import glob import unittest try: @@ -32,7 +31,7 @@ else: class DBEnvClosedEarlyCrash(unittest.TestCase): def setUp(self): - self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) try: os.mkdir(self.homeDir) except os.error: pass tempfile.tempdir = self.homeDir @@ -40,10 +39,8 @@ class DBEnvClosedEarlyCrash(unittest.TestCase): tempfile.tempdir = None def tearDown(self): - files = glob.glob(os.path.join(self.homeDir, '*')) - for file in files: - os.remove(file) - + from test import test_support + test_support.rmtree(self.homeDir) def test01_close_dbenv_before_db(self): dbenv = db.DBEnv() diff --git a/Lib/bsddb/test/test_join.py b/Lib/bsddb/test/test_join.py index 26c29124a7..f9e7f00b38 100644 --- a/Lib/bsddb/test/test_join.py +++ b/Lib/bsddb/test/test_join.py @@ -47,7 +47,7 @@ class JoinTestCase(unittest.TestCase): def setUp(self): self.filename = self.__class__.__name__ + '.db' - homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) self.homeDir = homeDir try: os.mkdir(homeDir) except os.error: pass @@ -56,10 +56,8 @@ class JoinTestCase(unittest.TestCase): def tearDown(self): self.env.close() - import glob - files = glob.glob(os.path.join(self.homeDir, '*')) - for file in files: - os.remove(file) + from test import test_support + test_support.rmtree(self.homeDir) def test01_join(self): if verbose: diff --git a/Lib/bsddb/test/test_lock.py b/Lib/bsddb/test/test_lock.py index 18469a19c2..28620799c8 100644 --- a/Lib/bsddb/test/test_lock.py +++ b/Lib/bsddb/test/test_lock.py @@ -2,7 +2,6 @@ TestCases for testing the locking sub-system. """ -import shutil import tempfile import time @@ -37,7 +36,8 @@ class LockingTestCase(unittest.TestCase): def tearDown(self): self.env.close() - shutil.rmtree(self.homeDir) + from test import test_support + test_support.rmtree(self.homeDir) def test01_simple(self): diff --git a/Lib/bsddb/test/test_misc.py b/Lib/bsddb/test/test_misc.py index 3d566f10a9..a9228f9c72 100644 --- a/Lib/bsddb/test/test_misc.py +++ b/Lib/bsddb/test/test_misc.py @@ -17,7 +17,7 @@ except ImportError: class MiscTestCase(unittest.TestCase): def setUp(self): self.filename = self.__class__.__name__ + '.db' - homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) self.homeDir = homeDir try: os.mkdir(homeDir) @@ -25,12 +25,9 @@ class MiscTestCase(unittest.TestCase): pass def tearDown(self): - try: - os.remove(self.filename) - except OSError: - pass - import shutil - shutil.rmtree(self.homeDir) + from test import test_support + test_support.unlink(self.filename) + test_support.rmtree(self.homeDir) def test01_badpointer(self): dbs = dbshelve.open(self.filename) diff --git a/Lib/bsddb/test/test_pickle.py b/Lib/bsddb/test/test_pickle.py index b87cc689ec..10ce6850c6 100644 --- a/Lib/bsddb/test/test_pickle.py +++ b/Lib/bsddb/test/test_pickle.py @@ -7,7 +7,6 @@ except ImportError: cPickle = None import unittest import tempfile -import glob try: # For Pythons w/distutils pybsddb @@ -25,7 +24,7 @@ class pickleTestCase(unittest.TestCase): db_name = 'test-dbobj.db' def setUp(self): - homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) self.homeDir = homeDir try: os.mkdir(homeDir) except os.error: pass @@ -35,9 +34,8 @@ class pickleTestCase(unittest.TestCase): del self.db if hasattr(self, 'env'): del self.env - files = glob.glob(os.path.join(self.homeDir, '*')) - for file in files: - os.remove(file) + from test import test_support + test_support.rmtree(self.homeDir) def _base_test_pickle_DBError(self, pickle): self.env = db.DBEnv() diff --git a/Lib/bsddb/test/test_recno.py b/Lib/bsddb/test/test_recno.py index 9630db19d0..1074a76284 100644 --- a/Lib/bsddb/test/test_recno.py +++ b/Lib/bsddb/test/test_recno.py @@ -24,12 +24,13 @@ letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' class SimpleRecnoTestCase(unittest.TestCase): def setUp(self): self.filename = tempfile.mktemp() + self.homeDir = None def tearDown(self): - try: - os.remove(self.filename) - except OSError, e: - if e.errno <> errno.EEXIST: raise + from test import test_support + test_support.unlink(self.filename) + if self.homeDir: + test_support.rmtree(self.homeDir) def test01_basic(self): d = db.DB() @@ -202,7 +203,8 @@ class SimpleRecnoTestCase(unittest.TestCase): just a line in the file, but you can set a different record delimiter if needed. """ - homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) + self.homeDir = homeDir source = os.path.join(homeDir, 'test_recno.txt') if not os.path.isdir(homeDir): os.mkdir(homeDir) diff --git a/Lib/bsddb/test/test_sequence.py b/Lib/bsddb/test/test_sequence.py index d7029e5cff..86c58b0c05 100644 --- a/Lib/bsddb/test/test_sequence.py +++ b/Lib/bsddb/test/test_sequence.py @@ -1,7 +1,6 @@ import unittest import os import tempfile -import glob try: # For Pythons w/distutils pybsddb @@ -13,7 +12,7 @@ except ImportError: class DBSequenceTest(unittest.TestCase): def setUp(self): self.int_32_max = 0x100000000 - self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) try: os.mkdir(self.homeDir) except os.error: @@ -38,9 +37,8 @@ class DBSequenceTest(unittest.TestCase): self.dbenv.close() del self.dbenv - files = glob.glob(os.path.join(self.homeDir, '*')) - for file in files: - os.remove(file) + from test import test_support + test_support.rmtree(self.homeDir) def test_get(self): self.seq = db.DBSequence(self.d, flags=0) diff --git a/Lib/bsddb/test/test_thread.py b/Lib/bsddb/test/test_thread.py index d14f6da1fa..8a8b313762 100644 --- a/Lib/bsddb/test/test_thread.py +++ b/Lib/bsddb/test/test_thread.py @@ -5,7 +5,6 @@ import os import sys import time import errno -import shutil import tempfile from random import random @@ -52,7 +51,7 @@ class BaseThreadedTestCase(unittest.TestCase): if verbose: dbutils._deadlock_VerboseFile = sys.stdout - homeDir = os.path.join(tempfile.gettempdir(), 'db_home') + homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) self.homeDir = homeDir try: os.mkdir(homeDir) @@ -69,12 +68,10 @@ class BaseThreadedTestCase(unittest.TestCase): self.d.open(self.filename, self.dbtype, self.dbopenflags|db.DB_CREATE) def tearDown(self): + from test import test_support + test_support.rmtree(self.homeDir) self.d.close() self.env.close() - try: - shutil.rmtree(self.homeDir) - except OSError, e: - if e.errno != errno.EEXIST: raise def setEnvOpts(self): pass diff --git a/Lib/test/test_bsddb3.py b/Lib/test/test_bsddb3.py index 5a4258e09b..27a0900535 100644 --- a/Lib/test/test_bsddb3.py +++ b/Lib/test/test_bsddb3.py @@ -2,10 +2,12 @@ """ Run all test cases. """ +import os import sys +import tempfile import time import unittest -from test.test_support import requires, verbose, run_unittest, unlink +from test.test_support import requires, verbose, run_unittest, unlink, rmtree # When running as a script instead of within the regrtest framework, skip the # requires test, since it's obvious we want to run them. @@ -85,6 +87,15 @@ def suite(): # For invocation through regrtest def test_main(): run_unittest(suite()) + db_home = os.path.join(tempfile.gettempdir(), 'db_home') + # The only reason to remove db_home is in case if there is an old + # one lying around. This might be by a different user, so just + # ignore errors. We should always make a unique name now. + try: + rmtree(db_home) + except: + pass + rmtree('db_home%d' % os.getpid()) # For invocation as a script if __name__ == '__main__': diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index d36d81cfce..207160665f 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -9,6 +9,7 @@ import socket import sys import os import os.path +import shutil import warnings import unittest @@ -64,6 +65,14 @@ def unlink(filename): except OSError: pass +def rmtree(path): + try: + shutil.rmtree(path) + except OSError, e: + # Unix returns ENOENT, Windows returns ESRCH. + if e.errno not in (errno.ENOENT, errno.ESRCH): + raise + def forget(modname): '''"Forget" a module was ever imported by removing it from sys.modules and deleting any .pyc and .pyo files.''' -- 2.50.1