Переглянути джерело

one the road for better unittesting

properly delete old test database when invoked ...
remove reduntant code ...
oz123 11 роки тому
батько
коміт
1c68513a43
2 змінених файлів з 25 додано та 105 видалено
  1. 17 5
      pwman/tests/db_tests.py
  2. 8 100
      pwman/tests/test_pwman.py

+ 17 - 5
pwman/tests/db_tests.py

@@ -36,7 +36,10 @@ default_config = {'Global': {'umask': '0100', 'colors': 'yes',
                              'cls_timeout': '5'
                              },
                   'Database': {'type': 'SQLite',
-                               'filename': os.path.join("tests", "pwman.db")},
+                               'filename':
+                               os.path.join(os.path.dirname(__file__),
+                                            "test.pwman.db")},
+
                   'Encryption': {'algorithm': 'AES'},
                   'Readline': {'history': os.path.join("tests",
                                                        "history")}
@@ -48,15 +51,20 @@ class SetupTester(object):
     def __init__(self):
         config.set_defaults(default_config)
         if not OSX:
-            xselpath = which("xsel")
-            config.set_value("Global", "xsel", xselpath)
+            self.xselpath = which("xsel")
+            config.set_value("Global", "xsel", self.xselpath)
         else:
-            xselpath = "xsel"
+            self.xselpath = "xsel"
+
+    def clean(self):
+        if os.path.exists(config.get_value('Database', 'filename')):
+            os.remove(config.get_value('Database', 'filename'))
 
+    def create(self):
         dbver = 0.4
         dbtype = config.get_value("Database", "type")
         db = pwman.data.factory.create(dbtype, dbver)
-        self.cli = PwmanCliNew(db, xselpath)
+        self.cli = PwmanCliNew(db, self.xselpath)
 
 
 class DBTests(unittest.TestCase):
@@ -68,6 +76,7 @@ class DBTests(unittest.TestCase):
         self.dbtype = config.get_value("Database", "type")
         self.db = pwman.data.factory.create(self.dbtype, dbver)
         self.tester = SetupTester()
+        self.tester.create()
 
     def test_db_created(self):
         "test that the right db instance was created"
@@ -105,6 +114,9 @@ class DBTests(unittest.TestCase):
         got_tags = self.tester.cli._tags(enc)
         self.assertEqual(2, len(got_tags))
 
+    #def tearDown(self):
+    #    self.tester.clean()
+
 
 class CLITests(unittest.TestCase):
     """test command line functionallity"""

+ 8 - 100
pwman/tests/test_pwman.py

@@ -20,110 +20,18 @@
 # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
 #============================================================================
 import os
-import os.path
-
 import sys
-sys.path.insert(0, os.getcwd())
-_saveconfig = True
-
-import sys
-
-from pwman.util.crypto import CryptoEngine
-
-if 'darwin' in sys.platform:
-    from pwman.ui.mac import PwmanCliMac as PwmanCli
-    from pwman.ui.mac import PwmanCliMacNew as PwmanCliNew
-    OSX = True
-elif 'win' in sys.platform:
-    from pwman.ui.cli import PwmanCli
-    from pwman.ui.win import PwmanCliWinNew as PwmanCliNew
-    OSX = False
-else:
-    from pwman.ui.cli import PwmanCliNew
-    OSX = False
-
-
-import pwman.util.config as config
-import pwman.data.factory
-
-
-def which(cmd):
-    _, cmdname = os.path.split(cmd)
-
-    for path in os.environ["PATH"].split(os.pathsep):
-        cmd = os.path.join(path, cmdname)
-        if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
-            return cmd
-
-    return None
-
-try:
-    # set cls_timout to negative number (e.g. -1) to disable
-    default_config = {'Global': {'umask': '0100', 'colors': 'yes',
-                                 'cls_timeout': '5'
-                                 },
-                      'Database': {'type': 'SQLite',
-                                   'filename':
-                                   os.path.join(os.path.dirname(__file__),
-                                   "test.pwman.db")},
-                      'Encryption': {'algorithm': 'AES'},
-                      'Readline': {'history': os.path.join("tests",
-                                                           "history")}
-                      }
-    config.set_defaults(default_config)
-    if 'win' in sys.platform:
-        try:
-            import colorama
-            colorama.init()
-        except ImportError:
-            config.set_value("Global", "colors", 'no')
-    if not OSX:
-        xselpath = which("xsel")
-        config.set_value("Global", "xsel", xselpath)
-    elif OSX:
-        pbcopypath = which("pbcopy")
-        config.set_value("Global", "xsel", pbcopypath)
-    # set umask before creating/opening any files
-    umask = int(config.get_value("Global", "umask"))
-    os.umask(umask)
-
-    enc = CryptoEngine.get()
-
-    dbtype = config.get_value("Database", "type")
-    # if it is done here, we could do the following:
-    # if db.ver == 0.4 :
-    #     db = pwman.data.factory.create(dbtyp, new_version)
-    # else:
-    #     we use the old code untouched ... insecure, but
-    #     keeps backwards compatibility ...
-    # if the database file exists check it's version
-    # else: force version 0.4
-    if os.path.exists(config.get_value("Database", "filename")):
-        dbver = pwman.data.factory.check_db_version(dbtype)
-        dbver = float(dbver.strip("\'"))
-    else:
-        dbver = 0.4
-    # the method create could create an old instance that
-    # accepts cPickle object or new style instance that
-    # accepts only strings.
-    # The user should be STRONGLY Prompted to CONVERT the
-    # database to the new format using a command line tool.
-    # version 0.5 pwman will depreciate that old and insecure
-    # code ...
-
-    db = pwman.data.factory.create(dbtype, dbver)
-    if dbver >= 0.4:
-        cli = PwmanCliNew(db, xselpath)
-    elif dbver < 0.4:
-        cli = PwmanCli(db, xselpath)
-except SystemExit, e:
-    sys.exit(e)
-
-
 import unittest
-from db_tests import DBTests
+from db_tests import (DBTests, SetupTester)
 from crypto_tests import CryptoTest
 
+# make sure we use local pwman
+sys.path.insert(0, os.getcwd())
+# check if old DB exists, if so remove it.
+# excuted only once when invoked upon import or
+# upon run
+SetupTester().clean()
+
 
 def suite():
     loader = unittest.TestLoader()