Files
tryton/.gitlab-scripts/doctest2python.py
T
Cédric Krier 406092be4d Check doctest files
They are converted to python files before running checks.
2024-01-14 19:21:56 +01:00

31 lines
880 B
Python
Executable File

#!/usr/bin/env python
import doctest
import glob
import os
from itertools import chain
from pathlib import Path
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
if __name__ == '__main__':
parser = doctest.DocTestParser()
for filename in chain(glob.glob('**/*/tests/*.rst', root_dir=ROOT_DIR)):
filename = Path(filename)
with open(filename, 'r') as scenario:
text = scenario.read()
with open(filename.with_suffix('.py'), 'w') as file:
newline = False
for line in parser.parse(text):
if isinstance(line, doctest.Example):
if newline:
file.write('\n')
file.write(line.source)
newline = False
elif not line:
pass
else:
newline = True