blob: e83a4d6426a8b2d05517eb76c77bb4e5220afd73 [file] [log] [blame]
Brett Cannonfd074152012-04-14 14:10:13 -04001import importlib
Nick Coghlan55175962013-07-28 22:11:50 +10002import shutil
Serhiy Storchaka6770f8a2016-11-20 16:16:06 +02003import stat
Eli Bendersky6c519992011-07-23 08:48:53 +03004import sys
5import os
6import unittest
7import socket
8import tempfile
9import errno
10from test import support
11
12TESTFN = support.TESTFN
Eli Bendersky6c519992011-07-23 08:48:53 +030013
14
15class TestSupport(unittest.TestCase):
Eli Bendersky6c519992011-07-23 08:48:53 +030016
17 def test_import_module(self):
18 support.import_module("ftplib")
19 self.assertRaises(unittest.SkipTest, support.import_module, "foo")
20
21 def test_import_fresh_module(self):
22 support.import_fresh_module("ftplib")
23
24 def test_get_attribute(self):
25 self.assertEqual(support.get_attribute(self, "test_get_attribute"),
26 self.test_get_attribute)
27 self.assertRaises(unittest.SkipTest, support.get_attribute, self, "foo")
28
Eli Bendersky7cd94a12011-07-23 15:00:31 +030029 @unittest.skip("failing buildbots")
Eli Bendersky6c519992011-07-23 08:48:53 +030030 def test_get_original_stdout(self):
31 self.assertEqual(support.get_original_stdout(), sys.stdout)
32
33 def test_unload(self):
34 import sched
35 self.assertIn("sched", sys.modules)
36 support.unload("sched")
37 self.assertNotIn("sched", sys.modules)
38
39 def test_unlink(self):
40 with open(TESTFN, "w") as f:
41 pass
42 support.unlink(TESTFN)
43 self.assertFalse(os.path.exists(TESTFN))
44 support.unlink(TESTFN)
45
46 def test_rmtree(self):
Serhiy Storchaka6770f8a2016-11-20 16:16:06 +020047 dirpath = support.TESTFN + 'd'
48 subdirpath = os.path.join(dirpath, 'subdir')
49 os.mkdir(dirpath)
50 os.mkdir(subdirpath)
51 support.rmtree(dirpath)
52 self.assertFalse(os.path.exists(dirpath))
53 with support.swap_attr(support, 'verbose', 0):
54 support.rmtree(dirpath)
Martin Panter33623b12015-11-24 22:12:05 +000055
Serhiy Storchaka6770f8a2016-11-20 16:16:06 +020056 os.mkdir(dirpath)
57 os.mkdir(subdirpath)
58 os.chmod(dirpath, stat.S_IRUSR|stat.S_IXUSR)
59 with support.swap_attr(support, 'verbose', 0):
60 support.rmtree(dirpath)
61 self.assertFalse(os.path.exists(dirpath))
62
63 os.mkdir(dirpath)
64 os.mkdir(subdirpath)
65 os.chmod(dirpath, 0)
66 with support.swap_attr(support, 'verbose', 0):
67 support.rmtree(dirpath)
68 self.assertFalse(os.path.exists(dirpath))
Eli Bendersky6c519992011-07-23 08:48:53 +030069
70 def test_forget(self):
Eli Bendersky8db645f2011-08-02 06:24:31 +030071 mod_filename = TESTFN + '.py'
72 with open(mod_filename, 'w') as f:
73 print('foo = 1', file=f)
Eli Bendersky2b6ee282011-08-03 05:18:33 +030074 sys.path.insert(0, os.curdir)
Brett Cannonfd074152012-04-14 14:10:13 -040075 importlib.invalidate_caches()
Eli Bendersky8db645f2011-08-02 06:24:31 +030076 try:
77 mod = __import__(TESTFN)
78 self.assertIn(TESTFN, sys.modules)
79
80 support.forget(TESTFN)
81 self.assertNotIn(TESTFN, sys.modules)
82 finally:
Eli Bendersky2b6ee282011-08-03 05:18:33 +030083 del sys.path[0]
Eli Bendersky8db645f2011-08-02 06:24:31 +030084 support.unlink(mod_filename)
Victor Stinner047b7ae2014-10-05 17:37:41 +020085 support.rmtree('__pycache__')
Eli Bendersky6c519992011-07-23 08:48:53 +030086
87 def test_HOST(self):
88 s = socket.socket()
89 s.bind((support.HOST, 0))
90 s.close()
91
92 def test_find_unused_port(self):
93 port = support.find_unused_port()
94 s = socket.socket()
95 s.bind((support.HOST, port))
96 s.close()
97
98 def test_bind_port(self):
99 s = socket.socket()
100 support.bind_port(s)
Charles-François Natali6e204602014-07-23 19:28:13 +0100101 s.listen()
Eli Bendersky6c519992011-07-23 08:48:53 +0300102 s.close()
103
Nick Coghlan55175962013-07-28 22:11:50 +1000104 # Tests for temp_dir()
105
106 def test_temp_dir(self):
107 """Test that temp_dir() creates and destroys its directory."""
108 parent_dir = tempfile.mkdtemp()
109 parent_dir = os.path.realpath(parent_dir)
110
111 try:
112 path = os.path.join(parent_dir, 'temp')
113 self.assertFalse(os.path.isdir(path))
114 with support.temp_dir(path) as temp_path:
115 self.assertEqual(temp_path, path)
116 self.assertTrue(os.path.isdir(path))
117 self.assertFalse(os.path.isdir(path))
118 finally:
Benjamin Petersonbb185ab2014-03-12 15:07:01 -0500119 support.rmtree(parent_dir)
Nick Coghlan55175962013-07-28 22:11:50 +1000120
121 def test_temp_dir__path_none(self):
122 """Test passing no path."""
123 with support.temp_dir() as temp_path:
124 self.assertTrue(os.path.isdir(temp_path))
125 self.assertFalse(os.path.isdir(temp_path))
126
127 def test_temp_dir__existing_dir__quiet_default(self):
128 """Test passing a directory that already exists."""
129 def call_temp_dir(path):
130 with support.temp_dir(path) as temp_path:
131 raise Exception("should not get here")
132
133 path = tempfile.mkdtemp()
134 path = os.path.realpath(path)
135 try:
136 self.assertTrue(os.path.isdir(path))
137 self.assertRaises(FileExistsError, call_temp_dir, path)
138 # Make sure temp_dir did not delete the original directory.
139 self.assertTrue(os.path.isdir(path))
140 finally:
141 shutil.rmtree(path)
142
143 def test_temp_dir__existing_dir__quiet_true(self):
144 """Test passing a directory that already exists with quiet=True."""
145 path = tempfile.mkdtemp()
146 path = os.path.realpath(path)
147
148 try:
149 with support.check_warnings() as recorder:
150 with support.temp_dir(path, quiet=True) as temp_path:
151 self.assertEqual(path, temp_path)
152 warnings = [str(w.message) for w in recorder.warnings]
153 # Make sure temp_dir did not delete the original directory.
154 self.assertTrue(os.path.isdir(path))
155 finally:
156 shutil.rmtree(path)
157
158 expected = ['tests may fail, unable to create temp dir: ' + path]
159 self.assertEqual(warnings, expected)
160
161 # Tests for change_cwd()
162
163 def test_change_cwd(self):
164 original_cwd = os.getcwd()
165
166 with support.temp_dir() as temp_path:
167 with support.change_cwd(temp_path) as new_cwd:
168 self.assertEqual(new_cwd, temp_path)
169 self.assertEqual(os.getcwd(), new_cwd)
170
171 self.assertEqual(os.getcwd(), original_cwd)
172
173 def test_change_cwd__non_existent_dir(self):
174 """Test passing a non-existent directory."""
175 original_cwd = os.getcwd()
176
177 def call_change_cwd(path):
178 with support.change_cwd(path) as new_cwd:
179 raise Exception("should not get here")
180
181 with support.temp_dir() as parent_dir:
182 non_existent_dir = os.path.join(parent_dir, 'does_not_exist')
183 self.assertRaises(FileNotFoundError, call_change_cwd,
184 non_existent_dir)
185
186 self.assertEqual(os.getcwd(), original_cwd)
187
188 def test_change_cwd__non_existent_dir__quiet_true(self):
189 """Test passing a non-existent directory with quiet=True."""
190 original_cwd = os.getcwd()
191
192 with support.temp_dir() as parent_dir:
193 bad_dir = os.path.join(parent_dir, 'does_not_exist')
194 with support.check_warnings() as recorder:
195 with support.change_cwd(bad_dir, quiet=True) as new_cwd:
196 self.assertEqual(new_cwd, original_cwd)
197 self.assertEqual(os.getcwd(), new_cwd)
198 warnings = [str(w.message) for w in recorder.warnings]
199
200 expected = ['tests may fail, unable to change CWD to: ' + bad_dir]
201 self.assertEqual(warnings, expected)
202
203 # Tests for change_cwd()
204
205 def test_change_cwd__chdir_warning(self):
206 """Check the warning message when os.chdir() fails."""
207 path = TESTFN + '_does_not_exist'
208 with support.check_warnings() as recorder:
209 with support.change_cwd(path=path, quiet=True):
210 pass
211 messages = [str(w.message) for w in recorder.warnings]
212 self.assertEqual(messages, ['tests may fail, unable to change CWD to: ' + path])
213
214 # Tests for temp_cwd()
215
Eli Bendersky6c519992011-07-23 08:48:53 +0300216 def test_temp_cwd(self):
217 here = os.getcwd()
218 with support.temp_cwd(name=TESTFN):
219 self.assertEqual(os.path.basename(os.getcwd()), TESTFN)
220 self.assertFalse(os.path.exists(TESTFN))
221 self.assertTrue(os.path.basename(os.getcwd()), here)
222
Nick Coghlan55175962013-07-28 22:11:50 +1000223
224 def test_temp_cwd__name_none(self):
225 """Test passing None to temp_cwd()."""
226 original_cwd = os.getcwd()
227 with support.temp_cwd(name=None) as new_cwd:
228 self.assertNotEqual(new_cwd, original_cwd)
229 self.assertTrue(os.path.isdir(new_cwd))
230 self.assertEqual(os.getcwd(), new_cwd)
231 self.assertEqual(os.getcwd(), original_cwd)
Ezio Melotti050a61f2012-09-21 16:53:07 +0300232
Eli Bendersky6c519992011-07-23 08:48:53 +0300233 def test_sortdict(self):
234 self.assertEqual(support.sortdict({3:3, 2:2, 1:1}), "{1: 1, 2: 2, 3: 3}")
235
236 def test_make_bad_fd(self):
237 fd = support.make_bad_fd()
238 with self.assertRaises(OSError) as cm:
239 os.write(fd, b"foo")
240 self.assertEqual(cm.exception.errno, errno.EBADF)
241
242 def test_check_syntax_error(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200243 support.check_syntax_error(self, "def class", lineno=1, offset=9)
Victor Stinner51d8c522016-02-08 17:57:02 +0100244 with self.assertRaises(AssertionError):
245 support.check_syntax_error(self, "x=1")
Eli Bendersky6c519992011-07-23 08:48:53 +0300246
247 def test_CleanImport(self):
248 import importlib
249 with support.CleanImport("asyncore"):
250 importlib.import_module("asyncore")
251
252 def test_DirsOnSysPath(self):
253 with support.DirsOnSysPath('foo', 'bar'):
254 self.assertIn("foo", sys.path)
255 self.assertIn("bar", sys.path)
256 self.assertNotIn("foo", sys.path)
257 self.assertNotIn("bar", sys.path)
258
259 def test_captured_stdout(self):
R David Murray5a33f812013-07-11 12:28:40 -0400260 with support.captured_stdout() as stdout:
Eli Benderskyd11c3e32011-07-23 08:51:14 +0300261 print("hello")
R David Murray5a33f812013-07-11 12:28:40 -0400262 self.assertEqual(stdout.getvalue(), "hello\n")
Eli Bendersky6c519992011-07-23 08:48:53 +0300263
264 def test_captured_stderr(self):
R David Murray5a33f812013-07-11 12:28:40 -0400265 with support.captured_stderr() as stderr:
Eli Benderskyd11c3e32011-07-23 08:51:14 +0300266 print("hello", file=sys.stderr)
R David Murray5a33f812013-07-11 12:28:40 -0400267 self.assertEqual(stderr.getvalue(), "hello\n")
Eli Bendersky6c519992011-07-23 08:48:53 +0300268
269 def test_captured_stdin(self):
R David Murray5a33f812013-07-11 12:28:40 -0400270 with support.captured_stdin() as stdin:
271 stdin.write('hello\n')
272 stdin.seek(0)
273 # call test code that consumes from sys.stdin
274 captured = input()
275 self.assertEqual(captured, "hello")
Eli Bendersky6c519992011-07-23 08:48:53 +0300276
277 def test_gc_collect(self):
278 support.gc_collect()
279
280 def test_python_is_optimized(self):
281 self.assertIsInstance(support.python_is_optimized(), bool)
282
283 def test_swap_attr(self):
284 class Obj:
285 x = 1
286 obj = Obj()
287 with support.swap_attr(obj, "x", 5):
288 self.assertEqual(obj.x, 5)
289 self.assertEqual(obj.x, 1)
290
291 def test_swap_item(self):
292 D = {"item":1}
293 with support.swap_item(D, "item", 5):
294 self.assertEqual(D["item"], 5)
295 self.assertEqual(D["item"], 1)
296
Gregory P. Smith4e72cce2015-04-14 13:26:06 -0700297 class RefClass:
298 attribute1 = None
299 attribute2 = None
300 _hidden_attribute1 = None
301 __magic_1__ = None
302
303 class OtherClass:
304 attribute2 = None
305 attribute3 = None
306 __magic_1__ = None
307 __magic_2__ = None
308
Gregory P. Smithe334e3f2015-04-14 12:56:53 -0700309 def test_detect_api_mismatch(self):
Gregory P. Smith4e72cce2015-04-14 13:26:06 -0700310 missing_items = support.detect_api_mismatch(self.RefClass,
311 self.OtherClass)
Gregory P. Smithe334e3f2015-04-14 12:56:53 -0700312 self.assertEqual({'attribute1'}, missing_items)
313
Gregory P. Smith4e72cce2015-04-14 13:26:06 -0700314 missing_items = support.detect_api_mismatch(self.OtherClass,
315 self.RefClass)
Gregory P. Smithe334e3f2015-04-14 12:56:53 -0700316 self.assertEqual({'attribute3', '__magic_2__'}, missing_items)
317
318 def test_detect_api_mismatch__ignore(self):
Gregory P. Smithe334e3f2015-04-14 12:56:53 -0700319 ignore = ['attribute1', 'attribute3', '__magic_2__', 'not_in_either']
320
Gregory P. Smith4e72cce2015-04-14 13:26:06 -0700321 missing_items = support.detect_api_mismatch(
322 self.RefClass, self.OtherClass, ignore=ignore)
Gregory P. Smithe334e3f2015-04-14 12:56:53 -0700323 self.assertEqual(set(), missing_items)
324
Gregory P. Smith4e72cce2015-04-14 13:26:06 -0700325 missing_items = support.detect_api_mismatch(
326 self.OtherClass, self.RefClass, ignore=ignore)
Gregory P. Smithe334e3f2015-04-14 12:56:53 -0700327 self.assertEqual(set(), missing_items)
328
Martin Panterd226d302015-11-14 11:47:00 +0000329 def test_check__all__(self):
330 extra = {'tempdir'}
331 blacklist = {'template'}
332 support.check__all__(self,
333 tempfile,
334 extra=extra,
335 blacklist=blacklist)
336
337 extra = {'TextTestResult', 'installHandler'}
338 blacklist = {'load_tests', "TestProgram", "BaseTestSuite"}
339
340 support.check__all__(self,
341 unittest,
342 ("unittest.result", "unittest.case",
343 "unittest.suite", "unittest.loader",
344 "unittest.main", "unittest.runner",
345 "unittest.signals"),
346 extra=extra,
347 blacklist=blacklist)
348
349 self.assertRaises(AssertionError, support.check__all__, self, unittest)
350
Eli Bendersky6c519992011-07-23 08:48:53 +0300351 # XXX -follows a list of untested API
352 # make_legacy_pyc
353 # is_resource_enabled
354 # requires
355 # fcmp
356 # umaks
357 # findfile
358 # check_warnings
359 # EnvironmentVarGuard
360 # TransientResource
361 # transient_internet
362 # run_with_locale
363 # set_memlimit
364 # bigmemtest
365 # precisionbigmemtest
366 # bigaddrspacetest
367 # requires_resource
368 # run_doctest
369 # threading_cleanup
370 # reap_threads
371 # reap_children
372 # strip_python_stderr
373 # args_from_interpreter_flags
374 # can_symlink
375 # skip_unless_symlink
Antoine Pitrou77e904e2013-10-08 23:04:32 +0200376 # SuppressCrashReport
Eli Bendersky6c519992011-07-23 08:48:53 +0300377
378
379def test_main():
380 tests = [TestSupport]
381 support.run_unittest(*tests)
382
383if __name__ == '__main__':
384 test_main()