rougail/tests/test_template.py

76 lines
2.3 KiB
Python

from os import listdir, mkdir
from os.path import join, isdir, isfile
from shutil import rmtree
from pytest import fixture
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)
@fixture(scope="module", params=test_ok)
def test_dir(request):
return request.param
def find_files(dirname, root, files):
for filename in listdir(dirname):
abs_dirname = join(dirname, filename)
root_file = root + [filename]
if isdir(join(dirname, filename)):
find_files(abs_dirname, root_file, files)
else:
files.add(join(*root_file))
def test_dictionary(test_dir):
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')
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)
config = Config(optiondescription)
config.property.read_only()
template.generate(config,
funcs_file,
distrib_dir,
tmp_dir,
dest_dir)
if isdir(join(dest_dir, 'test')):
list_templates = set()
find_files(join(dest_dir), [], list_templates)
else:
list_templates = set()
list_results = set()
find_files(join(test_dir, 'result'), [], list_results)
assert list_templates == list_results
for result in list_results:
template_file = join(dest_dir, result)
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
rmtree(dest_dir)
rmtree(tmp_dir)