Skip to content

Commit

Permalink
replace usages of imp with importlib
Browse files Browse the repository at this point in the history
  • Loading branch information
benbariteau committed Oct 3, 2023
1 parent 3d9edec commit 8f718d2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
6 changes: 6 additions & 0 deletions test/discovery_failure_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ def test_discover_test_with_broken_import(self):
r' Traceback \(most recent call last\):\n'
r' File "[^"]+", line \d+, in discover\n'
r' submod = __import__\(module_name, fromlist=\[str\(\'__trash\'\)\]\)\n'
r'( \^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^'
r'\^\^\^\^\n)?'
r' File "[^"]+", line \d+, in <module>\n'
r' import non_existent_module \# noqa: F401\n'
r'( \^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\n)?'
r' (ModuleNotFoundError|ImportError): No module named \'?non_existent_module\'?\n'
),
)
Expand All @@ -51,8 +54,11 @@ def test_discover_test_with_broken_import(self):
r'Traceback \(most recent call last\):\n'
r' File .+, line \d+, in discover\n'
r" submod = __import__\(module_name, fromlist=\[str\(\'__trash\'\)\]\)\n"
r'( \^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^'
r'\^\^\n)?'
r' File .+, line \d+, in <module>\n'
r' import non_existent_module \# noqa: F401\n'
r'( \^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^)?'
r"(ModuleNotFoundError|ImportError): No module named '?non_existent_module'?\n"
),
)
Expand Down
4 changes: 2 additions & 2 deletions test/test_runner_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import imp
import mock
from testify import assert_equal
from testify import setup
from testify import setup_teardown
from testify import test_case
from testify import test_runner
from types import ModuleType

from .test_runner_subdir.inheriting_class import InheritingClass

Expand Down Expand Up @@ -59,7 +59,7 @@ class PluginTestCase(test_case.TestCase):
"""
@setup
def build_module(self):
self.our_module = imp.new_module("our_module")
self.our_module = ModuleType("our_module")
setattr(self.our_module, "prepare_test_case", prepare_test_case)
setattr(self.our_module, "run_test_case", run_test_case)
setattr(self.our_module, "add_testcase_info", add_testcase_info)
Expand Down
13 changes: 3 additions & 10 deletions testify/test_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import pprint
import sys
import logging
import imp
from importlib.machinery import SourceFileLoader

import testify
from testify import exit
Expand All @@ -46,11 +46,6 @@ def load_plugins():
# The idea will be to check out the directory contents and pick up any files that seem to match what python knows how to
# import.

# To properly load the module, we'll need to identify what type it is by the file extension
suffix_map = {}
for suffix in imp.get_suffixes():
suffix_map[suffix[0]] = suffix

plugin_directories = [DEFAULT_PLUGIN_PATH]
if 'TESTIFY_PLUGIN_PATH' in os.environ:
plugin_directories += os.environ['TESTIFY_PLUGIN_PATH'].split(':')
Expand All @@ -60,16 +55,14 @@ def load_plugins():
for file_name in os.listdir(plugin_path):

# For any file that we know how to load, try to import it
if any(file_name.endswith('.py') and not file_name.startswith('.') for suffix in suffix_map.keys()):
if file_name.endswith('.py') and not file_name.startswith('.'):
full_file_path = os.path.join(plugin_path, file_name)
mod_name, suffix = os.path.splitext(file_name)
# Need some unlikely-to-clash unique-ish module name
mod_name = '_testify_plugin__' + mod_name

try:
plugin_modules.append(
imp.load_source(mod_name, full_file_path),
)
SourceFileLoader(mod_name, full_file_path).load_module()
except TypeError:
continue
except ImportError as e:
Expand Down

0 comments on commit 8f718d2

Please sign in to comment.