Skip to content

Commit

Permalink
Update test_import.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Acribbs committed Apr 19, 2023
1 parent 71b6a08 commit f6a5ffb
Showing 1 changed file with 15 additions and 73 deletions.
88 changes: 15 additions & 73 deletions tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,21 @@
This script is best run within nosetests::
nosetests tests/test_import.py
pytest tests/test_import.py
'''

import importlib
import os
import glob
import traceback
import imp

from nose.tools import ok_

# DIRECTORIES to examine
EXPRESSIONS = (
('FirstLevel', 'scpipelines/*.py'),
('SecondLevel', 'scpipelines/version.py'))


# Code to exclude
EXCLUDE = ()


def check_import(filename, outfile):

prefix, suffix = os.path.splitext(filename)
dirname, basename = os.path.split(prefix)

if basename in EXCLUDE:
return

if os.path.exists(prefix + ".pyc"):
os.remove(prefix + ".pyc")

# ignore script with pyximport for now, something does not work
pyxfile = os.path.join(dirname, "_") + basename + "x"
if os.path.exists(pyxfile):
return

try:
imp.load_source(basename, filename)

except ImportError as msg:
outfile.write("FAIL %s\n%s\n" % (basename, msg))
outfile.flush()
traceback.print_exc(file=outfile)
ok_(False, '%s scripts/modules - ImportError: %s' %
(basename, msg))
except Exception as msg:
outfile.write("FAIL %s\n%s\n" % (basename, msg))
outfile.flush()

traceback.print_exc(file=outfile)
ok_(False, '%s scripts/modules - Exception: %s' %
(basename, str(msg)))

ok_(True)


def test_imports():
'''test importing
Relative imports will cause a failure because
imp.load_source does not import modules that are in the same
directory as the module being loaded from source.
'''
outfile = open('test_import.log', 'a')

for label, expression in EXPRESSIONS:

files = glob.glob(expression)
files.sort()

for f in files:
if os.path.isdir(f):
continue
check_import.description = os.path.abspath(f)
yield(check_import, os.path.abspath(f), outfile)
import pytest

# define the directories to test
directories = ['tallytrin', 'tallytrin/python']

# define the test function
@pytest.mark.parametrize('directory', directories)
def test_imports(directory):
for file in os.listdir(directory):
if file.endswith('.py') and file != '__init__.py':
module_name = file[:-3]
module = importlib.import_module(f'{directory}.{module_name}')
assert module is not None

0 comments on commit f6a5ffb

Please sign in to comment.