test_zipimport_support.py 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# This test module covers support in various parts of the standard library
# for working with modules located inside zipfiles
# The tests are centralised in this fashion to make it easy to drop them
# if a platform doesn't support zipimport
import test.support
import os
import os.path
import sys
import textwrap
import zipfile
import zipimport
import doctest
import inspect
import linecache
import pdb
16
import unittest
17
from test.script_helper import (spawn_python, kill_python, assert_python_ok,
18
                                temp_dir, make_script, make_zip_script)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

verbose = test.support.verbose

# Library modules covered by this test set
#  pdb (Issue 4201)
#  inspect (Issue 4223)
#  doctest (Issue 4197)

# Other test modules with zipimport related tests
#  test_zipimport (of course!)
#  test_cmd_line_script (covers the zipimport support in runpy)

# Retrieve some helpers from other test cases
from test import test_doctest, sample_doctest


def _run_object_doctest(obj, module):
    # Direct doctest output (normally just errors) to real stdout; doctest
    # output shouldn't be compared by regrtest.
    save_stdout = sys.stdout
    sys.stdout = test.support.get_original_stdout()
    try:
        finder = doctest.DocTestFinder(verbose=verbose, recurse=False)
        runner = doctest.DocTestRunner(verbose=verbose)
        # Use the object's fully qualified name if it has one
        # Otherwise, use the module's name
        try:
            name = "%s.%s" % (obj.__module__, obj.__name__)
        except AttributeError:
            name = module.__name__
        for example in finder.find(obj, name, module):
            runner.run(example)
        f, t = runner.failures, runner.tries
        if f:
            raise test.support.TestFailed("%d of %d doctests failed" % (f, t))
    finally:
        sys.stdout = save_stdout
    if verbose:
        print ('doctest (%s) ... %d tests with zero failures' % (module.__name__, t))
    return f, t



62 63
class ZipSupportTests(unittest.TestCase):
    # This used to use the ImportHooksBaseTestCase to restore
64
    # the state of the import related information
65 66 67 68
    # in the sys module after each test. However, that restores
    # *too much* information and breaks for the invocation of
    # of test_doctest. So we do our own thing and leave
    # sys.modules alone.
69 70 71 72 73
    # We also clear the linecache and zipimport cache
    # just to avoid any bogus errors due to name reuse in the tests
    def setUp(self):
        linecache.clearcache()
        zipimport._zip_directory_cache.clear()
74 75 76 77
        self.path = sys.path[:]
        self.meta_path = sys.meta_path[:]
        self.path_hooks = sys.path_hooks[:]
        sys.path_importer_cache.clear()
78

79 80 81 82 83
    def tearDown(self):
        sys.path[:] = self.path
        sys.meta_path[:] = self.meta_path
        sys.path_hooks[:] = self.path_hooks
        sys.path_importer_cache.clear()
84 85 86 87

    def test_inspect_getsource_issue4223(self):
        test_src = "def foo(): pass\n"
        with temp_dir() as d:
Nick Coghlan's avatar
Nick Coghlan committed
88
            init_name = make_script(d, '__init__', test_src)
89 90
            name_in_zip = os.path.join('zip_pkg',
                                       os.path.basename(init_name))
Nick Coghlan's avatar
Nick Coghlan committed
91
            zip_name, run_name = make_zip_script(d, 'test_zip',
92 93 94 95
                                                init_name, name_in_zip)
            os.remove(init_name)
            sys.path.insert(0, zip_name)
            import zip_pkg
96 97 98 99
            try:
                self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
            finally:
                del sys.modules["zip_pkg"]
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

    def test_doctest_issue4197(self):
        # To avoid having to keep two copies of the doctest module's
        # unit tests in sync, this test works by taking the source of
        # test_doctest itself, rewriting it a bit to cope with a new
        # location, and then throwing it in a zip file to make sure
        # everything still works correctly
        test_src = inspect.getsource(test_doctest)
        test_src = test_src.replace(
                         "from test import test_doctest",
                         "import test_zipped_doctest as test_doctest")
        test_src = test_src.replace("test.test_doctest",
                                    "test_zipped_doctest")
        test_src = test_src.replace("test.sample_doctest",
                                    "sample_zipped_doctest")
        sample_src = inspect.getsource(sample_doctest)
        sample_src = sample_src.replace("test.test_doctest",
                                        "test_zipped_doctest")
        with temp_dir() as d:
Nick Coghlan's avatar
Nick Coghlan committed
119
            script_name = make_script(d, 'test_zipped_doctest',
120
                                            test_src)
Nick Coghlan's avatar
Nick Coghlan committed
121
            zip_name, run_name = make_zip_script(d, 'test_zip',
122 123 124 125 126 127 128 129 130 131 132 133
                                                script_name)
            z = zipfile.ZipFile(zip_name, 'a')
            z.writestr("sample_zipped_doctest.py", sample_src)
            z.close()
            if verbose:
                zip_file = zipfile.ZipFile(zip_name, 'r')
                print ('Contents of %r:' % zip_name)
                zip_file.printdir()
                zip_file.close()
            os.remove(script_name)
            sys.path.insert(0, zip_name)
            import test_zipped_doctest
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            try:
                # Some of the doc tests depend on the colocated text files
                # which aren't available to the zipped version (the doctest
                # module currently requires real filenames for non-embedded
                # tests). So we're forced to be selective about which tests
                # to run.
                # doctest could really use some APIs which take a text
                # string or a file object instead of a filename...
                known_good_tests = [
                    test_zipped_doctest.SampleClass,
                    test_zipped_doctest.SampleClass.NestedClass,
                    test_zipped_doctest.SampleClass.NestedClass.__init__,
                    test_zipped_doctest.SampleClass.__init__,
                    test_zipped_doctest.SampleClass.a_classmethod,
                    test_zipped_doctest.SampleClass.a_property,
                    test_zipped_doctest.SampleClass.a_staticmethod,
                    test_zipped_doctest.SampleClass.double,
                    test_zipped_doctest.SampleClass.get,
                    test_zipped_doctest.SampleNewStyleClass,
                    test_zipped_doctest.SampleNewStyleClass.__init__,
                    test_zipped_doctest.SampleNewStyleClass.double,
                    test_zipped_doctest.SampleNewStyleClass.get,
                    test_zipped_doctest.sample_func,
                    test_zipped_doctest.test_DocTest,
                    test_zipped_doctest.test_DocTestParser,
                    test_zipped_doctest.test_DocTestRunner.basics,
                    test_zipped_doctest.test_DocTestRunner.exceptions,
                    test_zipped_doctest.test_DocTestRunner.option_directives,
                    test_zipped_doctest.test_DocTestRunner.optionflags,
                    test_zipped_doctest.test_DocTestRunner.verbose_flag,
                    test_zipped_doctest.test_Example,
                    test_zipped_doctest.test_debug,
                    test_zipped_doctest.test_pdb_set_trace,
                    test_zipped_doctest.test_pdb_set_trace_nested,
                    test_zipped_doctest.test_testsource,
                    test_zipped_doctest.test_trailing_space_in_test,
                    test_zipped_doctest.test_DocTestSuite,
                    test_zipped_doctest.test_DocTestFinder,
                ]
                # These remaining tests are the ones which need access
                # to the data files, so we don't run them
                fail_due_to_missing_data_files = [
                    test_zipped_doctest.test_DocFileSuite,
                    test_zipped_doctest.test_testfile,
                    test_zipped_doctest.test_unittest_reportflags,
                ]
                for obj in known_good_tests:
                    _run_object_doctest(obj, test_zipped_doctest)
            finally:
                del sys.modules["test_zipped_doctest"]
184

185 186 187 188 189 190 191 192 193 194 195
    def test_doctest_main_issue4197(self):
        test_src = textwrap.dedent("""\
                    class Test:
                        ">>> 'line 2'"
                        pass

                    import doctest
                    doctest.testmod()
                    """)
        pattern = 'File "%s", line 2, in %s'
        with temp_dir() as d:
Nick Coghlan's avatar
Nick Coghlan committed
196
            script_name = make_script(d, 'script', test_src)
197
            rc, out, err = assert_python_ok(script_name)
198 199 200 201
            expected = pattern % (script_name, "__main__.Test")
            if verbose:
                print ("Expected line", expected)
                print ("Got stdout:")
202
                print (ascii(out))
203
            self.assertIn(expected.encode('utf-8'), out)
Nick Coghlan's avatar
Nick Coghlan committed
204
            zip_name, run_name = make_zip_script(d, "test_zip",
205
                                                script_name, '__main__.py')
206
            rc, out, err = assert_python_ok(zip_name)
207 208 209 210
            expected = pattern % (run_name, "__main__.Test")
            if verbose:
                print ("Expected line", expected)
                print ("Got stdout:")
211
                print (ascii(out))
212
            self.assertIn(expected.encode('utf-8'), out)
213

214 215 216 217 218 219
    def test_pdb_issue4201(self):
        test_src = textwrap.dedent("""\
                    def f():
                        pass

                    import pdb
220
                    pdb.Pdb(nosigint=True).runcall(f)
221 222
                    """)
        with temp_dir() as d:
Nick Coghlan's avatar
Nick Coghlan committed
223 224
            script_name = make_script(d, 'script', test_src)
            p = spawn_python(script_name)
225
            p.stdin.write(b'l\n')
Nick Coghlan's avatar
Nick Coghlan committed
226
            data = kill_python(p)
227
            self.assertIn(script_name.encode('utf-8'), data)
Nick Coghlan's avatar
Nick Coghlan committed
228
            zip_name, run_name = make_zip_script(d, "test_zip",
229
                                                script_name, '__main__.py')
Nick Coghlan's avatar
Nick Coghlan committed
230
            p = spawn_python(zip_name)
231
            p.stdin.write(b'l\n')
Nick Coghlan's avatar
Nick Coghlan committed
232
            data = kill_python(p)
233
            self.assertIn(run_name.encode('utf-8'), data)
234 235 236 237 238 239 240 241


def test_main():
    test.support.run_unittest(ZipSupportTests)
    test.support.reap_children()

if __name__ == '__main__':
    test_main()