blob: f7f3398015a889b35293db31c1e1c58b0350c0e9 [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
R David Murray5abd76a2012-09-10 10:15:58 -040032from test import (test_doctest, sample_doctest, sample_doctest_no_doctests,
33 sample_doctest_no_docstrings)
Nick Coghlanf088e5e2008-12-14 11:50:48 +000034
35
36def _run_object_doctest(obj, module):
Victor Stinner592f6792011-06-29 18:11:36 +020037 finder = doctest.DocTestFinder(verbose=verbose, recurse=False)
38 runner = doctest.DocTestRunner(verbose=verbose)
39 # Use the object's fully qualified name if it has one
40 # Otherwise, use the module's name
Nick Coghlanf088e5e2008-12-14 11:50:48 +000041 try:
Victor Stinner592f6792011-06-29 18:11:36 +020042 name = "%s.%s" % (obj.__module__, obj.__name__)
43 except AttributeError:
44 name = module.__name__
45 for example in finder.find(obj, name, module):
46 runner.run(example)
47 f, t = runner.failures, runner.tries
48 if f:
49 raise test.support.TestFailed("%d of %d doctests failed" % (f, t))
Nick Coghlanf088e5e2008-12-14 11:50:48 +000050 if verbose:
51 print ('doctest (%s) ... %d tests with zero failures' % (module.__name__, t))
52 return f, t
53
54
55
Nick Coghlan90be5fb2011-01-11 10:05:20 +000056class ZipSupportTests(unittest.TestCase):
57 # This used to use the ImportHooksBaseTestCase to restore
Nick Coghlanf088e5e2008-12-14 11:50:48 +000058 # the state of the import related information
Nick Coghlan90be5fb2011-01-11 10:05:20 +000059 # in the sys module after each test. However, that restores
60 # *too much* information and breaks for the invocation of
61 # of test_doctest. So we do our own thing and leave
62 # sys.modules alone.
Nick Coghlanf088e5e2008-12-14 11:50:48 +000063 # We also clear the linecache and zipimport cache
64 # just to avoid any bogus errors due to name reuse in the tests
65 def setUp(self):
66 linecache.clearcache()
67 zipimport._zip_directory_cache.clear()
Nick Coghlan90be5fb2011-01-11 10:05:20 +000068 self.path = sys.path[:]
69 self.meta_path = sys.meta_path[:]
70 self.path_hooks = sys.path_hooks[:]
71 sys.path_importer_cache.clear()
Nick Coghlanf088e5e2008-12-14 11:50:48 +000072
Nick Coghlan90be5fb2011-01-11 10:05:20 +000073 def tearDown(self):
74 sys.path[:] = self.path
75 sys.meta_path[:] = self.meta_path
76 sys.path_hooks[:] = self.path_hooks
77 sys.path_importer_cache.clear()
Nick Coghlanf088e5e2008-12-14 11:50:48 +000078
79 def test_inspect_getsource_issue4223(self):
80 test_src = "def foo(): pass\n"
81 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +000082 init_name = make_script(d, '__init__', test_src)
Nick Coghlanf088e5e2008-12-14 11:50:48 +000083 name_in_zip = os.path.join('zip_pkg',
84 os.path.basename(init_name))
Nick Coghlan260bd3e2009-11-16 06:49:25 +000085 zip_name, run_name = make_zip_script(d, 'test_zip',
Nick Coghlanf088e5e2008-12-14 11:50:48 +000086 init_name, name_in_zip)
87 os.remove(init_name)
88 sys.path.insert(0, zip_name)
89 import zip_pkg
Nick Coghlanef316572011-02-07 13:43:07 +000090 try:
91 self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
92 finally:
93 del sys.modules["zip_pkg"]
Nick Coghlanf088e5e2008-12-14 11:50:48 +000094
95 def test_doctest_issue4197(self):
96 # To avoid having to keep two copies of the doctest module's
97 # unit tests in sync, this test works by taking the source of
98 # test_doctest itself, rewriting it a bit to cope with a new
99 # location, and then throwing it in a zip file to make sure
100 # everything still works correctly
101 test_src = inspect.getsource(test_doctest)
102 test_src = test_src.replace(
103 "from test import test_doctest",
104 "import test_zipped_doctest as test_doctest")
105 test_src = test_src.replace("test.test_doctest",
106 "test_zipped_doctest")
107 test_src = test_src.replace("test.sample_doctest",
108 "sample_zipped_doctest")
R David Murray5abd76a2012-09-10 10:15:58 -0400109 # The sample doctest files rewritten to include in the zipped version.
110 sample_sources = {}
111 for mod in [sample_doctest, sample_doctest_no_doctests,
112 sample_doctest_no_docstrings]:
113 src = inspect.getsource(mod)
114 src = src.replace("test.test_doctest", "test_zipped_doctest")
115 # Rewrite the module name so that, for example,
116 # "test.sample_doctest" becomes "sample_zipped_doctest".
117 mod_name = mod.__name__.split(".")[-1]
118 mod_name = mod_name.replace("sample_", "sample_zipped_")
119 sample_sources[mod_name] = src
120
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000121 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000122 script_name = make_script(d, 'test_zipped_doctest',
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000123 test_src)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000124 zip_name, run_name = make_zip_script(d, 'test_zip',
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000125 script_name)
126 z = zipfile.ZipFile(zip_name, 'a')
R David Murray5abd76a2012-09-10 10:15:58 -0400127 for mod_name, src in sample_sources.items():
128 z.writestr(mod_name + ".py", src)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000129 z.close()
130 if verbose:
131 zip_file = zipfile.ZipFile(zip_name, 'r')
132 print ('Contents of %r:' % zip_name)
133 zip_file.printdir()
134 zip_file.close()
135 os.remove(script_name)
136 sys.path.insert(0, zip_name)
137 import test_zipped_doctest
Nick Coghlanef316572011-02-07 13:43:07 +0000138 try:
139 # Some of the doc tests depend on the colocated text files
140 # which aren't available to the zipped version (the doctest
141 # module currently requires real filenames for non-embedded
142 # tests). So we're forced to be selective about which tests
143 # to run.
144 # doctest could really use some APIs which take a text
145 # string or a file object instead of a filename...
146 known_good_tests = [
147 test_zipped_doctest.SampleClass,
148 test_zipped_doctest.SampleClass.NestedClass,
149 test_zipped_doctest.SampleClass.NestedClass.__init__,
150 test_zipped_doctest.SampleClass.__init__,
151 test_zipped_doctest.SampleClass.a_classmethod,
152 test_zipped_doctest.SampleClass.a_property,
153 test_zipped_doctest.SampleClass.a_staticmethod,
154 test_zipped_doctest.SampleClass.double,
155 test_zipped_doctest.SampleClass.get,
156 test_zipped_doctest.SampleNewStyleClass,
157 test_zipped_doctest.SampleNewStyleClass.__init__,
158 test_zipped_doctest.SampleNewStyleClass.double,
159 test_zipped_doctest.SampleNewStyleClass.get,
160 test_zipped_doctest.sample_func,
161 test_zipped_doctest.test_DocTest,
162 test_zipped_doctest.test_DocTestParser,
163 test_zipped_doctest.test_DocTestRunner.basics,
164 test_zipped_doctest.test_DocTestRunner.exceptions,
165 test_zipped_doctest.test_DocTestRunner.option_directives,
166 test_zipped_doctest.test_DocTestRunner.optionflags,
167 test_zipped_doctest.test_DocTestRunner.verbose_flag,
168 test_zipped_doctest.test_Example,
169 test_zipped_doctest.test_debug,
Nick Coghlanef316572011-02-07 13:43:07 +0000170 test_zipped_doctest.test_testsource,
171 test_zipped_doctest.test_trailing_space_in_test,
172 test_zipped_doctest.test_DocTestSuite,
173 test_zipped_doctest.test_DocTestFinder,
174 ]
Brett Cannon31f59292011-02-21 19:29:56 +0000175 # These tests are the ones which need access
Nick Coghlanef316572011-02-07 13:43:07 +0000176 # to the data files, so we don't run them
177 fail_due_to_missing_data_files = [
178 test_zipped_doctest.test_DocFileSuite,
179 test_zipped_doctest.test_testfile,
180 test_zipped_doctest.test_unittest_reportflags,
181 ]
Brett Cannon31f59292011-02-21 19:29:56 +0000182
Nick Coghlanef316572011-02-07 13:43:07 +0000183 for obj in known_good_tests:
184 _run_object_doctest(obj, test_zipped_doctest)
185 finally:
186 del sys.modules["test_zipped_doctest"]
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000187
Nick Coghlan38622002008-12-15 12:01:34 +0000188 def test_doctest_main_issue4197(self):
189 test_src = textwrap.dedent("""\
190 class Test:
191 ">>> 'line 2'"
192 pass
193
194 import doctest
195 doctest.testmod()
196 """)
197 pattern = 'File "%s", line 2, in %s'
198 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000199 script_name = make_script(d, 'script', test_src)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000200 rc, out, err = assert_python_ok(script_name)
Nick Coghlan38622002008-12-15 12:01:34 +0000201 expected = pattern % (script_name, "__main__.Test")
202 if verbose:
203 print ("Expected line", expected)
204 print ("Got stdout:")
Victor Stinner6722b5f2010-10-20 21:48:35 +0000205 print (ascii(out))
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000206 self.assertIn(expected.encode('utf-8'), out)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000207 zip_name, run_name = make_zip_script(d, "test_zip",
Nick Coghlan38622002008-12-15 12:01:34 +0000208 script_name, '__main__.py')
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000209 rc, out, err = assert_python_ok(zip_name)
Nick Coghlan38622002008-12-15 12:01:34 +0000210 expected = pattern % (run_name, "__main__.Test")
211 if verbose:
212 print ("Expected line", expected)
213 print ("Got stdout:")
Victor Stinner6722b5f2010-10-20 21:48:35 +0000214 print (ascii(out))
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000215 self.assertIn(expected.encode('utf-8'), out)
Nick Coghlan38622002008-12-15 12:01:34 +0000216
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000217 def test_pdb_issue4201(self):
218 test_src = textwrap.dedent("""\
219 def f():
220 pass
221
222 import pdb
Georg Brandl34748cd2010-12-04 17:11:36 +0000223 pdb.Pdb(nosigint=True).runcall(f)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000224 """)
225 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000226 script_name = make_script(d, 'script', test_src)
227 p = spawn_python(script_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000228 p.stdin.write(b'l\n')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000229 data = kill_python(p)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000230 self.assertIn(script_name.encode('utf-8'), data)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000231 zip_name, run_name = make_zip_script(d, "test_zip",
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000232 script_name, '__main__.py')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000233 p = spawn_python(zip_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000234 p.stdin.write(b'l\n')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000235 data = kill_python(p)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000236 self.assertIn(run_name.encode('utf-8'), data)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000237
238
239def test_main():
240 test.support.run_unittest(ZipSupportTests)
241 test.support.reap_children()
242
243if __name__ == '__main__':
244 test_main()