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

Feature Request: ShouldResult property #2381

Merged
merged 11 commits into from
Apr 16, 2024

Conversation

JustinGrote
Copy link
Contributor

@JustinGrote JustinGrote commented Jul 19, 2023

Fixes #1993

PR Summary

PR Checklist

  • PR has meaningful title
  • Summary describes changes
  • PR is ready to be merged
    • If not, use the arrow next to Create Pull Request to mark it as a draft. PR can be marked Ready for review when it's ready.
  • Tests are added/update (if required)
  • Documentation is updated/added (if required)

@JustinGrote
Copy link
Contributor Author

@fflaten @nohwnd please review

Copy link
Collaborator

@fflaten fflaten left a comment

Choose a reason for hiding this comment

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

Thanks again for looking into this 👏
Can't test and fully review for a few days, but added some initial thoughts.

src/csharp/Pester/Test.cs Outdated Show resolved Hide resolved
src/csharp/Pester/Test.cs Outdated Show resolved Hide resolved
src/Pester.Runtime.ps1 Outdated Show resolved Hide resolved
src/csharp/Pester/Test.cs Outdated Show resolved Hide resolved
src/functions/assertions/Be.ps1 Outdated Show resolved Hide resolved
@JustinGrote JustinGrote changed the title Feature Request: Expected, Actual, and Because as Test Property Fields Feature Request: ShouldResult property field Jul 26, 2023
@JustinGrote JustinGrote changed the title Feature Request: ShouldResult property field Feature Request: ShouldResult property Jul 26, 2023
@JustinGrote
Copy link
Contributor Author

@fflaten @nohwnd ready for re-review.

I strong typed the result object for all Should results, and kept the info in the TargetObject without exposing it outwardly. Plugins should still be able to access the info as needed.

src/functions/assertions/PesterThrow.ps1 Show resolved Hide resolved
src/functions/assertions/PesterThrow.ps1 Show resolved Hide resolved
src/functions/assertions/PesterThrow.ps1 Show resolved Hide resolved
Succeeded = $succeeded
FailureMessage = $failureMessage
ExpectResult = @{
Copy link
Member

Choose a reason for hiding this comment

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

Here it populates the values no matter if the should fails, in some other places it only populates them when should fails. What is the desired way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Defer to @fflaten but seems to me it makes sense to not populate it if it's successful for minor performance benefit, the only use that info would be is troubleshooting (e.g. it said successful but it shouldn't have been)

Copy link
Collaborator

@fflaten fflaten Jul 27, 2023

Choose a reason for hiding this comment

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

Wouldn't be exposed on success atm. so should avoid unnecessary cost imo 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All assertions should now be adjusted to not include this property on a success. Maybe we need tests for this?

@nohwnd
Copy link
Member

nohwnd commented Jul 27, 2023

Thanks for the PR, I am slightly worried about degrading perf by re-formatting the data where we don't need to, because the number of interactive runs where the functionality will be used is way smaller than the total of runs where this won't be used, and everyone will pay the price.

Maybe conditioning it based on option, or env var, or both is the best way forward here. Some options (like CI output) already use this pattern.

And then taking care to not reformat the same data in the assertions.

Co-authored-by: Frode Flaten <3436158+fflaten@users.noreply.github.com>
@JustinGrote
Copy link
Contributor Author

JustinGrote commented Jul 27, 2023

@fflaten @nohwnd The strong typing already showed me that Should -Invoke is actually defined in Mock and not in its own assertion, and was emitting pscustomobjects from there. There's also somewhere that multiple "trues" along with the final object are being sent and that's what's causing my latest tests to fail after merging @fflaten's change.

Given this, to avoid some more potential regressions I propose that the builtin assertions I have modified will continue to emit the strong typed ShouldResult, but invoke-assertion and CreateShouldErrorRecord should still have loose pscustomobject input (via a constructor override) to facilitate both loose and strong typed objects for the targetobject field. This will also address @nohwnd's concern we might break custom Should statements that include additional metadata.

@fflaten
Copy link
Collaborator

fflaten commented Jul 27, 2023

The strong typing already showed me that Should -Invoke is actually defined in Mock and not in its own assertion

Right. Not aware of the history there. I've been considering a PR to move those, Invoke-Pester and a couple others functions not used during import to separate files to make it easier to find them and be consistent - at least for public functions.

There's also somewhere that multiple "trues" along with the final object

Sounds like a bug and another benefit of strongly typing 🙂

It should've been typed already as the output requirements are documented and cast works. There's no way to access additional properties through Should, and hopefully users would stick to either Should -MyCustomAssertion (required output) or direct calls to function MyCustomAssertion (no required output besides throw on error) consistently.

However, I guess there's a slight chance someone leverages the loose mapping to include something for ex. unit-testing MyCustomAssertion-function directly. So probably smart to keep it loose until 6.0 and/or Better Should. Specifically #1423 might shakes things up either way.

@JustinGrote
Copy link
Contributor Author

JustinGrote commented Jul 27, 2023

I dropped the CreateShouldErrorRecord method back to object (34b4744) but all builtin items will still use the result type and I'll also fix up the shouldInvoke output as well to include actual/expected.

src/functions/Mock.ps1 Outdated Show resolved Hide resolved
@JustinGrote JustinGrote requested a review from fflaten July 31, 2023 03:05
@fflaten fflaten mentioned this pull request Apr 2, 2024
5 tasks
@nohwnd
Copy link
Member

nohwnd commented Apr 12, 2024

I've run it bunch of times, it does not seem to impact the speed negatively. Okay to merge imho.

@fflaten wanna have one last look?

@nohwnd nohwnd merged commit fa6f0b2 into pester:main Apr 16, 2024
14 checks passed
@JustinGrote JustinGrote deleted the JustinGrote/issue1993 branch April 16, 2024 14:52
@JustinGrote
Copy link
Contributor Author

@nohwnd thank you!

@nohwnd
Copy link
Member

nohwnd commented Apr 18, 2024

Thank you both :)

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.

Feature Request: Expected, Actual, and Because as Test Property Fields
3 participants