blob: 54ab86466c0042f6ea3719e6c52b23109684bf78 [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):
36 # Direct doctest output (normally just errors) to real stdout; doctest
37 # output shouldn't be compared by regrtest.
38 save_stdout = sys.stdout
39 sys.stdout = test.support.get_original_stdout()
40 try:
41 finder = doctest.DocTestFinder(verbose=verbose, recurse=False)
42 runner = doctest.DocTestRunner(verbose=verbose)
43 # Use the object's fully qualified name if it has one
44 # Otherwise, use the module's name
45 try:
46 name = "%s.%s" % (obj.__module__, obj.__name__)
47 except AttributeError:
48 name = module.__name__
49 for example in finder.find(obj, name, module):
50 runner.run(example)
51 f, t = runner.failures, runner.tries
52 if f:
53 raise test.support.TestFailed("%d of %d doctests failed" % (f, t))
54 finally:
55 sys.stdout = save_stdout
56 if verbose:
57 print ('doctest (%s) ... %d tests with zero failures' % (module.__name__, t))
58 return f, t
59
60
61
Nick Coghlan90be5fb2011-01-11 10:05:20 +000062class ZipSupportTests(unittest.TestCase):
63 # This used to use the ImportHooksBaseTestCase to restore
Nick Coghlanf088e5e2008-12-14 11:50:48 +000064 # the state of the import related information
Nick Coghlan90be5fb2011-01-11 10:05:20 +000065 # in the sys module after each test. However, that restores
66 # *too much* information and breaks for the invocation of
67 # of test_doctest. So we do our own thing and leave
68 # sys.modules alone.
Nick Coghlanf088e5e2008-12-14 11:50:48 +000069 # We also clear the linecache and zipimport cache
70 # just to avoid any bogus errors due to name reuse in the tests
71 def setUp(self):
72 linecache.clearcache()
73 zipimport._zip_directory_cache.clear()
Nick Coghlan90be5fb2011-01-11 10:05:20 +000074 self.path = sys.path[:]
75 self.meta_path = sys.meta_path[:]
76 self.path_hooks = sys.path_hooks[:]
77 sys.path_importer_cache.clear()
Nick Coghlanf088e5e2008-12-14 11:50:48 +000078
Nick Coghlan90be5fb2011-01-11 10:05:20 +000079 def tearDown(self):
80 sys.path[:] = self.path
81 sys.meta_path[:] = self.meta_path
82 sys.path_hooks[:] = self.path_hooks
83 sys.path_importer_cache.clear()
Nick Coghlanf088e5e2008-12-14 11:50:48 +000084
85 def test_inspect_getsource_issue4223(self):
86 test_src = "def foo(): pass\n"
87 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +000088 init_name = make_script(d, '__init__', test_src)
Nick Coghlanf088e5e2008-12-14 11:50:48 +000089 name_in_zip = os.path.join('zip_pkg',
90 os.path.basename(init_name))
Nick Coghlan260bd3e2009-11-16 06:49:25 +000091 zip_name, run_name = make_zip_script(d, 'test_zip',
Nick Coghlanf088e5e2008-12-14 11:50:48 +000092 init_name, name_in_zip)
93 os.remove(init_name)
94 sys.path.insert(0, zip_name)
95 import zip_pkg
96 self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
97
98 def test_doctest_issue4197(self):
99 # To avoid having to keep two copies of the doctest module's
100 # unit tests in sync, this test works by taking the source of
101 # test_doctest itself, rewriting it a bit to cope with a new
102 # location, and then throwing it in a zip file to make sure
103 # everything still works correctly
104 test_src = inspect.getsource(test_doctest)
105 test_src = test_src.replace(
106 "from test import test_doctest",
107 "import test_zipped_doctest as test_doctest")
108 test_src = test_src.replace("test.test_doctest",
109 "test_zipped_doctest")
110 test_src = test_src.replace("test.sample_doctest",
111 "sample_zipped_doctest")
112 sample_src = inspect.getsource(sample_doctest)
113 sample_src = sample_src.replace("test.test_doctest",
114 "test_zipped_doctest")
115 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000116 script_name = make_script(d, 'test_zipped_doctest',
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000117 test_src)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000118 zip_name, run_name = make_zip_script(d, 'test_zip',
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000119 script_name)
120 z = zipfile.ZipFile(zip_name, 'a')
121 z.writestr("sample_zipped_doctest.py", sample_src)
122 z.close()
123 if verbose:
124 zip_file = zipfile.ZipFile(zip_name, 'r')
125 print ('Contents of %r:' % zip_name)
126 zip_file.printdir()
127 zip_file.close()
128 os.remove(script_name)
129 sys.path.insert(0, zip_name)
130 import test_zipped_doctest
131 # Some of the doc tests depend on the colocated text files
132 # which aren't available to the zipped version (the doctest
133 # module currently requires real filenames for non-embedded
134 # tests). So we're forced to be selective about which tests
135 # to run.
136 # doctest could really use some APIs which take a text
137 # string or a file object instead of a filename...
138 known_good_tests = [
139 test_zipped_doctest.SampleClass,
140 test_zipped_doctest.SampleClass.NestedClass,
141 test_zipped_doctest.SampleClass.NestedClass.__init__,
142 test_zipped_doctest.SampleClass.__init__,
143 test_zipped_doctest.SampleClass.a_classmethod,
144 test_zipped_doctest.SampleClass.a_property,
145 test_zipped_doctest.SampleClass.a_staticmethod,
146 test_zipped_doctest.SampleClass.double,
147 test_zipped_doctest.SampleClass.get,
148 test_zipped_doctest.SampleNewStyleClass,
149 test_zipped_doctest.SampleNewStyleClass.__init__,
150 test_zipped_doctest.SampleNewStyleClass.double,
151 test_zipped_doctest.SampleNewStyleClass.get,
152 test_zipped_doctest.sample_func,
153 test_zipped_doctest.test_DocTest,
154 test_zipped_doctest.test_DocTestParser,
155 test_zipped_doctest.test_DocTestRunner.basics,
156 test_zipped_doctest.test_DocTestRunner.exceptions,
157 test_zipped_doctest.test_DocTestRunner.option_directives,
158 test_zipped_doctest.test_DocTestRunner.optionflags,
159 test_zipped_doctest.test_DocTestRunner.verbose_flag,
160 test_zipped_doctest.test_Example,
161 test_zipped_doctest.test_debug,
162 test_zipped_doctest.test_pdb_set_trace,
163 test_zipped_doctest.test_pdb_set_trace_nested,
164 test_zipped_doctest.test_testsource,
165 test_zipped_doctest.test_trailing_space_in_test,
166 test_zipped_doctest.test_DocTestSuite,
167 test_zipped_doctest.test_DocTestFinder,
168 ]
169 # These remaining tests are the ones which need access
170 # to the data files, so we don't run them
171 fail_due_to_missing_data_files = [
172 test_zipped_doctest.test_DocFileSuite,
173 test_zipped_doctest.test_testfile,
174 test_zipped_doctest.test_unittest_reportflags,
175 ]
176 for obj in known_good_tests:
177 _run_object_doctest(obj, test_zipped_doctest)
178
Nick Coghlan38622002008-12-15 12:01:34 +0000179 def test_doctest_main_issue4197(self):
180 test_src = textwrap.dedent("""\
181 class Test:
182 ">>> 'line 2'"
183 pass
184
185 import doctest
186 doctest.testmod()
187 """)
188 pattern = 'File "%s", line 2, in %s'
189 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000190 script_name = make_script(d, 'script', test_src)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000191 rc, out, err = assert_python_ok(script_name)
Nick Coghlan38622002008-12-15 12:01:34 +0000192 expected = pattern % (script_name, "__main__.Test")
193 if verbose:
194 print ("Expected line", expected)
195 print ("Got stdout:")
Victor Stinner6722b5f2010-10-20 21:48:35 +0000196 print (ascii(out))
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000197 self.assertIn(expected.encode('utf-8'), out)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000198 zip_name, run_name = make_zip_script(d, "test_zip",
Nick Coghlan38622002008-12-15 12:01:34 +0000199 script_name, '__main__.py')
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000200 rc, out, err = assert_python_ok(zip_name)
Nick Coghlan38622002008-12-15 12:01:34 +0000201 expected = pattern % (run_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 Coghlan38622002008-12-15 12:01:34 +0000207
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000208 def test_pdb_issue4201(self):
209 test_src = textwrap.dedent("""\
210 def f():
211 pass
212
213 import pdb
Georg Brandl34748cd2010-12-04 17:11:36 +0000214 pdb.Pdb(nosigint=True).runcall(f)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000215 """)
216 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000217 script_name = make_script(d, 'script', test_src)
218 p = spawn_python(script_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000219 p.stdin.write(b'l\n')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000220 data = kill_python(p)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000221 self.assertIn(script_name.encode('utf-8'), data)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000222 zip_name, run_name = make_zip_script(d, "test_zip",
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000223 script_name, '__main__.py')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000224 p = spawn_python(zip_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)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000227 self.assertIn(run_name.encode('utf-8'), data)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000228
229
230def test_main():
231 test.support.run_unittest(ZipSupportTests)
232 test.support.reap_children()
233
234if __name__ == '__main__':
235 test_main()