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
6 import test
.test_support
18 verbose
= test
.test_support
.verbose
20 # Library modules covered by this test set
22 # inspect (Issue 4223)
23 # doctest (Issue 4197)
25 # Other test modules with zipimport related tests
26 # test_zipimport (of course!)
27 # test_cmd_line_script (covers the zipimport support in runpy)
29 # Retrieve some helpers from other test cases
30 from test
import test_doctest
, sample_doctest
31 from test
.test_importhooks
import ImportHooksBaseTestCase
32 from test
.test_cmd_line_script
import temp_dir
, _run_python
, \
33 _spawn_python
, _kill_python
, \
35 _compile_test_script
, \
36 _make_test_zip
, _make_test_pkg
39 def _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
.test_support
.get_original_stdout()
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
50 name
= "%s.%s" % (obj
.__module
__, obj
.__name
__)
51 except AttributeError:
52 name
= module
.__name
__
53 for example
in finder
.find(obj
, name
, module
):
55 f
, t
= runner
.failures
, runner
.tries
57 raise test
.test_support
.TestFailed("%d of %d doctests failed" % (f
, t
))
59 sys
.stdout
= save_stdout
61 print 'doctest (%s) ... %d tests with zero failures' % (module
.__name
__, t
)
66 class 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
73 linecache
.clearcache()
74 zipimport
._zip
_directory
_cache
.clear()
75 ImportHooksBaseTestCase
.setUp(self
)
78 def test_inspect_getsource_issue4223(self
):
79 test_src
= "def foo(): pass\n"
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
)
87 sys
.path
.insert(0, zip_name
)
89 self
.assertEqual(inspect
.getsource(zip_pkg
.foo
), test_src
)
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',
111 zip_name
, run_name
= _make_test_zip(d
, 'test_zip',
113 z
= zipfile
.ZipFile(zip_name
, 'a')
114 z
.writestr("sample_zipped_doctest.py", sample_src
)
117 zip_file
= zipfile
.ZipFile(zip_name
, 'r')
118 print 'Contents of %r:' % zip_name
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
129 # doctest could really use some APIs which take a text
130 # string or a file object instead of a filename...
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
.old_test1
,
146 test_zipped_doctest
.old_test2
,
147 test_zipped_doctest
.old_test3
,
148 test_zipped_doctest
.old_test4
,
149 test_zipped_doctest
.sample_func
,
150 test_zipped_doctest
.test_DocTest
,
151 test_zipped_doctest
.test_DocTestParser
,
152 test_zipped_doctest
.test_DocTestRunner
.basics
,
153 test_zipped_doctest
.test_DocTestRunner
.exceptions
,
154 test_zipped_doctest
.test_DocTestRunner
.option_directives
,
155 test_zipped_doctest
.test_DocTestRunner
.optionflags
,
156 test_zipped_doctest
.test_DocTestRunner
.verbose_flag
,
157 test_zipped_doctest
.test_Example
,
158 test_zipped_doctest
.test_debug
,
159 test_zipped_doctest
.test_pdb_set_trace
,
160 test_zipped_doctest
.test_pdb_set_trace_nested
,
161 test_zipped_doctest
.test_testsource
,
162 test_zipped_doctest
.test_trailing_space_in_test
,
163 test_zipped_doctest
.test_DocTestSuite
,
164 test_zipped_doctest
.test_DocTestFinder
,
166 # These remaining tests are the ones which need access
167 # to the data files, so we don't run them
168 fail_due_to_missing_data_files
= [
169 test_zipped_doctest
.test_DocFileSuite
,
170 test_zipped_doctest
.test_testfile
,
171 test_zipped_doctest
.test_unittest_reportflags
,
173 for obj
in known_good_tests
:
174 _run_object_doctest(obj
, test_zipped_doctest
)
176 def test_doctest_main_issue4197(self
):
177 test_src
= textwrap
.dedent("""\
185 pattern
= 'File "%s", line 2, in %s'
186 with
temp_dir() as d
:
187 script_name
= _make_test_script(d
, 'script', test_src
)
188 exit_code
, data
= _run_python(script_name
)
189 expected
= pattern
% (script_name
, "__main__.Test")
191 print "Expected line", expected
194 self
.assert_(expected
in data
)
195 zip_name
, run_name
= _make_test_zip(d
, "test_zip",
196 script_name
, '__main__.py')
197 exit_code
, data
= _run_python(zip_name
)
198 expected
= pattern
% (run_name
, "__main__.Test")
200 print "Expected line", expected
203 self
.assert_(expected
in data
)
205 def test_pdb_issue4201(self
):
206 test_src
= textwrap
.dedent("""\
213 with
temp_dir() as d
:
214 script_name
= _make_test_script(d
, 'script', test_src
)
215 p
= _spawn_python(script_name
)
217 data
= _kill_python(p
)
218 self
.assert_(script_name
in data
)
219 zip_name
, run_name
= _make_test_zip(d
, "test_zip",
220 script_name
, '__main__.py')
221 p
= _spawn_python(zip_name
)
223 data
= _kill_python(p
)
224 self
.assert_(run_name
in data
)
228 test
.test_support
.run_unittest(ZipSupportTests
)
229 test
.test_support
.reap_children()
231 if __name__
== '__main__':