rougail/tests/test_upgrade.py

88 lines
2.7 KiB
Python

from shutil import rmtree, copyfile, copytree
from os import getcwd, makedirs
from os.path import isfile, join, isdir
from pytest import fixture, raises
from os import listdir
from json import load
from rougail import RougailUpgrade, RougailConfig
from rougail.error import DictConsistencyError
dico_dirs = 'tests/dictionaries_old'
dest_dir = 'tests/tmp'
test_ok = set()
for test in listdir(dico_dirs):
if isdir(join(dico_dirs, test)):
test_ok.add(test)
excludes = set([])
#excludes = set(['01base_file_utfchar'])
test_ok -= excludes
#test_ok = ['00load_autofreeze']
#test_ok = []
ORI_DIR = getcwd()
debug = False
#debug = True
test_ok = list(test_ok)
test_ok.sort()
if isdir(dest_dir):
rmtree(dest_dir)
makedirs(dest_dir)
copyfile(join(dico_dirs, '__init__.py'), join(dest_dir, '__init__.py'))
#RougailConfig['variable_namespace'] = 'configuration'
@fixture(scope="module", params=test_ok)
def test_dir(request):
return request.param
def launch_test(test_dir):
new_dest_dir = join(dest_dir, test_dir.rsplit('/', 1)[1])
makedirs(new_dest_dir)
upgrade = RougailUpgrade()
try:
upgrade.load_xml_from_folders(test_dir, new_dest_dir, RougailConfig['variable_namespace'], display=False)
except Exception as err:
# import traceback
# traceback.print_exc()
# print(f'cannot convert {test_dir}: {err}')
rmtree(new_dest_dir)
copytree(test_dir, new_dest_dir)
return
subfolder = join(test_dir, 'subfolder')
if isdir(subfolder):
makedirs(join(new_dest_dir, 'subfolder'))
upgrade.load_xml_from_folders(subfolder, join(new_dest_dir, 'subfolder'), RougailConfig['variable_namespace'], display=False)
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):
makedirs(join(new_dest_dir, 'extra_dirs', extra))
upgrade.load_xml_from_folders(subfolder, join(new_dest_dir, 'extra_dirs', extra), extra, display=False)
# copy others files
for filename in listdir(test_dir):
src_filename = join(test_dir, filename)
dst_filename = join(new_dest_dir, filename)
if filename.startswith('errno_') or filename in ['__init__.py']:
copyfile(src_filename, dst_filename)
elif filename in ['tmpl', 'makedict', 'tiramisu', 'result', 'patches']:
copytree(src_filename, dst_filename, symlinks=True)
def test_dictionary(test_dir):
assert getcwd() == ORI_DIR
test_dir = join(dico_dirs, test_dir)
launch_test(test_dir)
assert getcwd() == ORI_DIR