diff --git a/aqt/__init__.py b/aqt/__init__.py index be49c7d56..8fc8123d7 100644 --- a/aqt/__init__.py +++ b/aqt/__init__.py @@ -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 diff --git a/tests/test_run.py b/tests/test_run.py new file mode 100644 index 000000000..125c50652 --- /dev/null +++ b/tests/test_run.py @@ -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())