blob: 3f4cd3fca8b1d689a0fd3ba19ea4ac953833e5a3 [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
5import unittest
6import test.support
7import os
8import os.path
9import sys
10import textwrap
11import zipfile
12import zipimport
13import doctest
14import inspect
15import linecache
16import pdb
17
18verbose = test.support.verbose
19
20# Library modules covered by this test set
21# pdb (Issue 4201)
22# inspect (Issue 4223)
23# doctest (Issue 4197)
24
25# Other test modules with zipimport related tests
26# test_zipimport (of course!)
27# test_cmd_line_script (covers the zipimport support in runpy)
28
29# Retrieve some helpers from other test cases
30from test import test_doctest, sample_doctest
31from test.test_importhooks import ImportHooksBaseTestCase
32from test.test_cmd_line_script import temp_dir, _run_python, \
33 _spawn_python, _kill_python, \
34 _make_test_script, \
35 _compile_test_script, \
36 _make_test_zip, _make_test_pkg
37
38
39def _run_object_doctest(obj, module):
40 # Direct doctest output (normally just errors) to real stdout; doctest
41 # output shouldn't be compared by regrtest.
42 save_stdout = sys.stdout
43 sys.stdout = test.support.get_original_stdout()
44 try:
45 finder = doctest.DocTestFinder(verbose=verbose, recurse=False)
46 runner = doctest.DocTestRunner(verbose=verbose)
47 # Use the object's fully qualified name if it has one
48 # Otherwise, use the module's name
49 try:
50 name = "%s.%s" % (obj.__module__, obj.__name__)
51 except AttributeError:
52 name = module.__name__
53 for example in finder.find(obj, name, module):
54 runner.run(example)
55 f, t = runner.failures, runner.tries
56 if f:
57 raise test.support.TestFailed("%d of %d doctests failed" % (f, t))
58 finally:
59 sys.stdout = save_stdout
60 if verbose:
61 print ('doctest (%s) ... %d tests with zero failures' % (module.__name__, t))
62 return f, t
63
64
65
66class ZipSupportTests(ImportHooksBaseTestCase):
67 # We use the ImportHooksBaseTestCase to restore
68 # the state of the import related information
69 # in the sys module after each test
70 # We also clear the linecache and zipimport cache
71 # just to avoid any bogus errors due to name reuse in the tests
72 def setUp(self):
73 linecache.clearcache()
74 zipimport._zip_directory_cache.clear()
75 ImportHooksBaseTestCase.setUp(self)
76
77
78 def test_inspect_getsource_issue4223(self):
79 test_src = "def foo(): pass\n"
80 with temp_dir() as d:
81 init_name = _make_test_script(d, '__init__', test_src)
82 name_in_zip = os.path.join('zip_pkg',
83 os.path.basename(init_name))
84 zip_name, run_name = _make_test_zip(d, 'test_zip',
85 init_name, name_in_zip)
86 os.remove(init_name)
87 sys.path.insert(0, zip_name)
88 import zip_pkg
89 self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
90
91 def test_doctest_issue4197(self):
92 # To avoid having to keep two copies of the doctest module's
93 # unit tests in sync, this test works by taking the source of
94 # test_doctest itself, rewriting it a bit to cope with a new
95 # location, and then throwing it in a zip file to make sure
96 # everything still works correctly
97 test_src = inspect.getsource(test_doctest)
98 test_src = test_src.replace(
99 "from test import test_doctest",
100 "import test_zipped_doctest as test_doctest")
101 test_src = test_src.replace("test.test_doctest",
102 "test_zipped_doctest")
103 test_src = test_src.replace("test.sample_doctest",
104 "sample_zipped_doctest")
105 sample_src = inspect.getsource(sample_doctest)
106 sample_src = sample_src.replace("test.test_doctest",
107 "test_zipped_doctest")
108 with temp_dir() as d:
109 script_name = _make_test_script(d, 'test_zipped_doctest',
110 test_src)
111 zip_name, run_name = _make_test_zip(d, 'test_zip',
112 script_name)
113 z = zipfile.ZipFile(zip_name, 'a')
114 z.writestr("sample_zipped_doctest.py", sample_src)
115 z.close()
116 if verbose:
117 zip_file = zipfile.ZipFile(zip_name, 'r')
118 print ('Contents of %r:' % zip_name)
119 zip_file.printdir()
120 zip_file.close()
121 os.remove(script_name)
122 sys.path.insert(0, zip_name)
123 import test_zipped_doctest
124 # Some of the doc tests depend on the colocated text files
125 # which aren't available to the zipped version (the doctest
126 # module currently requires real filenames for non-embedded
127 # tests). So we're forced to be selective about which tests
128 # to run.
129 # doctest could really use some APIs which take a text
130 # string or a file object instead of a filename...
131 known_good_tests = [
132 test_zipped_doctest.SampleClass,
133 test_zipped_doctest.SampleClass.NestedClass,
134 test_zipped_doctest.SampleClass.NestedClass.__init__,
135 test_zipped_doctest.SampleClass.__init__,
136 test_zipped_doctest.SampleClass.a_classmethod,
137 test_zipped_doctest.SampleClass.a_property,
138 test_zipped_doctest.SampleClass.a_staticmethod,
139 test_zipped_doctest.SampleClass.double,
140 test_zipped_doctest.SampleClass.get,
141 test_zipped_doctest.SampleNewStyleClass,
142 test_zipped_doctest.SampleNewStyleClass.__init__,
143 test_zipped_doctest.SampleNewStyleClass.double,
144 test_zipped_doctest.SampleNewStyleClass.get,
145 test_zipped_doctest.sample_func,
146 test_zipped_doctest.test_DocTest,
147 test_zipped_doctest.test_DocTestParser,
148 test_zipped_doctest.test_DocTestRunner.basics,
149 test_zipped_doctest.test_DocTestRunner.exceptions,
150 test_zipped_doctest.test_DocTestRunner.option_directives,
151 test_zipped_doctest.test_DocTestRunner.optionflags,
152 test_zipped_doctest.test_DocTestRunner.verbose_flag,
153 test_zipped_doctest.test_Example,
154 test_zipped_doctest.test_debug,
155 test_zipped_doctest.test_pdb_set_trace,
156 test_zipped_doctest.test_pdb_set_trace_nested,
157 test_zipped_doctest.test_testsource,
158 test_zipped_doctest.test_trailing_space_in_test,
159 test_zipped_doctest.test_DocTestSuite,
160 test_zipped_doctest.test_DocTestFinder,
161 ]
162 # These remaining tests are the ones which need access
163 # to the data files, so we don't run them
164 fail_due_to_missing_data_files = [
165 test_zipped_doctest.test_DocFileSuite,
166 test_zipped_doctest.test_testfile,
167 test_zipped_doctest.test_unittest_reportflags,
168 ]
169 for obj in known_good_tests:
170 _run_object_doctest(obj, test_zipped_doctest)
171
Nick Coghlan38622002008-12-15 12:01:34 +0000172 def test_doctest_main_issue4197(self):
173 test_src = textwrap.dedent("""\
174 class Test:
175 ">>> 'line 2'"
176 pass
177
178 import doctest
179 doctest.testmod()
180 """)
181 pattern = 'File "%s", line 2, in %s'
182 with temp_dir() as d:
183 script_name = _make_test_script(d, 'script', test_src)
184 exit_code, data = _run_python(script_name)
185 expected = pattern % (script_name, "__main__.Test")
186 if verbose:
187 print ("Expected line", expected)
188 print ("Got stdout:")
189 print (data)
190 self.assert_(expected in data)
191 zip_name, run_name = _make_test_zip(d, "test_zip",
192 script_name, '__main__.py')
193 exit_code, data = _run_python(zip_name)
194 expected = pattern % (run_name, "__main__.Test")
195 if verbose:
196 print ("Expected line", expected)
197 print ("Got stdout:")
198 print (data)
199 self.assert_(expected in data)
200
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000201 def test_pdb_issue4201(self):
202 test_src = textwrap.dedent("""\
203 def f():
204 pass
205
206 import pdb
207 pdb.runcall(f)
208 """)
209 with temp_dir() as d:
210 script_name = _make_test_script(d, 'script', test_src)
211 p = _spawn_python(script_name)
212 p.stdin.write(b'l\n')
213 data = _kill_python(p).decode()
214 self.assert_(script_name in data)
215 zip_name, run_name = _make_test_zip(d, "test_zip",
216 script_name, '__main__.py')
217 p = _spawn_python(zip_name)
218 p.stdin.write(b'l\n')
219 data = _kill_python(p).decode()
220 self.assert_(run_name in data)
221
222
223def test_main():
224 test.support.run_unittest(ZipSupportTests)
225 test.support.reap_children()
226
227if __name__ == '__main__':
228 test_main()