From 543e52a0fd143c5f13c93863fabf12b422d9c6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zachar?= Date: Tue, 27 Feb 2024 16:21:55 +0100 Subject: [PATCH] Add directive `is-leaf` to force selecting node Expected usage with virtual tests while keeping the parent test as well Fix: #221 --- docs/features.rst | 8 ++++++++ fmf/base.py | 12 +++++++----- tests/core/is-leaf/data/.fmf/version | 1 + tests/core/is-leaf/data/foo/child.fmf | 0 tests/core/is-leaf/data/foo/main.fmf | 3 +++ tests/core/is-leaf/main.fmf | 7 +++++++ tests/core/is-leaf/test.sh | 14 ++++++++++++++ 7 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 tests/core/is-leaf/data/.fmf/version create mode 100644 tests/core/is-leaf/data/foo/child.fmf create mode 100644 tests/core/is-leaf/data/foo/main.fmf create mode 100644 tests/core/is-leaf/main.fmf create mode 100755 tests/core/is-leaf/test.sh diff --git a/docs/features.rst b/docs/features.rst index 7974e756..772fd75e 100644 --- a/docs/features.rst +++ b/docs/features.rst @@ -194,6 +194,14 @@ The key content attributes are not supposed to be hard-coded in the Flexible Metadata Format but freely configurable. Multiple key content attributes (e.g. script & backend) could be used as well. +Sometimes it is necessary to select node from the metadata tree +even though it is not leaf. For example, when virtual tests are +created from a parent test but one wants to keep the parent available +as a test as well. To do so, one can set fhe following directive:: + + /: + is-leaf: true + .. _virtual: diff --git a/fmf/base.py b/fmf/base.py index 2a38806e..c0fa86e8 100644 --- a/fmf/base.py +++ b/fmf/base.py @@ -250,10 +250,12 @@ def check(value, type_, name=None): for key, value in directives.items(): if key == "inherit": check(value, bool, name="inherit") - continue - # No other directive supported - raise fmf.utils.FormatError( - f"Unknown fmf directive '{key}' in '{self.name}'.") + elif key == "is-leaf": + check(value, bool, name="is-leaf") + else: + # No other directive supported + raise fmf.utils.FormatError( + f"Unknown fmf directive '{key}' in '{self.name}'.") # Everything ok, store the directives self._directives.update(directives) @@ -572,7 +574,7 @@ def grow(self, path): def climb(self, whole=False): """ Climb through the tree (iterate leaf/all nodes) """ - if whole or not self.children: + if whole or not self.children or self._directives.get("is-leaf"): yield self for name, child in self.children.items(): for node in child.climb(whole): diff --git a/tests/core/is-leaf/data/.fmf/version b/tests/core/is-leaf/data/.fmf/version new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/tests/core/is-leaf/data/.fmf/version @@ -0,0 +1 @@ +1 diff --git a/tests/core/is-leaf/data/foo/child.fmf b/tests/core/is-leaf/data/foo/child.fmf new file mode 100644 index 00000000..e69de29b diff --git a/tests/core/is-leaf/data/foo/main.fmf b/tests/core/is-leaf/data/foo/main.fmf new file mode 100644 index 00000000..8f83fb90 --- /dev/null +++ b/tests/core/is-leaf/data/foo/main.fmf @@ -0,0 +1,3 @@ +/: + is-leaf: true +test: echo diff --git a/tests/core/is-leaf/main.fmf b/tests/core/is-leaf/main.fmf new file mode 100644 index 00000000..c0bedd79 --- /dev/null +++ b/tests/core/is-leaf/main.fmf @@ -0,0 +1,7 @@ +summary: Verify functionality of the `is-leaf` directive +description: + Sometimes the node should stay reported as a object + even though it has child nodes. Using this directive + allows it. Real life example is to keep "/test" available + after virtual tests "/test/virtual_foo" and "test/virtual_bar" + are created. diff --git a/tests/core/is-leaf/test.sh b/tests/core/is-leaf/test.sh new file mode 100755 index 00000000..ddb40396 --- /dev/null +++ b/tests/core/is-leaf/test.sh @@ -0,0 +1,14 @@ +#!/bin/bash +. /usr/share/beakerlib/beakerlib.sh || exit 1 + +rlJournalStart + rlPhaseStartTest + rlRun "pushd data" + + rlRun -s "fmf ls" + rlAssertGrep "^/foo$" $rlRun_LOG + rlAssertGrep "^/foo/child$" $rlRun_LOG + + rlRun "popd" + rlPhaseEnd +rlJournalEnd