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