blob: b996e26f78d5f68c13314772cd177a399253e0fe [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
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000016from test.script_helper import (spawn_python, kill_python, assert_python_ok,
Georg Brandl1b37e872010-03-14 10:45:50 +000017 temp_dir, 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
31from test import test_doctest, sample_doctest
32from test.test_importhooks import ImportHooksBaseTestCase
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
62class ZipSupportTests(ImportHooksBaseTestCase):
63 # We use the ImportHooksBaseTestCase to restore
64 # the state of the import related information
65 # in the sys module after each test
66 # We also clear the linecache and zipimport cache
67 # just to avoid any bogus errors due to name reuse in the tests
68 def setUp(self):
69 linecache.clearcache()
70 zipimport._zip_directory_cache.clear()
71 ImportHooksBaseTestCase.setUp(self)
72
73
74 def test_inspect_getsource_issue4223(self):
75 test_src = "def foo(): pass\n"
76 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +000077 init_name = make_script(d, '__init__', test_src)
Nick Coghlanf088e5e2008-12-14 11:50:48 +000078 name_in_zip = os.path.join('zip_pkg',
79 os.path.basename(init_name))
Nick Coghlan260bd3e2009-11-16 06:49:25 +000080 zip_name, run_name = make_zip_script(d, 'test_zip',
Nick Coghlanf088e5e2008-12-14 11:50:48 +000081 init_name, name_in_zip)
82 os.remove(init_name)
83 sys.path.insert(0, zip_name)
84 import zip_pkg
85 self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
86
87 def test_doctest_issue4197(self):
88 # To avoid having to keep two copies of the doctest module's
89 # unit tests in sync, this test works by taking the source of
90 # test_doctest itself, rewriting it a bit to cope with a new
91 # location, and then throwing it in a zip file to make sure
92 # everything still works correctly
93 test_src = inspect.getsource(test_doctest)
94 test_src = test_src.replace(
95 "from test import test_doctest",
96 "import test_zipped_doctest as test_doctest")
97 test_src = test_src.replace("test.test_doctest",
98 "test_zipped_doctest")
99 test_src = test_src.replace("test.sample_doctest",
100 "sample_zipped_doctest")
101 sample_src = inspect.getsource(sample_doctest)
102 sample_src = sample_src.replace("test.test_doctest",
103 "test_zipped_doctest")
104 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000105 script_name = make_script(d, 'test_zipped_doctest',
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000106 test_src)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000107 zip_name, run_name = make_zip_script(d, 'test_zip',
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000108 script_name)
109 z = zipfile.ZipFile(zip_name, 'a')
110 z.writestr("sample_zipped_doctest.py", sample_src)
111 z.close()
112 if verbose:
113 zip_file = zipfile.ZipFile(zip_name, 'r')
114 print ('Contents of %r:' % zip_name)
115 zip_file.printdir()
116 zip_file.close()
117 os.remove(script_name)
118 sys.path.insert(0, zip_name)
119 import test_zipped_doctest
120 # Some of the doc tests depend on the colocated text files
121 # which aren't available to the zipped version (the doctest
122 # module currently requires real filenames for non-embedded
123 # tests). So we're forced to be selective about which tests
124 # to run.
125 # doctest could really use some APIs which take a text
126 # string or a file object instead of a filename...
127 known_good_tests = [
128 test_zipped_doctest.SampleClass,
129 test_zipped_doctest.SampleClass.NestedClass,
130 test_zipped_doctest.SampleClass.NestedClass.__init__,
131 test_zipped_doctest.SampleClass.__init__,
132 test_zipped_doctest.SampleClass.a_classmethod,
133 test_zipped_doctest.SampleClass.a_property,
134 test_zipped_doctest.SampleClass.a_staticmethod,
135 test_zipped_doctest.SampleClass.double,
136 test_zipped_doctest.SampleClass.get,
137 test_zipped_doctest.SampleNewStyleClass,
138 test_zipped_doctest.SampleNewStyleClass.__init__,
139 test_zipped_doctest.SampleNewStyleClass.double,
140 test_zipped_doctest.SampleNewStyleClass.get,
141 test_zipped_doctest.sample_func,
142 test_zipped_doctest.test_DocTest,
143 test_zipped_doctest.test_DocTestParser,
144 test_zipped_doctest.test_DocTestRunner.basics,
145 test_zipped_doctest.test_DocTestRunner.exceptions,
146 test_zipped_doctest.test_DocTestRunner.option_directives,
147 test_zipped_doctest.test_DocTestRunner.optionflags,
148 test_zipped_doctest.test_DocTestRunner.verbose_flag,
149 test_zipped_doctest.test_Example,
150 test_zipped_doctest.test_debug,
151 test_zipped_doctest.test_pdb_set_trace,
152 test_zipped_doctest.test_pdb_set_trace_nested,
153 test_zipped_doctest.test_testsource,
154 test_zipped_doctest.test_trailing_space_in_test,
155 test_zipped_doctest.test_DocTestSuite,
156 test_zipped_doctest.test_DocTestFinder,
157 ]
158 # These remaining tests are the ones which need access
159 # to the data files, so we don't run them
160 fail_due_to_missing_data_files = [
161 test_zipped_doctest.test_DocFileSuite,
162 test_zipped_doctest.test_testfile,
163 test_zipped_doctest.test_unittest_reportflags,
164 ]
165 for obj in known_good_tests:
166 _run_object_doctest(obj, test_zipped_doctest)
167
Nick Coghlan38622002008-12-15 12:01:34 +0000168 def test_doctest_main_issue4197(self):
169 test_src = textwrap.dedent("""\
170 class Test:
171 ">>> 'line 2'"
172 pass
173
174 import doctest
175 doctest.testmod()
176 """)
177 pattern = 'File "%s", line 2, in %s'
178 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000179 script_name = make_script(d, 'script', test_src)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000180 rc, out, err = assert_python_ok(script_name)
Nick Coghlan38622002008-12-15 12:01:34 +0000181 expected = pattern % (script_name, "__main__.Test")
182 if verbose:
183 print ("Expected line", expected)
184 print ("Got stdout:")
Victor Stinner6722b5f2010-10-20 21:48:35 +0000185 print (ascii(out))
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000186 self.assertIn(expected.encode('utf-8'), out)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000187 zip_name, run_name = make_zip_script(d, "test_zip",
Nick Coghlan38622002008-12-15 12:01:34 +0000188 script_name, '__main__.py')
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000189 rc, out, err = assert_python_ok(zip_name)
Nick Coghlan38622002008-12-15 12:01:34 +0000190 expected = pattern % (run_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 Coghlan38622002008-12-15 12:01:34 +0000196
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000197 def test_pdb_issue4201(self):
198 test_src = textwrap.dedent("""\
199 def f():
200 pass
201
202 import pdb
203 pdb.runcall(f)
204 """)
205 with temp_dir() as d:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000206 script_name = make_script(d, 'script', test_src)
207 p = spawn_python(script_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000208 p.stdin.write(b'l\n')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000209 data = kill_python(p)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000210 self.assertIn(script_name.encode('utf-8'), data)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000211 zip_name, run_name = make_zip_script(d, "test_zip",
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000212 script_name, '__main__.py')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000213 p = spawn_python(zip_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000214 p.stdin.write(b'l\n')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000215 data = kill_python(p)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000216 self.assertIn(run_name.encode('utf-8'), data)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000217
218
219def test_main():
220 test.support.run_unittest(ZipSupportTests)
221 test.support.reap_children()
222
223if __name__ == '__main__':
224 test_main()