Back to index

enigmail  1.4.3
unit-nsinstall.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 nsinstall import nsinstall
00009 
00010 class TestNsinstall(unittest.TestCase):
00011     """
00012     Unit tests for nsinstall.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 mkdirs(self, dir):
00029         d = os.path.join(self.tmpdir, dir)
00030         os.makedirs(d)
00031         return d
00032 
00033     def test_nsinstall_D(self):
00034         "Test nsinstall -D <dir>"
00035         testdir = os.path.join(self.tmpdir, "test")
00036         self.assertEqual(nsinstall(["-D", testdir]), 0)
00037         self.assert_(os.path.isdir(testdir))
00038 
00039     def test_nsinstall_basic(self):
00040         "Test nsinstall <file> <dir>"
00041         testfile = self.touch("testfile")
00042         testdir = self.mkdirs("testdir")
00043         self.assertEqual(nsinstall([testfile, testdir]), 0)
00044         self.assert_(os.path.isfile(os.path.join(testdir, "testfile")))
00045 
00046     def test_nsinstall_basic_recursive(self):
00047         "Test nsinstall <dir> <dest dir>"
00048         sourcedir = self.mkdirs("sourcedir")
00049         self.touch("testfile", sourcedir)
00050         destdir = self.mkdirs("destdir")
00051         self.assertEqual(nsinstall([sourcedir, destdir]), 0)
00052         testdir = os.path.join(destdir, "sourcedir")
00053         self.assert_(os.path.isdir(testdir))
00054         self.assert_(os.path.isfile(os.path.join(testdir, "testfile")))
00055 
00056     def test_nsinstall_multiple(self):
00057         "Test nsinstall <three files> <dest dir>"
00058         testfiles = [self.touch("testfile1"),
00059                      self.touch("testfile2"),
00060                      self.touch("testfile3")]
00061         testdir = self.mkdirs("testdir")
00062         self.assertEqual(nsinstall(testfiles + [testdir]), 0)
00063         for f in testfiles:
00064             self.assert_(os.path.isfile(os.path.join(testdir,
00065                                                      os.path.basename(f))))
00066 
00067     def test_nsinstall_dir_exists(self):
00068         "Test nsinstall <dir> <dest dir>, where <dest dir>/<dir> already exists"
00069         srcdir = self.mkdirs("test")
00070         destdir = self.mkdirs("testdir/test")
00071         self.assertEqual(nsinstall([srcdir, os.path.dirname(destdir)]), 0)
00072         self.assert_(os.path.isdir(destdir))
00073 
00074     def test_nsinstall_t(self):
00075         "Test that nsinstall -t works (preserve timestamp)"
00076         testfile = self.touch("testfile")
00077         testdir = self.mkdirs("testdir")
00078         # set mtime to now - 30 seconds
00079         t = int(time.time()) - 30
00080         os.utime(testfile, (t, t))
00081         self.assertEqual(nsinstall(["-t", testfile, testdir]), 0)
00082         destfile = os.path.join(testdir, "testfile")
00083         self.assert_(os.path.isfile(destfile))
00084         self.assertEqual(os.stat(testfile).st_mtime,
00085                          os.stat(destfile).st_mtime)
00086 
00087     if sys.platform != "win32":
00088         # can't run this test on windows, don't have real file modes there
00089         def test_nsinstall_m(self):
00090             "Test that nsinstall -m works (set mode)"
00091             testfile = self.touch("testfile")
00092             mode = 0600
00093             os.chmod(testfile, mode)
00094             testdir = self.mkdirs("testdir")
00095             self.assertEqual(nsinstall(["-m", "%04o" % mode, testfile, testdir]), 0)
00096             destfile = os.path.join(testdir, "testfile")
00097             self.assert_(os.path.isfile(destfile))
00098             self.assertEqual(os.stat(testfile).st_mode,
00099                              os.stat(destfile).st_mode)
00100 
00101     def test_nsinstall_d(self):
00102         "Test that nsinstall -d works (create directories in target)"
00103         # -d makes no sense to me, but ok!
00104         testfile = self.touch("testfile")
00105         testdir = self.mkdirs("testdir")
00106         destdir = os.path.join(testdir, "subdir")
00107         self.assertEqual(nsinstall(["-d", testfile, destdir]), 0)
00108         self.assert_(os.path.isdir(os.path.join(destdir, "testfile")))
00109 
00110     #TODO: implement -R, -l, -L and test them!
00111 
00112 if __name__ == '__main__':
00113   unittest.main()