Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | import unittest |
michael@0 | 2 | |
michael@0 | 3 | import os, sys, os.path, time |
michael@0 | 4 | from tempfile import mkdtemp |
michael@0 | 5 | from shutil import rmtree |
michael@0 | 6 | import mozunit |
michael@0 | 7 | from mozprocess import processhandler |
michael@0 | 8 | |
michael@0 | 9 | from nsinstall import nsinstall |
michael@0 | 10 | import nsinstall as nsinstall_module |
michael@0 | 11 | NSINSTALL_PATH = nsinstall_module.__file__ |
michael@0 | 12 | |
michael@0 | 13 | # Run the non-ASCII tests on (a) Windows, or (b) any platform with |
michael@0 | 14 | # sys.stdin.encoding set to UTF-8 |
michael@0 | 15 | import codecs |
michael@0 | 16 | RUN_NON_ASCII_TESTS = (sys.platform == "win32" or |
michael@0 | 17 | (sys.stdin.encoding is not None and |
michael@0 | 18 | codecs.lookup(sys.stdin.encoding) == codecs.lookup("utf-8"))) |
michael@0 | 19 | |
michael@0 | 20 | class TestNsinstall(unittest.TestCase): |
michael@0 | 21 | """ |
michael@0 | 22 | Unit tests for nsinstall.py |
michael@0 | 23 | """ |
michael@0 | 24 | def setUp(self): |
michael@0 | 25 | self.tmpdir = mkdtemp() |
michael@0 | 26 | |
michael@0 | 27 | def tearDown(self): |
michael@0 | 28 | # Unicode strings means non-ASCII children can be deleted properly on |
michael@0 | 29 | # Windows |
michael@0 | 30 | if sys.stdin.encoding is None: |
michael@0 | 31 | tmpdir = unicode(self.tmpdir) |
michael@0 | 32 | else: |
michael@0 | 33 | tmpdir = unicode(self.tmpdir, sys.stdin.encoding) |
michael@0 | 34 | rmtree(tmpdir) |
michael@0 | 35 | |
michael@0 | 36 | # utility methods for tests |
michael@0 | 37 | def touch(self, file, dir=None): |
michael@0 | 38 | if dir is None: |
michael@0 | 39 | dir = self.tmpdir |
michael@0 | 40 | f = os.path.join(dir, file) |
michael@0 | 41 | open(f, 'w').close() |
michael@0 | 42 | return f |
michael@0 | 43 | |
michael@0 | 44 | def mkdirs(self, dir): |
michael@0 | 45 | d = os.path.join(self.tmpdir, dir) |
michael@0 | 46 | os.makedirs(d) |
michael@0 | 47 | return d |
michael@0 | 48 | |
michael@0 | 49 | def test_nsinstall_D(self): |
michael@0 | 50 | "Test nsinstall -D <dir>" |
michael@0 | 51 | testdir = os.path.join(self.tmpdir, "test") |
michael@0 | 52 | self.assertEqual(nsinstall(["-D", testdir]), 0) |
michael@0 | 53 | self.assert_(os.path.isdir(testdir)) |
michael@0 | 54 | |
michael@0 | 55 | def test_nsinstall_basic(self): |
michael@0 | 56 | "Test nsinstall <file> <dir>" |
michael@0 | 57 | testfile = self.touch("testfile") |
michael@0 | 58 | testdir = self.mkdirs("testdir") |
michael@0 | 59 | self.assertEqual(nsinstall([testfile, testdir]), 0) |
michael@0 | 60 | self.assert_(os.path.isfile(os.path.join(testdir, "testfile"))) |
michael@0 | 61 | |
michael@0 | 62 | def test_nsinstall_basic_recursive(self): |
michael@0 | 63 | "Test nsinstall <dir> <dest dir>" |
michael@0 | 64 | sourcedir = self.mkdirs("sourcedir") |
michael@0 | 65 | self.touch("testfile", sourcedir) |
michael@0 | 66 | Xfile = self.touch("Xfile", sourcedir) |
michael@0 | 67 | copieddir = self.mkdirs("sourcedir/copieddir") |
michael@0 | 68 | self.touch("testfile2", copieddir) |
michael@0 | 69 | Xdir = self.mkdirs("sourcedir/Xdir") |
michael@0 | 70 | self.touch("testfile3", Xdir) |
michael@0 | 71 | |
michael@0 | 72 | destdir = self.mkdirs("destdir") |
michael@0 | 73 | |
michael@0 | 74 | self.assertEqual(nsinstall([sourcedir, destdir, |
michael@0 | 75 | '-X', Xfile, |
michael@0 | 76 | '-X', Xdir]), 0) |
michael@0 | 77 | |
michael@0 | 78 | testdir = os.path.join(destdir, "sourcedir") |
michael@0 | 79 | self.assert_(os.path.isdir(testdir)) |
michael@0 | 80 | self.assert_(os.path.isfile(os.path.join(testdir, "testfile"))) |
michael@0 | 81 | self.assert_(not os.path.exists(os.path.join(testdir, "Xfile"))) |
michael@0 | 82 | self.assert_(os.path.isdir(os.path.join(testdir, "copieddir"))) |
michael@0 | 83 | self.assert_(os.path.isfile(os.path.join(testdir, "copieddir", "testfile2"))) |
michael@0 | 84 | self.assert_(not os.path.exists(os.path.join(testdir, "Xdir"))) |
michael@0 | 85 | |
michael@0 | 86 | def test_nsinstall_multiple(self): |
michael@0 | 87 | "Test nsinstall <three files> <dest dir>" |
michael@0 | 88 | testfiles = [self.touch("testfile1"), |
michael@0 | 89 | self.touch("testfile2"), |
michael@0 | 90 | self.touch("testfile3")] |
michael@0 | 91 | testdir = self.mkdirs("testdir") |
michael@0 | 92 | self.assertEqual(nsinstall(testfiles + [testdir]), 0) |
michael@0 | 93 | for f in testfiles: |
michael@0 | 94 | self.assert_(os.path.isfile(os.path.join(testdir, |
michael@0 | 95 | os.path.basename(f)))) |
michael@0 | 96 | |
michael@0 | 97 | def test_nsinstall_dir_exists(self): |
michael@0 | 98 | "Test nsinstall <dir> <dest dir>, where <dest dir>/<dir> already exists" |
michael@0 | 99 | srcdir = self.mkdirs("test") |
michael@0 | 100 | destdir = self.mkdirs("testdir/test") |
michael@0 | 101 | self.assertEqual(nsinstall([srcdir, os.path.dirname(destdir)]), 0) |
michael@0 | 102 | self.assert_(os.path.isdir(destdir)) |
michael@0 | 103 | |
michael@0 | 104 | def test_nsinstall_t(self): |
michael@0 | 105 | "Test that nsinstall -t works (preserve timestamp)" |
michael@0 | 106 | testfile = self.touch("testfile") |
michael@0 | 107 | testdir = self.mkdirs("testdir") |
michael@0 | 108 | # set mtime to now - 30 seconds |
michael@0 | 109 | t = int(time.time()) - 30 |
michael@0 | 110 | os.utime(testfile, (t, t)) |
michael@0 | 111 | self.assertEqual(nsinstall(["-t", testfile, testdir]), 0) |
michael@0 | 112 | destfile = os.path.join(testdir, "testfile") |
michael@0 | 113 | self.assert_(os.path.isfile(destfile)) |
michael@0 | 114 | self.assertEqual(os.stat(testfile).st_mtime, |
michael@0 | 115 | os.stat(destfile).st_mtime) |
michael@0 | 116 | |
michael@0 | 117 | if sys.platform != "win32": |
michael@0 | 118 | # can't run this test on windows, don't have real file modes there |
michael@0 | 119 | def test_nsinstall_m(self): |
michael@0 | 120 | "Test that nsinstall -m works (set mode)" |
michael@0 | 121 | testfile = self.touch("testfile") |
michael@0 | 122 | mode = 0600 |
michael@0 | 123 | os.chmod(testfile, mode) |
michael@0 | 124 | testdir = self.mkdirs("testdir") |
michael@0 | 125 | self.assertEqual(nsinstall(["-m", "{0:04o}" |
michael@0 | 126 | .format(mode), testfile, testdir]), 0) |
michael@0 | 127 | destfile = os.path.join(testdir, "testfile") |
michael@0 | 128 | self.assert_(os.path.isfile(destfile)) |
michael@0 | 129 | self.assertEqual(os.stat(testfile).st_mode, |
michael@0 | 130 | os.stat(destfile).st_mode) |
michael@0 | 131 | |
michael@0 | 132 | def test_nsinstall_d(self): |
michael@0 | 133 | "Test that nsinstall -d works (create directories in target)" |
michael@0 | 134 | # -d makes no sense to me, but ok! |
michael@0 | 135 | testfile = self.touch("testfile") |
michael@0 | 136 | testdir = self.mkdirs("testdir") |
michael@0 | 137 | destdir = os.path.join(testdir, "subdir") |
michael@0 | 138 | self.assertEqual(nsinstall(["-d", testfile, destdir]), 0) |
michael@0 | 139 | self.assert_(os.path.isdir(os.path.join(destdir, "testfile"))) |
michael@0 | 140 | |
michael@0 | 141 | if RUN_NON_ASCII_TESTS: |
michael@0 | 142 | def test_nsinstall_non_ascii(self): |
michael@0 | 143 | "Test that nsinstall handles non-ASCII files" |
michael@0 | 144 | filename = u"\u2325\u3452\u2415\u5081" |
michael@0 | 145 | testfile = self.touch(filename) |
michael@0 | 146 | testdir = self.mkdirs(u"\u4241\u1D04\u1414") |
michael@0 | 147 | self.assertEqual(nsinstall([testfile.encode("utf-8"), |
michael@0 | 148 | testdir.encode("utf-8")]), 0) |
michael@0 | 149 | |
michael@0 | 150 | destfile = os.path.join(testdir, filename) |
michael@0 | 151 | self.assert_(os.path.isfile(destfile)) |
michael@0 | 152 | |
michael@0 | 153 | def test_nsinstall_non_ascii_subprocess(self): |
michael@0 | 154 | "Test that nsinstall as a subprocess handles non-ASCII files" |
michael@0 | 155 | filename = u"\u2325\u3452\u2415\u5081" |
michael@0 | 156 | testfile = self.touch(filename) |
michael@0 | 157 | testdir = self.mkdirs(u"\u4241\u1D04\u1414") |
michael@0 | 158 | # We don't use subprocess because it can't handle Unicode on |
michael@0 | 159 | # Windows <http://bugs.python.org/issue1759845>. mozprocess calls |
michael@0 | 160 | # CreateProcessW directly so it's perfect. |
michael@0 | 161 | p = processhandler.ProcessHandlerMixin([sys.executable, |
michael@0 | 162 | NSINSTALL_PATH, |
michael@0 | 163 | testfile, testdir]) |
michael@0 | 164 | p.run() |
michael@0 | 165 | rv = p.waitForFinish() |
michael@0 | 166 | |
michael@0 | 167 | self.assertEqual(rv, 0) |
michael@0 | 168 | destfile = os.path.join(testdir, filename) |
michael@0 | 169 | self.assert_(os.path.isfile(destfile)) |
michael@0 | 170 | |
michael@0 | 171 | #TODO: implement -R, -l, -L and test them! |
michael@0 | 172 | |
michael@0 | 173 | if __name__ == '__main__': |
michael@0 | 174 | mozunit.main() |