moulinette/test/conftest.py

181 lines
4.9 KiB
Python
Raw Permalink Normal View History

"""Pytest fixtures for testing."""
import sys
2019-12-21 17:36:20 +01:00
import toml
import yaml
import json
import os
2019-08-22 03:27:31 +02:00
import shutil
import pytest
def patch_translate(moulinette):
"""Configure translator to raise errors when there are missing keys."""
old_translate = moulinette.core.Translator.translate
def new_translate(self, key, *args, **kwargs):
if key not in self._translations[self.default_locale].keys():
message = "Unable to retrieve key '%s' for default locale!" % key
raise KeyError(message)
return old_translate(self, key, *args, **kwargs)
moulinette.core.Translator.translate = new_translate
def new_m18nn(self, key, *args, **kwargs):
return self._global.translate(key, *args, **kwargs)
moulinette.core.Moulinette18n.g = new_m18nn
def logging_configuration(moulinette):
"""Configure logging to use the custom logger."""
2021-12-24 01:18:03 +01:00
handlers = {"tty", "api"}
root_handlers = set(handlers)
2019-11-25 17:21:13 +01:00
level = "INFO"
tty_level = "INFO"
return {
2019-11-25 17:21:13 +01:00
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"tty-debug": {"format": "%(relativeCreated)-4d %(fmessage)s"},
"precise": {
"format": "%(asctime)-15s %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s" # noqa
},
},
2020-01-02 16:18:28 +01:00
"filters": {"action": {"()": "moulinette.utils.log.ActionFilter"}},
2019-11-25 17:21:13 +01:00
"handlers": {
"api": {
"level": level,
"class": "moulinette.interfaces.api.APIQueueHandler",
2019-08-22 03:27:31 +02:00
},
2019-11-25 17:21:13 +01:00
"tty": {
"level": tty_level,
"class": "moulinette.interfaces.cli.TTYHandler",
"formatter": "",
},
},
2019-11-25 17:21:13 +01:00
"loggers": {
2020-01-02 16:18:28 +01:00
"moulinette": {"level": level, "handlers": [], "propagate": True},
2019-11-25 17:21:13 +01:00
"moulinette.interface": {
"level": level,
"handlers": handlers,
"propagate": False,
},
},
2020-01-02 16:18:28 +01:00
"root": {"level": level, "handlers": root_handlers},
}
2020-01-02 16:50:21 +01:00
def patch_lock(moulinette):
2020-01-06 18:15:26 +01:00
moulinette.core.MoulinetteLock.base_lockfile = "moulinette_%s.lock"
2020-01-02 16:50:21 +01:00
2019-11-25 17:21:13 +01:00
@pytest.fixture(scope="session", autouse=True)
def moulinette(tmp_path_factory):
import moulinette
import moulinette.core
from moulinette.utils.log import configure_logging
# Can't call the namespace just 'test' because
# that would lead to some "import test" not importing the right stuff
namespace = "moulitest"
tmp_dir = str(tmp_path_factory.mktemp(namespace))
shutil.copy("./test/actionsmap/moulitest.yml", f"{tmp_dir}/moulitest.yml")
shutil.copytree("./test/src", f"{tmp_dir}/lib/{namespace}/")
shutil.copytree("./test/locales", f"{tmp_dir}/locales")
sys.path.insert(0, f"{tmp_dir}/lib")
patch_translate(moulinette)
2020-01-02 16:50:21 +01:00
patch_lock(moulinette)
configure_logging(logging_configuration(moulinette))
moulinette.m18n.set_locales_dir(f"{tmp_dir}/locales")
# Dirty hack to pass this path to Api() and Cli() init later
moulinette._actionsmap_path = f"{tmp_dir}/moulitest.yml"
return moulinette
@pytest.fixture
def moulinette_webapi(moulinette):
from webtest import TestApp
from webtest.app import CookiePolicy
# Dirty hack needed, otherwise cookies ain't reused between request .. not
# sure why :|
def return_true(self, cookie, request):
return True
2019-11-25 17:21:13 +01:00
CookiePolicy.return_ok_secure = return_true
2020-05-01 06:11:14 +02:00
from moulinette.interfaces.api import Interface as Api
2019-08-22 03:27:31 +02:00
return TestApp(Api(routes={}, actionsmap=moulinette._actionsmap_path)._app)
2019-08-22 03:27:31 +02:00
2020-01-02 05:33:10 +01:00
@pytest.fixture
2020-01-02 16:17:32 +01:00
def moulinette_cli(moulinette, mocker):
2020-01-02 05:33:10 +01:00
import argparse
2020-01-02 16:41:21 +01:00
2020-01-02 05:33:10 +01:00
parser = argparse.ArgumentParser(add_help=False)
2020-01-02 16:41:21 +01:00
parser.add_argument(
"--debug",
action="store_true",
default=False,
help="Log and print debug messages",
)
2020-01-02 16:17:32 +01:00
mocker.patch("os.isatty", return_value=True)
2020-05-01 06:11:14 +02:00
from moulinette.interfaces.cli import Interface as Cli
2020-05-01 14:03:23 +02:00
cli = Cli(top_parser=parser, actionsmap=moulinette._actionsmap_path)
2020-01-02 16:17:32 +01:00
mocker.stopall()
2020-01-02 05:33:10 +01:00
2020-05-01 06:11:14 +02:00
return cli
2020-01-02 05:33:10 +01:00
@pytest.fixture
def test_file(tmp_path):
2019-11-25 17:21:13 +01:00
test_text = "foo\nbar\n"
test_file = tmp_path / "test.txt"
2019-12-20 07:41:04 +01:00
test_file.write_bytes(test_text.encode())
return test_file
@pytest.fixture
def test_json(tmp_path):
2019-11-25 17:21:13 +01:00
test_json = json.dumps({"foo": "bar"})
test_file = tmp_path / "test.json"
2019-12-20 07:41:04 +01:00
test_file.write_bytes(test_json.encode())
return test_file
2019-12-21 17:36:20 +01:00
@pytest.fixture
def test_yaml(tmp_path):
test_yaml = yaml.dump({"foo": "bar"})
test_file = tmp_path / "test.txt"
2020-12-04 22:16:32 +01:00
test_file.write_bytes(test_yaml.encode())
2019-12-21 17:36:20 +01:00
return test_file
@pytest.fixture
def test_toml(tmp_path):
test_toml = toml.dumps({"foo": "bar"})
test_file = tmp_path / "test.txt"
2020-12-04 22:16:32 +01:00
test_file.write_bytes(test_toml.encode())
2019-12-21 17:36:20 +01:00
return test_file
@pytest.fixture
def user():
return os.getlogin()
@pytest.fixture
def test_url():
2019-11-25 17:21:13 +01:00
return "https://some.test.url/yolo.txt"