(self)
| 97 | del sys.modules["zip_pkg"] |
| 98 | |
| 99 | def test_doctest_issue4197(self): |
| 100 | # To avoid having to keep two copies of the doctest module's |
| 101 | # unit tests in sync, this test works by taking the source of |
| 102 | # test_doctest itself, rewriting it a bit to cope with a new |
| 103 | # location, and then throwing it in a zip file to make sure |
| 104 | # everything still works correctly |
| 105 | test_src = inspect.getsource(test_doctest) |
| 106 | test_src = test_src.replace( |
| 107 | "from test.test_doctest import test_doctest", |
| 108 | "import test_zipped_doctest as test_doctest") |
| 109 | test_src = test_src.replace("test.test_doctest.test_doctest", |
| 110 | "test_zipped_doctest") |
| 111 | test_src = test_src.replace("test.test_doctest.sample_doctest", |
| 112 | "sample_zipped_doctest") |
| 113 | # The sample doctest files rewritten to include in the zipped version. |
| 114 | sample_sources = {} |
| 115 | for mod in [sample_doctest, sample_doctest_no_doctests, |
| 116 | sample_doctest_no_docstrings, sample_doctest_skip]: |
| 117 | src = inspect.getsource(mod) |
| 118 | src = src.replace("test.test_doctest.test_doctest", "test_zipped_doctest") |
| 119 | # Rewrite the module name so that, for example, |
| 120 | # "test.sample_doctest" becomes "sample_zipped_doctest". |
| 121 | mod_name = mod.__name__.split(".")[-1] |
| 122 | mod_name = mod_name.replace("sample_", "sample_zipped_") |
| 123 | sample_sources[mod_name] = src |
| 124 | |
| 125 | with os_helper.temp_dir() as d: |
| 126 | script_name = make_script(d, 'test_zipped_doctest', |
| 127 | test_src) |
| 128 | zip_name, run_name = make_zip_script(d, 'test_zip', |
| 129 | script_name) |
| 130 | with zipfile.ZipFile(zip_name, 'a') as z: |
| 131 | for mod_name, src in sample_sources.items(): |
| 132 | z.writestr(mod_name + ".py", src) |
| 133 | if verbose: |
| 134 | with zipfile.ZipFile(zip_name, 'r') as zip_file: |
| 135 | print ('Contents of %r:' % zip_name) |
| 136 | zip_file.printdir() |
| 137 | os.remove(script_name) |
| 138 | sys.path.insert(0, zip_name) |
| 139 | import test_zipped_doctest |
| 140 | try: |
| 141 | # Some of the doc tests depend on the colocated text files |
| 142 | # which aren't available to the zipped version (the doctest |
| 143 | # module currently requires real filenames for non-embedded |
| 144 | # tests). So we're forced to be selective about which tests |
| 145 | # to run. |
| 146 | # doctest could really use some APIs which take a text |
| 147 | # string or a file object instead of a filename... |
| 148 | known_good_tests = [ |
| 149 | test_zipped_doctest.SampleClass, |
| 150 | test_zipped_doctest.SampleClass.NestedClass, |
| 151 | test_zipped_doctest.SampleClass.NestedClass.__init__, |
| 152 | test_zipped_doctest.SampleClass.__init__, |
| 153 | test_zipped_doctest.SampleClass.a_classmethod, |
| 154 | test_zipped_doctest.SampleClass.a_property, |
| 155 | test_zipped_doctest.SampleClass.a_staticmethod, |
| 156 | test_zipped_doctest.SampleClass.double, |
nothing calls this directly
no test coverage detected