瀏覽代碼

Fix bugs in pwman.__init__ and add tests

oz123 10 年之前
父節點
當前提交
4be104fc4b
共有 3 個文件被更改,包括 65 次插入14 次删除
  1. 7 9
      pwman/__init__.py
  2. 56 5
      pwman/tests/test_init.py
  3. 2 0
      pwman/tests/test_pwman.py

+ 7 - 9
pwman/__init__.py

@@ -57,7 +57,7 @@ _db_warn = (u"pwman3 detected that you are using the old database format"
             )
 
 
-def which(cmd):
+def which(cmd):  # pragma: no cover
     _, cmdname = os.path.split(cmd)
 
     for path in os.environ["PATH"].split(os.pathsep):
@@ -80,7 +80,7 @@ default_config = {'Global': {'umask': '0100', 'colors': 'yes',
                   }
 
 
-def parser_options(formatter_class=argparse.HelpFormatter):
+def parser_options(formatter_class=argparse.HelpFormatter):  # pragma: no cover
     parser = argparse.ArgumentParser(prog=appname,
                                      description=description,
                                      formatter_class=formatter_class)
@@ -103,13 +103,13 @@ def get_conf(args):
     return configp
 
 
-def set_xsel(config, OSX):
+def set_xsel(configp, OSX):
     if not OSX:
         xselpath = which("xsel")
-        config.set_value("Global", "xsel", xselpath)
+        configp.set_value("Global", "xsel", xselpath)
     elif OSX:
         pbcopypath = which("pbcopy")
-        config.set_value("Global", "xsel", pbcopypath)
+        configp.set_value("Global", "xsel", pbcopypath)
 
 
 def set_win_colors(config):  # pragma: no cover
@@ -117,12 +117,10 @@ def set_win_colors(config):  # pragma: no cover
         colorama.init()
 
 
-def set_umask(config):
-    umask = config.get_value("Global", "umask")
+def set_umask(configp):
+    umask = configp.get_value("Global", "umask")
     if re.search(r'^\d{4}$', umask):
         os.umask(int(umask))
-    else:
-        raise config.ConfigException("Could not determine umask from config!")
 
 
 def set_db(args, configp):

+ 56 - 5
pwman/tests/test_init.py

@@ -16,12 +16,63 @@
 # ============================================================================
 # Copyright (C) 2014 Oz Nahum Tiram <nahumoz@gmail.com>
 # ============================================================================
-
 import unittest
-import pwman
+from collections import namedtuple
+import os
+import os.path
+from pwman import set_xsel
+from pwman import (get_conf, get_conf_options)
+
+dummyfile = """
+[Encryption]
+
+[Readline]
+
+[Global]
+xsel = /usr/bin/xsel
+colors = yes
+cls_timeout = 5
+
+[Database]
+"""
+
+testdb = os.path.join(os.path.dirname(__file__), "test.pwman.db")
+
+with open('dummy.cfg', 'w') as d:
+    d.write(dummyfile)
+
+
+class TestInit(unittest.TestCase):
+
+    def test_set_xsel(self):
+        Args = namedtuple('args', 'cfile, dbase, algo')
+        args = Args(cfile='dummy.cfg', dbase='dummy.db', algo='AES')
+        xsel, dbtype, configp = get_conf_options(args, 'True')
+        set_xsel(configp, False)
+        set_xsel(configp, True)
+
+    def test_get_conf_file(self):
+        Args = namedtuple('args', 'cfile')
+        args = Args(cfile='dummy.cfg')
+        get_conf(args)
+
+    def test_get_conf_options(self):
+        Args = namedtuple('args', 'cfile, dbase, algo')
+        args = Args(cfile='dummy.cfg', dbase='dummy.db', algo='AES')
+        self.assertRaises(Exception, get_conf_options, (args, 'False'))
+        xsel, dbtype, configp = get_conf_options(args, 'True')
+        self.assertEqual(dbtype, 'SQLite')
+
+   # def test_umask(self):
+   #     Args = namedtuple('args', 'cfile, dbase, algo')
+   #     args = Args(cfile='dummy.cfg', dbase='dummy.db', algo='AES')
+   #     xsel, dbtype, configp = get_conf_options(args, 'True')
 
-class CryptoEngineTest(unittest.TestCase):
 
-    def setUp(self):
+if __name__ == '__main__':
 
-        pwma_dummy = pwman.parser_options()
+    try:
+        unittest.main(verbosity=2, failfast=True)
+    except SystemExit:
+        if os.path.exists(testdb):
+            os.remove(testdb)

+ 2 - 0
pwman/tests/test_pwman.py

@@ -35,6 +35,7 @@ from .test_sqlite import TestSQLite
 from .test_importer import TestImporter
 from .test_factory import TestFactory
 from .test_base_ui import TestBaseUI
+from .test_init import TestInit
 
 if 'win' not in sys.platform:
     from .test_complete_ui import (Ferrum, NEW_DB_PATH)
@@ -65,6 +66,7 @@ def suite():
     suite.addTest(loader.loadTestsFromTestCase(TestImporter))
     suite.addTest(loader.loadTestsFromTestCase(TestFactory))
     suite.addTest(loader.loadTestsFromTestCase(TestBaseUI))
+    suite.addTest(loader.loadTestsFromTestCase(TestInit))
     #if 'win' not in sys.platform:
     #    suite.addTest(loader.loadTestsFromTestCase(Ferrum))
     return suite