Tests: simplify profile removal

It turns out that `pytest-anki` does what we are trying to do already.

Note that `empty_anki_session_started` creates a temporary user too.
We are “overwriting“ it in `profile_created_and_loaded`
by calling `temporary_user`. It seems that doing this is safe.
This commit is contained in:
oakkitten 2022-04-07 23:35:42 +01:00
parent c53aa86a0d
commit 849ab43be7
3 changed files with 14 additions and 37 deletions

View File

@ -6,8 +6,7 @@ from dataclasses import dataclass
import aqt.operations.note import aqt.operations.note
import pytest import pytest
from PyQt5 import QtTest from PyQt5 import QtTest
from _pytest.monkeypatch import MonkeyPatch from pytest_anki._launch import anki_running, temporary_user # noqa
from pytest_anki._launch import anki_running # noqa
from plugin import AnkiConnect from plugin import AnkiConnect
from plugin.edit import Edit from plugin.edit import Edit
@ -45,40 +44,21 @@ def close_all_dialogs_and_wait_for_them_to_run_closing_callbacks():
wait_until(aqt.dialogs.allClosed) wait_until(aqt.dialogs.allClosed)
# largely analogous to `aqt.mw.pm.remove`.
# by default, the profile is moved to trash. this is a problem for us,
# as on some systems trash folders may not exist.
# we can't delete folder and *then* call `aqt.mw.pm.remove`,
# as it calls `profileFolder` and that *creates* the folder!
def remove_current_profile():
import os
import shutil
def send2trash(profile_folder):
assert profile_folder.endswith("User 1")
if os.path.exists(profile_folder):
shutil.rmtree(profile_folder)
with MonkeyPatch().context() as monkey:
monkey.setattr(aqt.profiles, "send2trash", send2trash)
aqt.mw.pm.remove(aqt.mw.pm.name)
@contextmanager @contextmanager
def empty_anki_session_started(): def empty_anki_session_started():
with anki_running( with anki_running(
qtbot=None, # noqa qtbot=None, # noqa
enable_web_debugging=False, enable_web_debugging=False,
profile_name="test_user",
) as session: ) as session:
yield session yield session
# backups are run in a thread and can lead to warnings when the thread dies
# after trying to open collection after it's been deleted
@contextmanager @contextmanager
def profile_loaded(session): def profile_created_and_loaded(session):
with temporary_user(session.base, "test_user", "en_US"):
with session.profile_loaded(): with session.profile_loaded():
aqt.mw.pm.profile["numBackups"] = 0 aqt.mw.pm.profile["numBackups"] = 0 # don't try to do backups
yield session yield session
@ -218,7 +198,7 @@ def session_scope_empty_session():
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def session_scope_session_with_profile_loaded(session_scope_empty_session): def session_scope_session_with_profile_loaded(session_scope_empty_session):
with profile_loaded(session_scope_empty_session): with profile_created_and_loaded(session_scope_empty_session):
yield session_scope_empty_session yield session_scope_empty_session
@ -237,11 +217,8 @@ def session_with_profile_loaded(session_scope_empty_session, request):
Tearing down the profile is significantly slower. Tearing down the profile is significantly slower.
""" """
if request.config.option.tear_down_profile_after_each_test: if request.config.option.tear_down_profile_after_each_test:
try: with profile_created_and_loaded(session_scope_empty_session):
with profile_loaded(session_scope_empty_session):
yield session_scope_empty_session yield session_scope_empty_session
finally:
remove_current_profile()
else: else:
session = request.getfixturevalue( session = request.getfixturevalue(
session_scope_session_with_profile_loaded.__name__ session_scope_session_with_profile_loaded.__name__

View File

@ -21,7 +21,7 @@ def test_reloadCollection(setup):
class TestProfiles: class TestProfiles:
def test_getProfiles(self, session_with_profile_loaded): def test_getProfiles(self, session_with_profile_loaded):
result = ac.getProfiles() result = ac.getProfiles()
assert result == ["User 1"] assert result == ["test_user"]
# waiting a little while gets rid of the cryptic warning: # waiting a little while gets rid of the cryptic warning:
# Qt warning: QXcbConnection: XCB error: 8 (BadMatch), sequence: 658, # Qt warning: QXcbConnection: XCB error: 8 (BadMatch), sequence: 658,
@ -29,7 +29,7 @@ class TestProfiles:
def test_loadProfile(self, session_with_profile_loaded): def test_loadProfile(self, session_with_profile_loaded):
aqt.mw.unloadProfileAndShowProfileManager() aqt.mw.unloadProfileAndShowProfileManager()
wait(0.1) wait(0.1)
ac.loadProfile(name="User 1") ac.loadProfile(name="test_user")
class TestExportImport: class TestExportImport:

View File

@ -15,7 +15,7 @@ from plugin import AnkiConnect
from tests.conftest import wait_until, \ from tests.conftest import wait_until, \
empty_anki_session_started, \ empty_anki_session_started, \
anki_connect_config_loaded, \ anki_connect_config_loaded, \
profile_loaded profile_created_and_loaded
@contextmanager @contextmanager
@ -72,7 +72,7 @@ def external_anki_entry_function(web_bind_port, exit_event):
with empty_anki_session_started() as session: with empty_anki_session_started() as session:
with anki_connect_config_loaded(session, web_bind_port): with anki_connect_config_loaded(session, web_bind_port):
with anki_connect_web_server_started(): with anki_connect_web_server_started():
with profile_loaded(session): with profile_created_and_loaded(session):
wait_until(exit_event.is_set) wait_until(exit_event.is_set)