diff --git a/python/examples/test_examples.py b/python/examples/test_examples.py index dc074db82..eaed592be 100644 --- a/python/examples/test_examples.py +++ b/python/examples/test_examples.py @@ -1,5 +1,4 @@ -import threading import pytest import py import echo_and_quit @@ -18,56 +17,32 @@ def datadir(): def test_echo_quit_plugin(acfactory): - bot_ac, bot_cfg = acfactory.get_online_config() - - def run_bot(): - print("*"*20 + " starting bot") - echo_and_quit.main([ - "echo", - "--show-ffi", - "--db", bot_ac.db_path, - "--email", bot_cfg["addr"], - "--password", bot_cfg["mail_pw"], - ]) - - t = threading.Thread(target=run_bot) - t.start() + botproc = acfactory.run_bot_process(echo_and_quit) ac1 = acfactory.get_one_online_account() - bot_contact = ac1.create_contact(bot_cfg["addr"]) + bot_contact = ac1.create_contact(botproc.addr) ch1 = ac1.create_chat_by_contact(bot_contact) ch1.send_text("hello") reply = ac1._evtracker.wait_next_incoming_message() assert "hello" in reply.text assert reply.chat == ch1 ch1.send_text("/quit") - t.join() + botproc.wait() -@pytest.mark.skip(reason="not implemented") +@pytest.mark.skip(reason="botproc-matching not implementing") def test_group_tracking_plugin(acfactory): - bot_ac, bot_cfg = acfactory.get_online_config() - - def run_bot(): - print("*"*20 + " starting bot") - print("*"*20 + " bot_ac.dbpath", bot_ac.db_path) - group_tracking.main([ - "group-tracking", - "--show-ffi", bot_ac.db_path, - "--db", bot_ac.db_path, - "--email", bot_cfg["addr"], - "--password", bot_cfg["mail_pw"], - ]) - - t = threading.Thread(target=run_bot) - t.setDaemon(1) - t.start() + botproc = acfactory.run_bot_process(group_tracking) ac1 = acfactory.get_one_online_account() - bot_contact = ac1.create_contact(bot_cfg["addr"]) - ch1 = ac1.create_chat_by_contact(bot_contact) + bot_contact = ac1.create_contact(botproc.addr) + ch1 = ac1.create_group_chat("bot test group") + ch1.add_contact(bot_contact) ch1.send_text("hello") ch1.add_contact(ac1.create_contact("x@example.org")) - # XXX wait for bot to receive things - t.join() + botproc.fnmatch_lines(""" + *member_added x@example.org* + """) + + botproc.kill() diff --git a/python/src/deltachat/testplugin.py b/python/src/deltachat/testplugin.py index 8bfebc4b2..c065cae0a 100644 --- a/python/src/deltachat/testplugin.py +++ b/python/src/deltachat/testplugin.py @@ -1,6 +1,7 @@ from __future__ import print_function import os import sys +import subprocess import pytest import requests import time @@ -241,11 +242,40 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig, datadir): ac.start() return ac + def run_bot_process(self, module): + fn = module.__file__ + + bot_ac, bot_cfg = self.get_online_config() + + popen = subprocess.Popen([ + sys.executable, + fn, + "--show-ffi", + "--db", bot_ac.db_path, + "--email", bot_cfg["addr"], + "--password", bot_cfg["mail_pw"], + ]) + bot = BotProcess(popen, bot_cfg) + self._finalizers.append(bot.kill) + return bot + am = AccountMaker() request.addfinalizer(am.finalize) return am +class BotProcess: + def __init__(self, popen, bot_cfg): + self.popen = popen + self.addr = bot_cfg["addr"] + + def kill(self): + self.popen.kill() + + def wait(self, timeout=30): + self.popen.wait(timeout=timeout) + + @pytest.fixture def tmp_db_path(tmpdir): return tmpdir.join("test.db").strpath