diff -r 78a3d3700233 Lib/idlelib/config.py --- a/Lib/idlelib/config.py Sun Jul 03 19:11:13 2016 -0400 +++ b/Lib/idlelib/config.py Mon Jul 04 00:04:09 2016 -0400 @@ -20,8 +20,9 @@ import os import sys - +import idlelib from configparser import ConfigParser +from io import StringIO from tkinter.font import Font, nametofont class InvalidConfigType(Exception): pass @@ -29,6 +30,8 @@ class InvalidFgBg(Exception): pass class InvalidTheme(Exception): pass +use_userfiles = not idlelib.testing + class IdleConfParser(ConfigParser): """ A ConfigParser specialised for idle configuration file handling @@ -115,17 +118,24 @@ self.set(section, option, value) return True - def RemoveFile(self): - "Remove user config file self.file from disk if it exists." - if os.path.exists(self.file): - os.remove(self.file) + def reset(self): + "Remove sections from userfile for testing." + for section in self.sections(): + self.remove_section(section) + + _load_funcs = {str: ConfigParser.read_string, + dict: ConfigParser.read_dict, + StringIO: ConfigParser.read_file} + + def load(self, source): + "Load user options for testing." + self._load_funcs[type(source)](self, source) def Save(self): """Update user configuration file. Remove empty sections. If resulting config isn't empty, write the file to disk. If config is empty, remove the file from disk if it exists. - """ if not self.IsEmpty(): fname = self.file @@ -139,6 +149,12 @@ else: self.RemoveFile() + def RemoveFile(self): + "Remove user config file self.file from disk if it exists." + if os.path.exists(self.file): + os.remove(self.file) + + class IdleConf: """Hold config parsers for all idle config files in singleton instance. @@ -161,24 +177,13 @@ def CreateConfigHandlers(self): "Populate default and user config parser dictionaries." - #build idle install path - if __name__ != '__main__': # we were imported - idleDir=os.path.dirname(__file__) - else: # we were exec'ed (for testing only) - idleDir=os.path.abspath(sys.path[0]) - userDir=self.GetUserCfgDir() - - defCfgFiles = {} - usrCfgFiles = {} - # TODO eliminate these temporaries by combining loops - for cfgType in self.config_types: #build config file names - defCfgFiles[cfgType] = os.path.join( - idleDir, 'config-' + cfgType + '.def') - usrCfgFiles[cfgType] = os.path.join( - userDir, 'config-' + cfgType + '.cfg') - for cfgType in self.config_types: #create config parsers - self.defaultCfg[cfgType] = IdleConfParser(defCfgFiles[cfgType]) - self.userCfg[cfgType] = IdleUserConfParser(usrCfgFiles[cfgType]) + idledir = os.path.dirname(__file__) + userdir = self.GetUserCfgDir() + for cfg_type in self.config_types: + self.defaultCfg[cfg_type] = IdleConfParser( + os.path.join(idledir, 'config-' + cfg_type + '.def')) + self.userCfg[cfg_type] = IdleUserConfParser( + os.path.join(userdir, 'config-' + cfg_type + '.cfg')) def GetUserCfgDir(self): """Return a filesystem directory for storing user config files. @@ -725,7 +730,8 @@ "Load all configuration files." for key in self.defaultCfg: self.defaultCfg[key].Load() - self.userCfg[key].Load() #same keys + if use_userfiles: + self.userCfg[key].Load() #same keys def SaveUserCfgFiles(self): "Write all loaded user configuration files to disk." @@ -735,6 +741,7 @@ idleConf = IdleConf() + # TODO Revise test output, write expanded unittest # if __name__ == '__main__': diff -r 78a3d3700233 Lib/idlelib/idle_test/test_config.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/idlelib/idle_test/test_config.py Mon Jul 04 00:04:09 2016 -0400 @@ -0,0 +1,33 @@ +"""Unittests for idlelib.config_help.py""" +import unittest + +#import idlelib +#idlelib.testing = True +# As long as idlelib.run imports something that imports config, +# above does not work for individual file run from IDLE. +# Instead, after config import, reset each user cfg, in setUpModule. + +def setUpModule(): + global config, idleconf + from idlelib import config + config.use_userfiles = False + idleconf = config.idleConf + for cfg in idleconf.config_types: + idleconf.userCfg[cfg].reset() + +def tearDownModule(): + config.use_userfiles = True + + +class UserLoadTest(unittest.TestCase): + + def test_user_load(self): + usermain = idleconf.userCfg['main'] + self.assertTrue(not usermain.sections()) + usermain.load('[General]\nautosave=2') + self.assertEqual(usermain['General']['autosave'],'2') + usermain.remove_section('General') + + +if __name__ == '__main__': + unittest.main(verbosity=2, exit=False)