rougail/tests/test_1_flattener.py

158 lines
4.4 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
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)
excludes = set([])
2023-10-12 08:17:30 +02:00
print('FIXME')
excludes = set([
'80leadership_subfamily',
'80valid_enum_variables',
])
2019-11-23 08:17:35 +01:00
test_ok -= excludes
test_raise -= excludes
2023-10-12 08:17:30 +02:00
#test_ok = ['01base_domainname_params']
2020-12-22 21:11:14 +01:00
#test_ok = []
2021-01-06 22:20:33 +01:00
#test_raise = ['80auto_autofreeze']
2020-12-24 17:37:14 +01:00
#test_raise = []
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)
return Rougail(rougailconfig)
2022-10-01 22:27:22 +02:00
def save(test_dir, eolobj, multi=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)
2020-12-24 12:41:10 +01:00
if isdir(tiramisu_dir):
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
2023-10-12 08:17:30 +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):
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
2023-10-12 08:17:30 +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:
# launch_flattener(eolobj)
# save(test_dir, eolobj)
# msg = str(err)
# assert err.value.errno in errno, f'expected errno: {errno}, errno: {err.value.errno}, msg: {err}'
# assert getcwd() == ORI_DIR