rougail/tests/test_1_flattener.py
Emmanuel Garette 2ca7b9a6fc personaze mode
2021-02-22 19:28:51 +01:00

108 lines
3 KiB
Python

from lxml import etree
from os import getcwd
from os.path import isfile, join, isdir
from pytest import fixture, raises
from os import listdir
from json import load
from rougail import RougailConvert, RougailConfig
from rougail.error import DictConsistencyError
dico_dirs = 'tests/dictionaries'
test_ok = set()
test_raise = set()
for test in listdir(dico_dirs):
if isdir(join(dico_dirs, test)):
if isdir(join(dico_dirs, test, 'tiramisu')):
test_ok.add(test)
elif test != '__pycache__':
test_raise.add(test)
excludes = set([])
#excludes = set(['01base_file_utfchar'])
test_ok -= excludes
test_raise -= excludes
#test_ok = ['00load_autofreeze']
#test_ok = []
#test_raise = ['80auto_autofreeze']
#test_raise = []
ORI_DIR = getcwd()
debug = False
#debug = True
test_ok = list(test_ok)
test_raise = list(test_raise)
test_ok.sort()
test_raise.sort()
@fixture(scope="module", params=test_ok)
def test_dir(request):
return request.param
@fixture(scope="module", params=test_raise)
def test_dir_error(request):
return request.param
def launch_flattener(test_dir, test_ok=False):
dirs = [test_dir]
subfolder = join(test_dir, 'subfolder')
if isdir(subfolder):
dirs.append(subfolder)
RougailConfig['dictionaries_dir'] = dirs
RougailConfig['extra_dictionaries'] = {}
if isdir(join(test_dir, 'extra_dirs')):
extras = listdir(join(test_dir, 'extra_dirs'))
extras.sort()
for extra in extras:
subfolder = join(test_dir, 'extra_dirs', extra)
if isdir(subfolder):
RougailConfig['extra_dictionaries'][extra] = [subfolder]
eosfunc = join(dico_dirs, '../eosfunc/test.py')
RougailConfig['functions_file'] = eosfunc
eolobj = RougailConvert()
tiramisu_objects = eolobj.save(None)
if 'children=[]' in tiramisu_objects.split('\n')[-2]:
raise Exception('empty tiramisu object?')
tiramisu_dir = join(test_dir, 'tiramisu')
if isdir(tiramisu_dir):
tiramisu_file = join(tiramisu_dir, 'base.py')
if not isfile(tiramisu_file) or debug:
with open(tiramisu_file, 'w') as fh:
fh.write(tiramisu_objects)
with open(tiramisu_file, 'r') as fh:
tiramisu_objects_ori = fh.read()
assert tiramisu_objects == tiramisu_objects_ori
def test_dictionary(test_dir):
assert getcwd() == ORI_DIR
test_dir = join(dico_dirs, test_dir)
launch_flattener(test_dir, True)
assert getcwd() == ORI_DIR
def test_error_dictionary(test_dir_error):
assert getcwd() == ORI_DIR
test_dir = join(dico_dirs, test_dir_error)
errno = 0
for i in listdir(test_dir):
if i.startswith('errno_'):
if errno:
raise Exception('multiple errno')
errno = int(i.split('_')[1])
with raises(DictConsistencyError) as err:
launch_flattener(test_dir)
if err.value.errno != errno:
print(f'expected errno: {errno}, errno: {err.value.errno}')
launch_flattener(test_dir)
assert getcwd() == ORI_DIR