Debian Codegraph @

Lib/test/test_zipimport_support.py

raw
1 # 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
5 import test.support
6 import os
7 import os.path
8 import sys
9 import textwrap
10 import zipfile
11 import zipimport
12 import doctest
13 import inspect
14 import linecache
15 import unittest
16 from test.support import os_helper
17 from test.support.script_helper import (spawn_python, kill_python, assert_python_ok,
18 make_script, make_zip_script)
19
20 verbose = 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
32 from test.test_doctest import (test_doctest,
33 sample_doctest, sample_doctest_no_doctests,
34 sample_doctest_no_docstrings, sample_doctest_skip)
35
36
37 def _run_object_doctest(obj, module):
38 finder = doctest.DocTestFinder(verbose=verbose, recurse=False)
39 runner = doctest.DocTestRunner(verbose=verbose)
40 # Use the object's fully qualified name if it has one
41 # Otherwise, use the module's name
42 try:
43 name = "%s.%s" % (obj.__module__, obj.__qualname__)
44 except AttributeError:
45 name = module.__name__
46 for example in finder.find(obj, name, module):
47 runner.run(example)
48 f, t = runner.failures, runner.tries
49 if f:
50 raise test.support.TestFailed("%d of %d doctests failed" % (f, t))
51 if verbose:
52 print ('doctest (%s) ... %d tests with zero failures' % (module.__name__, t))
53 return f, t
54
55
56
57 class ZipSupportTests(unittest.TestCase):
58 # This used to use the ImportHooksBaseTestCase to restore
59 # the state of the import related information
60 # in the sys module after each test. However, that restores
61 # *too much* information and breaks for the invocation
62 # of test_doctest. So we do our own thing and leave
63 # sys.modules alone.
64 # We also clear the linecache and zipimport cache
65 # just to avoid any bogus errors due to name reuse in the tests
66 def setUp(self):
67 linecache.clearcache()
68 zipimport._zip_directory_cache.clear()
69 self.path = sys.path[:]
70 self.meta_path = sys.meta_path[:]
71 self.path_hooks = sys.path_hooks[:]
72 sys.path_importer_cache.clear()
73
74 def tearDown(self):
75 sys.path[:] = self.path
76 sys.meta_path[:] = self.meta_path
77 sys.path_hooks[:] = self.path_hooks
78 sys.path_importer_cache.clear()
79
80 def test_inspect_getsource_issue4223(self):
81 test_src = "def foo(): pass\n"
82 with os_helper.temp_dir() as d:
83 init_name = make_script(d, '__init__', test_src)
84 name_in_zip = os.path.join('zip_pkg',
85 os.path.basename(init_name))
86 zip_name, run_name = make_zip_script(d, 'test_zip',
87 init_name, name_in_zip)
88 os.remove(init_name)
89 sys.path.insert(0, zip_name)
90 import zip_pkg
91 try:
92 self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
93 finally:
94 del sys.modules["zip_pkg"]
95
96 def test_doctest_issue4197(self):
97 # To avoid having to keep two copies of the doctest module's
98 # unit tests in sync, this test works by taking the source of
99 # test_doctest itself, rewriting it a bit to cope with a new
100 # location, and then throwing it in a zip file to make sure
101 # everything still works correctly
102 test_src = inspect.getsource(test_doctest)
103 test_src = test_src.replace(
104 "from test.test_doctest import test_doctest",
105 "import test_zipped_doctest as test_doctest")
106 test_src = test_src.replace("test.test_doctest.test_doctest",
107 "test_zipped_doctest")
108 test_src = test_src.replace("test.test_doctest.sample_doctest",
109 "sample_zipped_doctest")
110 # The sample doctest files rewritten to include in the zipped version.
111 sample_sources = {}
112 for mod in [sample_doctest, sample_doctest_no_doctests,
113 sample_doctest_no_docstrings, sample_doctest_skip]:
114 src = inspect.getsource(mod)
115 src = src.replace("test.test_doctest.test_doctest", "test_zipped_doctest")
116 # Rewrite the module name so that, for example,
117 # "test.sample_doctest" becomes "sample_zipped_doctest".
118 mod_name = mod.__name__.split(".")[-1]
119 mod_name = mod_name.replace("sample_", "sample_zipped_")
120 sample_sources[mod_name] = src
121
122 with os_helper.temp_dir() as d:
123 script_name = make_script(d, 'test_zipped_doctest',
124 test_src)
125 zip_name, run_name = make_zip_script(d, 'test_zip',
126 script_name)
127 with zipfile.ZipFile(zip_name, 'a') as z:
128 for mod_name, src in sample_sources.items():
129 z.writestr(mod_name + ".py", src)
130 if verbose:
131 with zipfile.ZipFile(zip_name, 'r') as zip_file:
132 print ('Contents of %r:' % zip_name)
133 zip_file.printdir()
134 os.remove(script_name)
135 sys.path.insert(0, zip_name)
136 import test_zipped_doctest
137 try:
138 # Some of the doc tests depend on the colocated text files
139 # which aren't available to the zipped version (the doctest
140 # module currently requires real filenames for non-embedded
141 # tests). So we're forced to be selective about which tests
142 # to run.
143 # doctest could really use some APIs which take a text
144 # string or a file object instead of a filename...
145 known_good_tests = [
146 test_zipped_doctest.SampleClass,
147 test_zipped_doctest.SampleClass.NestedClass,
148 test_zipped_doctest.SampleClass.NestedClass.__init__,
149 test_zipped_doctest.SampleClass.__init__,
150 test_zipped_doctest.SampleClass.a_classmethod,
151 test_zipped_doctest.SampleClass.a_property,
152 test_zipped_doctest.SampleClass.a_staticmethod,
153 test_zipped_doctest.SampleClass.double,
154 test_zipped_doctest.SampleClass.get,
155 test_zipped_doctest.SampleNewStyleClass,
156 test_zipped_doctest.SampleNewStyleClass.__init__,
157 test_zipped_doctest.SampleNewStyleClass.double,
158 test_zipped_doctest.SampleNewStyleClass.get,
159 test_zipped_doctest.sample_func,
160 test_zipped_doctest.test_DocTest,
161 test_zipped_doctest.test_DocTestParser,
162 test_zipped_doctest.test_DocTestRunner.basics,
163 test_zipped_doctest.test_DocTestRunner.exceptions,
164 test_zipped_doctest.test_DocTestRunner.option_directives,
165 test_zipped_doctest.test_DocTestRunner.optionflags,
166 test_zipped_doctest.test_DocTestRunner.verbose_flag,
167 test_zipped_doctest.test_Example,
168 test_zipped_doctest.test_debug,
169 test_zipped_doctest.test_testsource,
170 test_zipped_doctest.test_trailing_space_in_test,
171 test_zipped_doctest.test_DocTestSuite,
172 test_zipped_doctest.test_DocTestFinder,
173 ]
174 # These tests are the ones which need access
175 # to the data files, so we don't run them
176 fail_due_to_missing_data_files = [
177 test_zipped_doctest.test_DocFileSuite,
178 test_zipped_doctest.test_testfile,
179 test_zipped_doctest.test_unittest_reportflags,
180 ]
181
182 for obj in known_good_tests:
183 _run_object_doctest(obj, test_zipped_doctest)
184 finally:
185 del sys.modules["test_zipped_doctest"]
186
187 def test_doctest_main_issue4197(self):
188 test_src = textwrap.dedent("""\
189 class Test:
190 ">>> 'line 2'"
191 pass
192
193 import doctest
194 doctest.testmod()
195 """)
196 pattern = 'File "%s", line 2, in %s'
197 with os_helper.temp_dir() as d:
198 script_name = make_script(d, 'script', test_src)
199 rc, out, err = assert_python_ok(script_name)
200 expected = pattern % (script_name, "__main__.Test")
201 if verbose:
202 print ("Expected line", expected)
203 print ("Got stdout:")
204 print (ascii(out))
205 self.assertIn(expected.encode('utf-8'), out)
206 zip_name, run_name = make_zip_script(d, "test_zip",
207 script_name, '__main__.py')
208 rc, out, err = assert_python_ok(zip_name)
209 expected = pattern % (run_name, "__main__.Test")
210 if verbose:
211 print ("Expected line", expected)
212 print ("Got stdout:")
213 print (ascii(out))
214 self.assertIn(expected.encode('utf-8'), out)
215
216 def test_pdb_issue4201(self):
217 test_src = textwrap.dedent("""\
218 def f():
219 pass
220
221 import pdb
222 pdb.Pdb(nosigint=True).runcall(f)
223 """)
224 with os_helper.temp_dir() as d:
225 script_name = make_script(d, 'script', test_src)
226 p = spawn_python(script_name)
227 p.stdin.write(b'l\n')
228 data = kill_python(p)
229 # bdb/pdb applies normcase to its filename before displaying
230 self.assertIn(os.path.normcase(script_name.encode('utf-8')), data)
231 zip_name, run_name = make_zip_script(d, "test_zip",
232 script_name, '__main__.py')
233 p = spawn_python(zip_name)
234 p.stdin.write(b'l\n')
235 data = kill_python(p)
236 # bdb/pdb applies normcase to its filename before displaying
237 self.assertIn(os.path.normcase(run_name.encode('utf-8')), data)
238
239
240 def tearDownModule():
241 test.support.reap_children()
242
243 if __name__ == '__main__':
244 unittest.main()