Allow to run App&GUI without entering the main event loop

This commit is contained in:
krassowski 2017-09-21 03:02:39 +00:00
parent bd414595de
commit b863d7972c
2 changed files with 52 additions and 4 deletions

View file

@ -233,11 +233,14 @@ def run():
"Please notify support of this error:\n\n"+
traceback.format_exc())
def _run():
def _run(argv=None, exec=True):
global mw
if argv is None:
argv = sys.argv
# parse args
opts, args = parseArgs(sys.argv)
opts, args = parseArgs(argv)
opts.base = opts.base or ""
opts.profile = opts.profile or ""
@ -250,7 +253,7 @@ def _run():
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
# create the app
app = AnkiApp(sys.argv)
app = AnkiApp(argv)
QCoreApplication.setApplicationName("Anki")
if app.secondInstance():
# we've signaled the primary instance, so we should close
@ -285,4 +288,7 @@ environment points to a valid, writable folder.""")
# load the main window
import aqt.main
mw = aqt.main.AnkiQt(app, pm, args)
app.exec_()
if exec:
app.exec()
else:
return app

42
tests/test_run.py Normal file
View file

@ -0,0 +1,42 @@
# coding: utf-8
from contextlib import contextmanager
import aqt
from aqt import _run
from aqt.profiles import ProfileManager
@contextmanager
def temporaryUser(name="__Temporary Test User__"):
pm = ProfileManager(base="")
if name in pm.profiles():
raise Exception(f"Could not create a temporary user with name {name}")
pm.create(name)
pm.name = name
yield name
pm.remove(name)
def test_run():
# we need a new user for the test
with temporaryUser() as name:
app = _run(argv=["anki", "-p", name], exec=False)
assert app
aqt.mw.cleanupAndExit()
# clean up what was spoiled
aqt.mw = None
# remove hooks added during app initialization
from anki import hooks
hooks._hooks = {}
# test_nextIvl will fail on some systems if the locales are not restored
import locale
locale.setlocale(locale.LC_ALL, locale.getdefaultlocale())