rougail/tests/test_1_flattener.py

115 lines
3.2 KiB
Python
Raw Normal View History

2019-11-23 08:17:35 +01:00
from lxml import etree
from os import getcwd
2019-11-23 08:17:35 +01:00
from os.path import isfile, join, isdir
from pytest import fixture, raises
from os import listdir
from json import load
2021-02-14 10:10:48 +01:00
from rougail import RougailConvert, RougailConfig
2020-07-20 18:13:53 +02:00
from rougail.error import DictConsistencyError
2019-11-23 08:17:35 +01:00
dico_dirs = 'tests/dictionaries'
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([])
#excludes = set(['01base_file_utfchar'])
2019-11-23 08:17:35 +01:00
test_ok -= excludes
test_raise -= excludes
2021-01-23 21:15:26 +01:00
#test_ok = ['00load_autofreeze']
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()
@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
2020-04-21 09:22:45 +02:00
def launch_flattener(test_dir, test_ok=False):
2020-12-24 16:02:20 +01:00
dirs = [test_dir]
2019-11-23 08:17:35 +01:00
subfolder = join(test_dir, 'subfolder')
if isdir(subfolder):
2020-12-24 16:02:20 +01:00
dirs.append(subfolder)
2021-02-14 10:10:48 +01:00
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):
2021-02-14 10:10:48 +01:00
RougailConfig['extra_dictionaries'][extra] = [subfolder]
2019-11-24 20:25:09 +01:00
eosfunc = join(dico_dirs, '../eosfunc/test.py')
2021-02-14 10:10:48 +01:00
RougailConfig['functions_file'] = eosfunc
eolobj = RougailConvert()
tiramisu_objects = eolobj.save(None)
2020-12-24 17:37:14 +01:00
if 'children=[]' in tiramisu_objects.split('\n')[-2]:
raise Exception('empty tiramisu object?')
2020-07-29 08:59:40 +02:00
tiramisu_dir = join(test_dir, 'tiramisu')
2020-12-24 12:41:10 +01:00
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
2019-11-23 08:17:35 +01:00
def test_dictionary(test_dir):
assert getcwd() == ORI_DIR
2019-11-23 08:17:35 +01:00
test_dir = join(dico_dirs, test_dir)
2020-04-21 09:22:45 +02:00
launch_flattener(test_dir, True)
assert getcwd() == ORI_DIR
2019-11-23 08:17:35 +01:00
def test_error_dictionary(test_dir_error):
assert getcwd() == ORI_DIR
2019-11-23 08:17:35 +01:00
test_dir = join(dico_dirs, test_dir_error)
2020-12-24 12:41:10 +01:00
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:
2020-02-14 17:59:39 +01:00
launch_flattener(test_dir)
2021-01-16 08:50:11 +01:00
if err.value.errno != errno:
print(f'expected errno: {errno}, errno: {err.value.errno}')
launch_flattener(test_dir)
assert getcwd() == ORI_DIR
2019-11-23 08:17:35 +01:00
def test_no_dtd():
2021-02-14 10:10:48 +01:00
RougailConfig['dtdfilename'] = 'notexists.dtd'
2020-02-14 17:59:39 +01:00
with raises(IOError):
2021-02-14 10:10:48 +01:00
eolobj = RougailConvert()