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