mirror of
https://github.com/python/cpython.git
synced 2025-08-16 06:40:56 +00:00
[3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123)
(cherry picked from commit f7cc7d296c
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
d2ec87f740
commit
a9bb0092d0
19 changed files with 68 additions and 64 deletions
|
@ -50,6 +50,7 @@ from test.support.os_helper import (
|
||||||
TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE)
|
TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE)
|
||||||
from test.support import script_helper
|
from test.support import script_helper
|
||||||
from test.support import threading_helper
|
from test.support import threading_helper
|
||||||
|
from test.support.testcase import ExtraAssertions
|
||||||
from test.test_importlib.util import uncache
|
from test.test_importlib.util import uncache
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
try:
|
try:
|
||||||
|
@ -364,7 +365,7 @@ class ModuleSnapshot(types.SimpleNamespace):
|
||||||
|
|
||||||
|
|
||||||
@force_not_colorized_test_class
|
@force_not_colorized_test_class
|
||||||
class ImportTests(unittest.TestCase):
|
class ImportTests(unittest.TestCase, ExtraAssertions):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
remove_files(TESTFN)
|
remove_files(TESTFN)
|
||||||
|
@ -570,7 +571,7 @@ class ImportTests(unittest.TestCase):
|
||||||
import test as x
|
import test as x
|
||||||
import test.support
|
import test.support
|
||||||
self.assertIs(x, test, x.__name__)
|
self.assertIs(x, test, x.__name__)
|
||||||
self.assertTrue(hasattr(test.support, "__file__"))
|
self.assertHasAttr(test.support, "__file__")
|
||||||
|
|
||||||
# import x.y.z as w binds z as w
|
# import x.y.z as w binds z as w
|
||||||
import test.support as y
|
import test.support as y
|
||||||
|
@ -641,7 +642,7 @@ class ImportTests(unittest.TestCase):
|
||||||
sys.path.insert(0, os.curdir)
|
sys.path.insert(0, os.curdir)
|
||||||
try:
|
try:
|
||||||
mod = __import__(TESTFN)
|
mod = __import__(TESTFN)
|
||||||
self.assertTrue(mod.__file__.endswith('.py'))
|
self.assertEndsWith(mod.__file__, '.py')
|
||||||
os.remove(source)
|
os.remove(source)
|
||||||
del sys.modules[TESTFN]
|
del sys.modules[TESTFN]
|
||||||
make_legacy_pyc(source)
|
make_legacy_pyc(source)
|
||||||
|
@ -1447,7 +1448,7 @@ func_filename = func.__code__.co_filename
|
||||||
self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
|
self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
|
||||||
|
|
||||||
|
|
||||||
class PathsTests(unittest.TestCase):
|
class PathsTests(unittest.TestCase, ExtraAssertions):
|
||||||
SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8',
|
SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8',
|
||||||
'test\u00b0\u00b3\u00b2')
|
'test\u00b0\u00b3\u00b2')
|
||||||
path = TESTFN
|
path = TESTFN
|
||||||
|
@ -1497,11 +1498,11 @@ class PathsTests(unittest.TestCase):
|
||||||
self.fail("could not import 'test_unc_path' from %r: %r"
|
self.fail("could not import 'test_unc_path' from %r: %r"
|
||||||
% (unc, e))
|
% (unc, e))
|
||||||
self.assertEqual(mod.testdata, 'test_unc_path')
|
self.assertEqual(mod.testdata, 'test_unc_path')
|
||||||
self.assertTrue(mod.__file__.startswith(unc), mod.__file__)
|
self.assertStartsWith(mod.__file__, unc)
|
||||||
unload("test_unc_path")
|
unload("test_unc_path")
|
||||||
|
|
||||||
|
|
||||||
class RelativeImportTests(unittest.TestCase):
|
class RelativeImportTests(unittest.TestCase, ExtraAssertions):
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
unload("test.relimport")
|
unload("test.relimport")
|
||||||
|
@ -1510,7 +1511,7 @@ class RelativeImportTests(unittest.TestCase):
|
||||||
def test_relimport_star(self):
|
def test_relimport_star(self):
|
||||||
# This will import * from .test_import.
|
# This will import * from .test_import.
|
||||||
from .. import relimport
|
from .. import relimport
|
||||||
self.assertTrue(hasattr(relimport, "RelativeImportTests"))
|
self.assertHasAttr(relimport, "RelativeImportTests")
|
||||||
|
|
||||||
def test_issue3221(self):
|
def test_issue3221(self):
|
||||||
# Note for mergers: the 'absolute' tests from the 2.x branch
|
# Note for mergers: the 'absolute' tests from the 2.x branch
|
||||||
|
@ -1827,7 +1828,7 @@ class TestSymbolicallyLinkedPackage(unittest.TestCase):
|
||||||
|
|
||||||
|
|
||||||
@cpython_only
|
@cpython_only
|
||||||
class ImportlibBootstrapTests(unittest.TestCase):
|
class ImportlibBootstrapTests(unittest.TestCase, ExtraAssertions):
|
||||||
# These tests check that importlib is bootstrapped.
|
# These tests check that importlib is bootstrapped.
|
||||||
|
|
||||||
def test_frozen_importlib(self):
|
def test_frozen_importlib(self):
|
||||||
|
@ -1840,7 +1841,7 @@ class ImportlibBootstrapTests(unittest.TestCase):
|
||||||
self.assertIs(mod, _bootstrap)
|
self.assertIs(mod, _bootstrap)
|
||||||
self.assertEqual(mod.__name__, 'importlib._bootstrap')
|
self.assertEqual(mod.__name__, 'importlib._bootstrap')
|
||||||
self.assertEqual(mod.__package__, 'importlib')
|
self.assertEqual(mod.__package__, 'importlib')
|
||||||
self.assertTrue(mod.__file__.endswith('_bootstrap.py'), mod.__file__)
|
self.assertEndsWith(mod.__file__, '_bootstrap.py')
|
||||||
|
|
||||||
def test_frozen_importlib_external_is_bootstrap_external(self):
|
def test_frozen_importlib_external_is_bootstrap_external(self):
|
||||||
from importlib import _bootstrap_external
|
from importlib import _bootstrap_external
|
||||||
|
@ -1848,7 +1849,7 @@ class ImportlibBootstrapTests(unittest.TestCase):
|
||||||
self.assertIs(mod, _bootstrap_external)
|
self.assertIs(mod, _bootstrap_external)
|
||||||
self.assertEqual(mod.__name__, 'importlib._bootstrap_external')
|
self.assertEqual(mod.__name__, 'importlib._bootstrap_external')
|
||||||
self.assertEqual(mod.__package__, 'importlib')
|
self.assertEqual(mod.__package__, 'importlib')
|
||||||
self.assertTrue(mod.__file__.endswith('_bootstrap_external.py'), mod.__file__)
|
self.assertEndsWith(mod.__file__, '_bootstrap_external.py')
|
||||||
|
|
||||||
def test_there_can_be_only_one(self):
|
def test_there_can_be_only_one(self):
|
||||||
# Issue #15386 revealed a tricky loophole in the bootstrapping
|
# Issue #15386 revealed a tricky loophole in the bootstrapping
|
||||||
|
@ -2698,7 +2699,7 @@ class TestSinglePhaseSnapshot(ModuleSnapshot):
|
||||||
|
|
||||||
|
|
||||||
@requires_singlephase_init
|
@requires_singlephase_init
|
||||||
class SinglephaseInitTests(unittest.TestCase):
|
class SinglephaseInitTests(unittest.TestCase, ExtraAssertions):
|
||||||
|
|
||||||
NAME = '_testsinglephase'
|
NAME = '_testsinglephase'
|
||||||
|
|
||||||
|
@ -2869,7 +2870,7 @@ class SinglephaseInitTests(unittest.TestCase):
|
||||||
self.assertEqual(mod.__file__, self.FILE)
|
self.assertEqual(mod.__file__, self.FILE)
|
||||||
self.assertEqual(mod.__spec__.origin, self.ORIGIN)
|
self.assertEqual(mod.__spec__.origin, self.ORIGIN)
|
||||||
if not isolated:
|
if not isolated:
|
||||||
self.assertTrue(issubclass(mod.error, Exception))
|
self.assertIsSubclass(mod.error, Exception)
|
||||||
self.assertEqual(mod.int_const, 1969)
|
self.assertEqual(mod.int_const, 1969)
|
||||||
self.assertEqual(mod.str_const, 'something different')
|
self.assertEqual(mod.str_const, 'something different')
|
||||||
self.assertIsInstance(mod._module_initialized, float)
|
self.assertIsInstance(mod._module_initialized, float)
|
||||||
|
|
|
@ -21,7 +21,7 @@ class PathHookTests:
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
# Path hook should handle a directory where a known extension module
|
# Path hook should handle a directory where a known extension module
|
||||||
# exists.
|
# exists.
|
||||||
self.assertTrue(hasattr(self.hook(util.EXTENSIONS.path), 'find_spec'))
|
self.assertHasAttr(self.hook(util.EXTENSIONS.path), 'find_spec')
|
||||||
|
|
||||||
|
|
||||||
(Frozen_PathHooksTests,
|
(Frozen_PathHooksTests,
|
||||||
|
|
|
@ -61,7 +61,7 @@ class ExecModuleTests(abc.LoaderTests):
|
||||||
module.main()
|
module.main()
|
||||||
|
|
||||||
self.assertTrue(module.initialized)
|
self.assertTrue(module.initialized)
|
||||||
self.assertTrue(hasattr(module, '__spec__'))
|
self.assertHasAttr(module, '__spec__')
|
||||||
self.assertEqual(module.__spec__.origin, 'frozen')
|
self.assertEqual(module.__spec__.origin, 'frozen')
|
||||||
return module, stdout.getvalue()
|
return module, stdout.getvalue()
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ class ExecModuleTests(abc.LoaderTests):
|
||||||
for attr, value in check.items():
|
for attr, value in check.items():
|
||||||
self.assertEqual(getattr(module, attr), value)
|
self.assertEqual(getattr(module, attr), value)
|
||||||
self.assertEqual(output, 'Hello world!\n')
|
self.assertEqual(output, 'Hello world!\n')
|
||||||
self.assertTrue(hasattr(module, '__spec__'))
|
self.assertHasAttr(module, '__spec__')
|
||||||
self.assertEqual(module.__spec__.loader_state.origname, name)
|
self.assertEqual(module.__spec__.loader_state.origname, name)
|
||||||
|
|
||||||
def test_package(self):
|
def test_package(self):
|
||||||
|
@ -136,7 +136,7 @@ class InspectLoaderTests:
|
||||||
exec(code, mod.__dict__)
|
exec(code, mod.__dict__)
|
||||||
with captured_stdout() as stdout:
|
with captured_stdout() as stdout:
|
||||||
mod.main()
|
mod.main()
|
||||||
self.assertTrue(hasattr(mod, 'initialized'))
|
self.assertHasAttr(mod, 'initialized')
|
||||||
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
|
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
|
||||||
|
|
||||||
def test_get_source(self):
|
def test_get_source(self):
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
"""Test that sys.modules is used properly by import."""
|
"""Test that sys.modules is used properly by import."""
|
||||||
from test.test_importlib import util
|
from test.test_importlib import util
|
||||||
|
from test.support.testcase import ExtraAssertions
|
||||||
import sys
|
import sys
|
||||||
from types import MethodType
|
from types import MethodType
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -45,7 +46,7 @@ class UseCache:
|
||||||
) = util.test_both(UseCache, __import__=util.__import__)
|
) = util.test_both(UseCache, __import__=util.__import__)
|
||||||
|
|
||||||
|
|
||||||
class ImportlibUseCache(UseCache, unittest.TestCase):
|
class ImportlibUseCache(UseCache, unittest.TestCase, ExtraAssertions):
|
||||||
|
|
||||||
# Pertinent only to PEP 302; exec_module() doesn't return a module.
|
# Pertinent only to PEP 302; exec_module() doesn't return a module.
|
||||||
|
|
||||||
|
@ -78,7 +79,7 @@ class ImportlibUseCache(UseCache, unittest.TestCase):
|
||||||
with self.create_mock('pkg.__init__', 'pkg.module') as importer:
|
with self.create_mock('pkg.__init__', 'pkg.module') as importer:
|
||||||
with util.import_state(meta_path=[importer]):
|
with util.import_state(meta_path=[importer]):
|
||||||
module = self.__import__('pkg.module')
|
module = self.__import__('pkg.module')
|
||||||
self.assertTrue(hasattr(module, 'module'))
|
self.assertHasAttr(module, 'module')
|
||||||
self.assertEqual(id(module.module),
|
self.assertEqual(id(module.module),
|
||||||
id(sys.modules['pkg.module']))
|
id(sys.modules['pkg.module']))
|
||||||
|
|
||||||
|
@ -88,7 +89,7 @@ class ImportlibUseCache(UseCache, unittest.TestCase):
|
||||||
with self.create_mock('pkg.__init__', 'pkg.module') as importer:
|
with self.create_mock('pkg.__init__', 'pkg.module') as importer:
|
||||||
with util.import_state(meta_path=[importer]):
|
with util.import_state(meta_path=[importer]):
|
||||||
module = self.__import__('pkg', fromlist=['module'])
|
module = self.__import__('pkg', fromlist=['module'])
|
||||||
self.assertTrue(hasattr(module, 'module'))
|
self.assertHasAttr(module, 'module')
|
||||||
self.assertEqual(id(module.module),
|
self.assertEqual(id(module.module),
|
||||||
id(sys.modules['pkg.module']))
|
id(sys.modules['pkg.module']))
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ class HandlingFromlist:
|
||||||
with util.import_state(meta_path=[importer]):
|
with util.import_state(meta_path=[importer]):
|
||||||
module = self.__import__('module', fromlist=['non_existent'])
|
module = self.__import__('module', fromlist=['non_existent'])
|
||||||
self.assertEqual(module.__name__, 'module')
|
self.assertEqual(module.__name__, 'module')
|
||||||
self.assertFalse(hasattr(module, 'non_existent'))
|
self.assertNotHasAttr(module, 'non_existent')
|
||||||
|
|
||||||
def test_module_from_package(self):
|
def test_module_from_package(self):
|
||||||
# [module]
|
# [module]
|
||||||
|
@ -71,7 +71,7 @@ class HandlingFromlist:
|
||||||
with util.import_state(meta_path=[importer]):
|
with util.import_state(meta_path=[importer]):
|
||||||
module = self.__import__('pkg', fromlist=['module'])
|
module = self.__import__('pkg', fromlist=['module'])
|
||||||
self.assertEqual(module.__name__, 'pkg')
|
self.assertEqual(module.__name__, 'pkg')
|
||||||
self.assertTrue(hasattr(module, 'module'))
|
self.assertHasAttr(module, 'module')
|
||||||
self.assertEqual(module.module.__name__, 'pkg.module')
|
self.assertEqual(module.module.__name__, 'pkg.module')
|
||||||
|
|
||||||
def test_nonexistent_from_package(self):
|
def test_nonexistent_from_package(self):
|
||||||
|
@ -79,7 +79,7 @@ class HandlingFromlist:
|
||||||
with util.import_state(meta_path=[importer]):
|
with util.import_state(meta_path=[importer]):
|
||||||
module = self.__import__('pkg', fromlist=['non_existent'])
|
module = self.__import__('pkg', fromlist=['non_existent'])
|
||||||
self.assertEqual(module.__name__, 'pkg')
|
self.assertEqual(module.__name__, 'pkg')
|
||||||
self.assertFalse(hasattr(module, 'non_existent'))
|
self.assertNotHasAttr(module, 'non_existent')
|
||||||
|
|
||||||
def test_module_from_package_triggers_ModuleNotFoundError(self):
|
def test_module_from_package_triggers_ModuleNotFoundError(self):
|
||||||
# If a submodule causes an ModuleNotFoundError because it tries
|
# If a submodule causes an ModuleNotFoundError because it tries
|
||||||
|
@ -107,7 +107,7 @@ class HandlingFromlist:
|
||||||
mock['pkg'].__all__ = ['module']
|
mock['pkg'].__all__ = ['module']
|
||||||
module = self.__import__('pkg', fromlist=fromlist)
|
module = self.__import__('pkg', fromlist=fromlist)
|
||||||
self.assertEqual(module.__name__, 'pkg')
|
self.assertEqual(module.__name__, 'pkg')
|
||||||
self.assertTrue(hasattr(module, 'module'))
|
self.assertHasAttr(module, 'module')
|
||||||
self.assertEqual(module.module.__name__, 'pkg.module')
|
self.assertEqual(module.module.__name__, 'pkg.module')
|
||||||
|
|
||||||
def test_using_star(self):
|
def test_using_star(self):
|
||||||
|
@ -125,8 +125,8 @@ class HandlingFromlist:
|
||||||
mock['pkg'].__all__ = ['module1']
|
mock['pkg'].__all__ = ['module1']
|
||||||
module = self.__import__('pkg', fromlist=['module2', '*'])
|
module = self.__import__('pkg', fromlist=['module2', '*'])
|
||||||
self.assertEqual(module.__name__, 'pkg')
|
self.assertEqual(module.__name__, 'pkg')
|
||||||
self.assertTrue(hasattr(module, 'module1'))
|
self.assertHasAttr(module, 'module1')
|
||||||
self.assertTrue(hasattr(module, 'module2'))
|
self.assertHasAttr(module, 'module2')
|
||||||
self.assertEqual(module.module1.__name__, 'pkg.module1')
|
self.assertEqual(module.module1.__name__, 'pkg.module1')
|
||||||
self.assertEqual(module.module2.__name__, 'pkg.module2')
|
self.assertEqual(module.module2.__name__, 'pkg.module2')
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ class HandlingFromlist:
|
||||||
importer['pkg'].__all__ = ['non_existent']
|
importer['pkg'].__all__ = ['non_existent']
|
||||||
module = self.__import__('pkg', fromlist=['*'])
|
module = self.__import__('pkg', fromlist=['*'])
|
||||||
self.assertEqual(module.__name__, 'pkg')
|
self.assertEqual(module.__name__, 'pkg')
|
||||||
self.assertFalse(hasattr(module, 'non_existent'))
|
self.assertNotHasAttr(module, 'non_existent')
|
||||||
|
|
||||||
def test_star_in_all(self):
|
def test_star_in_all(self):
|
||||||
with util.mock_spec('pkg.__init__') as importer:
|
with util.mock_spec('pkg.__init__') as importer:
|
||||||
|
@ -144,7 +144,7 @@ class HandlingFromlist:
|
||||||
importer['pkg'].__all__ = ['*']
|
importer['pkg'].__all__ = ['*']
|
||||||
module = self.__import__('pkg', fromlist=['*'])
|
module = self.__import__('pkg', fromlist=['*'])
|
||||||
self.assertEqual(module.__name__, 'pkg')
|
self.assertEqual(module.__name__, 'pkg')
|
||||||
self.assertFalse(hasattr(module, '*'))
|
self.assertNotHasAttr(module, '*')
|
||||||
|
|
||||||
def test_invalid_type(self):
|
def test_invalid_type(self):
|
||||||
with util.mock_spec('pkg.__init__') as importer:
|
with util.mock_spec('pkg.__init__') as importer:
|
||||||
|
|
|
@ -43,7 +43,7 @@ class CallingOrder:
|
||||||
self.assertIsNone(importlib._bootstrap._find_spec('nothing',
|
self.assertIsNone(importlib._bootstrap._find_spec('nothing',
|
||||||
None))
|
None))
|
||||||
self.assertEqual(len(w), 1)
|
self.assertEqual(len(w), 1)
|
||||||
self.assertTrue(issubclass(w[-1].category, ImportWarning))
|
self.assertIsSubclass(w[-1].category, ImportWarning)
|
||||||
|
|
||||||
|
|
||||||
(Frozen_CallingOrder,
|
(Frozen_CallingOrder,
|
||||||
|
|
|
@ -80,7 +80,7 @@ class FinderTests:
|
||||||
self.assertIsNone(self.find('os'))
|
self.assertIsNone(self.find('os'))
|
||||||
self.assertIsNone(sys.path_importer_cache[path_entry])
|
self.assertIsNone(sys.path_importer_cache[path_entry])
|
||||||
self.assertEqual(len(w), 1)
|
self.assertEqual(len(w), 1)
|
||||||
self.assertTrue(issubclass(w[-1].category, ImportWarning))
|
self.assertIsSubclass(w[-1].category, ImportWarning)
|
||||||
|
|
||||||
def test_path_importer_cache_empty_string(self):
|
def test_path_importer_cache_empty_string(self):
|
||||||
# The empty string should create a finder using the cwd.
|
# The empty string should create a finder using the cwd.
|
||||||
|
|
|
@ -81,7 +81,7 @@ class RelativeImports:
|
||||||
self.__import__('pkg') # For __import__().
|
self.__import__('pkg') # For __import__().
|
||||||
module = self.__import__('', global_, fromlist=['mod2'], level=1)
|
module = self.__import__('', global_, fromlist=['mod2'], level=1)
|
||||||
self.assertEqual(module.__name__, 'pkg')
|
self.assertEqual(module.__name__, 'pkg')
|
||||||
self.assertTrue(hasattr(module, 'mod2'))
|
self.assertHasAttr(module, 'mod2')
|
||||||
self.assertEqual(module.mod2.attr, 'pkg.mod2')
|
self.assertEqual(module.mod2.attr, 'pkg.mod2')
|
||||||
self.relative_import_test(create, globals_, callback)
|
self.relative_import_test(create, globals_, callback)
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ class RelativeImports:
|
||||||
module = self.__import__('', global_, fromlist=['module'],
|
module = self.__import__('', global_, fromlist=['module'],
|
||||||
level=1)
|
level=1)
|
||||||
self.assertEqual(module.__name__, 'pkg')
|
self.assertEqual(module.__name__, 'pkg')
|
||||||
self.assertTrue(hasattr(module, 'module'))
|
self.assertHasAttr(module, 'module')
|
||||||
self.assertEqual(module.module.attr, 'pkg.module')
|
self.assertEqual(module.module.attr, 'pkg.module')
|
||||||
self.relative_import_test(create, globals_, callback)
|
self.relative_import_test(create, globals_, callback)
|
||||||
|
|
||||||
|
@ -131,7 +131,7 @@ class RelativeImports:
|
||||||
module = self.__import__('', global_, fromlist=['subpkg2'],
|
module = self.__import__('', global_, fromlist=['subpkg2'],
|
||||||
level=2)
|
level=2)
|
||||||
self.assertEqual(module.__name__, 'pkg')
|
self.assertEqual(module.__name__, 'pkg')
|
||||||
self.assertTrue(hasattr(module, 'subpkg2'))
|
self.assertHasAttr(module, 'subpkg2')
|
||||||
self.assertEqual(module.subpkg2.attr, 'pkg.subpkg2.__init__')
|
self.assertEqual(module.subpkg2.attr, 'pkg.subpkg2.__init__')
|
||||||
self.relative_import_test(create, globals_, callback)
|
self.relative_import_test(create, globals_, callback)
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import unittest
|
||||||
|
|
||||||
from importlib import resources
|
from importlib import resources
|
||||||
from . import util
|
from . import util
|
||||||
|
from test.support.testcase import ExtraAssertions
|
||||||
|
|
||||||
|
|
||||||
class CommonTests(util.CommonTests, unittest.TestCase):
|
class CommonTests(util.CommonTests, unittest.TestCase):
|
||||||
|
@ -12,7 +13,7 @@ class CommonTests(util.CommonTests, unittest.TestCase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class PathTests:
|
class PathTests(ExtraAssertions):
|
||||||
def test_reading(self):
|
def test_reading(self):
|
||||||
"""
|
"""
|
||||||
Path should be readable and a pathlib.Path instance.
|
Path should be readable and a pathlib.Path instance.
|
||||||
|
@ -20,7 +21,7 @@ class PathTests:
|
||||||
target = resources.files(self.data) / 'utf-8.file'
|
target = resources.files(self.data) / 'utf-8.file'
|
||||||
with resources.as_file(target) as path:
|
with resources.as_file(target) as path:
|
||||||
self.assertIsInstance(path, pathlib.Path)
|
self.assertIsInstance(path, pathlib.Path)
|
||||||
self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
|
self.assertEndsWith(path.name, "utf-8.file")
|
||||||
self.assertEqual('Hello, UTF-8 world!\n', path.read_text(encoding='utf-8'))
|
self.assertEqual('Hello, UTF-8 world!\n', path.read_text(encoding='utf-8'))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,7 @@ class FinderTests(abc.FinderTests):
|
||||||
if error.errno != errno.ENOENT:
|
if error.errno != errno.ENOENT:
|
||||||
raise
|
raise
|
||||||
loader = self.import_(mapping['.root'], test)
|
loader = self.import_(mapping['.root'], test)
|
||||||
self.assertTrue(hasattr(loader, 'load_module'))
|
self.assertHasAttr(loader, 'load_module')
|
||||||
return loader
|
return loader
|
||||||
|
|
||||||
def test_module(self):
|
def test_module(self):
|
||||||
|
@ -100,7 +100,7 @@ class FinderTests(abc.FinderTests):
|
||||||
with util.create_modules('pkg.__init__', 'pkg.sub') as mapping:
|
with util.create_modules('pkg.__init__', 'pkg.sub') as mapping:
|
||||||
pkg_dir = os.path.dirname(mapping['pkg.__init__'])
|
pkg_dir = os.path.dirname(mapping['pkg.__init__'])
|
||||||
loader = self.import_(pkg_dir, 'pkg.sub')
|
loader = self.import_(pkg_dir, 'pkg.sub')
|
||||||
self.assertTrue(hasattr(loader, 'load_module'))
|
self.assertHasAttr(loader, 'load_module')
|
||||||
|
|
||||||
# [sub package]
|
# [sub package]
|
||||||
def test_package_in_package(self):
|
def test_package_in_package(self):
|
||||||
|
@ -108,7 +108,7 @@ class FinderTests(abc.FinderTests):
|
||||||
with context as mapping:
|
with context as mapping:
|
||||||
pkg_dir = os.path.dirname(mapping['pkg.__init__'])
|
pkg_dir = os.path.dirname(mapping['pkg.__init__'])
|
||||||
loader = self.import_(pkg_dir, 'pkg.sub')
|
loader = self.import_(pkg_dir, 'pkg.sub')
|
||||||
self.assertTrue(hasattr(loader, 'load_module'))
|
self.assertHasAttr(loader, 'load_module')
|
||||||
|
|
||||||
# [package over modules]
|
# [package over modules]
|
||||||
def test_package_over_module(self):
|
def test_package_over_module(self):
|
||||||
|
@ -129,7 +129,7 @@ class FinderTests(abc.FinderTests):
|
||||||
file.write("# test file for importlib")
|
file.write("# test file for importlib")
|
||||||
try:
|
try:
|
||||||
loader = self._find(finder, 'mod', loader_only=True)
|
loader = self._find(finder, 'mod', loader_only=True)
|
||||||
self.assertTrue(hasattr(loader, 'load_module'))
|
self.assertHasAttr(loader, 'load_module')
|
||||||
finally:
|
finally:
|
||||||
os.unlink('mod.py')
|
os.unlink('mod.py')
|
||||||
|
|
||||||
|
|
|
@ -15,12 +15,12 @@ class PathHookTest:
|
||||||
|
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
with util.create_modules('dummy') as mapping:
|
with util.create_modules('dummy') as mapping:
|
||||||
self.assertTrue(hasattr(self.path_hook()(mapping['.root']),
|
self.assertHasAttr(self.path_hook()(mapping['.root']),
|
||||||
'find_spec'))
|
'find_spec')
|
||||||
|
|
||||||
def test_empty_string(self):
|
def test_empty_string(self):
|
||||||
# The empty string represents the cwd.
|
# The empty string represents the cwd.
|
||||||
self.assertTrue(hasattr(self.path_hook()(''), 'find_spec'))
|
self.assertHasAttr(self.path_hook()(''), 'find_spec')
|
||||||
|
|
||||||
|
|
||||||
(Frozen_PathHookTest,
|
(Frozen_PathHookTest,
|
||||||
|
|
|
@ -43,14 +43,12 @@ class InheritanceTests:
|
||||||
def test_subclasses(self):
|
def test_subclasses(self):
|
||||||
# Test that the expected subclasses inherit.
|
# Test that the expected subclasses inherit.
|
||||||
for subclass in self.subclasses:
|
for subclass in self.subclasses:
|
||||||
self.assertTrue(issubclass(subclass, self.__test),
|
self.assertIsSubclass(subclass, self.__test)
|
||||||
"{0} is not a subclass of {1}".format(subclass, self.__test))
|
|
||||||
|
|
||||||
def test_superclasses(self):
|
def test_superclasses(self):
|
||||||
# Test that the class inherits from the expected superclasses.
|
# Test that the class inherits from the expected superclasses.
|
||||||
for superclass in self.superclasses:
|
for superclass in self.superclasses:
|
||||||
self.assertTrue(issubclass(self.__test, superclass),
|
self.assertIsSubclass(self.__test, superclass)
|
||||||
"{0} is not a superclass of {1}".format(superclass, self.__test))
|
|
||||||
|
|
||||||
|
|
||||||
class MetaPathFinder(InheritanceTests):
|
class MetaPathFinder(InheritanceTests):
|
||||||
|
@ -416,14 +414,14 @@ class InspectLoaderSourceToCodeTests:
|
||||||
# Since compile() can handle strings, so should source_to_code().
|
# Since compile() can handle strings, so should source_to_code().
|
||||||
source = 'attr = 42'
|
source = 'attr = 42'
|
||||||
module = self.source_to_module(source)
|
module = self.source_to_module(source)
|
||||||
self.assertTrue(hasattr(module, 'attr'))
|
self.assertHasAttr(module, 'attr')
|
||||||
self.assertEqual(module.attr, 42)
|
self.assertEqual(module.attr, 42)
|
||||||
|
|
||||||
def test_source_to_code_bytes(self):
|
def test_source_to_code_bytes(self):
|
||||||
# Since compile() can handle bytes, so should source_to_code().
|
# Since compile() can handle bytes, so should source_to_code().
|
||||||
source = b'attr = 42'
|
source = b'attr = 42'
|
||||||
module = self.source_to_module(source)
|
module = self.source_to_module(source)
|
||||||
self.assertTrue(hasattr(module, 'attr'))
|
self.assertHasAttr(module, 'attr')
|
||||||
self.assertEqual(module.attr, 42)
|
self.assertEqual(module.attr, 42)
|
||||||
|
|
||||||
def test_source_to_code_path(self):
|
def test_source_to_code_path(self):
|
||||||
|
@ -757,7 +755,7 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
|
||||||
warnings.simplefilter('ignore', DeprecationWarning)
|
warnings.simplefilter('ignore', DeprecationWarning)
|
||||||
module = self.loader.load_module(self.name)
|
module = self.loader.load_module(self.name)
|
||||||
self.verify_module(module)
|
self.verify_module(module)
|
||||||
self.assertFalse(hasattr(module, '__path__'))
|
self.assertNotHasAttr(module, '__path__')
|
||||||
|
|
||||||
def test_get_source_encoding(self):
|
def test_get_source_encoding(self):
|
||||||
# Source is considered encoded in UTF-8 by default unless otherwise
|
# Source is considered encoded in UTF-8 by default unless otherwise
|
||||||
|
|
|
@ -430,8 +430,7 @@ class StartupTests:
|
||||||
for name, module in sys.modules.items():
|
for name, module in sys.modules.items():
|
||||||
if isinstance(module, types.ModuleType):
|
if isinstance(module, types.ModuleType):
|
||||||
with self.subTest(name=name):
|
with self.subTest(name=name):
|
||||||
self.assertTrue(hasattr(module, '__loader__'),
|
self.assertHasAttr(module, '__loader__')
|
||||||
'{!r} lacks a __loader__ attribute'.format(name))
|
|
||||||
if self.machinery.BuiltinImporter.find_spec(name):
|
if self.machinery.BuiltinImporter.find_spec(name):
|
||||||
self.assertIsNot(module.__loader__, None)
|
self.assertIsNot(module.__loader__, None)
|
||||||
elif self.machinery.FrozenImporter.find_spec(name):
|
elif self.machinery.FrozenImporter.find_spec(name):
|
||||||
|
@ -441,7 +440,7 @@ class StartupTests:
|
||||||
for name, module in sys.modules.items():
|
for name, module in sys.modules.items():
|
||||||
if isinstance(module, types.ModuleType):
|
if isinstance(module, types.ModuleType):
|
||||||
with self.subTest(name=name):
|
with self.subTest(name=name):
|
||||||
self.assertTrue(hasattr(module, '__spec__'))
|
self.assertHasAttr(module, '__spec__')
|
||||||
if self.machinery.BuiltinImporter.find_spec(name):
|
if self.machinery.BuiltinImporter.find_spec(name):
|
||||||
self.assertIsNot(module.__spec__, None)
|
self.assertIsNot(module.__spec__, None)
|
||||||
elif self.machinery.FrozenImporter.find_spec(name):
|
elif self.machinery.FrozenImporter.find_spec(name):
|
||||||
|
|
|
@ -9,6 +9,7 @@ import unittest
|
||||||
|
|
||||||
from test.support import threading_helper
|
from test.support import threading_helper
|
||||||
from test.test_importlib import util as test_util
|
from test.test_importlib import util as test_util
|
||||||
|
from test.support.testcase import ExtraAssertions
|
||||||
|
|
||||||
|
|
||||||
class CollectInit:
|
class CollectInit:
|
||||||
|
@ -58,7 +59,7 @@ class TestingImporter(abc.MetaPathFinder, abc.Loader):
|
||||||
self.load_count += 1
|
self.load_count += 1
|
||||||
|
|
||||||
|
|
||||||
class LazyLoaderTests(unittest.TestCase):
|
class LazyLoaderTests(unittest.TestCase, ExtraAssertions):
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
|
@ -125,12 +126,12 @@ class LazyLoaderTests(unittest.TestCase):
|
||||||
# Deleting an attribute should stay deleted.
|
# Deleting an attribute should stay deleted.
|
||||||
module = self.new_module()
|
module = self.new_module()
|
||||||
del module.attr
|
del module.attr
|
||||||
self.assertFalse(hasattr(module, 'attr'))
|
self.assertNotHasAttr(module, 'attr')
|
||||||
|
|
||||||
def test_delete_preexisting_attr(self):
|
def test_delete_preexisting_attr(self):
|
||||||
module = self.new_module()
|
module = self.new_module()
|
||||||
del module.__name__
|
del module.__name__
|
||||||
self.assertFalse(hasattr(module, '__name__'))
|
self.assertNotHasAttr(module, '__name__')
|
||||||
|
|
||||||
def test_module_substitution_error(self):
|
def test_module_substitution_error(self):
|
||||||
with test_util.uncache(TestingImporter.module_name):
|
with test_util.uncache(TestingImporter.module_name):
|
||||||
|
|
|
@ -8,6 +8,7 @@ import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from test.test_importlib import util
|
from test.test_importlib import util
|
||||||
|
from test.support.testcase import ExtraAssertions
|
||||||
|
|
||||||
# needed tests:
|
# needed tests:
|
||||||
#
|
#
|
||||||
|
@ -53,7 +54,7 @@ def namespace_tree_context(**kwargs):
|
||||||
with import_context, sys_modules_context():
|
with import_context, sys_modules_context():
|
||||||
yield
|
yield
|
||||||
|
|
||||||
class NamespacePackageTest(unittest.TestCase):
|
class NamespacePackageTest(unittest.TestCase, ExtraAssertions):
|
||||||
"""
|
"""
|
||||||
Subclasses should define self.root and self.paths (under that root)
|
Subclasses should define self.root and self.paths (under that root)
|
||||||
to be added to sys.path.
|
to be added to sys.path.
|
||||||
|
@ -80,7 +81,7 @@ class SingleNamespacePackage(NamespacePackageTest):
|
||||||
|
|
||||||
def test_simple_repr(self):
|
def test_simple_repr(self):
|
||||||
import foo.one
|
import foo.one
|
||||||
self.assertTrue(repr(foo).startswith("<module 'foo' (namespace) from ["))
|
self.assertStartsWith(repr(foo), "<module 'foo' (namespace) from [")
|
||||||
|
|
||||||
|
|
||||||
class DynamicPathNamespacePackage(NamespacePackageTest):
|
class DynamicPathNamespacePackage(NamespacePackageTest):
|
||||||
|
|
|
@ -8,8 +8,9 @@ import unittest
|
||||||
|
|
||||||
from importlib.util import cache_from_source
|
from importlib.util import cache_from_source
|
||||||
from test.support.os_helper import create_empty_file
|
from test.support.os_helper import create_empty_file
|
||||||
|
from test.support.testcase import ExtraAssertions
|
||||||
|
|
||||||
class TestImport(unittest.TestCase):
|
class TestImport(unittest.TestCase, ExtraAssertions):
|
||||||
|
|
||||||
def __init__(self, *args, **kw):
|
def __init__(self, *args, **kw):
|
||||||
self.package_name = 'PACKAGE_'
|
self.package_name = 'PACKAGE_'
|
||||||
|
@ -55,7 +56,7 @@ class TestImport(unittest.TestCase):
|
||||||
except SyntaxError: pass
|
except SyntaxError: pass
|
||||||
else: raise RuntimeError('Failed to induce SyntaxError') # self.fail()?
|
else: raise RuntimeError('Failed to induce SyntaxError') # self.fail()?
|
||||||
self.assertNotIn(self.module_name, sys.modules)
|
self.assertNotIn(self.module_name, sys.modules)
|
||||||
self.assertFalse(hasattr(sys.modules[self.package_name], 'foo'))
|
self.assertNotHasAttr(sys.modules[self.package_name], 'foo')
|
||||||
|
|
||||||
# ...make up a variable name that isn't bound in __builtins__
|
# ...make up a variable name that isn't bound in __builtins__
|
||||||
var = 'a'
|
var = 'a'
|
||||||
|
|
|
@ -237,7 +237,7 @@ class ModuleSpecMethodsTests:
|
||||||
self.spec.loader = NewLoader()
|
self.spec.loader = NewLoader()
|
||||||
module = self.util.module_from_spec(self.spec)
|
module = self.util.module_from_spec(self.spec)
|
||||||
sys.modules[self.name] = module
|
sys.modules[self.name] = module
|
||||||
self.assertFalse(hasattr(module, 'eggs'))
|
self.assertNotHasAttr(module, 'eggs')
|
||||||
self.bootstrap._exec(self.spec, module)
|
self.bootstrap._exec(self.spec, module)
|
||||||
|
|
||||||
self.assertEqual(module.eggs, 1)
|
self.assertEqual(module.eggs, 1)
|
||||||
|
@ -348,9 +348,9 @@ class ModuleSpecMethodsTests:
|
||||||
self.assertIs(loaded.__loader__, self.spec.loader)
|
self.assertIs(loaded.__loader__, self.spec.loader)
|
||||||
self.assertEqual(loaded.__package__, self.spec.parent)
|
self.assertEqual(loaded.__package__, self.spec.parent)
|
||||||
self.assertIs(loaded.__spec__, self.spec)
|
self.assertIs(loaded.__spec__, self.spec)
|
||||||
self.assertFalse(hasattr(loaded, '__path__'))
|
self.assertNotHasAttr(loaded, '__path__')
|
||||||
self.assertFalse(hasattr(loaded, '__file__'))
|
self.assertNotHasAttr(loaded, '__file__')
|
||||||
self.assertFalse(hasattr(loaded, '__cached__'))
|
self.assertNotHasAttr(loaded, '__cached__')
|
||||||
|
|
||||||
|
|
||||||
(Frozen_ModuleSpecMethodsTests,
|
(Frozen_ModuleSpecMethodsTests,
|
||||||
|
|
|
@ -321,7 +321,7 @@ class MagicNumberTests:
|
||||||
|
|
||||||
def test_incorporates_rn(self):
|
def test_incorporates_rn(self):
|
||||||
# The magic number uses \r\n to come out wrong when splitting on lines.
|
# The magic number uses \r\n to come out wrong when splitting on lines.
|
||||||
self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n'))
|
self.assertEndsWith(self.util.MAGIC_NUMBER, b'\r\n')
|
||||||
|
|
||||||
|
|
||||||
(Frozen_MagicNumberTests,
|
(Frozen_MagicNumberTests,
|
||||||
|
|
|
@ -10,6 +10,7 @@ from test import support
|
||||||
from test.support import import_helper
|
from test.support import import_helper
|
||||||
from test.support import is_apple_mobile
|
from test.support import is_apple_mobile
|
||||||
from test.support import os_helper
|
from test.support import os_helper
|
||||||
|
from test.support.testcase import ExtraAssertions
|
||||||
import unittest
|
import unittest
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -80,7 +81,7 @@ def specialize_class(cls, kind, base=None, **kwargs):
|
||||||
elif not isinstance(base, type):
|
elif not isinstance(base, type):
|
||||||
base = base[kind]
|
base = base[kind]
|
||||||
name = '{}_{}'.format(kind, cls.__name__)
|
name = '{}_{}'.format(kind, cls.__name__)
|
||||||
bases = (cls, base)
|
bases = (cls, base, ExtraAssertions)
|
||||||
specialized = types.new_class(name, bases)
|
specialized = types.new_class(name, bases)
|
||||||
specialized.__module__ = cls.__module__
|
specialized.__module__ = cls.__module__
|
||||||
specialized._NAME = cls.__name__
|
specialized._NAME = cls.__name__
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue