rougail-output-doc/tests/test_load.py

153 lines
6 KiB
Python
Raw Normal View History

2024-12-02 20:21:31 +01:00
import os
2025-05-04 14:29:51 +02:00
import warnings
from json import dumps, loads
2024-07-10 21:27:48 +02:00
from pytest import fixture # , raises
from pathlib import Path
2025-05-04 14:29:51 +02:00
from contextlib import chdir
2024-12-02 20:21:31 +01:00
from rougail import Rougail
from rougail.output_doc import RougailOutputDoc as RougailOutput
2024-07-10 21:27:48 +02:00
2024-12-02 20:21:31 +01:00
from rougail_tests.utils import get_structures_list, get_rougail_config, get_values_for_config
2024-07-10 21:27:48 +02:00
2025-05-04 14:29:51 +02:00
HERE = Path(__file__).parent
2024-12-02 20:21:31 +01:00
excludes = []
excludes = [
2024-07-10 21:27:48 +02:00
'60_5family_dynamic_unknown_suffix',
'60_5family_dynamic_variable_outside_sub_suffix',
2024-12-02 20:21:31 +01:00
]
test_ok = get_structures_list(excludes)
2025-11-21 08:15:00 +01:00
# test_ok = [HERE.parent.parent / "rougail-tests" / "structures" / "00_2default_calculated_variable"]
2024-12-02 20:21:31 +01:00
os.environ['COLUMNS'] = '80'
2024-07-10 21:27:48 +02:00
2024-12-02 20:21:31 +01:00
def idfn(fixture_value):
return fixture_value.name
2024-07-10 21:27:48 +02:00
2024-12-02 20:21:31 +01:00
@fixture(scope="module", params=test_ok, ids=idfn)
2024-07-10 21:27:48 +02:00
def test_dir(request):
return request.param
2025-10-14 13:50:02 +02:00
EXT = {'github': 'md', 'asciidoc': 'adoc', 'json': 'json', 'console': 'sh', 'gitlab': 'gitlab.md', "html": "html"}
#EXT = {'console': 'sh'}
def gen_cases(ext_name):
# if ext_name != "json":
# yield [False, True, True, False]
# return
for without_family in [False, True]:
for comment in [False, True]:
for examples in [False, True]:
if comment and not examples:
continue
if examples and ext_name == "json":
continue
if without_family and examples:
continue
for changelog in [False, True]:
if changelog and examples:
continue
if not without_family and changelog:
continue
if changelog and ext_name == "json":
continue
yield without_family, comment, examples, changelog
def _test_structural_files(test_dir, namespace):
2025-05-04 14:29:51 +02:00
with chdir(HERE):
rougailconfig = get_rougail_config(test_dir, namespace, relative_to=HERE)
if not rougailconfig:
return
rougailconfig['tiramisu_cache'] = 'p.py'
##################################
rougailconfig['step.output'] = 'doc'
doc = None
for ext_name, ext in EXT.items():
rougailconfig['doc.contents'] = []
rougailconfig['doc.output_format'] = ext_name
for without_family, comment, examples, changelog in gen_cases(ext_name):
# print(f"ext_name: {ext_name}, without_family: {without_family}, comment: {comment}, examples: {examples}, changelog: {changelog}")
if changelog:
rougailconfig["doc.contents"] = ["changelog"]
rougailconfig["doc.previous_json_file"] = str(HERE / "empty" / "out.json")
# FIXME
rougailconfig['doc.without_family'] = True
elif examples:
rougailconfig['doc.contents'] = ['example']
if comment:
rougailconfig["doc.comment_examples"] = True
else:
rougailconfig["doc.comment_examples"] = False
else:
rougailconfig['doc.contents'] = ['variables']
if without_family:
rougailconfig['doc.without_family'] = True
else:
rougailconfig['doc.without_family'] = False
if doc is None:
with warnings.catch_warnings(record=True) as rougail_wn:
warnings.simplefilter("always", UserWarning)
rougail = Rougail(rougailconfig)
config = rougail.run()
doc = RougailOutput(config, rougailconfig=rougailconfig)
with warnings.catch_warnings(record=True) as wn:
warnings.simplefilter("always", UserWarning)
generated_output = doc.run()[1]
warns = [str(w.message) for w in rougail_wn]
for w in wn:
m = str(w.message)
if m not in warns:
warns.append(m)
##################################
dir_name = 'test'
if namespace:
dir_name += '_namespace'
elif (test_dir / 'force_namespace').is_file():
continue
##################################
if examples:
dir_name += '_examples'
if comment:
dir_name += '_comment'
if without_family:
dir_name += '_without_family'
test_file = test_dir.name
if changelog:
test_file += ".changelog"
##################################
output_file = HERE / 'results' / dir_name / (test_file + "." + ext)
# print('====', output_file)
if not output_file.is_file():
if not output_file.parent.is_dir():
output_file.parent.mkdir()
with output_file.open('w') as outfh:
outfh.write(generated_output)
with output_file.open() as outfh:
attented_output = outfh.read()
assert generated_output == attented_output, f'filename {output_file}'
#
output_file = HERE / 'results' / dir_name / ("warnings_" + test_dir.name)
if not output_file.is_file():
if not output_file.parent.is_dir():
output_file.parent.mkdir()
with output_file.open('w') as outfh:
outfh.write(dumps(warns))
with output_file.open() as outfh:
attented_output = loads(outfh.read())
assert warns == attented_output, f'filename {output_file}'
def test_structural_files(test_dir):
_test_structural_files(test_dir, False)
def test_no_namespace_structural_files(test_dir):
_test_structural_files(test_dir, True)