From cd91e959daa2c21787c5fe878f1a4072d89b18d1 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 2 Jun 2019 13:25:15 +0300 Subject: Add: fable.py scaffolding for running scenarios --- echo.py | 40 ++++++++++++++++------------------------ fable.py | 32 ++++++++++++++++++++++++++++++++ ftt-codegen | 14 ++++++++++---- 3 files changed, 58 insertions(+), 28 deletions(-) create mode 100644 fable.py diff --git a/echo.py b/echo.py index abc0e52..7a5f186 100644 --- a/echo.py +++ b/echo.py @@ -1,43 +1,35 @@ import subprocess -context = {} - -def _save(name, value): - context[name] = value - -def _get(name): - return context[name] - def assertEqual(a, b): if a != b: raise Exception( 'expected {!r} == {!r}, but was disappointed'.format(a, b)) -def _run_echo(args): +def _run_echo(ctx, args): cmd = '/bin/echo' p = subprocess.Popen( [cmd] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) out, err = p.communicate() - context["stdout"] = out - context["stderr"] = err - context["exit_code"] = p.returncode + ctx["stdout"] = out + ctx["stderr"] = err + ctx["exit_code"] = p.returncode -def run_echo_without_args(): - _run_echo([]) +def run_echo_without_args(ctx): + _run_echo(ctx, []) -def run_echo_with_args(args=None): - _run_echo(args.split()) +def run_echo_with_args(ctx, args=None): + _run_echo(ctx, args.split()) -def exit_code_is_zero(exit_code=None): +def exit_code_is_zero(ctx, exit_code=None): exit_code = int(exit_code) - assertEqual(_get("exit_code"), exit_code) + assertEqual(ctx['exit_code'], exit_code) -def stdout_is_a_newline(): - assertEqual(_get('stdout'), '\n') +def stdout_is_a_newline(ctx): + assertEqual(ctx['stdout'], '\n') -def stdout_is_text(text=None): - assertEqual(_get('stdout'), text + '\n') +def stdout_is_text(ctx, text=None): + assertEqual(ctx['stdout'], text + '\n') -def stderr_is_empty(): - assertEqual(_get('stderr'), '') +def stderr_is_empty(ctx): + assertEqual(ctx['stderr'], '') diff --git a/fable.py b/fable.py new file mode 100644 index 0000000..71c949a --- /dev/null +++ b/fable.py @@ -0,0 +1,32 @@ +import sys + +class Context: + + def __init__(self): + self._vars = {} + + def get(self, key, default=None): + return self._vars.get(key, default) + + def __getitem__(self, key): + return self._vars[key] + + def __setitem__(self, key, value): + self._vars[key] = value + + +class Run: + + def __init__(self): + self._name = None + self._context = None + + def get_context(self): + return self._context + + def start(self, name): + self._context = Context() + self._name = name + + def end(self): + sys.stdout.write('OK: {}\n'.format(self._name)) diff --git a/ftt-codegen b/ftt-codegen index ef6e3b9..f8a57b4 100755 --- a/ftt-codegen +++ b/ftt-codegen @@ -8,6 +8,12 @@ import CommonMark_bkrs as CommonMark import yaml +hardcoded = ''' +import fable +run = fable.Run() +''' + + class Scenario: def __init__(self): @@ -113,7 +119,7 @@ def codegen(f, step, bindings): for arg in args: f.write('"{}": "{}"\n'.format(arg, args[arg])) f.write('}\n') - f.write('{}(**args)\n'.format(function)) + f.write('{}(run.get_context(), **args)\n'.format(function)) def debug(msg): if False: @@ -126,6 +132,7 @@ bindings = yaml.safe_load(open(sys.argv[1])) debug('reading prelude') prelude = open(sys.argv[2]).read() +sys.stdout.write(hardcoded) sys.stdout.write(prelude) debug('reading inputs') @@ -147,10 +154,9 @@ for s in fable.get_scenarios(): f = sys.stdout for s in scenarios: name = s.get_name() - debug('scenario: {}'.format(name)) + f.write('run.start("{}")\n'.format(name)) for step in s.get_steps(): - debug(' step: {}'.format(step)) codegen(f, step, bindings) - f.write('print("OK: %s" % "{}")\n'.format(name)) + f.write('run.end()\n') debug('ok') -- cgit v1.2.1