1 from __future__
import absolute_import
9 from tempfile
import mkdtemp
10 from shutil
import rmtree
12 from mozprocess
import processhandler
14 from nsinstall
import nsinstall
15 import nsinstall
as nsinstall_module
16 NSINSTALL_PATH
= nsinstall_module
.__file
__
18 # Run the non-ASCII tests on (a) Windows, or (b) any platform with
19 # sys.stdin.encoding set to UTF-8
21 RUN_NON_ASCII_TESTS
= (sys
.platform
== "win32" or
22 (sys
.stdin
.encoding
is not None and
23 codecs
.lookup(sys
.stdin
.encoding
) == codecs
.lookup("utf-8")))
26 class TestNsinstall(unittest
.TestCase
):
28 Unit tests for nsinstall.py
32 self
.tmpdir
= mkdtemp()
35 # Unicode strings means non-ASCII children can be deleted properly on
37 if sys
.stdin
.encoding
is None:
38 tmpdir
= six
.ensure_text(self
.tmpdir
)
40 tmpdir
= six
.ensure_text(self
.tmpdir
, sys
.stdin
.encoding
)
43 # utility methods for tests
44 def touch(self
, file, dir=None):
47 f
= os
.path
.join(dir, file)
51 def mkdirs(self
, dir):
52 d
= os
.path
.join(self
.tmpdir
, dir)
56 def test_nsinstall_D(self
):
57 "Test nsinstall -D <dir>"
58 testdir
= os
.path
.join(self
.tmpdir
, "test")
59 self
.assertEqual(nsinstall(["-D", testdir
]), 0)
60 self
.assert_(os
.path
.isdir(testdir
))
62 def test_nsinstall_basic(self
):
63 "Test nsinstall <file> <dir>"
64 testfile
= self
.touch("testfile")
65 testdir
= self
.mkdirs("testdir")
66 self
.assertEqual(nsinstall([testfile
, testdir
]), 0)
67 self
.assert_(os
.path
.isfile(os
.path
.join(testdir
, "testfile")))
69 def test_nsinstall_basic_recursive(self
):
70 "Test nsinstall <dir> <dest dir>"
71 sourcedir
= self
.mkdirs("sourcedir")
72 self
.touch("testfile", sourcedir
)
73 Xfile
= self
.touch("Xfile", sourcedir
)
74 copieddir
= self
.mkdirs("sourcedir/copieddir")
75 self
.touch("testfile2", copieddir
)
76 Xdir
= self
.mkdirs("sourcedir/Xdir")
77 self
.touch("testfile3", Xdir
)
79 destdir
= self
.mkdirs("destdir")
81 self
.assertEqual(nsinstall([sourcedir
, destdir
,
85 testdir
= os
.path
.join(destdir
, "sourcedir")
86 self
.assert_(os
.path
.isdir(testdir
))
87 self
.assert_(os
.path
.isfile(os
.path
.join(testdir
, "testfile")))
88 self
.assert_(not os
.path
.exists(os
.path
.join(testdir
, "Xfile")))
89 self
.assert_(os
.path
.isdir(os
.path
.join(testdir
, "copieddir")))
90 self
.assert_(os
.path
.isfile(os
.path
.join(
91 testdir
, "copieddir", "testfile2")))
92 self
.assert_(not os
.path
.exists(os
.path
.join(testdir
, "Xdir")))
94 def test_nsinstall_multiple(self
):
95 "Test nsinstall <three files> <dest dir>"
96 testfiles
= [self
.touch("testfile1"),
97 self
.touch("testfile2"),
98 self
.touch("testfile3")]
99 testdir
= self
.mkdirs("testdir")
100 self
.assertEqual(nsinstall(testfiles
+ [testdir
]), 0)
102 self
.assert_(os
.path
.isfile(os
.path
.join(testdir
,
103 os
.path
.basename(f
))))
105 def test_nsinstall_dir_exists(self
):
106 "Test nsinstall <dir> <dest dir>, where <dest dir>/<dir> already exists"
107 srcdir
= self
.mkdirs("test")
108 destdir
= self
.mkdirs("testdir/test")
109 self
.assertEqual(nsinstall([srcdir
, os
.path
.dirname(destdir
)]), 0)
110 self
.assert_(os
.path
.isdir(destdir
))
112 def test_nsinstall_t(self
):
113 "Test that nsinstall -t works (preserve timestamp)"
114 testfile
= self
.touch("testfile")
115 testdir
= self
.mkdirs("testdir")
116 # set mtime to now - 30 seconds
117 t
= int(time
.time()) - 30
118 os
.utime(testfile
, (t
, t
))
119 self
.assertEqual(nsinstall(["-t", testfile
, testdir
]), 0)
120 destfile
= os
.path
.join(testdir
, "testfile")
121 self
.assert_(os
.path
.isfile(destfile
))
122 self
.assertEqual(os
.stat(testfile
).st_mtime
,
123 os
.stat(destfile
).st_mtime
)
125 @unittest.skipIf(sys
.platform
== "win32", "Windows doesn't have real file modes")
126 def test_nsinstall_m(self
):
127 "Test that nsinstall -m works (set mode)"
128 testfile
= self
.touch("testfile")
130 os
.chmod(testfile
, mode
)
131 testdir
= self
.mkdirs("testdir")
132 self
.assertEqual(nsinstall(["-m", "{0:04o}"
133 .format(mode
), testfile
, testdir
]), 0)
134 destfile
= os
.path
.join(testdir
, "testfile")
135 self
.assert_(os
.path
.isfile(destfile
))
136 self
.assertEqual(os
.stat(testfile
).st_mode
,
137 os
.stat(destfile
).st_mode
)
139 def test_nsinstall_d(self
):
140 "Test that nsinstall -d works (create directories in target)"
141 # -d makes no sense to me, but ok!
142 testfile
= self
.touch("testfile")
143 testdir
= self
.mkdirs("testdir")
144 destdir
= os
.path
.join(testdir
, "subdir")
145 self
.assertEqual(nsinstall(["-d", testfile
, destdir
]), 0)
146 self
.assert_(os
.path
.isdir(os
.path
.join(destdir
, "testfile")))
148 @unittest.skipIf(not RUN_NON_ASCII_TESTS
, "Skipping non ascii tests")
149 def test_nsinstall_non_ascii(self
):
150 "Test that nsinstall handles non-ASCII files"
151 filename
= u
"\u2325\u3452\u2415\u5081"
152 testfile
= self
.touch(filename
)
153 testdir
= self
.mkdirs(u
"\u4241\u1D04\u1414")
154 self
.assertEqual(nsinstall([testfile
.encode("utf-8"),
155 testdir
.encode("utf-8")]), 0)
157 destfile
= os
.path
.join(testdir
, filename
)
158 self
.assert_(os
.path
.isfile(destfile
))
160 # Executing nsinstall.py with python 2 is not supported.
161 @unittest.skipIf(not RUN_NON_ASCII_TESTS
or sys
.version_info
[0] == 2,
162 "Skipping non ascii tests")
163 def test_nsinstall_non_ascii_subprocess(self
):
164 "Test that nsinstall as a subprocess handles non-ASCII files"
165 filename
= u
"\u2325\u3452\u2415\u5081"
166 testfile
= self
.touch(filename
)
167 testdir
= self
.mkdirs(u
"\u4241\u1D04\u1414")
168 # We don't use subprocess because it can't handle Unicode on
169 # Windows <http://bugs.python.org/issue1759845>. mozprocess calls
170 # CreateProcessW directly so it's perfect.
171 p
= processhandler
.ProcessHandlerMixin([sys
.executable
,
177 self
.assertEqual(rv
, 0)
178 destfile
= os
.path
.join(testdir
, filename
)
179 self
.assert_(os
.path
.isfile(destfile
))
181 # TODO: implement -R, -l, -L and test them!
184 if __name__
== '__main__':