浏览代码

Remove all config accessing from testing

oz123 10 年之前
父节点
当前提交
d9aae9bf29
共有 4 个文件被更改,包括 173 次插入155 次删除
  1. 140 143
      pwman/tests/db_tests.py
  2. 1 1
      pwman/tests/test_crypto_engine.py
  3. 5 4
      pwman/tests/test_pwman.py
  4. 27 7
      pwman/tests/test_tools.py

+ 140 - 143
pwman/tests/db_tests.py

@@ -28,11 +28,8 @@ from pwman.util.crypto_engine import CryptoEngine
 from pwman import default_config, set_xsel
 from pwman.ui import get_ui_platform
 from pwman.ui.tools import CMDLoop, CliMenuItem
-from pwman import (parser_options, get_conf_options, get_conf_file, set_umask)
+from pwman import (parser_options, get_conf_options, get_conf, set_umask)
 from pwman.data.database import __DB_FORMAT__
-from pwman.ui.mac import PwmanCliMacNew
-from pwman.ui.win import PwmanCliWinNew
-from collections import namedtuple
 import sys
 import unittest
 if sys.version_info.major > 2:
@@ -86,7 +83,7 @@ class DBTests(unittest.TestCase):
     def setUp(self):
         "test that the right db instance was created"
         dbver = __DB_FORMAT__
-        self.dbtype = config.get_value("Database", "type")
+        self.dbtype = 'SQLite'
         self.db = factory.create(self.dbtype, dbver, testdb)
         self.tester = SetupTester(dbver, testdb)
         self.tester.create()
@@ -176,32 +173,32 @@ class DBTests(unittest.TestCase):
         self.assertEqual("test", db._filename)
 
 
-class TestDBFalseConfig(unittest.TestCase):
-
-    def setUp(self):
-        # filename = default_config['Database'].pop('filename')
-        self.fname1 = default_config['Database'].pop('filename')
-        self.fname = config._conf['Database'].pop('filename')
-
-    @unittest.skip("Legacy test. Database and file should always be given")
-    def test_db_missing_conf_parameter(self):
-        self.assertRaises(DatabaseException, factory.create,
-                          'SQLite', __DB_FORMAT__)
-
-    def test_get_ui_platform(self):
-        uiclass, osx = get_ui_platform('windows')
-        self.assertFalse(osx)
-        self.assertEqual(uiclass.__name__, PwmanCliWinNew.__name__)
-        uiclass, osx = get_ui_platform('darwin')
-        self.assertTrue(osx)
-        self.assertEqual(uiclass.__name__, PwmanCliMacNew.__name__)
-        del(uiclass)
-        del(osx)
-
-    def tearDown(self):
-        config.set_value('Database', 'filename', self.fname)
-        default_config['Database']['filename'] = self.fname1
-        config._conf['Database']['filename'] = self.fname
+#class TestDBFalseConfig(unittest.TestCase):
+#
+#    def setUp(self):
+#        # filename = default_config['Database'].pop('filename')
+#        self.fname1 = default_config['Database'].pop('filename')
+#        self.fname = config._conf['Database'].pop('filename')
+#
+#    @unittest.skip("Legacy test. Database and file should always be given")
+#    def test_db_missing_conf_parameter(self):
+#        self.assertRaises(DatabaseException, factory.create,
+#                          'SQLite', __DB_FORMAT__)
+#
+#    def test_get_ui_platform(self):
+#        uiclass, osx = get_ui_platform('windows')
+#        self.assertFalse(osx)
+#        self.assertEqual(uiclass.__name__, PwmanCliWinNew.__name__)
+#        uiclass, osx = get_ui_platform('darwin')
+#        self.assertTrue(osx)
+#        self.assertEqual(uiclass.__name__, PwmanCliMacNew.__name__)
+#        del(uiclass)
+#        del(osx)
+#
+#    def tearDown(self):
+#        config.set_value('Database', 'filename', self.fname)
+#        default_config['Database']['filename'] = self.fname1
+#        config._conf['Database']['filename'] = self.fname
 
 
 class CLITests(unittest.TestCase):
@@ -212,7 +209,7 @@ class CLITests(unittest.TestCase):
 
     def setUp(self):
         "test that the right db instance was created"
-        self.dbtype = config.get_value("Database", "type")
+        self.dbtype = 'SQLite'
         self.db = factory.create(self.dbtype, __DB_FORMAT__, testdb)
         self.tester = SetupTester(__DB_FORMAT__, testdb)
         self.tester.create()
@@ -349,7 +346,7 @@ class CLITests(unittest.TestCase):
         self.tester.cli._db.editnode(2, node)
 
     def test_get_pass_conf(self):
-        numerics, leet, s_chars = get_pass_conf()
+        numerics, leet, s_chars = get_pass_conf(self.tester.cli.config)
         self.assertFalse(numerics)
         self.assertFalse(leet)
         self.assertFalse(s_chars)
@@ -393,122 +390,122 @@ class FactoryTest(unittest.TestCase):
         self.assertRaises(DatabaseException, factory.create, 'UNKNOWN')
 
 
-class ConfigTest(unittest.TestCase):
-
-    def setUp(self):
-        "test that the right db instance was created"
-        dbver = 0.4
-        self.dbtype = config.get_value("Database", "type")
-        self.db = factory.create(self.dbtype, dbver, testdb)
-        self.tester = SetupTester(dbver, testdb)
-        self.tester.create()
-        self.orig_config = config._conf.copy()
-        self.orig_config['Encryption'] = {'algorithm': 'AES'}
-
-    def test_config_write(self):
-        _filename = os.path.join(os.path.dirname(__file__),
-                                 'testing_config')
-        config._file = _filename
-        config.save(_filename)
-        self.assertTrue(_filename)
-        os.remove(_filename)
-
-    def test_config_write_with_none(self):
-        _filename = os.path.join(os.path.dirname(__file__),
-                                 'testing_config')
-        config._file = _filename
-        config.save()
-        self.assertTrue(os.path.exists(_filename))
-        os.remove(_filename)
-
-    def test_write_no_permission(self):
-        # this test will pass if you run as root ...
-        # assuming you are not doing something like that
-        self.assertRaises(config.ConfigException, config.save,
-                          '/root/test_config')
-
-    def test_add_default(self):
-        config.add_defaults({'Section1': {'name': 'value'}})
-        self.assertIn('Section1', config._defaults)
-        config._defaults.pop('Section1')
-
-    def test_get_conf(self):
-        cnf = config.get_conf()
-        cnf_keys = cnf.keys()
-        self.assertTrue('Encryption' in cnf_keys)
-        self.assertTrue('Readline' in cnf_keys)
-        self.assertTrue('Global' in cnf_keys)
-        self.assertTrue('Database' in cnf_keys)
-
-    def test_load_conf(self):
-        self.assertRaises(config.ConfigException, config.load, 'NoSuchFile')
-        # Everything should be ok
-        config.save('TestConfig.ini')
-        config.load('TestConfig.ini')
+#class ConfigTest(unittest.TestCase):
+#
+#    def setUp(self):
+#        "test that the right db instance was created"
+#        dbver = 0.4
+#        self.dbtype = config.get_value("Database", "type")
+#        self.db = factory.create(self.dbtype, dbver, testdb)
+#        self.tester = SetupTester(dbver, testdb)
+#        self.tester.create()
+#        self.orig_config = config._conf.copy()
+#        self.orig_config['Encryption'] = {'algorithm': 'AES'}
+#
+#    def test_config_write(self):
+#        _filename = os.path.join(os.path.dirname(__file__),
+#                                 'testing_config')
+#        config._file = _filename
+#        config.save(_filename)
+#        self.assertTrue(_filename)
+#        os.remove(_filename)
+#
+#    def test_config_write_with_none(self):
+#        _filename = os.path.join(os.path.dirname(__file__),
+#                                 'testing_config')
+#        config._file = _filename
+#        config.save()
+#        self.assertTrue(os.path.exists(_filename))
+#        os.remove(_filename)
+
+#    def test_write_no_permission(self):
+#        # this test will pass if you run as root ...
+#        # assuming you are not doing something like that
+#        self.assertRaises(config.ConfigException, config.save,
+#                          '/root/test_config')
+
+#    def test_add_default(self):
+#        config.add_defaults({'Section1': {'name': 'value'}})
+#        self.assertIn('Section1', config._defaults)
+#        config._defaults.pop('Section1')
+
+#    def test_get_conf(self):
+#        cnf = config.get_conf()
+#        cnf_keys = cnf.keys()
+#        self.assertTrue('Encryption' in cnf_keys)
+#        self.assertTrue('Readline' in cnf_keys)
+#        self.assertTrue('Global' in cnf_keys)
+#        self.assertTrue('Database' in cnf_keys)
+
+#    def test_load_conf(self):
+#        self.assertRaises(config.ConfigException, config.load, 'NoSuchFile')
+#        # Everything should be ok
+#        config.save('TestConfig.ini')
+#        config.load('TestConfig.ini')
         # let's corrupt the file
-        cfg = open('TestConfig.ini', 'w')
-        cfg.write('Corruption')
-        cfg.close()
-        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(dummyfile)
-        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')
-
-    def test_set_xsel(self):
-        set_xsel(config, False)
-
-        set_xsel(config, True)
-        if sys.platform == 'linux2':
-            self.assertEqual(None, config._conf['Global']['xsel'])
-
-    def test_get_conf_file(self):
-        Args = namedtuple('args', 'cfile')
-        args = Args(cfile='nosuchfile')
+#        cfg = open('TestConfig.ini', 'w')
+#        cfg.write('Corruption')
+#        cfg.close()
+#        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(dummyfile)
+#        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')
+
+#    def test_set_xsel(self):
+#        set_xsel(config, False)
+
+#        set_xsel(config, True)
+#        if sys.platform == 'linux2':
+#            self.assertEqual(None, config._conf['Global']['xsel'])
+
+#    def test_get_conf_file(self):
+#        Args = namedtuple('args', 'cfile')
+#        args = Args(cfile='nosuchfile')
         # setting the default
         # in case the user specifies cfile as command line option
         # and that file does not exist!
-        foo = config._conf.copy()
-        get_conf_file(args)
+#        foo = config._conf.copy()
+#        get_conf_file(args)
         # args.cfile does not exist, hence the config values
         # should be the same as in the defaults
-        config.set_config(foo)
-
-    def test_get_conf_options(self):
-        Args = namedtuple('args', 'cfile, dbase, algo')
-        args = Args(cfile='nosuchfile', dbase='dummy.db', algo='AES')
-        self.assertRaises(Exception, get_conf_options, (args, 'False'))
-        config._defaults['Database']['type'] = 'SQLite'
-        # config._conf['Database']['type'] = 'SQLite'
-        xsel, dbtype = get_conf_options(args, 'True')
-        self.assertEqual(dbtype, 'SQLite')
-
-    def test_set_conf(self):
-        set_conf_f = getattr(config, 'set_conf')
-        private_conf = getattr(config, '_conf')
-        set_conf_f({'Config': 'OK'})
-        self.assertDictEqual({'Config': 'OK'}, config._conf)
-        config._conf = private_conf
-
-    def test_umask(self):
-        config._defaults = {'Global': {}}
-        self.assertRaises(config.ConfigException, set_umask, config)
-
-    def tearDown(self):
-        config._conf = self.orig_config.copy()
+#        config.set_config(foo)
+
+#   def test_get_conf_options(self):
+#       Args = namedtuple('args', 'cfile, dbase, algo')
+#        args = Args(cfile='nosuchfile', dbase='dummy.db', algo='AES')
+#        self.assertRaises(Exception, get_conf_options, (args, 'False'))
+#        config._defaults['Database']['type'] = 'SQLite'
+#        # config._conf['Database']['type'] = 'SQLite'
+#        xsel, dbtype = get_conf_options(args, 'True')
+#        self.assertEqual(dbtype, 'SQLite')
+
+#    def test_set_conf(self):
+#        set_conf_f = getattr(config, 'set_conf')
+#        private_conf = getattr(config, '_conf')
+#        set_conf_f({'Config': 'OK'})
+#        self.assertDictEqual({'Config': 'OK'}, config._conf)
+#        config._conf = private_conf
+
+#    def test_umask(self):
+#        config._defaults = {'Global': {}}
+#        self.assertRaises(config.ConfigException, set_umask, config)
+
+#    def tearDown(self):
+#        config._conf = self.orig_config.copy()
 
 if __name__ == '__main__':
     # make sure we use local pwman

+ 1 - 1
pwman/tests/test_crypto_engine.py

@@ -16,7 +16,7 @@ default_config = {'Global': {'umask': '0100', 'colors': 'yes',
                                                        "history")}
                   }
 
-config.set_defaults(default_config)
+#config.set_defaults(default_config)
 
 give_key = lambda msg: "12345"
 give_wrong_key = lambda msg: "verywrongtkey"

+ 5 - 4
pwman/tests/test_pwman.py

@@ -21,8 +21,9 @@
 import os
 import sys
 import unittest
-from .db_tests import (DBTests, SetupTester, CLITests, ConfigTest,
-                       TestDBFalseConfig, FactoryTest)
+from .db_tests import (DBTests, SetupTester, CLITests, #ConfigTest,
+                       #TestDBFalseConfig,
+                       FactoryTest)
 
 #from .crypto_tests import CryptoTest
 from .test_crypto_engine import CryptoEngineTest
@@ -48,9 +49,9 @@ def suite():
     suite.addTest(loader.loadTestsFromTestCase(DBTests))
     #suite.addTest(loader.loadTestsFromTestCase(CryptoTest))
     suite.addTest(loader.loadTestsFromTestCase(CLITests))
-    suite.addTest(loader.loadTestsFromTestCase(ConfigTest))
+    #suite.addTest(loader.loadTestsFromTestCase(ConfigTest))
     suite.addTest(loader.loadTestsFromTestCase(FactoryTest))
-    suite.addTest(loader.loadTestsFromTestCase(TestDBFalseConfig))
+    #suite.addTest(loader.loadTestsFromTestCase(TestDBFalseConfig))
     suite.addTest(loader.loadTestsFromTestCase(CryptoEngineTest))
     suite.addTest(loader.loadTestsFromTestCase(TestConfig))
     #if 'win' not in sys.platform:

+ 27 - 7
pwman/tests/test_tools.py

@@ -53,14 +53,34 @@ default_config['Database'] = {'type': 'SQLite',
                                            "test.pwman.db")
                               }
 
+with open(os.path.join(os.path.dirname(__file__), 'test.conf'), 'w') as f:
+    f.write("""
+[Encryption]
+algorithm = Blowfish
+
+[Global]
+xsel = /usr/bin/xsel
+colors = yes
+umask = 0100
+cls_timeout = 5
+
+[Database]
+type = SQLite
+""")
+
 
 class SetupTester(object):
 
     def __init__(self, dbver=None, filename=None):
-        config.set_defaults(default_config)
+        self.configp = config.Config(os.path.join(os.path.dirname(__file__),
+                                                  "test.conf"),
+                                     default_config)
+        self.configp.set_value('Database', 'filename',
+                               os.path.join(os.path.dirname(__file__),
+                                            "test.pwman.db"))
         if not OSX:
             self.xselpath = which("xsel")
-            config.set_value("Global", "xsel", self.xselpath)
+            self.configp.set_value("Global", "xsel", self.xselpath)
         else:
             self.xselpath = "xsel"
 
@@ -68,8 +88,8 @@ class SetupTester(object):
         self.filename = filename
 
     def clean(self):
-        if os.path.exists(config.get_value('Database', 'filename')):
-            os.remove(config.get_value('Database', 'filename'))
+        if os.path.exists(self.configp.get_value('Database', 'filename')):
+            os.remove(self.configp.get_value('Database', 'filename'))
 
         if os.path.exists(os.path.join(os.path.dirname(__file__),
                                        'testing_config')):
@@ -77,7 +97,7 @@ class SetupTester(object):
                                    'testing_config'))
 
     def create(self):
-        dbtype = config.get_value("Database", "type")
+        dbtype = 'SQLite'
         db = factory.create(dbtype, self.dbver, self.filename)
-
-        self.cli = PwmanCliNew(db, self.xselpath, DummyCallback)
+        self.cli = PwmanCliNew(db, self.xselpath, DummyCallback,
+                               config_parser=self.configp)