changedetection.io/backend/tests/conftest.py

56 wiersze
1.6 KiB
Python
Czysty Zwykły widok Historia

2021-02-16 20:36:41 +00:00
#!/usr/bin/python3
import pytest
from backend import changedetection_app
from backend import store
2021-02-21 13:26:19 +00:00
import os
2021-02-16 20:36:41 +00:00
# https://github.com/pallets/flask/blob/1.1.2/examples/tutorial/tests/test_auth.py
# Much better boilerplate than the docs
# https://www.python-boilerplate.com/py3+flask+pytest/
2021-02-21 12:41:00 +00:00
global app
2021-02-16 20:36:41 +00:00
@pytest.fixture(scope='session')
def app(request):
"""Create application for the tests."""
datastore_path = "./test-datastore"
2021-02-21 12:41:00 +00:00
2021-02-21 13:08:34 +00:00
try:
os.mkdir(datastore_path)
except FileExistsError:
pass
# Enable a BASE_URL for notifications to work (so we can look for diff/ etc URLs)
os.environ["BASE_URL"] = "http://mysite.com/"
# Unlink test output files
files = ['test-datastore/output.txt',
"{}/url-watches.json".format(datastore_path),
'test-datastore/notification.txt']
for file in files:
try:
os.unlink(file)
except FileNotFoundError:
pass
2021-02-21 12:41:00 +00:00
2021-02-16 20:36:41 +00:00
app_config = {'datastore_path': datastore_path}
2021-02-21 13:21:14 +00:00
datastore = store.ChangeDetectionStore(datastore_path=app_config['datastore_path'], include_default_watches=False)
2021-02-21 12:41:00 +00:00
app = changedetection_app(app_config, datastore)
2021-03-01 13:29:21 +00:00
app.config['STOP_THREADS'] = True
2021-02-16 20:36:41 +00:00
def teardown():
2021-02-21 12:41:00 +00:00
datastore.stop_thread = True
2021-03-01 13:29:21 +00:00
app.config.exit.set()
for fname in ["url-watches.json", "count.txt", "output.txt"]:
try:
os.unlink("{}/{}".format(datastore_path, fname))
except FileNotFoundError:
# This is fine in the case of a failure.
pass
2021-02-16 20:42:26 +00:00
request.addfinalizer(teardown)
2021-02-27 08:05:25 +00:00
yield app