-
-
Notifications
You must be signed in to change notification settings - Fork 387
add fail-fast guard for Kotlin inline function mocking (#1030) #1421
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
Conversation
β¦into fix/1030-inline-function-error # Conflicts: # modules/mockk/src/jvmTest/kotlin/io/mockk/it/InlineMemberFunctionFailFastTest.kt
|
Thanks for looking into this! |
| try { | ||
| val kotlinFunction = this.kotlinFunction | ||
| if (kotlinFunction != null && kotlinFunction.isInline) return true | ||
| } catch (_: Throwable) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you catching Throwable here? Can't we be more specific and catch just the Exception types that we expect to be thrown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the catch! π
I agree that catching Throwable is too broad here. Iβll narrow it down to the concrete cases we actually expect when accessing kotlinFunction (e.g., KotlinReflectionInternalError and UnsupportedOperationException).
In those cases, Iβll fall back to the @kotlin.internal.InlineOnly annotation check.
Iβll push an update shortly!
| } | ||
| } | ||
|
|
||
| private fun inlineHint(): String = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we inline (pun intended :P ) this instead of having it in a separate method, if it's used only once?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, fair point. π
I only pulled inlineHint() out so itβd be easier to tweak or remove later, but since itβs only used once, Iβm fine with just inlining it.
Iβll make that change and push an update soon.
| } | ||
|
|
||
| @Test | ||
| fun stubOfInlineFunctionViaReflectionThrowsFailFastError() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π Review Summary
This PR does a great job of improving the error messages when trying to mock Kotlin inline functions. The changes are clear and the new tests provide good coverage. I've added a couple of minor suggestions to improve code style and reduce duplication.
π General Feedback
- The new error messages are much more descriptive and will be very helpful for users.
- The addition of tests for the new behavior is excellent.
- The approach to detect inline functions seems robust.
Fixes #1030 : "Throwing a descriptive error upon encountering inline function"
Kotlin
inlinefunctions are inlined at the call site, meaning their actual method calls disappear at runtime.As a result, proxy-based mocking is fundamentally impossible.
Previously, attempts to mock inline functions would fail with vague messages like:
"Failed matching mocking signature..."or"Missing mocked calls...", making it difficult to debug.This change classifies such failures into two clear categories and provides more actionable feedback: