Tests: refactor testing approach

Instead of fiddling with sys.path, we instead use a custom
sys.meta_path importer
This commit is contained in:
Mathieu Duponchelle 2018-10-31 00:41:31 +01:00
parent ae3ffd3ac8
commit d64bbc1e0c
5 changed files with 32 additions and 41 deletions

View file

@ -566,7 +566,7 @@ def TIME_ARGS(time):
time % Gst.SECOND) time % Gst.SECOND)
__all__.append('TIME_ARGS') __all__.append('TIME_ARGS')
import _gi_gst from gi.overrides import _gi_gst
_gi_gst _gi_gst
# maybe more python and less C some day if core turns a bit more introspection # maybe more python and less C some day if core turns a bit more introspection

View file

@ -8,12 +8,3 @@ gstpython = python.extension_module('_gi_gst',
install_dir : pygi_override_dir, install_dir : pygi_override_dir,
include_directories : [configinc], include_directories : [configinc],
dependencies : [gst_dep, python_dep, pygobject_dep]) dependencies : [gst_dep, python_dep, pygobject_dep])
gi_overrides_build_dir = meson.current_build_dir()
# Workaround to get uninstalled working.
foreach source: pysources
run_command(python, '-c', 'import os; os.symlink("@0@/@1@", "@2@/@3@")'.format(
meson.current_source_dir(), source,
gi_overrides_build_dir, source))
endforeach

View file

@ -1,6 +1,11 @@
# Don't try to use wildcards to replace the list of tests below. # Don't try to use wildcards to replace the list of tests below.
# http://www.gnu.org/software/automake/manual/automake.html#Wildcards # http://www.gnu.org/software/automake/manual/automake.html#Wildcards
# Keep this list sorted! # Keep this list sorted!
TEST_ENVIRONMENT = \
GST_OVERRIDE_SRC_PATH="$(abs_top_srcdir)/gi/overrides" \
GST_OVERRIDE_BUILD_PATH="$(abs_top_builddir)/gi/overrides"
tests = \ tests = \
test_gst.py \ test_gst.py \
test_types.py test_types.py
@ -20,10 +25,10 @@ clean-local:
rm -rf *.pyc *.pyo rm -rf *.pyc *.pyo
check-local: check-local:
$(PYTHON) $(srcdir)/runtests.py $(tests) $(TEST_ENVIRONMENT) $(PYTHON) $(srcdir)/runtests.py $(tests)
%.check: % %.check: %
$(PYTHON) $(srcdir)/runtests.py $* $(TEST_ENVIRONMENT) $(PYTHON) $(srcdir)/runtests.py $*
%.forever: % %.forever: %
$(srcdir)/cleanup.py $(srcdir)/cleanup.py
@while true; do \ @while true; do \

View file

@ -25,11 +25,10 @@ if runcmd.returncode() != 0
error('Could not configure testsuite config file.' + runcmd.stderr()) error('Could not configure testsuite config file.' + runcmd.stderr())
endif endif
gi_dir = join_paths(pygi_override_dir, '..', '..')
foreach i: tests foreach i: tests
test_name = i.get(0) test_name = i.get(0)
env = environment() env = environment()
env.prepend('PYTHONPATH', [gi_dir, gi_overrides_build_dir]) env.set('GST_OVERRIDE_SRC_PATH', join_paths (meson.current_source_dir(), '..', 'gi', 'overrides'))
env.set('GST_OVERRIDE_BUILD_PATH', join_paths (meson.current_build_dir(), '..', 'gi', 'overrides'))
test(test_name, python, args: [runtests, i.get(1)], env: env) test(test_name, python, args: [runtests, i.get(1)], env: env)
endforeach endforeach

View file

@ -1,32 +1,28 @@
import os import os
import gi.overrides import sys
import imp
try: class GstOverrideImport:
import mesonconfig def find_module(self, fullname, path=None):
except ImportError: if fullname in ('gi.overrides.Gst', 'gi.overrides._gi_gst'):
mesonconfig = None return self
pass return None
FILE = os.path.realpath(__file__) def load_module(self, name):
if not gi.overrides.__path__[0].endswith("gst-python/gi/overrides"): if name in sys.modules:
local_overrides = None return sys.modules[name]
# our overrides don't take precedence, let's fix it
for i, path in enumerate(gi.overrides.__path__):
if path.endswith("gst-python/gi/overrides"):
local_overrides = path
if local_overrides: fp, pathname, description = imp.find_module(name.split('.')[-1], [
gi.overrides.__path__.remove(local_overrides) os.environ.get('GST_OVERRIDE_SRC_PATH'),
else: os.environ.get('GST_OVERRIDE_BUILD_PATH'),
local_overrides = os.path.abspath(os.path.join(FILE, "../", "../", "gi", "overrides")) ])
gi.overrides.__path__.insert(0, local_overrides) try:
module = imp.load_module(name, fp, pathname, description)
finally:
if fp:
fp.close()
sys.modules[name] = module
return module
if mesonconfig: sys.meta_path.insert(0, GstOverrideImport())
gi.overrides.__path__.insert(0, os.path.abspath(os.path.join(mesonconfig.path, "gi", "overrides")))
# Execute previously set sitecustomize.py script if it existed
if os.environ.get("GST_ENV"):
old_sitecustomize = os.path.join(os.path.dirname(__file__),
"old.sitecustomize.gstuninstalled.py")
if os.path.exists(old_sitecustomize):
exec(compile(open(old_sitecustomize).read(), old_sitecustomize, 'exec'))