Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error if malformed test metadata is given #1905

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/test/script/data/.fmf/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
9 changes: 9 additions & 0 deletions tests/test/script/data/tests/foo.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/pos:
summary: Normal test metadata should be accepted
test: 'false'
framework: shell

/neg:
summary: Malformed test metadata should be rejected
test: false
framework: shell
1 change: 1 addition & 0 deletions tests/test/script/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
summary: Verify test metadata should be string
23 changes: 23 additions & 0 deletions tests/test/script/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
. /usr/share/beakerlib/beakerlib.sh || exit 1

rlJournalStart
rlPhaseStartSetup
rlRun "pushd data"
rlRun "set -o pipefail"
rlPhaseEnd

rlPhaseStartTest "Normal test metadata"
rlRun -s "tmt tests ls /pos" 0
rlAssertGrep "/tests/foo/pos" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Malformed test metadata"
rlRun -s "tmt tests ls /neg" "1-255"
rlAssertGrep "Field '/tests/foo/neg:test' must be a string, 'bool' found" $rlRun_LOG
rlPhaseEnd

rlPhaseStartCleanup
rlRun "popd"
rlPhaseEnd
rlJournalEnd
6 changes: 3 additions & 3 deletions tmt/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
from tmt.lint import LinterOutcome, LinterReturn
from tmt.result import Result, ResultOutcome
from tmt.utils import (Command, EnvironmentType, FmfContextType, Path,
ShellScript, WorkdirArgumentType, field, verdict)
ShellScript, WorkdirArgumentType, field,
normalize_shell_script, verdict)

if sys.version_info >= (3, 8):
from typing import Literal, TypedDict
Expand Down Expand Up @@ -911,8 +912,7 @@ class Test(
# `test` is mandatory, must exist, so how to initialize if it's missing :(
test: Optional[ShellScript] = field(
default=None,
normalize=lambda key_address, raw_value, logger:
None if raw_value is None else ShellScript(raw_value))
normalize=normalize_shell_script)
path: Optional[Path] = field(
default=None,
normalize=lambda key_address, raw_value, logger:
Expand Down
19 changes: 19 additions & 0 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4391,6 +4391,25 @@ def normalize_shell_script_list(
raise NormalizationError(key_address, value, 'a string or a list of strings')


def normalize_shell_script(
key_address: str,
value: Union[None, str],
logger: tmt.log.Logger) -> Optional[ShellScript]:
"""
Normalize a single shell script input that may be unset.

:param value: input value from key source.
"""
happz marked this conversation as resolved.
Show resolved Hide resolved

if value is None:
return None

if isinstance(value, str):
return ShellScript(value)

raise NormalizationError(key_address, value, 'a string')


def normalize_fmf_context(
key_address: str,
value: Optional[Dict[Any, Any]],
Expand Down