From 71d60540f441304f5010fd42284a0c70f9b36af9 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 12 Apr 2021 11:04:33 +0300 Subject: feat(python lib/runcmd): run commands in sub-directory --- share/python/lib/runcmd.md | 20 ++++++++++++++++++++ share/python/lib/runcmd.py | 11 ++++++++++- share/python/lib/runcmd.yaml | 8 ++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/share/python/lib/runcmd.md b/share/python/lib/runcmd.md index 4615f69..5f99b2a 100644 --- a/share/python/lib/runcmd.md +++ b/share/python/lib/runcmd.md @@ -37,6 +37,16 @@ then exit code is 0 and command is successful ~~~ +## Successful execution in a sub-directory + +~~~scenario +given a directory xyzzy +when I run, in xyzzy, /bin/pwd +then exit code is 0 +then command is successful +then stdout contains "/xyzzy" +~~~ + ## Failed execution ~~~scenario @@ -45,6 +55,15 @@ then exit code is not 0 and command fails ~~~ +## Failed execution in a sub-directory + +~~~scenario +given a directory xyzzy +when I try to run, in xyzzy, /bin/false +then exit code is not 0 +and command fails +~~~ + # Check we can prepend to $PATH This scenario verifies that we can add a directory to the beginning of @@ -187,6 +206,7 @@ template: python bindings: - runcmd.yaml - runcmd_test.yaml +- files.yaml functions: - runcmd.py - runcmd_test.py diff --git a/share/python/lib/runcmd.py b/share/python/lib/runcmd.py index a2564c6..cc4fd38 100644 --- a/share/python/lib/runcmd.py +++ b/share/python/lib/runcmd.py @@ -113,9 +113,18 @@ def runcmd_step(ctx, argv0=None, args=None): runcmd_exit_code_is_zero(ctx) +def runcmd_step_in(ctx, dirname=None, argv0=None, args=None): + runcmd_try_to_run_in(ctx, dirname=dirname, argv0=argv0, args=args) + runcmd_exit_code_is_zero(ctx) + + def runcmd_try_to_run(ctx, argv0=None, args=None): + runcmd_try_to_run_in(ctx, dirname=None, argv0=argv0, args=args) + + +def runcmd_try_to_run_in(ctx, dirname=None, argv0=None, args=None): argv = [shlex.quote(argv0)] + shlex.split(args) - runcmd_run(ctx, argv) + runcmd_run(ctx, argv, cwd=dirname) # diff --git a/share/python/lib/runcmd.yaml b/share/python/lib/runcmd.yaml index 48dde90..a5119d8 100644 --- a/share/python/lib/runcmd.yaml +++ b/share/python/lib/runcmd.yaml @@ -10,10 +10,18 @@ regex: true function: runcmd_step +- when: I run, in (?P\S+), (?P\S+)(?P.*) + regex: true + function: runcmd_step_in + - when: I try to run (?P\S+)(?P.*) regex: true function: runcmd_try_to_run +- when: I try to run, in (?P\S+), (?P\S+)(?P.*) + regex: true + function: runcmd_try_to_run_in + # Steps to examine exit code of latest command. - then: exit code is {exit} -- cgit v1.2.1