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

Add "invert_result" to test() option #14244

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions docs/markdown/snippets/invert_result.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## `invert_result` option added to test()

This is very similar to "should_fail", but does not change the test results to
"Expected Fail" / "Unexpected Pass".

"Expected Fail" and "Unexpected Pass" are useful when they communicate to
developers that additional work needs to be done, e.g. as part of Test
Driven Development or to demonstrate problems with some edge cases that need
to be addressed in the future.

In other cases, a non-zero status code is actually the whole test, e.g. a
program under test that is expected to exit with a non-zero status code when
given invalid arguments or a bad configuration file. This is the expected
behavior and hence should be reported as "Pass", not "Expected Fail".

"invert_result" and "should_fail" can be combined to produce "Unexpected Pass"
for non-zero status codes and "Expected Fail" for zero status codes.

```meson
test(
'fail if config file is missing',
executable(...),
args: [ '--config', '/path/does/not/exist' ],
invert_result: true)
```
14 changes: 13 additions & 1 deletion docs/yaml/functions/benchmark.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,19 @@ kwargs:
default: false
description: |
when true the test is considered passed if the
executable returns a non-zero return value (i.e. reports an error)
executable returns a non-zero return value (i.e. reports an error).
This replaces the test result "Ok" with "Unexpected Pass" and "Fail"
with "Expected Fail".

invert_result:
type: bool
default: false
since: 1.8
description: |
when true the test is considered passed if the executable returns a
non-zero return value (i.e. reports an error). When used together with
`should_fail` non-zero return values are reported as "Unexpected Pass"
and zero return values as "Expected Fail".

suite:
type: str | list[str]
Expand Down
3 changes: 2 additions & 1 deletion mesonbuild/backend/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ class TestSerialisation:
cmd_args: T.List[str]
env: mesonlib.EnvironmentVariables
should_fail: bool
invert_result: bool
timeout: T.Optional[int]
workdir: T.Optional[str]
extra_paths: T.List[str]
Expand Down Expand Up @@ -1301,7 +1302,7 @@ def create_test_serialisation(self, tests: T.List['Test']) -> T.List[TestSeriali
ts = TestSerialisation(t.get_name(), t.project_name, t.suite, cmd, is_cross,
exe_wrapper, self.environment.need_exe_wrapper(),
t.is_parallel, cmd_args, t_env,
t.should_fail, t.timeout, t.workdir,
t.should_fail, t.invert_result, t.timeout, t.workdir,
extra_paths, t.protocol, t.priority,
isinstance(exe, (build.Target, build.CustomTargetIndex)),
isinstance(exe, build.Executable),
Expand Down
1 change: 1 addition & 0 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,7 @@ def make_test(self, node: mparser.BaseNode,
kwargs['args'],
env,
kwargs['should_fail'],
kwargs['invert_result'],
kwargs['timeout'],
kwargs['workdir'],
kwargs['protocol'],
Expand Down
3 changes: 2 additions & 1 deletion mesonbuild/interpreter/interpreterobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ def __init__(self, name: str, project: str, suite: T.List[str],
is_parallel: bool,
cmd_args: T.List[T.Union[str, mesonlib.File, build.Target, ExternalProgram]],
env: mesonlib.EnvironmentVariables,
should_fail: bool, timeout: int, workdir: T.Optional[str], protocol: str,
should_fail: bool, invert_result: bool, timeout: int, workdir: T.Optional[str], protocol: str,
priority: int, verbose: bool):
super().__init__()
self.name = name
Expand All @@ -797,6 +797,7 @@ def __init__(self, name: str, project: str, suite: T.List[str],
self.cmd_args = cmd_args
self.env = env
self.should_fail = should_fail
self.invert_result = invert_result
self.timeout = timeout
self.workdir = workdir
self.protocol = TestProtocol.from_str(protocol)
Expand Down
1 change: 1 addition & 0 deletions mesonbuild/interpreter/kwargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BaseTest(TypedDict):

args: T.List[T.Union[str, File, build.Target, ExternalProgram]]
should_fail: bool
invert_result: bool
timeout: int
workdir: T.Optional[str]
depends: T.List[T.Union[build.CustomTarget, build.BuildTarget]]
Expand Down
1 change: 1 addition & 0 deletions mesonbuild/interpreter/type_checking.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ def link_whole_validator(values: T.List[T.Union[StaticLibrary, CustomTarget, Cus
KwargInfo('args', ContainerTypeInfo(list, (str, File, BuildTarget, CustomTarget, CustomTargetIndex, ExternalProgram)),
listify=True, default=[]),
KwargInfo('should_fail', bool, default=False),
KwargInfo('invert_result', bool, default=False, since='1.8'),
KwargInfo('timeout', int, default=30),
KwargInfo('workdir', (str, NoneType), default=None,
validator=lambda x: 'must be an absolute path' if not os.path.isabs(x) else None),
Expand Down
8 changes: 6 additions & 2 deletions mesonbuild/mtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ def __init__(self, test: TestSerialisation, test_env: T.Dict[str, str],
self.cmd: T.Optional[T.List[str]] = None
self.env = test_env
self.should_fail = test.should_fail
self.invert_result = test.invert_result
self.project = test.project_name
self.junit: T.Optional[et.ElementTree] = None
self.is_parallel = is_parallel
Expand Down Expand Up @@ -988,8 +989,11 @@ def _complete(self) -> None:
if self.res == TestResult.RUNNING:
self.res = TestResult.OK
assert isinstance(self.res, TestResult)
if self.should_fail and self.res in (TestResult.OK, TestResult.FAIL):
self.res = TestResult.UNEXPECTEDPASS if self.res is TestResult.OK else TestResult.EXPECTEDFAIL
if self.res in (TestResult.OK, TestResult.FAIL):
if self.invert_result:
self.res = TestResult.FAIL if self.res is TestResult.OK else TestResult.OK
if self.should_fail:
self.res = TestResult.UNEXPECTEDPASS if self.res is TestResult.OK else TestResult.EXPECTEDFAIL
if self.stdo and not self.stdo.endswith('\n'):
self.stdo += '\n'
if self.stde and not self.stde.endswith('\n'):
Expand Down
4 changes: 4 additions & 0 deletions test cases/common/281 inverted result/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
project('inverted result', 'c')

test('must_fail_and_does', executable('ret1', 'ret1.c'),
invert_result: true)
1 change: 1 addition & 0 deletions test cases/common/281 inverted result/ret1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int main() { return 1; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project('inverted result', 'c')

test('must_not_fail_and_does_not', executable('ret0', 'ret0.c'),
should_fail: true,
invert_result: true)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int main() { return 0; }
4 changes: 4 additions & 0 deletions test cases/failing test/7 inverted result/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
project('inverted result', 'c')

test('must_fail_but_does_not', executable('ret0', 'ret0.c'),
invert_result: true)
1 change: 1 addition & 0 deletions test cases/failing test/7 inverted result/ret0.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int main() { return 0; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project('inverted result', 'c')

test('must_not_fail_but_does', executable('ret1', 'ret1.c'),
should_fail: true,
invert_result: true)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int main() { return 1; }
Loading