blob: 88561017503ff8be07ce7fddf4950334c3e271d7 [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
Nick Coghlan90be5fb2011-01-11 10:05:20 +000015import unittest
Berker Peksagce643912015-05-06 06:33:17 +030016from test.support.script_helper import (spawn_python, kill_python, assert_python_ok,
17 make_script, make_zip_script)
Nick Coghlanf088e5e2008-12-14 11:50:48 +000018
19verbose = test.support.verbose
20
21# Library modules covered by this test set
22# pdb (Issue 4201)
23# inspect (Issue 4223)
24# doctest (Issue 4197)
25
26# Other test modules with zipimport related tests
27# test_zipimport (of course!)
28# test_cmd_line_script (covers the zipimport support in runpy)
29
30# Retrieve some helpers from other test cases
R David Murray5abd76a2012-09-10 10:15:58 -040031from test import (test_doctest, sample_doctest, sample_doctest_no_doctests,
32 sample_doctest_no_docstrings)
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:
Serhiy Storchaka521e5862014-07-22 15:00:37 +030041 name = "%s.%s" % (obj.__module__, obj.__qualname__)
Victor Stinner592f6792011-06-29 18:11:36 +020042 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
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020059 # *too much* information and breaks for the invocation
Nick Coghlan90be5fb2011-01-11 10:05:20 +000060 # 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"
Berker Peksagce643912015-05-06 06:33:17 +030080 with test.support.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")
R David Murray5abd76a2012-09-10 10:15:58 -0400108 # The sample doctest files rewritten to include in the zipped version.
109 sample_sources = {}
110 for mod in [sample_doctest, sample_doctest_no_doctests,
111 sample_doctest_no_docstrings]:
112 src = inspect.getsource(mod)
113 src = src.replace("test.test_doctest", "test_zipped_doctest")
114 # Rewrite the module name so that, for example,
115 # "test.sample_doctest" becomes "sample_zipped_doctest".
116 mod_name = mod.__name__.split(".")[-1]
117 mod_name = mod_name.replace("sample_", "sample_zipped_")
118 sample_sources[mod_name] = src
119
Berker Peksagce643912015-05-06 06:33:17 +0300120 with test.support.temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000121 script_name = make_script(d, 'test_zipped_doctest',
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000122 test_src)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000123 zip_name, run_name = make_zip_script(d, 'test_zip',
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000124 script_name)
Serhiy Storchaka5b10b982019-03-05 10:06:26 +0200125 with zipfile.ZipFile(zip_name, 'a') as z:
126 for mod_name, src in sample_sources.items():
127 z.writestr(mod_name + ".py", src)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000128 if verbose:
Serhiy Storchaka5b10b982019-03-05 10:06:26 +0200129 with zipfile.ZipFile(zip_name, 'r') as zip_file:
130 print ('Contents of %r:' % zip_name)
131 zip_file.printdir()
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000132 os.remove(script_name)
133 sys.path.insert(0, zip_name)
134 import test_zipped_doctest
Nick Coghlanef316572011-02-07 13:43:07 +0000135 try:
136 # Some of the doc tests depend on the colocated text files
137 # which aren't available to the zipped version (the doctest
138 # module currently requires real filenames for non-embedded
139 # tests). So we're forced to be selective about which tests
140 # to run.
141 # doctest could really use some APIs which take a text
142 # string or a file object instead of a filename...
143 known_good_tests = [
144 test_zipped_doctest.SampleClass,
145 test_zipped_doctest.SampleClass.NestedClass,
146 test_zipped_doctest.SampleClass.NestedClass.__init__,
147 test_zipped_doctest.SampleClass.__init__,
148 test_zipped_doctest.SampleClass.a_classmethod,
149 test_zipped_doctest.SampleClass.a_property,
150 test_zipped_doctest.SampleClass.a_staticmethod,
151 test_zipped_doctest.SampleClass.double,
152 test_zipped_doctest.SampleClass.get,
153 test_zipped_doctest.SampleNewStyleClass,
154 test_zipped_doctest.SampleNewStyleClass.__init__,
155 test_zipped_doctest.SampleNewStyleClass.double,
156 test_zipped_doctest.SampleNewStyleClass.get,
157 test_zipped_doctest.sample_func,
158 test_zipped_doctest.test_DocTest,
159 test_zipped_doctest.test_DocTestParser,
160 test_zipped_doctest.test_DocTestRunner.basics,
161 test_zipped_doctest.test_DocTestRunner.exceptions,
162 test_zipped_doctest.test_DocTestRunner.option_directives,
163 test_zipped_doctest.test_DocTestRunner.optionflags,
164 test_zipped_doctest.test_DocTestRunner.verbose_flag,
165 test_zipped_doctest.test_Example,
166 test_zipped_doctest.test_debug,
Nick Coghlanef316572011-02-07 13:43:07 +0000167 test_zipped_doctest.test_testsource,
168 test_zipped_doctest.test_trailing_space_in_test,
169 test_zipped_doctest.test_DocTestSuite,
170 test_zipped_doctest.test_DocTestFinder,
171 ]
Brett Cannon31f59292011-02-21 19:29:56 +0000172 # These tests are the ones which need access
Nick Coghlanef316572011-02-07 13:43:07 +0000173 # to the data files, so we don't run them
174 fail_due_to_missing_data_files = [
175 test_zipped_doctest.test_DocFileSuite,
176 test_zipped_doctest.test_testfile,
177 test_zipped_doctest.test_unittest_reportflags,
178 ]
Brett Cannon31f59292011-02-21 19:29:56 +0000179
Nick Coghlanef316572011-02-07 13:43:07 +0000180 for obj in known_good_tests:
181 _run_object_doctest(obj, test_zipped_doctest)
182 finally:
183 del sys.modules["test_zipped_doctest"]
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000184
Nick Coghlan38622002008-12-15 12:01:34 +0000185 def test_doctest_main_issue4197(self):
186 test_src = textwrap.dedent("""\
187 class Test:
188 ">>> 'line 2'"
189 pass
190
191 import doctest
192 doctest.testmod()
193 """)
194 pattern = 'File "%s", line 2, in %s'
Berker Peksagce643912015-05-06 06:33:17 +0300195 with test.support.temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000196 script_name = make_script(d, 'script', test_src)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000197 rc, out, err = assert_python_ok(script_name)
Nick Coghlan38622002008-12-15 12:01:34 +0000198 expected = pattern % (script_name, "__main__.Test")
199 if verbose:
200 print ("Expected line", expected)
201 print ("Got stdout:")
Victor Stinner6722b5f2010-10-20 21:48:35 +0000202 print (ascii(out))
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000203 self.assertIn(expected.encode('utf-8'), out)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000204 zip_name, run_name = make_zip_script(d, "test_zip",
Nick Coghlan38622002008-12-15 12:01:34 +0000205 script_name, '__main__.py')
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000206 rc, out, err = assert_python_ok(zip_name)
Nick Coghlan38622002008-12-15 12:01:34 +0000207 expected = pattern % (run_name, "__main__.Test")
208 if verbose:
209 print ("Expected line", expected)
210 print ("Got stdout:")
Victor Stinner6722b5f2010-10-20 21:48:35 +0000211 print (ascii(out))
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000212 self.assertIn(expected.encode('utf-8'), out)
Nick Coghlan38622002008-12-15 12:01:34 +0000213
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000214 def test_pdb_issue4201(self):
215 test_src = textwrap.dedent("""\
216 def f():
217 pass
218
219 import pdb
Georg Brandl34748cd2010-12-04 17:11:36 +0000220 pdb.Pdb(nosigint=True).runcall(f)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000221 """)
Berker Peksagce643912015-05-06 06:33:17 +0300222 with test.support.temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000223 script_name = make_script(d, 'script', test_src)
224 p = spawn_python(script_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000225 p.stdin.write(b'l\n')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000226 data = kill_python(p)
Tim Golden6d09f092013-10-25 18:38:16 +0100227 # bdb/pdb applies normcase to its filename before displaying
228 self.assertIn(os.path.normcase(script_name.encode('utf-8')), data)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000229 zip_name, run_name = make_zip_script(d, "test_zip",
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000230 script_name, '__main__.py')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000231 p = spawn_python(zip_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000232 p.stdin.write(b'l\n')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000233 data = kill_python(p)
Tim Golden6d09f092013-10-25 18:38:16 +0100234 # bdb/pdb applies normcase to its filename before displaying
235 self.assertIn(os.path.normcase(run_name.encode('utf-8')), data)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000236
237
Zachary Ware38c707e2015-04-13 15:00:43 -0500238def tearDownModule():
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000239 test.support.reap_children()
240
241if __name__ == '__main__':
Zachary Ware38c707e2015-04-13 15:00:43 -0500242 unittest.main()