def test_loader_retrievers(builtin_pkg): from dtale.cli.loaders import get_py35_loader, get_py33_loader orig_import = __import__ mock_importlib_util = mock.Mock() mock_importlib_machinery = mock.Mock() mock_imp = mock.Mock() def import_mock(name, *args, **kwargs): if name == "importlib.util": return mock_importlib_util if name == "importlib.machinery": return mock_importlib_machinery if name == "imp": return mock_imp return orig_import(name, *args, **kwargs) with ExitStack() as stack: stack.enter_context( mock.patch("{}.__import__".format(builtin_pkg), side_effect=import_mock) ) assert get_py35_loader("custom.loaders", "loader.py") is not None assert mock_importlib_util.spec_from_file_location.called_once() mock_spec = mock_importlib_util.spec_from_file_location.return_value assert mock_importlib_util.module_from_spec.called_once() mock_loader = mock_spec.loader.return_value assert mock_loader.exec_module.called_once() assert get_py33_loader("custom.loaders", "loader.py") is not None assert mock_importlib_machinery.SourceFileLoader.called_once() mock_sourcefileloader = mock_importlib_machinery.SourceFileLoader.return_value assert mock_sourcefileloader.load_module.called_once()
def test_loader_retrievers(): from dtale.cli.loaders import get_py35_loader, get_py33_loader, get_py2_loader orig_import = __import__ mock_importlib_util = mock.Mock() mock_importlib_machinery = mock.Mock() mock_imp = mock.Mock() def import_mock(name, *args, **kwargs): if name == 'importlib.util': return mock_importlib_util if name == 'importlib.machinery': return mock_importlib_machinery if name == 'imp': return mock_imp return orig_import(name, *args, **kwargs) with ExitStack() as stack: if PY3: stack.enter_context( mock.patch('builtins.__import__', side_effect=import_mock)) else: stack.enter_context( mock.patch('__builtin__.__import__', side_effect=import_mock)) assert get_py35_loader('custom.loaders', 'loader.py') is not None assert mock_importlib_util.spec_from_file_location.called_once() mock_spec = mock_importlib_util.spec_from_file_location.return_value assert mock_importlib_util.module_from_spec.called_once() mock_loader = mock_spec.loader.return_value assert mock_loader.exec_module.called_once() assert get_py33_loader('custom.loaders', 'loader.py') is not None assert mock_importlib_machinery.SourceFileLoader.called_once() mock_sourcefileloader = mock_importlib_machinery.SourceFileLoader.return_value assert mock_sourcefileloader.load_module.called_once() assert get_py2_loader('custom.loaders', 'loader.py') is not None assert mock_imp.load_source.called_once()