rougail/tests/test_1_flattener.py

165 lines
4.7 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 = ['01base_shorthand']
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
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
2023-10-12 08:17:30 +02:00
def get_tiramisu_filename(test_dir, subdir, multi):
if not multi:
filename = 'base.py'
else:
filename = 'multi.py'
return join(test_dir, subdir, filename)
2022-11-02 22:52:50 +01:00
2023-10-12 08:17:30 +02:00
def load_rougail_object(test_dir, multi=False):
2022-10-01 22:27:22 +02:00
rougailconfig = RougailConfig.copy()
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
rougailconfig['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):
rougailconfig['extra_dictionaries'][extra] = [subfolder]
rougailconfig['tiramisu_cache'] = get_tiramisu_filename(test_dir, 'tmp', multi)
rougailconfig['custom_types']['custom'] = CustomOption
2023-10-12 08:17:30 +02:00
return Rougail(rougailconfig)
2022-10-01 22:27:22 +02:00
2024-06-19 17:36:18 +02:00
def save(test_dir, eolobj, multi=False, error=False):
2023-10-12 08:17:30 +02:00
tiramisu_tmp = get_tiramisu_filename(test_dir, 'tmp', multi)
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)
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):
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)
eolobj = load_rougail_object(test_dir_)
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_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)
eolobj = load_rougail_object(test_dir_, 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 = []
eolobj = load_rougail_object(test_dir_)
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))
if isdir(tiramisu_tmp_dir):
rmtree(tiramisu_tmp_dir)
assert getcwd() == ORI_DIR