7 from shutil
import rmtree
8 from tempfile
import mkdtemp
11 import nsinstall
as nsinstall_module
13 from nsinstall
import nsinstall
15 NSINSTALL_PATH
= nsinstall_module
.__file
__
17 # Run the non-ASCII tests on (a) Windows, or (b) any platform with
18 # sys.stdin.encoding set to UTF-8
21 RUN_NON_ASCII_TESTS
= sys
.platform
== "win32" or (
22 sys
.stdin
.encoding
is not None
23 and codecs
.lookup(sys
.stdin
.encoding
) == codecs
.lookup("utf-8")
27 class TestNsinstall(unittest
.TestCase
):
29 Unit tests for nsinstall.py
33 self
.tmpdir
= mkdtemp()
36 # Unicode strings means non-ASCII children can be deleted properly on
38 if sys
.stdin
.encoding
is None:
39 tmpdir
= six
.ensure_text(self
.tmpdir
)
41 tmpdir
= six
.ensure_text(self
.tmpdir
, sys
.stdin
.encoding
)
44 # utility methods for tests
45 def touch(self
, file, dir=None):
48 f
= os
.path
.join(dir, file)
52 def mkdirs(self
, dir):
53 d
= os
.path
.join(self
.tmpdir
, dir)
57 def test_nsinstall_D(self
):
58 "Test nsinstall -D <dir>"
59 testdir
= os
.path
.join(self
.tmpdir
, "test")
60 self
.assertEqual(nsinstall(["-D", testdir
]), 0)
61 self
.assert_(os
.path
.isdir(testdir
))
63 def test_nsinstall_basic(self
):
64 "Test nsinstall <file> <dir>"
65 testfile
= self
.touch("testfile")
66 testdir
= self
.mkdirs("testdir")
67 self
.assertEqual(nsinstall([testfile
, testdir
]), 0)
68 self
.assert_(os
.path
.isfile(os
.path
.join(testdir
, "testfile")))
70 def test_nsinstall_basic_recursive(self
):
71 "Test nsinstall <dir> <dest dir>"
72 sourcedir
= self
.mkdirs("sourcedir")
73 self
.touch("testfile", sourcedir
)
74 Xfile
= self
.touch("Xfile", sourcedir
)
75 copieddir
= self
.mkdirs("sourcedir/copieddir")
76 self
.touch("testfile2", copieddir
)
77 Xdir
= self
.mkdirs("sourcedir/Xdir")
78 self
.touch("testfile3", Xdir
)
80 destdir
= self
.mkdirs("destdir")
82 self
.assertEqual(nsinstall([sourcedir
, destdir
, "-X", Xfile
, "-X", Xdir
]), 0)
84 testdir
= os
.path
.join(destdir
, "sourcedir")
85 self
.assert_(os
.path
.isdir(testdir
))
86 self
.assert_(os
.path
.isfile(os
.path
.join(testdir
, "testfile")))
87 self
.assert_(not os
.path
.exists(os
.path
.join(testdir
, "Xfile")))
88 self
.assert_(os
.path
.isdir(os
.path
.join(testdir
, "copieddir")))
89 self
.assert_(os
.path
.isfile(os
.path
.join(testdir
, "copieddir", "testfile2")))
90 self
.assert_(not os
.path
.exists(os
.path
.join(testdir
, "Xdir")))
92 def test_nsinstall_multiple(self
):
93 "Test nsinstall <three files> <dest dir>"
95 self
.touch("testfile1"),
96 self
.touch("testfile2"),
97 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
, os
.path
.basename(f
))))
104 def test_nsinstall_dir_exists(self
):
105 "Test nsinstall <dir> <dest dir>, where <dest dir>/<dir> already exists"
106 srcdir
= self
.mkdirs("test")
107 destdir
= self
.mkdirs("testdir/test")
108 self
.assertEqual(nsinstall([srcdir
, os
.path
.dirname(destdir
)]), 0)
109 self
.assert_(os
.path
.isdir(destdir
))
111 def test_nsinstall_t(self
):
112 "Test that nsinstall -t works (preserve timestamp)"
113 testfile
= self
.touch("testfile")
114 testdir
= self
.mkdirs("testdir")
115 # set mtime to now - 30 seconds
116 t
= int(time
.time()) - 30
117 os
.utime(testfile
, (t
, t
))
118 self
.assertEqual(nsinstall(["-t", testfile
, testdir
]), 0)
119 destfile
= os
.path
.join(testdir
, "testfile")
120 self
.assert_(os
.path
.isfile(destfile
))
121 self
.assertEqual(os
.stat(testfile
).st_mtime
, os
.stat(destfile
).st_mtime
)
123 @unittest.skipIf(sys
.platform
== "win32", "Windows doesn't have real file modes")
124 def test_nsinstall_m(self
):
125 "Test that nsinstall -m works (set mode)"
126 testfile
= self
.touch("testfile")
128 os
.chmod(testfile
, mode
)
129 testdir
= self
.mkdirs("testdir")
131 nsinstall(["-m", "{0:04o}".format(mode
), testfile
, testdir
]), 0
133 destfile
= os
.path
.join(testdir
, "testfile")
134 self
.assert_(os
.path
.isfile(destfile
))
135 self
.assertEqual(os
.stat(testfile
).st_mode
, os
.stat(destfile
).st_mode
)
137 def test_nsinstall_d(self
):
138 "Test that nsinstall -d works (create directories in target)"
139 # -d makes no sense to me, but ok!
140 testfile
= self
.touch("testfile")
141 testdir
= self
.mkdirs("testdir")
142 destdir
= os
.path
.join(testdir
, "subdir")
143 self
.assertEqual(nsinstall(["-d", testfile
, destdir
]), 0)
144 self
.assert_(os
.path
.isdir(os
.path
.join(destdir
, "testfile")))
146 @unittest.skipIf(not RUN_NON_ASCII_TESTS
, "Skipping non ascii tests")
147 def test_nsinstall_non_ascii(self
):
148 "Test that nsinstall handles non-ASCII files"
149 filename
= "\u2325\u3452\u2415\u5081"
150 testfile
= self
.touch(filename
)
151 testdir
= self
.mkdirs("\u4241\u1D04\u1414")
153 nsinstall([testfile
.encode("utf-8"), testdir
.encode("utf-8")]), 0
156 destfile
= os
.path
.join(testdir
, filename
)
157 self
.assert_(os
.path
.isfile(destfile
))
159 # Executing nsinstall.py with python 2 is not supported.
161 not RUN_NON_ASCII_TESTS
or sys
.version_info
[0] == 2, "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
= "\u2325\u3452\u2415\u5081"
166 testfile
= self
.touch(filename
)
167 testdir
= self
.mkdirs("\u4241\u1D04\u1414")
168 p
= subprocess
.Popen([sys
.executable
, NSINSTALL_PATH
, testfile
, testdir
])
171 self
.assertEqual(rv
, 0)
172 destfile
= os
.path
.join(testdir
, filename
)
173 self
.assert_(os
.path
.isfile(destfile
))
175 # TODO: implement -R, -l, -L and test them!
178 if __name__
== "__main__":