import configparser
import doctest
import importlib
import os
import pkgutil
import re
import shutil
import sys
import types
import warnings
import tempfile
from unittest import mock
import nafcodec
def _load_tests_from_module(tests, module, globs, setUp=None, tearDown=None):
for attr in (getattr(module, x) for x in dir(module) if not x.startswith("_")):
if isinstance(attr, types.ModuleType):
suite = doctest.DocTestSuite(
attr,
globs,
setUp=setUp,
tearDown=tearDown,
optionflags=+doctest.ELLIPSIS,
)
tests.addTests(suite)
return tests
def load_tests(loader, tests, ignore):
_current_cwd = os.getcwd()
def setUp(self):
warnings.simplefilter("ignore")
os.chdir(os.path.realpath(os.path.join(__file__, os.path.pardir, "data")))
def tearDown(self):
os.chdir(_current_cwd)
warnings.simplefilter(warnings.defaultaction)
if sys.argv[0].endswith("green"):
return tests
packages = [None, nafcodec]
for pkg in iter(packages.pop, None):
for _, subpkgname, subispkg in pkgutil.walk_packages(pkg.__path__):
if (
subpkgname == "__main__"
or subpkgname.startswith("tests")
or subpkgname.startswith("cli")
):
continue
module = importlib.import_module(".".join([pkg.__name__, subpkgname]))
globs = dict(nafcodec=nafcodec, tempfile=tempfile, **module.__dict__)
tests.addTests(
doctest.DocTestSuite(
module,
globs=globs,
setUp=setUp,
tearDown=tearDown,
optionflags=+doctest.ELLIPSIS,
)
)
if subispkg and subpkgname != "tests":
packages.append(module)
return tests