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