Skip to content

Commit

Permalink
exec-runnable-recipe resolver: add support for arguments
Browse files Browse the repository at this point in the history
This adds support for used defined arguments to be passed while
running the executables that will generate the runnables recipe JSON
content.

It gives the opportunity for either calling the executables with a
particular option that will output the runnables (instead of other
action), or tweaking the type of runnables that will be generated.

Signed-off-by: Cleber Rosa <[email protected]>
  • Loading branch information
clebergnu committed Sep 18, 2024
1 parent 3d2facb commit 458e53d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 2 deletions.
8 changes: 8 additions & 0 deletions avocado/plugins/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ def configure(self, parser):
allow_multiple=True,
)

settings.add_argparser_to_option(
namespace="resolver.exec_runnables_recipe.arguments",
metavar="ARGS",
parser=parser,
long_arg="--resolver-exec-arguments",
allow_multiple=True,
)

help_msg = "Writes runnable recipe files to a directory."
settings.register_option(
section="list.recipes",
Expand Down
21 changes: 20 additions & 1 deletion avocado/plugins/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import json
import os
import re
import shlex
import subprocess

from avocado.core.extension_manager import PluginPriority
Expand Down Expand Up @@ -218,6 +219,19 @@ def initialize(self):
help_msg=help_msg,
)

help_msg = (
"Command line options (space separated) that will be added "
"to the executable when executing it as a producer of "
"runnables-recipe JSON content."
)
settings.register_option(
section="resolver.exec_runnables_recipe",
key="arguments",
key_type=str,
default="",
help_msg=help_msg,
)


class ExecRunnablesRecipeResolver(BaseExec, Resolver):
name = "exec-runnables-recipe"
Expand All @@ -239,9 +253,14 @@ def resolve(self, reference):
if exec_criteria is not None:
return exec_criteria

args = self.config.get("resolver.exec_runnables_recipe.arguments")
if args:
cmd = [reference] + shlex.split(args)
else:
cmd = reference
try:
process = subprocess.Popen(
reference,
cmd,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand Down
8 changes: 8 additions & 0 deletions avocado/plugins/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ def configure(self, parser):
allow_multiple=True,
)

settings.add_argparser_to_option(
namespace="resolver.exec_runnables_recipe.arguments",
metavar="ARGS",
parser=parser,
long_arg="--resolver-exec-arguments",
allow_multiple=True,
)

parser_common_args.add_tag_filter_args(parser)

def run(self, config):
Expand Down
15 changes: 15 additions & 0 deletions docs/source/guides/writer/chapters/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,18 @@ run``. Example::

exec-test true-test
exec-test false-test

If the executable to be run needs arguments, you can pass it via the
``--resolver-exec-arguments`` or the underlying
``resolver.exec_runnable_recipe.arguments`` option. The following
script receives an optional parameter that can change the type of the
tests it generates:

.. literalinclude:: ../../../../../examples/nrunner/resolvers/exec_runnables_recipe_kind.sh

In order to have those tests resolved as ``tap`` tests, one can run::

$ avocado list --resolver-run-executables --resolver-exec-arguments tap examples/nrunner/resolvers/exec_runnables_recipe_kind.sh

tap true-test
tap false-test
3 changes: 3 additions & 0 deletions examples/nrunner/resolvers/exec_runnables_recipe_kind.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
kind=${1:-exec-test}
echo "[{\"kind\": \"$kind\",\"uri\": \"/bin/true\",\"identifier\": \"true-test\"},{\"kind\": \"$kind\",\"uri\": \"/bin/false\",\"identifier\": \"false-test\"}]"
2 changes: 1 addition & 1 deletion selftests/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"nrunner-requirement": 28,
"unit": 678,
"jobs": 11,
"functional-parallel": 311,
"functional-parallel": 312,
"functional-serial": 7,
"optional-plugins": 0,
"optional-plugins-golang": 2,
Expand Down
21 changes: 21 additions & 0 deletions selftests/functional/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,27 @@ def test_exec_runnable_recipe_enabled(self):
)
self.assertIn(b"exec-test: 2\n", result.stdout)

@skipUnlessPathExists("/bin/sh")
def test_exec_runnable_recipe_args(self):
resolver_path = os.path.join(
BASEDIR,
"examples",
"nrunner",
"resolvers",
"exec_runnables_recipe_kind.sh",
)
cmd_line = f"{AVOCADO} -V list --resolver-run-executables --resolver-exec-arguments tap {resolver_path}"
result = process.run(cmd_line)
self.assertIn(
b"tap true-test /bin/true exec-runnables-recipe",
result.stdout,
)
self.assertIn(
b"tap false-test /bin/false exec-runnables-recipe",
result.stdout,
)
self.assertIn(b"tap: 2\n", result.stdout)


class ResolverFunctionalTmp(TestCaseTmpDir):
def test_runnables_recipe(self):
Expand Down

0 comments on commit 458e53d

Please sign in to comment.