def testShortening(self): """There was a bug (Issue 66) where when a longer name was read and a shorter name was written, the extra characters of the longer name would remain in the entry""" file_ = open('temp.ini', 'w') file_.write(TEST_TEXT) file_.close() self.c.read('temp.ini') self.c.set('second', 'available', 'abcdefghijklmnop') self.c.write('temp.ini') c2 = ConfigParser() c2.read('temp.ini') c2.set('second', 'available', 'short') c2.write('temp.ini') self.c.read('temp.ini') assert self.c.get('second', 'available', '') == 'short', \ self.c.get('second', 'available', '')
class TestConfigParser(unittest.TestCase): """ Tests the main ConfigParser class """ def setUp(self): """ Creates a ConfigParser to play with """ self.c = ConfigParser() def testRead(self): """Ensures that it can read from a file correctly""" file_ = testFile() self.c.read(file_) assert self.c._sections == {'second': {'good': 'yes', 'bad': 'no', 'available': 'yes', 'funny-name_mate': 'crusty the clown'}, 'main': {'running': u'on\t\u0100\u01100', 'testing': 'false', 'two words': 'are better than one', 'no_value': '', 'power': 'on', 'level': '5'} }, self.c._sections def testReadFileName(self): """Can read text""" goodDict = {'second': {'good': 'yes', 'bad': 'no', 'available': 'yes', 'funny-name_mate': 'crusty the clown'}, 'main': {'running': u'on\t\u0100\u01100', 'testing': 'false', 'two words': 'are better than one', 'no_value': '', 'power': 'on', 'level': '5'}} file_ = open('temp.ini', 'w') file_.write(TEST_TEXT) file_.close() self.c.read('temp.ini') assert self.c._sections == goodDict, self.c._sections # Can read unicode filenames self.c = ConfigParser() self.c.read(u'temp.ini') assert self.c._sections == goodDict, self.c._sections # Can read funny string object filenames class MyStr(str): """Simply overrides string to make it a different type""" self.c.read(MyStr('temp.ini')) assert self.c._sections == goodDict, self.c._sections def testWrite(self): """Test that it writes the file nicely""" file_ = testFile() self.c.read(file_) # Remove an option del self.c._sections['main']['testing'] # Change an option self.c._sections['second']['bad'] = 'definately not! ' # Add an option self.c._sections['second']['squishy'] = 'Indeed' # Add a section at the end self.c._sections['middle'] = {'is here': 'yes'} # write the file file_.seek(0) self.c.write(file_) file_.seek(0) result = file_.readlines() result = map(unicode, result, ['utf8']*len(result)) goodResult = ['nosection=here\n', '[main]\n', 'level=5\n', 'power : on\n', u'running =on\t\u0100\u01100\n', 'two words = \tare better than one\n', 'no_value = \n', '\n', '\n', '[second]\n', 'good :yes\n', 'bad:\tdefinately not! \n', '# comment=1\n', '~comment2=2\n', 'available\t= yes\n', 'funny-name_mate: crusty the clown\n', 'squishy = Indeed\n', '\n', '[middle]\n', 'is here = yes'] if result != goodResult: print for good, got in zip(goodResult, result): if good != got: print 'Different', repr(good), repr(got) self.fail('See above printout') def testWriteNewFile(self): """Shouldn't have any trouble writing a write only file""" file_ = open('temp.ini', 'w') self.c.set('main', 'testing', 'de repente') self.c.write(file_) file_.close() file_ = open('temp.ini') try: data = file_.read() assert data == '[main]\ntesting = de repente', data finally: file_.close() os.remove('temp.ini') def testWriteFromFileName(self): """If we pass write a file name, it should open or create that file and write or update it""" if os.path.exists('temp.ini'): os.remove('temp.ini') self.c.set('main', 'testing', 'de repente') self.c.write('temp.ini') file_ = open('temp.ini') try: data = file_.read() assert data == '[main]\ntesting = de repente', data finally: file_.close() os.remove('temp.ini') # Test updating an existing file self.c.set('main', 'testing', 'ok') self.c.write('temp.ini') file_ = open('temp.ini') try: data = file_.read() assert data == '[main]\ntesting = ok', data finally: file_.close() os.remove('temp.ini') def testGet(self): """Tests the get method""" file_ = testFile() self.c.read(file_) assert self.c.get('main', 'testing') == 'false' assert self.c.get('main', 'not exists', 'default') == 'default' self.failUnlessRaises(ValueError, self.c.get, 'main', 'not exists') def testSet(self): """Test the set method""" file_ = testFile() self.c.read(file_) # An existing option self.c.set('main', 'testing', 'perhaps') assert self.c._sections['main']['testing'] == 'perhaps' # A new and numeric option self.c.set('main', 'new option', 4.1) self.assertEquals(self.c._sections['main']['new option'], '4.1') # A new option in a new section self.c.set('new section', 'new option', 4.1) assert self.c._sections['new section']['new option'] == '4.1' def testDel(self): """Should be able to delete a section and/or a value in a section""" file_ = testFile() self.c.read(file_) assert self.c._sections['main']['level'] == '5' self.c.delete('main', 'level') assert not self.c._sections['main'].has_key('level') self.c.delete('main') assert not self.c._sections.has_key('main') def testShortening(self): """There was a bug (Issue 66) where when a longer name was read and a shorter name was written, the extra characters of the longer name would remain in the entry""" file_ = open('temp.ini', 'w') file_.write(TEST_TEXT) file_.close() self.c.read('temp.ini') self.c.set('second', 'available', 'abcdefghijklmnop') self.c.write('temp.ini') c2 = ConfigParser() c2.read('temp.ini') c2.set('second', 'available', 'short') c2.write('temp.ini') self.c.read('temp.ini') assert self.c.get('second', 'available', '') == 'short', \ self.c.get('second', 'available', '') def testUnicodeSet(self): """ Should be able to set unicode option values with both python internal unicode strings or raw string containing utf8 encoded data """ file_ = open('temp.ini', 'w') file_.write(TEST_TEXT) file_.close() self.c.read('temp.ini') self.c.set('main', 'power', '\xc4\x80\xc4\x900') self.c.set('main', 'name', unicode('\xc4\x80\xc4\x900', 'utf8')) self.c.set('newSecy', 'unicode', unicode('\xc4\x80\xc4\x900', 'utf8')) self.c.write('temp.ini') c2 = ConfigParser() c2.read('temp.ini') val = unicode('\xc4\x80\xc4\x900', 'utf8') self.assertEquals(c2.main.power, val) self.assertEquals(c2.main.name, val) self.assertEquals(c2.newSecy.unicode, val) def testUnicodeFileName(self): """ Should be able to write to unicode filenames """ # Invent a unicode filename dirName = unicode('\xc4\x80\xc4\x900', 'utf8') fn = os.path.join(dirName, dirName) fn += '.ini' if not os.path.exists(dirName): os.mkdir(dirName) # Write some test data to our unicode file file_ = open(fn, 'wb') file_.write(TEST_TEXT) file_.close() # See if we can read and write it self.c.read(fn) self.assertEquals(self.c.main.power, 'on') self.c.main.power = 'off' self.c.write() # Check that it was written ok c2 = ConfigParser() c2.read(fn) self.assertEquals(c2.main.power, 'off') # Clean up os.remove(fn) os.rmdir(dirName)