Commit cd482fec authored by Brett Cannon's avatar Brett Cannon

Issue #16803: test.test_importlib.source now tests frozen and source code

parent e9ad3599
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
from .. import util from .. import util
from . import util as source_util from . import util as source_util
from importlib import _bootstrap importlib = util.import_importlib('importlib')
from importlib import machinery machinery = util.import_importlib('importlib.machinery')
import os import os
import sys import sys
from test import support as test_support from test import support as test_support
...@@ -11,7 +12,7 @@ import unittest ...@@ -11,7 +12,7 @@ import unittest
@util.case_insensitive_tests @util.case_insensitive_tests
class CaseSensitivityTest(unittest.TestCase): class CaseSensitivityTest:
"""PEP 235 dictates that on case-preserving, case-insensitive file systems """PEP 235 dictates that on case-preserving, case-insensitive file systems
that imports are case-sensitive unless the PYTHONCASEOK environment that imports are case-sensitive unless the PYTHONCASEOK environment
...@@ -21,11 +22,11 @@ class CaseSensitivityTest(unittest.TestCase): ...@@ -21,11 +22,11 @@ class CaseSensitivityTest(unittest.TestCase):
assert name != name.lower() assert name != name.lower()
def find(self, path): def find(self, path):
finder = machinery.FileFinder(path, finder = self.machinery.FileFinder(path,
(machinery.SourceFileLoader, (self.machinery.SourceFileLoader,
machinery.SOURCE_SUFFIXES), self.machinery.SOURCE_SUFFIXES),
(machinery.SourcelessFileLoader, (self.machinery.SourcelessFileLoader,
machinery.BYTECODE_SUFFIXES)) self.machinery.BYTECODE_SUFFIXES))
return finder.find_module(self.name) return finder.find_module(self.name)
def sensitivity_test(self): def sensitivity_test(self):
...@@ -41,7 +42,7 @@ class CaseSensitivityTest(unittest.TestCase): ...@@ -41,7 +42,7 @@ class CaseSensitivityTest(unittest.TestCase):
def test_sensitive(self): def test_sensitive(self):
with test_support.EnvironmentVarGuard() as env: with test_support.EnvironmentVarGuard() as env:
env.unset('PYTHONCASEOK') env.unset('PYTHONCASEOK')
if b'PYTHONCASEOK' in _bootstrap._os.environ: if b'PYTHONCASEOK' in self.importlib._bootstrap._os.environ:
self.skipTest('os.environ changes not reflected in ' self.skipTest('os.environ changes not reflected in '
'_os.environ') '_os.environ')
sensitive, insensitive = self.sensitivity_test() sensitive, insensitive = self.sensitivity_test()
...@@ -52,7 +53,7 @@ class CaseSensitivityTest(unittest.TestCase): ...@@ -52,7 +53,7 @@ class CaseSensitivityTest(unittest.TestCase):
def test_insensitive(self): def test_insensitive(self):
with test_support.EnvironmentVarGuard() as env: with test_support.EnvironmentVarGuard() as env:
env.set('PYTHONCASEOK', '1') env.set('PYTHONCASEOK', '1')
if b'PYTHONCASEOK' not in _bootstrap._os.environ: if b'PYTHONCASEOK' not in self.importlib._bootstrap._os.environ:
self.skipTest('os.environ changes not reflected in ' self.skipTest('os.environ changes not reflected in '
'_os.environ') '_os.environ')
sensitive, insensitive = self.sensitivity_test() sensitive, insensitive = self.sensitivity_test()
...@@ -61,10 +62,9 @@ class CaseSensitivityTest(unittest.TestCase): ...@@ -61,10 +62,9 @@ class CaseSensitivityTest(unittest.TestCase):
self.assertTrue(hasattr(insensitive, 'load_module')) self.assertTrue(hasattr(insensitive, 'load_module'))
self.assertIn(self.name, insensitive.get_filename(self.name)) self.assertIn(self.name, insensitive.get_filename(self.name))
Frozen_CaseSensitivityTest, Source_CaseSensitivityTest = util.test_both(
def test_main(): CaseSensitivityTest, importlib=importlib, machinery=machinery)
test_support.run_unittest(CaseSensitivityTest)
if __name__ == '__main__': if __name__ == '__main__':
test_main() unittest.main()
from .. import abc from .. import abc
from .. import util
from . import util as source_util from . import util as source_util
from importlib import machinery machinery = util.import_importlib('importlib.machinery')
import errno import errno
import os import os
import py_compile import py_compile
...@@ -13,7 +15,7 @@ import unittest ...@@ -13,7 +15,7 @@ import unittest
import warnings import warnings
class FinderTests(unittest.TestCase, abc.FinderTests): class FinderTests(abc.FinderTests):
"""For a top-level module, it should just be found directly in the """For a top-level module, it should just be found directly in the
directory being searched. This is true for a directory with source directory being searched. This is true for a directory with source
...@@ -38,11 +40,11 @@ class FinderTests(unittest.TestCase, abc.FinderTests): ...@@ -38,11 +40,11 @@ class FinderTests(unittest.TestCase, abc.FinderTests):
""" """
def get_finder(self, root): def get_finder(self, root):
loader_details = [(machinery.SourceFileLoader, loader_details = [(self.machinery.SourceFileLoader,
machinery.SOURCE_SUFFIXES), self.machinery.SOURCE_SUFFIXES),
(machinery.SourcelessFileLoader, (self.machinery.SourcelessFileLoader,
machinery.BYTECODE_SUFFIXES)] self.machinery.BYTECODE_SUFFIXES)]
return machinery.FileFinder(root, *loader_details) return self.machinery.FileFinder(root, *loader_details)
def import_(self, root, module): def import_(self, root, module):
return self.get_finder(root).find_module(module) return self.get_finder(root).find_module(module)
...@@ -123,8 +125,8 @@ class FinderTests(unittest.TestCase, abc.FinderTests): ...@@ -123,8 +125,8 @@ class FinderTests(unittest.TestCase, abc.FinderTests):
def test_empty_string_for_dir(self): def test_empty_string_for_dir(self):
# The empty string from sys.path means to search in the cwd. # The empty string from sys.path means to search in the cwd.
finder = machinery.FileFinder('', (machinery.SourceFileLoader, finder = self.machinery.FileFinder('', (self.machinery.SourceFileLoader,
machinery.SOURCE_SUFFIXES)) self.machinery.SOURCE_SUFFIXES))
with open('mod.py', 'w') as file: with open('mod.py', 'w') as file:
file.write("# test file for importlib") file.write("# test file for importlib")
try: try:
...@@ -135,8 +137,8 @@ class FinderTests(unittest.TestCase, abc.FinderTests): ...@@ -135,8 +137,8 @@ class FinderTests(unittest.TestCase, abc.FinderTests):
def test_invalidate_caches(self): def test_invalidate_caches(self):
# invalidate_caches() should reset the mtime. # invalidate_caches() should reset the mtime.
finder = machinery.FileFinder('', (machinery.SourceFileLoader, finder = self.machinery.FileFinder('', (self.machinery.SourceFileLoader,
machinery.SOURCE_SUFFIXES)) self.machinery.SOURCE_SUFFIXES))
finder._path_mtime = 42 finder._path_mtime = 42
finder.invalidate_caches() finder.invalidate_caches()
self.assertEqual(finder._path_mtime, -1) self.assertEqual(finder._path_mtime, -1)
...@@ -180,11 +182,9 @@ class FinderTests(unittest.TestCase, abc.FinderTests): ...@@ -180,11 +182,9 @@ class FinderTests(unittest.TestCase, abc.FinderTests):
finder = self.get_finder(file_obj.name) finder = self.get_finder(file_obj.name)
self.assertEqual((None, []), finder.find_loader('doesnotexist')) self.assertEqual((None, []), finder.find_loader('doesnotexist'))
Frozen_FinderTests, Source_FinderTests = util.test_both(FinderTests, machinery=machinery)
def test_main():
from test.support import run_unittest
run_unittest(FinderTests)
if __name__ == '__main__': if __name__ == '__main__':
test_main() unittest.main()
from .. import util
from . import util as source_util from . import util as source_util
from importlib import machinery machinery = util.import_importlib('importlib.machinery')
import unittest import unittest
class PathHookTest(unittest.TestCase): class PathHookTest:
"""Test the path hook for source.""" """Test the path hook for source."""
def path_hook(self): def path_hook(self):
return machinery.FileFinder.path_hook((machinery.SourceFileLoader, return self.machinery.FileFinder.path_hook((self.machinery.SourceFileLoader,
machinery.SOURCE_SUFFIXES)) self.machinery.SOURCE_SUFFIXES))
def test_success(self): def test_success(self):
with source_util.create_modules('dummy') as mapping: with source_util.create_modules('dummy') as mapping:
...@@ -21,11 +23,8 @@ class PathHookTest(unittest.TestCase): ...@@ -21,11 +23,8 @@ class PathHookTest(unittest.TestCase):
# The empty string represents the cwd. # The empty string represents the cwd.
self.assertTrue(hasattr(self.path_hook()(''), 'find_module')) self.assertTrue(hasattr(self.path_hook()(''), 'find_module'))
Frozen_PathHookTest, Source_PathHooktest = util.test_both(PathHookTest, machinery=machinery)
def test_main():
from test.support import run_unittest
run_unittest(PathHookTest)
if __name__ == '__main__': if __name__ == '__main__':
test_main() unittest.main()
from .. import util
from . import util as source_util from . import util as source_util
from importlib import _bootstrap machinery = util.import_importlib('importlib.machinery')
import codecs import codecs
import re import re
import sys import sys
...@@ -13,7 +15,7 @@ import unittest ...@@ -13,7 +15,7 @@ import unittest
CODING_RE = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII) CODING_RE = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII)
class EncodingTest(unittest.TestCase): class EncodingTest:
"""PEP 3120 makes UTF-8 the default encoding for source code """PEP 3120 makes UTF-8 the default encoding for source code
[default encoding]. [default encoding].
...@@ -35,7 +37,7 @@ class EncodingTest(unittest.TestCase): ...@@ -35,7 +37,7 @@ class EncodingTest(unittest.TestCase):
with source_util.create_modules(self.module_name) as mapping: with source_util.create_modules(self.module_name) as mapping:
with open(mapping[self.module_name], 'wb') as file: with open(mapping[self.module_name], 'wb') as file:
file.write(source) file.write(source)
loader = _bootstrap.SourceFileLoader(self.module_name, loader = self.machinery.SourceFileLoader(self.module_name,
mapping[self.module_name]) mapping[self.module_name])
return loader.load_module(self.module_name) return loader.load_module(self.module_name)
...@@ -84,8 +86,10 @@ class EncodingTest(unittest.TestCase): ...@@ -84,8 +86,10 @@ class EncodingTest(unittest.TestCase):
with self.assertRaises(SyntaxError): with self.assertRaises(SyntaxError):
self.run_test(source) self.run_test(source)
Frozen_EncodingTest, Source_EncodingTest = util.test_both(EncodingTest, machinery=machinery)
class LineEndingTest(unittest.TestCase): class LineEndingTest:
r"""Source written with the three types of line endings (\n, \r\n, \r) r"""Source written with the three types of line endings (\n, \r\n, \r)
need to be readable [cr][crlf][lf].""" need to be readable [cr][crlf][lf]."""
...@@ -97,7 +101,7 @@ class LineEndingTest(unittest.TestCase): ...@@ -97,7 +101,7 @@ class LineEndingTest(unittest.TestCase):
with source_util.create_modules(module_name) as mapping: with source_util.create_modules(module_name) as mapping:
with open(mapping[module_name], 'wb') as file: with open(mapping[module_name], 'wb') as file:
file.write(source) file.write(source)
loader = _bootstrap.SourceFileLoader(module_name, loader = self.machinery.SourceFileLoader(module_name,
mapping[module_name]) mapping[module_name])
return loader.load_module(module_name) return loader.load_module(module_name)
...@@ -113,11 +117,9 @@ class LineEndingTest(unittest.TestCase): ...@@ -113,11 +117,9 @@ class LineEndingTest(unittest.TestCase):
def test_lf(self): def test_lf(self):
self.run_test(b'\n') self.run_test(b'\n')
Frozen_LineEndings, Source_LineEndings = util.test_both(LineEndingTest, machinery=machinery)
def test_main():
from test.support import run_unittest
run_unittest(EncodingTest, LineEndingTest)
if __name__ == '__main__': if __name__ == '__main__':
test_main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment