server-env/server_environment/tests/common.py

44 lines
1.3 KiB
Python

# Copyright 2018 Camptocamp (https://www.camptocamp.com).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import os
from contextlib import contextmanager
from unittest.mock import patch
from odoo.tests import common
from odoo.addons.server_environment import server_env
from odoo.tools.config import config
class ServerEnvironmentCase(common.TransactionCase):
def setUp(self):
super().setUp()
self._original_running_env = config.get('running_env')
config['running_env'] = 'testing'
def tearDown(self):
super().tearDown()
config['running_env'] = self._original_running_env
@contextmanager
def set_config_dir(self, path):
original_dir = server_env._dir
if path and not os.path.isabs(path):
path = os.path.join(os.path.dirname(__file__,), path)
server_env._dir = path
try:
yield
finally:
server_env._dir = original_dir
@contextmanager
def set_env_variables(self, public=None, secret=None):
newkeys = {}
if public:
newkeys['SERVER_ENV_CONFIG'] = public
if secret:
newkeys['SERVER_ENV_CONFIG_SECRET'] = secret
with patch.dict('os.environ', newkeys):
yield