Commits

Anonymous committed 4a551ed

fixed keyring.core when the cfg doensn't exists or is not complete

  • Participants
  • Parent commits c59fdce

Comments (0)

Files changed (3)

 -------
 
 ---
+0.3
+---
+
+* Fixed keyring.core when the user doesn't have a cfg, or is not
+  properly configured.
+
+---
 0.2
 ---
 
 import ConfigParser
 import imp
 import sys
-import backend
+
+from keyring import logger
+from keyring import backend
 
 def set_keyring(keyring):
     """Set current keyring backend.
         config.read(keyring_cfg)
         # load the keyring-path option
         try:
-            keyring_path = config.get("backend", "keyring-path").strip()
+            if config.has_section("backend"):
+                keyring_path = config.get("backend", "keyring-path").strip()
+            else:
+                keyring_path = None
         except ConfigParser.NoOptionError:
             keyring_path = None
+
         # load the keyring class name, and load it
         try:
-            keyring_name = config.get("backend", "default-keyring").strip()
+            if config.has_section("backend"):
+                keyring_name = config.get("backend", "default-keyring").strip()
+            else:
+                raise ConfigParser.NoOptionError('backend', 'default-keyring')
 
             def load_module(name, path):
                 """Load the specified module from the disk.

keyring/tests/test_core.py

 
 Created by Kang Zhang on 2009-08-09
 """
-
 import unittest
 import os
 import sys
+import tempfile
+import shutil
+
 import keyring.backend
 import keyring.core
 
 
         os.remove(KEYRINGRC)
 
+    def test_load_config(self):
+        tempdir = tempfile.mkdtemp()
+        old_location = os.getcwd()
+        os.chdir(tempdir)
+        personal_cfg = os.path.join(os.path.expanduser("~"), "keyringrc.cfg")
+        if os.path.exists(personal_cfg):
+            os.rename(personal_cfg, personal_cfg+'.old')
+            personal_renamed = True
+        else:
+            personal_renamed = False
+
+        # loading with an empty environment
+        keyring.core.load_config()
+
+        # loading with a file that doesn't have a backend section
+        cfg = os.path.join(tempdir, "keyringrc.cfg")
+        f = open(cfg, 'w')
+        f.write('[keyring]')
+        f.close()
+        keyring.core.load_config()
+
+        # loading with a file that doesn't have a default-keyring value
+        cfg = os.path.join(tempdir, "keyringrc.cfg")
+        f = open(cfg, 'w')
+        f.write('[backend]')
+        f.close()
+        keyring.core.load_config()
+
+        os.chdir(old_location)
+        shutil.rmtree(tempdir)
+        if personal_renamed:
+            os.rename(personal_cfg+'.old', personal_cfg)
 
 def test_suite():
     suite = unittest.TestSuite()