rougail/tests/test_1_flattener.py

200 lines
6 KiB
Python
Raw Normal View History

2019-11-23 08:17:35 +01:00
from pytest import fixture, raises
2023-10-12 08:17:30 +02:00
from os import getcwd, listdir, environ, makedirs
from os.path import isfile, join, isdir, dirname
from shutil import rmtree, copyfile
import logging
2019-11-23 08:17:35 +01:00
2022-01-19 18:24:00 +01:00
environ['TIRAMISU_LOCALE'] = 'en'
2023-10-12 08:17:30 +02:00
from rougail import Rougail, RougailConfig
2020-07-20 18:13:53 +02:00
from rougail.error import DictConsistencyError
from .custom import CustomOption
2019-11-23 08:17:35 +01:00
2023-10-12 08:17:30 +02:00
logger = logging.getLogger()
logger.setLevel(logging.INFO)
2019-11-23 08:17:35 +01:00
dico_dirs = 'tests/dictionaries'
2019-11-23 08:17:35 +01:00
2023-01-07 21:37:23 +01:00
# if test_3_template.py failed, this temporary directory must be removed
tmp_dir = join(dico_dirs, 'tmp')
if isdir(tmp_dir):
rmtree(tmp_dir)
2019-11-23 08:17:35 +01:00
test_ok = set()
test_raise = set()
for test in listdir(dico_dirs):
if isdir(join(dico_dirs, test)):
2020-07-29 08:59:40 +02:00
if isdir(join(dico_dirs, test, 'tiramisu')):
2019-11-23 08:17:35 +01:00
test_ok.add(test)
2020-07-20 18:13:53 +02:00
elif test != '__pycache__':
2019-11-23 08:17:35 +01:00
test_raise.add(test)
2023-10-12 08:17:30 +02:00
excludes = set([
2024-06-19 17:36:18 +02:00
'80family_several',
2023-10-12 08:17:30 +02:00
])
2019-11-23 08:17:35 +01:00
test_ok -= excludes
test_raise -= excludes
#test_ok = ['04_5disabled_calculation_variable3']
2020-12-22 21:11:14 +01:00
#test_ok = []
2024-06-19 17:36:18 +02:00
#test_raise = ['88valid_enum_invalid_default']
#test_raise = []
2024-06-19 17:36:18 +02:00
test_multi = True
#test_multi = False
test_base = test_multi
#test_base = True
#test_base = False
test_no_namespace = test_multi
#test_no_namespace = True
#test_no_namespace = False
2019-11-23 08:17:35 +01:00
ORI_DIR = getcwd()
2020-11-08 09:43:45 +01:00
debug = False
#debug = True
2019-11-23 08:17:35 +01:00
test_ok = list(test_ok)
test_raise = list(test_raise)
test_ok.sort()
test_raise.sort()
2023-10-12 08:17:30 +02:00
@fixture(scope="module", params=test_ok)
2019-11-23 08:17:35 +01:00
def test_dir(request):
return request.param
2023-10-12 08:17:30 +02:00
@fixture(scope="module", params=test_raise)
2019-11-23 08:17:35 +01:00
def test_dir_error(request):
return request.param
def get_tiramisu_filename(test_dir, subdir, multi, namespace):
if not namespace and not multi:
filename = 'no_namespace.py'
elif not multi:
2023-10-12 08:17:30 +02:00
filename = 'base.py'
else:
filename = 'multi.py'
return join(test_dir, subdir, filename)
2022-11-02 22:52:50 +01:00
def load_rougail_object(test_dir, rougailconfig, multi=False, namespace=False):
rougailconfig['functions_file'] = [join(dico_dirs, '../eosfunc/test.py')]
2023-10-12 08:17:30 +02:00
dirs = [join(test_dir, 'dictionaries', 'rougail')]
subfolder = join(test_dir, 'dictionaries', 'rougail2')
2019-11-23 08:17:35 +01:00
if isdir(subfolder):
2020-12-24 16:02:20 +01:00
dirs.append(subfolder)
2022-10-01 22:27:22 +02:00
rougailconfig['dictionaries_dir'] = dirs
extra_dictionaries = {}
2023-10-12 08:17:30 +02:00
extras = listdir(join(test_dir, 'dictionaries'))
extras.sort()
for extra in extras:
if extra in ['rougail', 'rougail2']:
continue
subfolder = join(test_dir, 'dictionaries', extra)
if isdir(subfolder):
extra_dictionaries[extra] = [subfolder]
if extra_dictionaries:
rougailconfig['extra_dictionaries'] = extra_dictionaries
rougailconfig['tiramisu_cache'] = get_tiramisu_filename(test_dir, 'tmp', multi, namespace)
rougailconfig['custom_types']['custom'] = CustomOption
2023-10-12 08:17:30 +02:00
return Rougail(rougailconfig)
2022-10-01 22:27:22 +02:00
def save(test_dir, eolobj, multi=False, namespace=False, error=False):
tiramisu_tmp = get_tiramisu_filename(test_dir, 'tmp', multi, namespace)
2023-10-12 08:17:30 +02:00
tiramisu_tmp_dir = dirname(tiramisu_tmp)
if isdir(tiramisu_tmp_dir):
rmtree(tiramisu_tmp_dir)
makedirs(tiramisu_tmp_dir)
tiramisu_objects = eolobj.get_config()
tiramisu_file = get_tiramisu_filename(test_dir, 'tiramisu', multi, namespace)
2023-10-12 08:17:30 +02:00
tiramisu_dir = dirname(tiramisu_file)
2024-06-19 17:36:18 +02:00
if not error:
if not isdir(tiramisu_dir):
raise Exception(f'creates {tiramisu_dir}')
2020-12-24 12:41:10 +01:00
if not isfile(tiramisu_file) or debug:
2023-10-12 08:17:30 +02:00
copyfile(tiramisu_tmp, tiramisu_file)
with open(tiramisu_tmp, 'r') as fh:
tiramisu_objects = fh.read()
2020-12-24 12:41:10 +01:00
with open(tiramisu_file, 'r') as fh:
tiramisu_objects_ori = fh.read()
assert tiramisu_objects == tiramisu_objects_ori
2024-06-19 17:36:18 +02:00
if isdir(tiramisu_tmp_dir):
rmtree(tiramisu_tmp_dir)
2019-11-23 08:17:35 +01:00
def test_dictionary(test_dir):
if not test_no_namespace:
print('NAMESPACE!')
return
2022-11-03 22:16:52 +01:00
assert getcwd() == ORI_DIR
2023-10-12 08:17:30 +02:00
test_dir_ = join(dico_dirs, test_dir)
rougailconfig = RougailConfig.copy()
rougailconfig['main_namespace'] = None
if isfile(join(test_dir_, 'force_namespace')):
return
eolobj = load_rougail_object(test_dir_, rougailconfig)
2022-11-03 22:16:52 +01:00
if not eolobj:
return
save(test_dir_, eolobj)
assert getcwd() == ORI_DIR
2022-10-01 22:27:22 +02:00
def test_dictionary_namespace(test_dir):
if not test_base:
print('BASE!')
return
assert getcwd() == ORI_DIR
test_dir_ = join(dico_dirs, test_dir)
rougailconfig = RougailConfig.copy()
rougailconfig['main_namespace'] = 'Rougail'
eolobj = load_rougail_object(test_dir_, rougailconfig, namespace=True)
if not eolobj:
return
save(test_dir_, eolobj, namespace=True)
assert getcwd() == ORI_DIR
2022-10-01 22:27:22 +02:00
def test_dictionary_multi(test_dir):
2024-06-19 17:36:18 +02:00
if not test_multi:
print('MULTI!')
return
2022-10-01 22:27:22 +02:00
assert getcwd() == ORI_DIR
2023-10-12 08:17:30 +02:00
test_dir_ = join(dico_dirs, test_dir)
rougailconfig = RougailConfig.copy()
rougailconfig['main_namespace'] = 'Rougail'
eolobj = load_rougail_object(test_dir_, rougailconfig, multi=True)
2022-11-03 22:16:52 +01:00
if not eolobj:
return
2023-10-12 08:17:30 +02:00
eolobj.add_path_prefix('1')
eolobj.add_path_prefix('2')
2022-11-03 22:16:52 +01:00
save(test_dir_, eolobj, multi=True)
assert getcwd() == ORI_DIR
2019-11-23 08:17:35 +01:00
2024-06-19 17:36:18 +02:00
def test_error_dictionary(test_dir_error):
assert getcwd() == ORI_DIR
test_dir_ = join(dico_dirs, test_dir_error)
errno = []
rougailconfig = RougailConfig.copy()
rougailconfig['main_namespace'] = 'Rougail'
2024-08-25 12:22:22 +02:00
eolobj = load_rougail_object(test_dir_, rougailconfig, namespace=True)
2024-06-19 17:36:18 +02:00
if eolobj is None:
return
for i in listdir(test_dir_):
if i.startswith('errno_'):
errno.append(int(i.split('_')[1]))
if not errno:
errno.append(0)
with raises(DictConsistencyError) as err:
save(test_dir_, eolobj, error=True)
assert err.value.errno in errno, f'expected errno: {errno}, errno: {err.value.errno}, msg: {err}'
tiramisu_tmp_dir = dirname(get_tiramisu_filename(test_dir_, 'tmp', False, True))
2024-06-19 17:36:18 +02:00
if isdir(tiramisu_tmp_dir):
rmtree(tiramisu_tmp_dir)
assert getcwd() == ORI_DIR