from pathlib import Path
from rougail import Rougail
from rougail.output_display import RougailOutputDisplay as RougailOutput

from rougail_tests.utils import get_rougail_config

EXT = {'console': 'sh', 'github': 'md'}


def test_error_mandatory_hidden():
    rougailconfig = get_rougail_config(Path("tests/errors/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    config.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors-results' / f'display.{ext}'
        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}'


def test_error_mandatory_family_hidden():
    rougailconfig = get_rougail_config(Path("tests/errors2/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    config.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors2-results' / f'display.{ext}'
        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}'


def test_error_mandatory_family():
    rougailconfig = get_rougail_config(Path("tests/errors3/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    config.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors3-results' / f'display.{ext}'
        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}'
