mirror of
https://github.com/chatmail/core.git
synced 2026-05-20 07:16:31 +03:00
refactor session_liveconfig_producer to become a more geneal testprocess management object
This commit is contained in:
@@ -17,7 +17,6 @@ import requests
|
|||||||
from . import Account, const
|
from . import Account, const
|
||||||
from .events import FFIEventLogger, FFIEventTracker
|
from .events import FFIEventLogger, FFIEventTracker
|
||||||
from _pytest._code import Source
|
from _pytest._code import Source
|
||||||
from deltachat.direct_imap import DirectImap
|
|
||||||
|
|
||||||
import deltachat
|
import deltachat
|
||||||
|
|
||||||
@@ -126,47 +125,49 @@ def pytest_report_header(config, startdir):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def session_liveconfig_producer(request):
|
def testprocess(request):
|
||||||
""" provide live account configs, cached on a per-test-process scope so that test functions
|
return TestProcess(request.config.option.liveconfig)
|
||||||
can re-use already known live configs. Depending on the --liveconfig option this comes from
|
|
||||||
a HTTP provider or a file with a line specifying each accounts config.
|
|
||||||
"""
|
|
||||||
liveconfig_opt = request.config.option.liveconfig
|
|
||||||
if not liveconfig_opt:
|
|
||||||
def skip_producer():
|
|
||||||
pytest.skip("specify DCC_NEW_TMP_EMAIL or --liveconfig to provide live accounts for testing")
|
|
||||||
return skip_producer
|
|
||||||
|
|
||||||
configlist = []
|
|
||||||
if not liveconfig_opt.startswith("http"):
|
|
||||||
for line in open(liveconfig_opt):
|
|
||||||
if line.strip() and not line.strip().startswith('#'):
|
|
||||||
d = {}
|
|
||||||
for part in line.split():
|
|
||||||
name, value = part.split("=")
|
|
||||||
d[name] = value
|
|
||||||
configlist.append(d)
|
|
||||||
|
|
||||||
def from_file_producer():
|
class TestProcess:
|
||||||
return iter(configlist)
|
def __init__(self, liveconfig_opt):
|
||||||
|
self.liveconfig_opt = liveconfig_opt
|
||||||
|
|
||||||
return from_file_producer
|
def get_liveconfig_producer(self):
|
||||||
|
""" provide live account configs, cached on a per-test-process scope
|
||||||
|
so that test functions can re-use already known live configs.
|
||||||
|
Depending on the --liveconfig option this comes from
|
||||||
|
a HTTP provider or a file with a line specifying each accounts config.
|
||||||
|
"""
|
||||||
|
liveconfig_opt = self.liveconfig_opt
|
||||||
|
if not liveconfig_opt:
|
||||||
|
pytest.skip("specify DCC_NEW_TMP_EMAIL or --liveconfig to provide live accounts")
|
||||||
|
|
||||||
def caching_producer_from_url():
|
configlist = []
|
||||||
for index in range(10):
|
if not liveconfig_opt.startswith("http"):
|
||||||
try:
|
for line in open(liveconfig_opt):
|
||||||
yield configlist[index]
|
if line.strip() and not line.strip().startswith('#'):
|
||||||
except IndexError:
|
d = {}
|
||||||
res = requests.post(liveconfig_opt)
|
for part in line.split():
|
||||||
if res.status_code != 200:
|
name, value = part.split("=")
|
||||||
pytest.fail("creating newtmpuser failed with code {}: '{}'".format(res.status_code, res.text))
|
d[name] = value
|
||||||
d = res.json()
|
configlist.append(d)
|
||||||
config = dict(addr=d["email"], mail_pw=d["password"])
|
|
||||||
configlist.append(config)
|
|
||||||
yield config
|
|
||||||
pytest.fail("more than 10 live accounts requested. Is a test running wild?")
|
|
||||||
|
|
||||||
return caching_producer_from_url
|
yield from iter(configlist)
|
||||||
|
else:
|
||||||
|
for index in range(10):
|
||||||
|
try:
|
||||||
|
yield configlist[index]
|
||||||
|
except IndexError:
|
||||||
|
res = requests.post(liveconfig_opt)
|
||||||
|
if res.status_code != 200:
|
||||||
|
pytest.fail("creat newtmpuser failed {}: '{}'".format(
|
||||||
|
res.status_code, res.text))
|
||||||
|
d = res.json()
|
||||||
|
config = dict(addr=d["email"], mail_pw=d["password"])
|
||||||
|
configlist.append(config)
|
||||||
|
yield config
|
||||||
|
pytest.fail("more than 10 live accounts requested. Is a test running wild?")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -203,10 +204,10 @@ class ACFactory:
|
|||||||
_finalizers: List[Callable[[], None]]
|
_finalizers: List[Callable[[], None]]
|
||||||
_accounts: List[Account]
|
_accounts: List[Account]
|
||||||
|
|
||||||
def __init__(self, strict_tls, tmpdir, session_liveconfig_producer, data) -> None:
|
def __init__(self, strict_tls, tmpdir, testprocess, data) -> None:
|
||||||
self.strict_tls = strict_tls
|
self.strict_tls = strict_tls
|
||||||
self.tmpdir = tmpdir
|
self.tmpdir = tmpdir
|
||||||
self._liveconfig_producer = session_liveconfig_producer()
|
self._liveconfig_producer = testprocess.get_liveconfig_producer()
|
||||||
self.data = data
|
self.data = data
|
||||||
|
|
||||||
self._finalizers = []
|
self._finalizers = []
|
||||||
@@ -389,6 +390,8 @@ class ACFactory:
|
|||||||
return bot
|
return bot
|
||||||
|
|
||||||
def init_direct_imap(self, acc):
|
def init_direct_imap(self, acc):
|
||||||
|
from deltachat.direct_imap import DirectImap
|
||||||
|
|
||||||
if not hasattr(acc, "direct_imap"):
|
if not hasattr(acc, "direct_imap"):
|
||||||
acc.direct_imap = imap = DirectImap(acc)
|
acc.direct_imap = imap = DirectImap(acc)
|
||||||
for folder in imap.list_folders():
|
for folder in imap.list_folders():
|
||||||
@@ -428,9 +431,9 @@ class ACFactory:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def acfactory(pytestconfig, tmpdir, request, session_liveconfig_producer, data):
|
def acfactory(pytestconfig, tmpdir, request, testprocess, data):
|
||||||
strict_tls = pytestconfig.getoption("--strict-tls")
|
strict_tls = pytestconfig.getoption("--strict-tls")
|
||||||
am = ACFactory(strict_tls, tmpdir, session_liveconfig_producer, data)
|
am = ACFactory(strict_tls, tmpdir, testprocess, data)
|
||||||
request.addfinalizer(am.finalize)
|
request.addfinalizer(am.finalize)
|
||||||
yield am
|
yield am
|
||||||
if hasattr(request.node, "rep_call") and request.node.rep_call.failed:
|
if hasattr(request.node, "rep_call") and request.node.rep_call.failed:
|
||||||
|
|||||||
Reference in New Issue
Block a user