blob: 0c93a8cc1adac2507f5b7599af308373c4036867 [file] [log] [blame]
Nick Coghlanf088e5e2008-12-14 11:50:48 +00001# This test module covers support in various parts of the standard library
2# for working with modules located inside zipfiles
3# The tests are centralised in this fashion to make it easy to drop them
4# if a platform doesn't support zipimport
Nick Coghlanf088e5e2008-12-14 11:50:48 +00005import test.support
6import os
7import os.path
8import sys
9import textwrap
10import zipfile
11import zipimport
12import doctest
13import inspect
14import linecache
15import pdb
Nick Coghlan90be5fb2011-01-11 10:05:20 +000016import unittest
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000017from test.script_helper import (spawn_python, kill_python, assert_python_ok,
Georg Brandl1b37e872010-03-14 10:45:50 +000018 temp_dir, make_script, make_zip_script)
Nick Coghlanf088e5e2008-12-14 11:50:48 +000019
20verbose = test.support.verbose
21
22# Library modules covered by this test set
23# pdb (Issue 4201)
24# inspect (Issue 4223)
25# doctest (Issue 4197)
26
27# Other test modules with zipimport related tests
28# test_zipimport (of course!)
29# test_cmd_line_script (covers the zipimport support in runpy)
30
31# Retrieve some helpers from other test cases
32from test import test_doctest, sample_doctest
Nick Coghlanf088e5e2008-12-14 11:50:48 +000033
34
35def _run_object_doctest(obj, module):
Victor Stinner592f6792011-06-29 18:11:36 +020036 finder = doctest.DocTestFinder(verbose=verbose, recurse=False)
37 runner = doctest.DocTestRunner(verbose=verbose)
38 # Use the object's fully qualified name if it has one
39 # Otherwise, use the module's name
Nick Coghlanf088e5e2008-12-14 11:50:48 +000040 try:
Victor Stinner592f6792011-06-29 18:11:36 +020041 name = "%s.%s" % (obj.__module__, obj.__name__)
42 except AttributeError:
43 name = module.__name__
44 for example in finder.find(obj, name, module):
45 runner.run(example)
46 f, t = runner.failures, runner.tries
47 if f:
48 raise test.support.TestFailed("%d of %d doctests failed" % (f, t))
Nick Coghlanf088e5e2008-12-14 11:50:48 +000049 if verbose:
50 print ('doctest (%s) ... %d tests with zero failures' % (module.__name__, t))
51 return f, t
52
53
54
Nick Coghlan90be5fb2011-01-11 10:05:20 +000055class ZipSupportTests(unittest.TestCase):
56 # This used to use the ImportHooksBaseTestCase to restore
Nick Coghlanf088e5e2008-12-14 11:50:48 +000057 # the state of the import related information
Nick Coghlan90be5fb2011-01-11 10:05:20 +000058 # in the sys module after each test. However, that restores
59 # *too much* information and breaks for the invocation of
60 # of test_doctest. So we do our own thing and leave
61 # sys.modules alone.
Nick Coghlanf088e5e2008-12-14 11:50:48 +000062 # We also clear the linecache and zipimport cache
63 # just to avoid any bogus errors due to name reuse in the tests
64 def setUp(self):
65 linecache.clearcache()
66 zipimport._zip_directory_cache.clear()
Nick Coghlan90be5fb2011-01-11 10:05:20 +000067 self.path = sys.path[:]
68 self.meta_path = sys.meta_path[:]
69 self.path_hooks = sys.path_hooks[:]
70 sys.path_importer_cache.clear()
Nick Coghlanf088e5e2008-12-14 11:50:48 +000071
Nick Coghlan90be5fb2011-01-11 10:05:20 +000072 def tearDown(self):
73 sys.path[:] = self.path
74 sys.meta_path[:] = self.meta_path
75 sys.path_hooks[:] = self.path_hooks
76 sys.path_importer_cache.clear()
Nick Coghlanf088e5e2008-12-14 11:50:48 +000077
78 def test_inspect_getsource_issue4223(self):
79 test_src = "def foo(): pass\n"
80 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +000081 init_name = make_script(d, '__init__', test_src)
Nick Coghlanf088e5e2008-12-14 11:50:48 +000082 name_in_zip = os.path.join('zip_pkg',
83 os.path.basename(init_name))
Nick Coghlan260bd3e2009-11-16 06:49:25 +000084 zip_name, run_name = make_zip_script(d, 'test_zip',
Nick Coghlanf088e5e2008-12-14 11:50:48 +000085 init_name, name_in_zip)
86 os.remove(init_name)
87 sys.path.insert(0, zip_name)
88 import zip_pkg
Nick Coghlanef316572011-02-07 13:43:07 +000089 try:
90 self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
91 finally:
92 del sys.modules["zip_pkg"]
Nick Coghlanf088e5e2008-12-14 11:50:48 +000093
94 def test_doctest_issue4197(self):
95 # To avoid having to keep two copies of the doctest module's
96 # unit tests in sync, this test works by taking the source of
97 # test_doctest itself, rewriting it a bit to cope with a new
98 # location, and then throwing it in a zip file to make sure
99 # everything still works correctly
100 test_src = inspect.getsource(test_doctest)
101 test_src = test_src.replace(
102 "from test import test_doctest",
103 "import test_zipped_doctest as test_doctest")
104 test_src = test_src.replace("test.test_doctest",
105 "test_zipped_doctest")
106 test_src = test_src.replace("test.sample_doctest",
107 "sample_zipped_doctest")
108 sample_src = inspect.getsource(sample_doctest)
109 sample_src = sample_src.replace("test.test_doctest",
110 "test_zipped_doctest")
111 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000112 script_name = make_script(d, 'test_zipped_doctest',
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000113 test_src)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000114 zip_name, run_name = make_zip_script(d, 'test_zip',
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000115 script_name)
116 z = zipfile.ZipFile(zip_name, 'a')
117 z.writestr("sample_zipped_doctest.py", sample_src)
118 z.close()
119 if verbose:
120 zip_file = zipfile.ZipFile(zip_name, 'r')
121 print ('Contents of %r:' % zip_name)
122 zip_file.printdir()
123 zip_file.close()
124 os.remove(script_name)
125 sys.path.insert(0, zip_name)
126 import test_zipped_doctest
Nick Coghlanef316572011-02-07 13:43:07 +0000127 try:
128 # Some of the doc tests depend on the colocated text files
129 # which aren't available to the zipped version (the doctest
130 # module currently requires real filenames for non-embedded
131 # tests). So we're forced to be selective about which tests
132 # to run.
133 # doctest could really use some APIs which take a text
134 # string or a file object instead of a filename...
135 known_good_tests = [
136 test_zipped_doctest.SampleClass,
137 test_zipped_doctest.SampleClass.NestedClass,
138 test_zipped_doctest.SampleClass.NestedClass.__init__,
139 test_zipped_doctest.SampleClass.__init__,
140 test_zipped_doctest.SampleClass.a_classmethod,
141 test_zipped_doctest.SampleClass.a_property,
142 test_zipped_doctest.SampleClass.a_staticmethod,
143 test_zipped_doctest.SampleClass.double,
144 test_zipped_doctest.SampleClass.get,
145 test_zipped_doctest.SampleNewStyleClass,
146 test_zipped_doctest.SampleNewStyleClass.__init__,
147 test_zipped_doctest.SampleNewStyleClass.double,
148 test_zipped_doctest.SampleNewStyleClass.get,
149 test_zipped_doctest.sample_func,
150 test_zipped_doctest.test_DocTest,
151 test_zipped_doctest.test_DocTestParser,
152 test_zipped_doctest.test_DocTestRunner.basics,
153 test_zipped_doctest.test_DocTestRunner.exceptions,
154 test_zipped_doctest.test_DocTestRunner.option_directives,
155 test_zipped_doctest.test_DocTestRunner.optionflags,
156 test_zipped_doctest.test_DocTestRunner.verbose_flag,
157 test_zipped_doctest.test_Example,
158 test_zipped_doctest.test_debug,
Nick Coghlanef316572011-02-07 13:43:07 +0000159 test_zipped_doctest.test_testsource,
160 test_zipped_doctest.test_trailing_space_in_test,
161 test_zipped_doctest.test_DocTestSuite,
162 test_zipped_doctest.test_DocTestFinder,
163 ]
Brett Cannon31f59292011-02-21 19:29:56 +0000164 # These tests are the ones which need access
Nick Coghlanef316572011-02-07 13:43:07 +0000165 # to the data files, so we don't run them
166 fail_due_to_missing_data_files = [
167 test_zipped_doctest.test_DocFileSuite,
168 test_zipped_doctest.test_testfile,
169 test_zipped_doctest.test_unittest_reportflags,
170 ]
Brett Cannon31f59292011-02-21 19:29:56 +0000171
Nick Coghlanef316572011-02-07 13:43:07 +0000172 for obj in known_good_tests:
173 _run_object_doctest(obj, test_zipped_doctest)
174 finally:
175 del sys.modules["test_zipped_doctest"]
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000176
Nick Coghlan38622002008-12-15 12:01:34 +0000177 def test_doctest_main_issue4197(self):
178 test_src = textwrap.dedent("""\
179 class Test:
180 ">>> 'line 2'"
181 pass
182
183 import doctest
184 doctest.testmod()
185 """)
186 pattern = 'File "%s", line 2, in %s'
187 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000188 script_name = make_script(d, 'script', test_src)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000189 rc, out, err = assert_python_ok(script_name)
Nick Coghlan38622002008-12-15 12:01:34 +0000190 expected = pattern % (script_name, "__main__.Test")
191 if verbose:
192 print ("Expected line", expected)
193 print ("Got stdout:")
Victor Stinner6722b5f2010-10-20 21:48:35 +0000194 print (ascii(out))
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000195 self.assertIn(expected.encode('utf-8'), out)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000196 zip_name, run_name = make_zip_script(d, "test_zip",
Nick Coghlan38622002008-12-15 12:01:34 +0000197 script_name, '__main__.py')
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000198 rc, out, err = assert_python_ok(zip_name)
Nick Coghlan38622002008-12-15 12:01:34 +0000199 expected = pattern % (run_name, "__main__.Test")
200 if verbose:
201 print ("Expected line", expected)
202 print ("Got stdout:")
Victor Stinner6722b5f2010-10-20 21:48:35 +0000203 print (ascii(out))
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000204 self.assertIn(expected.encode('utf-8'), out)
Nick Coghlan38622002008-12-15 12:01:34 +0000205
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000206 def test_pdb_issue4201(self):
207 test_src = textwrap.dedent("""\
208 def f():
209 pass
210
211 import pdb
Georg Brandl34748cd2010-12-04 17:11:36 +0000212 pdb.Pdb(nosigint=True).runcall(f)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000213 """)
214 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000215 script_name = make_script(d, 'script', test_src)
216 p = spawn_python(script_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000217 p.stdin.write(b'l\n')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000218 data = kill_python(p)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000219 self.assertIn(script_name.encode('utf-8'), data)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000220 zip_name, run_name = make_zip_script(d, "test_zip",
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000221 script_name, '__main__.py')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000222 p = spawn_python(zip_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000223 p.stdin.write(b'l\n')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000224 data = kill_python(p)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000225 self.assertIn(run_name.encode('utf-8'), data)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000226
227
228def test_main():
229 test.support.run_unittest(ZipSupportTests)
230 test.support.reap_children()
231
232if __name__ == '__main__':
233 test_main()