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 should_error kwarg to test() #13559

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gerioldman
Copy link

This PR adds a new kwarg named 'should_error' to the test call that is similar to should_fail. It behaves the same, the only difference is that it expects error instead of fail.

The use case is that I am running some some tests where I purposefully inject failures that end up as 'Bail out!' with TAP, so the test result is expected to be error.

@eli-schwartz
Copy link
Member

should_fail is equivalent to TAP's "todo" status. How does should_error map to TAP?

@gerioldman
Copy link
Author

gerioldman commented Aug 20, 2024

@eli-schwartz

If should_fail maps to "todo", then I guess I've been abusing how should_fail works. I think should_error would map to an expected 'Bail out!', which is a test ended due to a fatal error. I guess it can be interpreted as "todo" as well.

My use case is that I have tests I intentionally inject with failures/errors and I expect the test to fail to see if the failure is caught. In which case, "failing" actually means pass, in the sense that Meson returns 0, not failing the CI job.

should_fail does this for failures, so Meson returns 0, but there is no way to do this currently for errors.

Example CI run with this PR's content:
https://gitlab.com/gergo.krisztian/opentestloader/-/jobs/7608089075

@eli-schwartz
Copy link
Member

If should_fail maps to "todo", then I guess I've been abusing how should_fail works. I think should_error would map to an expected 'Bail out!', which is a test ended due to a fatal error. I guess it can be interpreted as "todo" as well.

Semantically, a should_fail results in meson logging the test result as an "Expected Fail:" instead of an "Ok:", and if the test starts passing it is logged as an "Unexpected Pass:", again instead of an actual "Ok:".

The reason there are 3 different statuses for this is because the "Expected Fail" list is a list of things you should be reminded to take care of at some point and "Unexpected Pass:" is stuff you have fixed and need to remove the todo note so that you start checking it again.

...

On a practical level, "Expected Fail" doesn't result in a test results failure and "Unexpected Pass" does, which means setting should_fail effectively inverts the exit code.

I am discontented with the confusion this causes. I would like to see should_fail deprecated, have it renamed to expected_fail, and have a brand new success_returncode implemented for people who want to positively test (result: "Ok") that a command binary returns errors when run.

Also, probably, expected_fail should only be permitted when the test itself is run via protocol: 'exitcode'. e.g. users of TAP should be relying on emitting "todo" markers to flag this, and users who simply want to purposefully inject test failures would be better off with asserting that a return code other than 0 should be considered the "Ok" condition.

@gerioldman gerioldman force-pushed the expected_error branch 5 times, most recently from 6b62894 to 93a175b Compare September 4, 2024 14:35
@gerioldman
Copy link
Author

@eli-schwartz Is the current commit what you had in mind?

Copy link
Member

@eli-schwartz eli-schwartz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general thrust of this commit is indeed matching the high-level vision I imagined.

Some implementation notes below.

if kwargs['should_fail'] is not None and kwargs['expected_fail'] is not None:
raise InvalidArguments('Tried to use both \'should_fail\' and \'expected_fail\'')
elif kwargs['should_fail'] is not None:
mlog.deprecation('Use expected_fail instead of should_fail.', location=node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use FeatureDeprecated.single_use here, so we can gate the deprecation warning based on the project's minimum supported meson_version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually hold on, this is already covered in the KwargInfo handling via

deprecated='1.6.0', deprecated_message='Use expected_fail instead of should_fail'),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it, I thought KwargInfo handling didn't give exact location at first

Comment on lines 2249 to 2251
kwargs['expected_fail'] = kwargs['should_fail']
elif kwargs['expected_fail'] is None:
kwargs['expected_fail'] = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would set a new expected_fail = kwargs['should_fail'] variable here to avoid mutating the inputs of the current function.

/cc @dcbaker

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably a good idea, done :)

@@ -1038,6 +1039,8 @@ class TestRunExitCode(TestRun):
def complete(self) -> None:
if self.res != TestResult.RUNNING:
pass
elif self.returncode == self.success_returncode:
self.res = TestResult.OK
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given below we handle the else case as such:

else:
    self.res = TestResult.FAIL if bool(self.returncode) else TestResult.OK

Seems like a TestResult.OK is emitted for either a returncode of 0, or a returncode of an overridden success_returncode. Is this intentional?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an oversight on my part, corrected it and I added a test for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants