95 lines
4.4 KiB
Python
95 lines
4.4 KiB
Python
from pathlib import Path
|
|
from rougail import Rougail, RougailConfig
|
|
from rougail.user_data_yaml import RougailUserDataYaml
|
|
from rougail.output_doc import RougailOutputDoc
|
|
from json import load, dump, loads, dumps
|
|
import shutil, os
|
|
|
|
from rougail_tests.utils import config_to_dict
|
|
|
|
os.environ['COLUMNS'] = '80'
|
|
|
|
TUTORIAL_ROUGAIL = "../rougail-tutorials_builder/examples/"
|
|
TMP = Path("tutorial_tmp")
|
|
RESULT = Path("tests") / "result_tutorial"
|
|
|
|
def test_tutorial():
|
|
tmp_structural_files = TMP / "structural"
|
|
tmp_types_files = TMP / "types"
|
|
tmp_previous = TMP / "previous.json"
|
|
if TMP.is_dir():
|
|
shutil.rmtree(TMP)
|
|
tmp_structural_files.mkdir(parents=True)
|
|
tmp_types_files.mkdir()
|
|
namespace_support = False
|
|
tmp_structural_files.mkdir(exist_ok=True)
|
|
for src_exercice in sorted(Path(TUTORIAL_ROUGAIL).iterdir()):
|
|
result_files = RESULT / src_exercice.name
|
|
result_files.mkdir(parents=True, exist_ok=True)
|
|
if (src_exercice / "namespace").is_file():
|
|
namespace_support = True
|
|
src_types = src_exercice / "types"
|
|
if src_types.is_dir():
|
|
for types in src_types.iterdir():
|
|
for t in types.iterdir():
|
|
shutil.copy(t, tmp_types_files / t.name)
|
|
for src_namespace in (src_exercice / "structural_files").iterdir():
|
|
if not src_namespace.is_dir():
|
|
continue
|
|
structure_directory = tmp_structural_files / src_namespace.name
|
|
structure_directory.mkdir(exist_ok=True)
|
|
for filename in src_namespace.iterdir():
|
|
if not filename.name.endswith('.yml'):
|
|
continue
|
|
shutil.copyfile(filename, structure_directory / filename.name)
|
|
for console in [False, True]:
|
|
for changelog in [True, False]:
|
|
if changelog and not tmp_previous.is_file():
|
|
continue
|
|
if console and changelog:
|
|
continue
|
|
rougailconfig = RougailConfig.copy()
|
|
rougailconfig['step.output'] = 'doc'
|
|
if changelog:
|
|
rougailconfig["doc.contents"] = ["changelog"]
|
|
rougailconfig["doc.changelog.previous_json_file"] = str(tmp_previous)
|
|
result_doc = result_files / "doc_changelog.sh"
|
|
rougailconfig['doc.output_format'] = 'console'
|
|
elif console:
|
|
result_doc = result_files / "doc.sh"
|
|
rougailconfig['doc.output_format'] = 'console'
|
|
else:
|
|
result_doc = result_files / "doc.json"
|
|
rougailconfig['doc.output_format'] = 'json'
|
|
if namespace_support:
|
|
rougailconfig['main_namespace'] = 'firefox'
|
|
rougailconfig['main_structural_directories'] = [str(tmp_structural_files / "firefox")]
|
|
if (tmp_structural_files / 'foxyproxy').is_dir():
|
|
rougailconfig['extra_namespaces'] = {"foxyproxy": [str(tmp_structural_files / "foxyproxy")]} # extra_namespaces
|
|
else:
|
|
rougailconfig['main_namespace'] = None
|
|
rougailconfig['main_structural_directories'] = [str(structure_directory)]
|
|
types = [str(t) for t in tmp_types_files.iterdir()]
|
|
if types:
|
|
rougailconfig['types'] = types
|
|
tiramisu_tmp = TMP / "tiramisu.py"
|
|
try:
|
|
rougailconfig['tiramisu_cache'] = str(tiramisu_tmp)
|
|
except:
|
|
rougailconfig['cli.tiramisu_cache'] = str(tiramisu_tmp)
|
|
#
|
|
rougail = Rougail(rougailconfig)
|
|
config = rougail.run()
|
|
doc = RougailOutputDoc(config, rougailconfig=rougailconfig)
|
|
generated_output = doc.run()[1]
|
|
if not result_doc.is_file():
|
|
if not result_doc.parent.is_dir():
|
|
result_doc.parent.mkdir()
|
|
with result_doc.open('w') as outfh:
|
|
outfh.write(generated_output)
|
|
with result_doc.open() as outfh:
|
|
attented_output = outfh.read()
|
|
assert generated_output == attented_output, f'filename {result_doc}'
|
|
if not changelog and not console:
|
|
shutil.copy(result_doc, tmp_previous)
|
|
shutil.rmtree(TMP)
|