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