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