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 |
| 32 | from test import test_doctest, sample_doctest |
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: |
Victor Stinner | 592f679 | 2011-06-29 18:11:36 +0200 | [diff] [blame] | 41 | name = "%s.%s" % (obj.__module__, obj.__name__) |
| 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 |
| 59 | # *too much* information and breaks for the invocation of |
| 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" |
| 80 | with 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") |
| 108 | sample_src = inspect.getsource(sample_doctest) |
| 109 | sample_src = sample_src.replace("test.test_doctest", |
| 110 | "test_zipped_doctest") |
| 111 | with temp_dir() as d: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 112 | script_name = make_script(d, 'test_zipped_doctest', |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 113 | test_src) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 114 | zip_name, run_name = make_zip_script(d, 'test_zip', |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 115 | script_name) |
| 116 | z = zipfile.ZipFile(zip_name, 'a') |
| 117 | z.writestr("sample_zipped_doctest.py", sample_src) |
| 118 | z.close() |
| 119 | if verbose: |
| 120 | zip_file = zipfile.ZipFile(zip_name, 'r') |
| 121 | print ('Contents of %r:' % zip_name) |
| 122 | zip_file.printdir() |
| 123 | zip_file.close() |
| 124 | os.remove(script_name) |
| 125 | sys.path.insert(0, zip_name) |
| 126 | import test_zipped_doctest |
Nick Coghlan | ef31657 | 2011-02-07 13:43:07 +0000 | [diff] [blame] | 127 | try: |
| 128 | # Some of the doc tests depend on the colocated text files |
| 129 | # which aren't available to the zipped version (the doctest |
| 130 | # module currently requires real filenames for non-embedded |
| 131 | # tests). So we're forced to be selective about which tests |
| 132 | # to run. |
| 133 | # doctest could really use some APIs which take a text |
| 134 | # string or a file object instead of a filename... |
| 135 | known_good_tests = [ |
| 136 | test_zipped_doctest.SampleClass, |
| 137 | test_zipped_doctest.SampleClass.NestedClass, |
| 138 | test_zipped_doctest.SampleClass.NestedClass.__init__, |
| 139 | test_zipped_doctest.SampleClass.__init__, |
| 140 | test_zipped_doctest.SampleClass.a_classmethod, |
| 141 | test_zipped_doctest.SampleClass.a_property, |
| 142 | test_zipped_doctest.SampleClass.a_staticmethod, |
| 143 | test_zipped_doctest.SampleClass.double, |
| 144 | test_zipped_doctest.SampleClass.get, |
| 145 | test_zipped_doctest.SampleNewStyleClass, |
| 146 | test_zipped_doctest.SampleNewStyleClass.__init__, |
| 147 | test_zipped_doctest.SampleNewStyleClass.double, |
| 148 | test_zipped_doctest.SampleNewStyleClass.get, |
| 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, |
Nick Coghlan | ef31657 | 2011-02-07 13:43:07 +0000 | [diff] [blame] | 159 | test_zipped_doctest.test_testsource, |
| 160 | test_zipped_doctest.test_trailing_space_in_test, |
| 161 | test_zipped_doctest.test_DocTestSuite, |
| 162 | test_zipped_doctest.test_DocTestFinder, |
| 163 | ] |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 164 | # These tests are the ones which need access |
Nick Coghlan | ef31657 | 2011-02-07 13:43:07 +0000 | [diff] [blame] | 165 | # to the data files, so we don't run them |
| 166 | fail_due_to_missing_data_files = [ |
| 167 | test_zipped_doctest.test_DocFileSuite, |
| 168 | test_zipped_doctest.test_testfile, |
| 169 | test_zipped_doctest.test_unittest_reportflags, |
| 170 | ] |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 171 | |
Nick Coghlan | ef31657 | 2011-02-07 13:43:07 +0000 | [diff] [blame] | 172 | for obj in known_good_tests: |
| 173 | _run_object_doctest(obj, test_zipped_doctest) |
| 174 | finally: |
| 175 | del sys.modules["test_zipped_doctest"] |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 176 | |
Nick Coghlan | 3862200 | 2008-12-15 12:01:34 +0000 | [diff] [blame] | 177 | def test_doctest_main_issue4197(self): |
| 178 | test_src = textwrap.dedent("""\ |
| 179 | class Test: |
| 180 | ">>> 'line 2'" |
| 181 | pass |
| 182 | |
| 183 | import doctest |
| 184 | doctest.testmod() |
| 185 | """) |
| 186 | pattern = 'File "%s", line 2, in %s' |
| 187 | with temp_dir() as d: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 188 | script_name = make_script(d, 'script', test_src) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 189 | rc, out, err = assert_python_ok(script_name) |
Nick Coghlan | 3862200 | 2008-12-15 12:01:34 +0000 | [diff] [blame] | 190 | expected = pattern % (script_name, "__main__.Test") |
| 191 | if verbose: |
| 192 | print ("Expected line", expected) |
| 193 | print ("Got stdout:") |
Victor Stinner | 6722b5f | 2010-10-20 21:48:35 +0000 | [diff] [blame] | 194 | print (ascii(out)) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 195 | self.assertIn(expected.encode('utf-8'), out) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 196 | zip_name, run_name = make_zip_script(d, "test_zip", |
Nick Coghlan | 3862200 | 2008-12-15 12:01:34 +0000 | [diff] [blame] | 197 | script_name, '__main__.py') |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 198 | rc, out, err = assert_python_ok(zip_name) |
Nick Coghlan | 3862200 | 2008-12-15 12:01:34 +0000 | [diff] [blame] | 199 | expected = pattern % (run_name, "__main__.Test") |
| 200 | if verbose: |
| 201 | print ("Expected line", expected) |
| 202 | print ("Got stdout:") |
Victor Stinner | 6722b5f | 2010-10-20 21:48:35 +0000 | [diff] [blame] | 203 | print (ascii(out)) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 204 | self.assertIn(expected.encode('utf-8'), out) |
Nick Coghlan | 3862200 | 2008-12-15 12:01:34 +0000 | [diff] [blame] | 205 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 206 | def test_pdb_issue4201(self): |
| 207 | test_src = textwrap.dedent("""\ |
| 208 | def f(): |
| 209 | pass |
| 210 | |
| 211 | import pdb |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 212 | pdb.Pdb(nosigint=True).runcall(f) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 213 | """) |
| 214 | with temp_dir() as d: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 215 | script_name = make_script(d, 'script', test_src) |
| 216 | p = spawn_python(script_name) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 217 | p.stdin.write(b'l\n') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 218 | data = kill_python(p) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 219 | self.assertIn(script_name.encode('utf-8'), data) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 220 | zip_name, run_name = make_zip_script(d, "test_zip", |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 221 | script_name, '__main__.py') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 222 | p = spawn_python(zip_name) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 223 | p.stdin.write(b'l\n') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 224 | data = kill_python(p) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 225 | self.assertIn(run_name.encode('utf-8'), data) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 226 | |
| 227 | |
| 228 | def test_main(): |
| 229 | test.support.run_unittest(ZipSupportTests) |
| 230 | test.support.reap_children() |
| 231 | |
| 232 | if __name__ == '__main__': |
| 233 | test_main() |