rougail/tests/test_template.py

92 lines
2.8 KiB
Python
Raw Normal View History

2019-12-02 10:33:46 +01:00
from os import listdir, mkdir
from os.path import join, isdir, isfile
from shutil import rmtree
2020-02-14 17:59:39 +01:00
from pytest import fixture, mark
2019-12-02 10:33:46 +01:00
from lxml.etree import parse
from tiramisu import Config
from rougail import template, load
from rougail.config import dtdfilename
template_dirs = 'tests/templates'
test_ok = listdir(template_dirs)
2020-02-14 17:59:39 +01:00
#test_ok = ['60extra_group']
2019-12-02 10:33:46 +01:00
@fixture(scope="module", params=test_ok)
def test_dir(request):
return request.param
2020-07-06 19:47:45 +02:00
def find_files(dirname: str,
root: list,
files: set,
) -> None:
2019-12-22 11:04:39 +01:00
for filename in listdir(dirname):
abs_dirname = join(dirname, filename)
root_file = root + [filename]
if isdir(join(dirname, filename)):
2020-07-06 19:47:45 +02:00
find_files(abs_dirname,
root_file,
files,
)
2019-12-22 11:04:39 +01:00
else:
files.add(join(*root_file))
2020-02-14 17:59:39 +01:00
@mark.asyncio
async def test_dictionary(test_dir):
2019-12-02 10:33:46 +01:00
test_dir = join(template_dirs, test_dir)
tmp_dir = join(test_dir, 'tmp')
funcs_file = join(template_dirs, '../eosfunc/test.py')
distrib_dir = join(test_dir, 'tmpl')
2019-12-02 10:33:46 +01:00
if isdir(tmp_dir):
rmtree(tmp_dir)
mkdir(tmp_dir)
dest_dir = join(test_dir, 'dest')
if isdir(dest_dir):
rmtree(dest_dir)
mkdir(dest_dir)
with open(join(test_dir, '00-base.xml')) as fileio:
xmlroot = parse(fileio).getroot()
optiondescription = load(xmlroot,
dtdfilename,
funcs_file)
2020-02-14 17:59:39 +01:00
config = await Config(optiondescription)
await config.property.read_only()
await template.generate(config,
funcs_file,
distrib_dir,
tmp_dir,
dest_dir,
2020-07-08 13:18:49 +02:00
dest_dir,
join(dest_dir, 'rougail.conf'),
'rougail.conf',
)
2020-02-14 17:59:39 +01:00
list_templates = set()
if isdir(dest_dir):
2020-07-06 19:47:45 +02:00
find_files(dest_dir,
[],
list_templates,
)
2019-12-22 11:04:39 +01:00
list_results = set()
2020-02-14 17:59:39 +01:00
if isdir(join(test_dir, 'result')):
2020-07-06 19:47:45 +02:00
find_files(join(test_dir, 'result'),
[],
list_results,
)
2019-12-02 10:33:46 +01:00
assert list_templates == list_results
2019-12-22 11:04:39 +01:00
for result in list_results:
template_file = join(dest_dir, result)
2019-12-02 10:33:46 +01:00
if not isfile(template_file):
raise Exception(f'{template_file} is not generated')
with open(join(test_dir, 'result', result), 'r') as fh:
result_file = fh.read()
with open(template_file, 'r') as fh:
generated_file = fh.read()
assert result_file == generated_file
2019-12-22 11:29:57 +01:00
rmtree(dest_dir)
rmtree(tmp_dir)