Back to index

enigmail  1.4.3
unit-buildlist.py
Go to the documentation of this file.
00001 import unittest
00002 
00003 import os, sys, os.path, time
00004 from tempfile import mkdtemp
00005 from shutil import rmtree
00006 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
00007 
00008 from buildlist import addEntriesToListFile
00009 
00010 class TestBuildList(unittest.TestCase):
00011   """
00012   Unit tests for buildlist.py
00013   """
00014   def setUp(self):
00015     self.tmpdir = mkdtemp()
00016 
00017   def tearDown(self):
00018     rmtree(self.tmpdir)
00019 
00020   # utility methods for tests
00021   def touch(self, file, dir=None):
00022     if dir is None:
00023       dir = self.tmpdir
00024     f = os.path.join(dir, file)
00025     open(f, 'w').close()
00026     return f
00027 
00028   def assertFileContains(self, filename, l):
00029     """Assert that the lines in the file |filename| are equal
00030     to the contents of the list |l|, in order."""
00031     l = l[:]
00032     f = open(filename, 'r')
00033     lines = [line.rstrip() for line in f.readlines()]
00034     f.close()
00035     for line in lines:
00036       self.assert_(len(l) > 0, "ran out of expected lines! (expected '%s', got '%s')" % (l, lines))
00037       self.assertEqual(line, l.pop(0))
00038     self.assert_(len(l) == 0, "not enough lines in file! (expected '%s', got '%s'" % (l, lines))
00039 
00040   def test_basic(self):
00041     "Test that addEntriesToListFile works when file doesn't exist."
00042     testfile = os.path.join(self.tmpdir, "test.list")
00043     l = ["a", "b", "c"]
00044     addEntriesToListFile(testfile, l)
00045     self.assertFileContains(testfile, l)
00046     # ensure that attempting to add the same entries again doesn't change it
00047     addEntriesToListFile(testfile, l)
00048     self.assertFileContains(testfile, l)
00049 
00050   def test_append(self):
00051     "Test adding new entries."
00052     testfile = os.path.join(self.tmpdir, "test.list")
00053     l = ["a", "b", "c"]
00054     addEntriesToListFile(testfile, l)
00055     self.assertFileContains(testfile, l)
00056     l2 = ["x","y","z"]
00057     addEntriesToListFile(testfile, l2)
00058     l.extend(l2)
00059     self.assertFileContains(testfile, l)
00060 
00061   def test_append_some(self):
00062     "Test adding new entries mixed with existing entries."
00063     testfile = os.path.join(self.tmpdir, "test.list")
00064     l = ["a", "b", "c"]
00065     addEntriesToListFile(testfile, l)
00066     self.assertFileContains(testfile, l)
00067     addEntriesToListFile(testfile, ["a", "x", "c", "z"])
00068     self.assertFileContains(testfile, ["a", "b", "c", "x", "z"])
00069 
00070   def test_add_multiple(self):
00071     """Test that attempting to add the same entry multiple times results in
00072     only one entry being added."""
00073     testfile = os.path.join(self.tmpdir, "test.list")
00074     addEntriesToListFile(testfile, ["a","b","a","a","b"])
00075     self.assertFileContains(testfile, ["a","b"])
00076     addEntriesToListFile(testfile, ["c","a","c","b","c"])
00077     self.assertFileContains(testfile, ["a","b","c"])
00078 
00079 if __name__ == '__main__':
00080   unittest.main()