2019-06-28 19:54:45 +02:00
|
|
|
"""Pytest fixtures for testing."""
|
|
|
|
|
2021-11-16 18:15:09 +01:00
|
|
|
import sys
|
2019-12-21 17:36:20 +01:00
|
|
|
import toml
|
|
|
|
import yaml
|
2019-06-28 19:54:45 +02:00
|
|
|
import json
|
|
|
|
import os
|
2021-11-16 18:15:09 +01:00
|
|
|
|
2019-08-22 03:27:31 +02:00
|
|
|
import shutil
|
2019-06-28 19:54:45 +02:00
|
|
|
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():
|
2020-12-31 23:57:10 +01:00
|
|
|
message = "Unable to retrieve key '%s' for default locale!" % key
|
2019-06-28 19:54:45 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2021-11-16 18:15:09 +01:00
|
|
|
def logging_configuration(moulinette):
|
2019-06-28 19:54:45 +02:00
|
|
|
"""Configure logging to use the custom logger."""
|
2021-12-24 01:18:03 +01:00
|
|
|
handlers = {"tty", "api"}
|
2019-06-28 19:54:45 +02:00
|
|
|
root_handlers = set(handlers)
|
|
|
|
|
2019-11-25 17:21:13 +01:00
|
|
|
level = "INFO"
|
|
|
|
tty_level = "INFO"
|
2019-06-28 19:54:45 +02:00
|
|
|
|
2019-08-22 12:30:26 +02:00
|
|
|
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
|
2019-06-28 19:54:45 +02:00
|
|
|
},
|
|
|
|
},
|
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-06-28 19:54:45 +02:00
|
|
|
},
|
|
|
|
},
|
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,
|
2019-06-28 19:54:45 +02:00
|
|
|
},
|
|
|
|
},
|
2020-01-02 16:18:28 +01:00
|
|
|
"root": {"level": level, "handlers": root_handlers},
|
2019-06-28 19:54:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
2019-08-22 12:30:26 +02:00
|
|
|
def moulinette(tmp_path_factory):
|
2019-06-28 19:54:45 +02:00
|
|
|
import moulinette
|
2021-11-16 18:15:09 +01:00
|
|
|
import moulinette.core
|
|
|
|
from moulinette.utils.log import configure_logging
|
2019-06-28 19:54:45 +02:00
|
|
|
|
2019-08-22 12:30:26 +02:00
|
|
|
# Can't call the namespace just 'test' because
|
|
|
|
# that would lead to some "import test" not importing the right stuff
|
|
|
|
namespace = "moulitest"
|
2021-11-16 18:15:09 +01:00
|
|
|
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")
|
|
|
|
|
2019-06-28 19:54:45 +02:00
|
|
|
patch_translate(moulinette)
|
2020-01-02 16:50:21 +01:00
|
|
|
patch_lock(moulinette)
|
2019-08-22 12:30:26 +02:00
|
|
|
|
2021-11-16 18:15:09 +01:00
|
|
|
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"
|
2019-06-28 19:54:45 +02:00
|
|
|
|
|
|
|
return moulinette
|
|
|
|
|
|
|
|
|
2019-11-19 21:06:55 +01:00
|
|
|
@pytest.fixture
|
2019-08-22 12:30:26 +02:00
|
|
|
def moulinette_webapi(moulinette):
|
2019-11-19 21:06:55 +01:00
|
|
|
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
|
|
|
|
2019-11-19 21:06:55 +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
|
|
|
|
2021-11-16 18:15:09 +01: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
|
|
|
|
2021-11-16 18:15:09 +01: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
|
|
|
|
|
|
|
|
2019-06-28 19:54:45 +02: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())
|
2019-06-28 19:54:45 +02:00
|
|
|
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())
|
2019-06-28 19:54:45 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-06-28 19:54:45 +02:00
|
|
|
@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"
|