Explorar o código

Add some testing for configuration mechanism

oz123 %!s(int64=10) %!d(string=hai) anos
pai
achega
c742f1d6d2
Modificáronse 2 ficheiros con 37 adicións e 9 borrados
  1. 3 5
      pwman/__init__.py
  2. 34 4
      pwman/tests/db_tests.py

+ 3 - 5
pwman/__init__.py

@@ -104,7 +104,7 @@ def parser_options(formatter_class=argparse.HelpFormatter):
     return parser
 
 
-def get_conf(args):
+def get_conf_file(args):
     config_dir = os.path.expanduser("~/.pwman")
 
     if not os.path.isdir(config_dir):
@@ -162,8 +162,7 @@ def set_algorithm(args, config):
 
 
 def get_conf_options(args, OSX):
-
-    config = get_conf(args)
+    config = get_conf_file(args)
     xselpath = config.get_value("Global", "xsel")
     if not xselpath:
         set_xsel(config, OSX)
@@ -174,8 +173,7 @@ def get_conf_options(args, OSX):
     set_algorithm(args, config)
     dbtype = config.get_value("Database", "type")
     if not dbtype:
-        print("Could not read the Database type from the config!")
-        sys.exit(1)
+        raise Exception("Could not read the Database type from the config!")
 
     return xselpath, dbtype
 

+ 34 - 4
pwman/tests/db_tests.py

@@ -30,6 +30,7 @@ from pwman import which, default_config
 from pwman.ui import get_ui_platform
 from pwman.ui.base import get_pass_conf
 from pwman.ui.tools import CMDLoop, CliMenuItem
+from pwman import parser_options, get_conf_options
 
 import unittest
 import StringIO
@@ -46,7 +47,7 @@ def node_factory(username, password, url, notes, tags=None):
     node.notes = notes
     tags = [TagNew(tn) for tn in tags]
     node.tags = tags
-    
+
     return node
 
 # TODO: fix hard coded db versions! 0.4 should be replaced with
@@ -137,6 +138,7 @@ class SetupTester(object):
 
 
 class DBTests(unittest.TestCase):
+
     """test everything related to db"""
 
     def setUp(self):
@@ -236,8 +238,8 @@ class TestDBFalseConfig(unittest.TestCase):
         self.fname = config._conf['Database'].pop('filename')
 
     def test_db_missing_conf_parameter(self):
-            self.assertRaises(DatabaseException, factory.create,
-                              'SQLite', 0.4)
+        self.assertRaises(DatabaseException, factory.create,
+                          'SQLite', 0.4)
 
     def tearDown(self):
         config.set_value('Database', 'filename', self.fname)
@@ -246,6 +248,7 @@ class TestDBFalseConfig(unittest.TestCase):
 
 
 class CLITests(unittest.TestCase):
+
     """
     test command line functionallity
     """
@@ -308,7 +311,7 @@ class CLITests(unittest.TestCase):
     # the node is still not added !
 
     def test_add_new_entry(self):
-        #node = NewNode('alice', 'dough!', 'example.com',
+        # node = NewNode('alice', 'dough!', 'example.com',
         #               'lorem impsum')
 
         node = NewNode()
@@ -456,3 +459,30 @@ class ConfigTest(unittest.TestCase):
         self.assertRaises(config.ConfigException, config.load,
                           'TestConfig.ini')
         os.remove('TestConfig.ini')
+
+    def test_all_config(self):
+        sys.argv = ['pwman3']
+        default_config['Database'] = {'type': '',
+                                      'filename': ''}
+        _save_conf = config._conf.copy()
+        config._conf = {}
+        with open('dummy.conf', 'w') as dummy:
+            dummy.write("""
+[Encryption]
+
+[Readline]
+
+[Global]
+xsel = /usr/bin/xsel
+colors = yes
+umask = 0100
+cls_timeout = 5
+
+[Database]
+""")
+        sys.argv = ['pwman3', '-d', '', '-c', 'dummy.conf']
+        p2 = parser_options()
+        args = p2.parse_args()
+        self.assertRaises(Exception, get_conf_options, args, False)
+        config._conf = _save_conf.copy()
+        os.unlink('dummy.conf')