+++ /dev/null
-# This test module covers support in various parts of the standard library\r
-# for working with modules located inside zipfiles\r
-# The tests are centralised in this fashion to make it easy to drop them\r
-# if a platform doesn't support zipimport\r
-import test.test_support\r
-import os\r
-import os.path\r
-import sys\r
-import textwrap\r
-import zipfile\r
-import zipimport\r
-import doctest\r
-import inspect\r
-import linecache\r
-import pdb\r
-import warnings\r
-from test.script_helper import (spawn_python, kill_python, run_python,\r
- temp_dir, make_script, make_zip_script)\r
-\r
-verbose = test.test_support.verbose\r
-\r
-# Library modules covered by this test set\r
-# pdb (Issue 4201)\r
-# inspect (Issue 4223)\r
-# doctest (Issue 4197)\r
-\r
-# Other test modules with zipimport related tests\r
-# test_zipimport (of course!)\r
-# test_cmd_line_script (covers the zipimport support in runpy)\r
-\r
-# Retrieve some helpers from other test cases\r
-from test import test_doctest, sample_doctest\r
-from test.test_importhooks import ImportHooksBaseTestCase\r
-\r
-\r
-def _run_object_doctest(obj, module):\r
- # Direct doctest output (normally just errors) to real stdout; doctest\r
- # output shouldn't be compared by regrtest.\r
- save_stdout = sys.stdout\r
- sys.stdout = test.test_support.get_original_stdout()\r
- try:\r
- finder = doctest.DocTestFinder(verbose=verbose, recurse=False)\r
- runner = doctest.DocTestRunner(verbose=verbose)\r
- # Use the object's fully qualified name if it has one\r
- # Otherwise, use the module's name\r
- try:\r
- name = "%s.%s" % (obj.__module__, obj.__name__)\r
- except AttributeError:\r
- name = module.__name__\r
- for example in finder.find(obj, name, module):\r
- runner.run(example)\r
- f, t = runner.failures, runner.tries\r
- if f:\r
- raise test.test_support.TestFailed("%d of %d doctests failed" % (f, t))\r
- finally:\r
- sys.stdout = save_stdout\r
- if verbose:\r
- print 'doctest (%s) ... %d tests with zero failures' % (module.__name__, t)\r
- return f, t\r
-\r
-\r
-\r
-class ZipSupportTests(ImportHooksBaseTestCase):\r
- # We use the ImportHooksBaseTestCase to restore\r
- # the state of the import related information\r
- # in the sys module after each test\r
- # We also clear the linecache and zipimport cache\r
- # just to avoid any bogus errors due to name reuse in the tests\r
- def setUp(self):\r
- linecache.clearcache()\r
- zipimport._zip_directory_cache.clear()\r
- ImportHooksBaseTestCase.setUp(self)\r
-\r
-\r
- def test_inspect_getsource_issue4223(self):\r
- test_src = "def foo(): pass\n"\r
- with temp_dir() as d:\r
- init_name = make_script(d, '__init__', test_src)\r
- name_in_zip = os.path.join('zip_pkg',\r
- os.path.basename(init_name))\r
- zip_name, run_name = make_zip_script(d, 'test_zip',\r
- init_name, name_in_zip)\r
- os.remove(init_name)\r
- sys.path.insert(0, zip_name)\r
- import zip_pkg\r
- self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)\r
-\r
- def test_doctest_issue4197(self):\r
- # To avoid having to keep two copies of the doctest module's\r
- # unit tests in sync, this test works by taking the source of\r
- # test_doctest itself, rewriting it a bit to cope with a new\r
- # location, and then throwing it in a zip file to make sure\r
- # everything still works correctly\r
- test_src = inspect.getsource(test_doctest)\r
- test_src = test_src.replace(\r
- "from test import test_doctest",\r
- "import test_zipped_doctest as test_doctest")\r
- test_src = test_src.replace("test.test_doctest",\r
- "test_zipped_doctest")\r
- test_src = test_src.replace("test.sample_doctest",\r
- "sample_zipped_doctest")\r
- sample_src = inspect.getsource(sample_doctest)\r
- sample_src = sample_src.replace("test.test_doctest",\r
- "test_zipped_doctest")\r
- with temp_dir() as d:\r
- script_name = make_script(d, 'test_zipped_doctest',\r
- test_src)\r
- zip_name, run_name = make_zip_script(d, 'test_zip',\r
- script_name)\r
- z = zipfile.ZipFile(zip_name, 'a')\r
- z.writestr("sample_zipped_doctest.py", sample_src)\r
- z.close()\r
- if verbose:\r
- zip_file = zipfile.ZipFile(zip_name, 'r')\r
- print 'Contents of %r:' % zip_name\r
- zip_file.printdir()\r
- zip_file.close()\r
- os.remove(script_name)\r
- sys.path.insert(0, zip_name)\r
- import test_zipped_doctest\r
- # Some of the doc tests depend on the colocated text files\r
- # which aren't available to the zipped version (the doctest\r
- # module currently requires real filenames for non-embedded\r
- # tests). So we're forced to be selective about which tests\r
- # to run.\r
- # doctest could really use some APIs which take a text\r
- # string or a file object instead of a filename...\r
- known_good_tests = [\r
- test_zipped_doctest.SampleClass,\r
- test_zipped_doctest.SampleClass.NestedClass,\r
- test_zipped_doctest.SampleClass.NestedClass.__init__,\r
- test_zipped_doctest.SampleClass.__init__,\r
- test_zipped_doctest.SampleClass.a_classmethod,\r
- test_zipped_doctest.SampleClass.a_property,\r
- test_zipped_doctest.SampleClass.a_staticmethod,\r
- test_zipped_doctest.SampleClass.double,\r
- test_zipped_doctest.SampleClass.get,\r
- test_zipped_doctest.SampleNewStyleClass,\r
- test_zipped_doctest.SampleNewStyleClass.__init__,\r
- test_zipped_doctest.SampleNewStyleClass.double,\r
- test_zipped_doctest.SampleNewStyleClass.get,\r
- test_zipped_doctest.old_test1,\r
- test_zipped_doctest.old_test2,\r
- test_zipped_doctest.old_test3,\r
- test_zipped_doctest.old_test4,\r
- test_zipped_doctest.sample_func,\r
- test_zipped_doctest.test_DocTest,\r
- test_zipped_doctest.test_DocTestParser,\r
- test_zipped_doctest.test_DocTestRunner.basics,\r
- test_zipped_doctest.test_DocTestRunner.exceptions,\r
- test_zipped_doctest.test_DocTestRunner.option_directives,\r
- test_zipped_doctest.test_DocTestRunner.optionflags,\r
- test_zipped_doctest.test_DocTestRunner.verbose_flag,\r
- test_zipped_doctest.test_Example,\r
- test_zipped_doctest.test_debug,\r
- test_zipped_doctest.test_pdb_set_trace,\r
- test_zipped_doctest.test_pdb_set_trace_nested,\r
- test_zipped_doctest.test_testsource,\r
- test_zipped_doctest.test_trailing_space_in_test,\r
- test_zipped_doctest.test_DocTestSuite,\r
- test_zipped_doctest.test_DocTestFinder,\r
- ]\r
- # These remaining tests are the ones which need access\r
- # to the data files, so we don't run them\r
- fail_due_to_missing_data_files = [\r
- test_zipped_doctest.test_DocFileSuite,\r
- test_zipped_doctest.test_testfile,\r
- test_zipped_doctest.test_unittest_reportflags,\r
- ]\r
- # Needed for test_DocTestParser and test_debug\r
- deprecations = [\r
- # Ignore all warnings about the use of class Tester in this module.\r
- ("class Tester is deprecated", DeprecationWarning)]\r
- if sys.py3kwarning:\r
- deprecations += [\r
- ("backquote not supported", SyntaxWarning),\r
- ("execfile.. not supported", DeprecationWarning)]\r
- with test.test_support.check_warnings(*deprecations):\r
- for obj in known_good_tests:\r
- _run_object_doctest(obj, test_zipped_doctest)\r
-\r
- def test_doctest_main_issue4197(self):\r
- test_src = textwrap.dedent("""\\r
- class Test:\r
- ">>> 'line 2'"\r
- pass\r
-\r
- import doctest\r
- doctest.testmod()\r
- """)\r
- pattern = 'File "%s", line 2, in %s'\r
- with temp_dir() as d:\r
- script_name = make_script(d, 'script', test_src)\r
- exit_code, data = run_python(script_name)\r
- expected = pattern % (script_name, "__main__.Test")\r
- if verbose:\r
- print "Expected line", expected\r
- print "Got stdout:"\r
- print data\r
- self.assertIn(expected, data)\r
- zip_name, run_name = make_zip_script(d, "test_zip",\r
- script_name, '__main__.py')\r
- exit_code, data = run_python(zip_name)\r
- expected = pattern % (run_name, "__main__.Test")\r
- if verbose:\r
- print "Expected line", expected\r
- print "Got stdout:"\r
- print data\r
- self.assertIn(expected, data)\r
-\r
- def test_pdb_issue4201(self):\r
- test_src = textwrap.dedent("""\\r
- def f():\r
- pass\r
-\r
- import pdb\r
- pdb.runcall(f)\r
- """)\r
- with temp_dir() as d:\r
- script_name = make_script(d, 'script', test_src)\r
- p = spawn_python(script_name)\r
- p.stdin.write('l\n')\r
- data = kill_python(p)\r
- self.assertIn(script_name, data)\r
- zip_name, run_name = make_zip_script(d, "test_zip",\r
- script_name, '__main__.py')\r
- p = spawn_python(zip_name)\r
- p.stdin.write('l\n')\r
- data = kill_python(p)\r
- self.assertIn(run_name, data)\r
-\r
-\r
-def test_main():\r
- test.test_support.run_unittest(ZipSupportTests)\r
- test.test_support.reap_children()\r
-\r
-if __name__ == '__main__':\r
- test_main()\r