from behave import *
from typing import Optional
import subprocess

try:
    from steps.common import Case
except ModuleNotFoundError:
    from common import Case



def invoke(context, args: Optional[list[str]]):
    case: Case = context.case
    if args is None:
        args = []
    case.result = subprocess.run(
        [context.bin, context.case.infile] + args,
        capture_output=True,
        env=context.env,
        encoding="utf-8"
    )
    print("Return code: {code}\nStdOut: {out}\nStdErr: {err}".format(
        code=case.result.returncode,
        out=case.result.stdout,
        err=case.result.stderr
    ))


@when(u'allmytoes is started without further arguments')
def step_allmytoes_is_started(context):
    invoke(context, None)


@when(u'allmytoes is started with arguments {arguments}')
def step_allmytoes_is_started_with_arguments(context, arguments):
    invoke(context, arguments.split(" "))
