Skip to content

Conversation

@RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Aug 15, 2024

fixes #4777

test.Properties.Set will throw on a null reason:

https://github.com/nunit/nunit/blob/main/src/NUnitFramework/framework/Internal/PropertyBag.cs#L51

So I'm adding a null-check up-front. If that's not necessary or desirable I can remove it.

@RenderMichael
Copy link
Contributor Author

I was worried the stack trace might suffer from the earlier null check, but in my opinion it's been improved :)

Before:
image

After:
image

Copy link
Member

@manfred-brands manfred-brands left a comment

Choose a reason for hiding this comment

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

Thanks @RenderMichael
I think throwing an exception at the earliest opportunity is normally an improvement, but I'm not sure here.

There is a slight chance of an unexpected exception when a user did:
[Ignore(null!, Until = "2025-01-06")]

The other issues is throwing an exception in an Attribute constructor.
We had issues in the past where this stopped test discovery, instead of failing the tests itself.

@RenderMichael
Copy link
Contributor Author

That reasoning makes sense. My initial motivation was to have argument validation when I'm scanning through [Ignore]-ed methods without executing them.

I am in fact noticing a problem with the assembly scanning when adding [Ignore(null!)] on a test with a [TestCaseSource(Type, string)] attribute:
image
image

This is happening on build, even before any test has been run. I'm not sure if this is a bug that's logged somewhere.

However, this is still happening without my changes. I will remove the argument validation either way, to minimize any possible issues.

@RenderMichael RenderMichael changed the title Make IgnoreAttribute.Reason public, null check up-front Make IgnoreAttribute.Reason public Aug 15, 2024
@RenderMichael
Copy link
Contributor Author

There is a slight chance of an unexpected exception when a user did:
[Ignore(null!, Until = "2025-01-06")]

Doesn't this mean that reason can be null? There's even special handling for a null reason, and [Ignore(null, Until = ...)] is the only way to reach it:
image

Can we make reason nullable then?

@manfred-brands
Copy link
Member

Can we make reason nullable then?

I don't think it should be nullable, but don't know the reasoning behind the null handling.
Hey, I didn't even new there was an Ignore Until.
Maybe someone else from the @nunit/framework-team can remember.

Note that even if we declare it as non-nullable, if the consuming project is not nullable aware, they can specify [Ignore(null)] without getting a compile warning.

@RenderMichael
Copy link
Contributor Author

A null reason seems like an error in some cases, but valid in others.

I propose changing the IgnoreAttribute constructor to do Reason = reason ?? "", and change the check in AddIgnoreUntilReason to string.IsNullOrEmpty to retain existing behavior of avoiding a whitespace at the end.

This will change existing code which does [Ignore("", Until = "2025-01-06")] to avoid a trailing whitespace.

@OsirisTerje
Copy link
Member

I believe that will trigger a roslyn warning. Which then has to be suppressed.
IMHO, the task was to expose reason as a property. I dont think there is a need to change behaviour, even if well intended.

PS. Not at pc right now

@OsirisTerje
Copy link
Member

OsirisTerje commented Aug 15, 2024

@CharliePoole Do you remember the Until option and code around that? Never used it myself, not even aware of it, and not seen it used either.

@RenderMichael
Copy link
Contributor Author

the task was to expose reason as a property. I dont think there is a need to change behaviour, even if well intended.

In that case the PR is ready for review.

@CharliePoole
Copy link
Member

@OsirisTerje I think I was involved when it was first added but lots has changed with the introduction of nullables.

Basically, however, the intention of Ignore was to always require a reason. We got some pushback on that from users who wanted an easy way to skip a test without leaving a note behind, but we resisted it. I think the loophole that @manfred-brands points out was probably unintended, but it's possible someone thought that the existence of a date served as a reason... i.e. that something would be fixed by that date. IMO, that "something" should be given as the reason.

This, btw, is why Ignore is treated as a type of warning as opposed to tests we always want to skip, e.g. on a particular platform. A warning without any text isn't much of a warning!

Copy link
Member

@OsirisTerje OsirisTerje left a comment

Choose a reason for hiding this comment

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

Lgtm

@OsirisTerje OsirisTerje merged commit 5521bb4 into nunit:main Aug 15, 2024
@RenderMichael RenderMichael deleted the ignore-reason branch August 15, 2024 19:47
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.

Publicly expose IgnoreAttribute.Reason

4 participants