From bff6f54a403411e7730b405fa7ff0de81caf9f44 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 9 Oct 2020 10:21:24 +0300 Subject: refactor: move Subplot bindings, functions files to subplot/ This makes the source tree a little cleaner, I find. --- jt.md | 8 +++--- jt.py | 28 ------------------- jt.yaml | 16 ----------- runcmd.py | 77 ----------------------------------------------------- runcmd.yaml | 13 --------- subplot/jt.py | 28 +++++++++++++++++++ subplot/jt.yaml | 16 +++++++++++ subplot/runcmd.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ subplot/runcmd.yaml | 13 +++++++++ 9 files changed, 138 insertions(+), 138 deletions(-) delete mode 100644 jt.py delete mode 100644 jt.yaml delete mode 100644 runcmd.py delete mode 100644 runcmd.yaml create mode 100644 subplot/jt.py create mode 100644 subplot/jt.yaml create mode 100644 subplot/runcmd.py create mode 100644 subplot/runcmd.yaml diff --git a/jt.md b/jt.md index c8beace..fd867db 100644 --- a/jt.md +++ b/jt.md @@ -62,9 +62,9 @@ author: - Lars Wirzenius - Daniel Silverstone bindings: -- jt.yaml -- runcmd.yaml +- subplot/jt.yaml +- subplot/runcmd.yaml functions: -- jt.py -- runcmd.py +- subplot/jt.py +- subplot/runcmd.py ... diff --git a/jt.py b/jt.py deleted file mode 100644 index 2c5a4fc..0000000 --- a/jt.py +++ /dev/null @@ -1,28 +0,0 @@ -import logging -import os - - -def run_jt_init(ctx, dirname=None, journalname=None, title=None): - runcmd(ctx, [_binary("jt"), "init", dirname, journalname, title]) - - -def run_jt_list_journals(ctx): - runcmd(ctx, [_binary("jt"), "list-journals"]) - - -def run_jt_is_journal(ctx, dirname=None): - runcmd(ctx, [_binary("jt"), "is-journal", dirname]) - - -def _binary(name): - return os.path.join(srcdir, "target", "debug", "jt2") - - -def is_directory(ctx, dirname=None): - logging.debug("checking if %r is a directory", dirname) - assert_eq(os.path.isdir(dirname), True) - - -def output_contains(ctx, pattern=None): - logging.debug("checking if %r contains", ctx["stdout"], pattern) - assert_eq(pattern in ctx["stdout"], True) diff --git a/jt.yaml b/jt.yaml deleted file mode 100644 index 863dbeb..0000000 --- a/jt.yaml +++ /dev/null @@ -1,16 +0,0 @@ -- when: I run jt init (?P\S+) (?P\S+) "(?P.*)" - regex: true - function: run_jt_init - -- when: I run jt list-journals - function: run_jt_list_journals - -- when: I run jt is-journal {dirname} - function: run_jt_is_journal - -- then: directory {dirname} exists - function: is_directory - -- then: output contains "(?P<pattern>.*)" - regex: true - function: output_contains diff --git a/runcmd.py b/runcmd.py deleted file mode 100644 index 7193c15..0000000 --- a/runcmd.py +++ /dev/null @@ -1,77 +0,0 @@ -# Some step implementations for running commands and capturing the result. - -import subprocess - - -# Run a command, capture its stdout, stderr, and exit code in context. -def runcmd(ctx, argv, **kwargs): - p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs) - stdout, stderr = p.communicate("") - ctx["argv"] = argv - ctx["stdout"] = stdout.decode("utf-8") - ctx["stderr"] = stderr.decode("utf-8") - ctx["exit"] = p.returncode - - -# Check that latest exit code captured by runcmd was a specific one. -def exit_code_is(ctx, wanted): - if ctx.get("exit") != wanted: - print("context:", ctx.as_dict()) - assert_eq(ctx.get("exit"), wanted) - - -# Check that latest exit code captured by runcmd was not a specific one. -def exit_code_is_not(ctx, unwanted): - if ctx.get("exit") == unwanted: - print("context:", ctx.as_dict()) - assert_ne(ctx.get("exit"), unwanted) - - -# Check that latest exit code captured by runcmd was zero. -def exit_code_zero(ctx): - exit_code_is(ctx, 0) - - -# Check that latest exit code captured by runcmd was not zero. -def exit_code_nonzero(ctx): - exit_code_is_not(ctx, 0) - - -# Check that stdout of latest runcmd contains a specific string. -def stdout_contains(ctx, pattern=None): - stdout = ctx.get("stdout", "") - if pattern not in stdout: - print("pattern:", repr(pattern)) - print("stdout:", repr(stdout)) - print("ctx:", ctx.as_dict()) - assert_eq(pattern in stdout, True) - - -# Check that stdout of latest runcmd does not contain a specific string. -def stdout_does_not_contain(ctx, pattern=None): - stdout = ctx.get("stdout", "") - if pattern in stdout: - print("pattern:", repr(pattern)) - print("stdout:", repr(stdout)) - print("ctx:", ctx.as_dict()) - assert_eq(pattern not in stdout, True) - - -# Check that stderr of latest runcmd does contains a specific string. -def stderr_contains(ctx, pattern=None): - stderr = ctx.get("stderr", "") - if pattern not in stderr: - print("pattern:", repr(pattern)) - print("stderr:", repr(stderr)) - print("ctx:", ctx.as_dict()) - assert_eq(pattern in stderr, True) - - -# Check that stderr of latest runcmd does not contain a specific string. -def stderr_does_not_contain(ctx, pattern=None): - stderr = ctx.get("stderr", "") - if pattern not in stderr: - print("pattern:", repr(pattern)) - print("stderr:", repr(stderr)) - print("ctx:", ctx.as_dict()) - assert_eq(pattern not in stderr, True) diff --git a/runcmd.yaml b/runcmd.yaml deleted file mode 100644 index 02e5ee1..0000000 --- a/runcmd.yaml +++ /dev/null @@ -1,13 +0,0 @@ -- then: exit code is non-zero - function: exit_code_nonzero - -- then: output matches /(?P<pattern>.+)/ - function: stdout_contains - regex: true - -- then: stderr matches /(?P<pattern>.+)/ - function: stderr_contains - regex: true - -- then: program finished successfully - function: exit_code_zero diff --git a/subplot/jt.py b/subplot/jt.py new file mode 100644 index 0000000..2c5a4fc --- /dev/null +++ b/subplot/jt.py @@ -0,0 +1,28 @@ +import logging +import os + + +def run_jt_init(ctx, dirname=None, journalname=None, title=None): + runcmd(ctx, [_binary("jt"), "init", dirname, journalname, title]) + + +def run_jt_list_journals(ctx): + runcmd(ctx, [_binary("jt"), "list-journals"]) + + +def run_jt_is_journal(ctx, dirname=None): + runcmd(ctx, [_binary("jt"), "is-journal", dirname]) + + +def _binary(name): + return os.path.join(srcdir, "target", "debug", "jt2") + + +def is_directory(ctx, dirname=None): + logging.debug("checking if %r is a directory", dirname) + assert_eq(os.path.isdir(dirname), True) + + +def output_contains(ctx, pattern=None): + logging.debug("checking if %r contains", ctx["stdout"], pattern) + assert_eq(pattern in ctx["stdout"], True) diff --git a/subplot/jt.yaml b/subplot/jt.yaml new file mode 100644 index 0000000..863dbeb --- /dev/null +++ b/subplot/jt.yaml @@ -0,0 +1,16 @@ +- when: I run jt init (?P<dirname>\S+) (?P<journalname>\S+) "(?P<title>.*)" + regex: true + function: run_jt_init + +- when: I run jt list-journals + function: run_jt_list_journals + +- when: I run jt is-journal {dirname} + function: run_jt_is_journal + +- then: directory {dirname} exists + function: is_directory + +- then: output contains "(?P<pattern>.*)" + regex: true + function: output_contains diff --git a/subplot/runcmd.py b/subplot/runcmd.py new file mode 100644 index 0000000..7193c15 --- /dev/null +++ b/subplot/runcmd.py @@ -0,0 +1,77 @@ +# Some step implementations for running commands and capturing the result. + +import subprocess + + +# Run a command, capture its stdout, stderr, and exit code in context. +def runcmd(ctx, argv, **kwargs): + p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs) + stdout, stderr = p.communicate("") + ctx["argv"] = argv + ctx["stdout"] = stdout.decode("utf-8") + ctx["stderr"] = stderr.decode("utf-8") + ctx["exit"] = p.returncode + + +# Check that latest exit code captured by runcmd was a specific one. +def exit_code_is(ctx, wanted): + if ctx.get("exit") != wanted: + print("context:", ctx.as_dict()) + assert_eq(ctx.get("exit"), wanted) + + +# Check that latest exit code captured by runcmd was not a specific one. +def exit_code_is_not(ctx, unwanted): + if ctx.get("exit") == unwanted: + print("context:", ctx.as_dict()) + assert_ne(ctx.get("exit"), unwanted) + + +# Check that latest exit code captured by runcmd was zero. +def exit_code_zero(ctx): + exit_code_is(ctx, 0) + + +# Check that latest exit code captured by runcmd was not zero. +def exit_code_nonzero(ctx): + exit_code_is_not(ctx, 0) + + +# Check that stdout of latest runcmd contains a specific string. +def stdout_contains(ctx, pattern=None): + stdout = ctx.get("stdout", "") + if pattern not in stdout: + print("pattern:", repr(pattern)) + print("stdout:", repr(stdout)) + print("ctx:", ctx.as_dict()) + assert_eq(pattern in stdout, True) + + +# Check that stdout of latest runcmd does not contain a specific string. +def stdout_does_not_contain(ctx, pattern=None): + stdout = ctx.get("stdout", "") + if pattern in stdout: + print("pattern:", repr(pattern)) + print("stdout:", repr(stdout)) + print("ctx:", ctx.as_dict()) + assert_eq(pattern not in stdout, True) + + +# Check that stderr of latest runcmd does contains a specific string. +def stderr_contains(ctx, pattern=None): + stderr = ctx.get("stderr", "") + if pattern not in stderr: + print("pattern:", repr(pattern)) + print("stderr:", repr(stderr)) + print("ctx:", ctx.as_dict()) + assert_eq(pattern in stderr, True) + + +# Check that stderr of latest runcmd does not contain a specific string. +def stderr_does_not_contain(ctx, pattern=None): + stderr = ctx.get("stderr", "") + if pattern not in stderr: + print("pattern:", repr(pattern)) + print("stderr:", repr(stderr)) + print("ctx:", ctx.as_dict()) + assert_eq(pattern not in stderr, True) diff --git a/subplot/runcmd.yaml b/subplot/runcmd.yaml new file mode 100644 index 0000000..02e5ee1 --- /dev/null +++ b/subplot/runcmd.yaml @@ -0,0 +1,13 @@ +- then: exit code is non-zero + function: exit_code_nonzero + +- then: output matches /(?P<pattern>.+)/ + function: stdout_contains + regex: true + +- then: stderr matches /(?P<pattern>.+)/ + function: stderr_contains + regex: true + +- then: program finished successfully + function: exit_code_zero -- cgit v1.2.1