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