-
-
Notifications
You must be signed in to change notification settings - Fork 623
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
Unstable AutoMockable output when protocols have associated type #1359
Comments
@art-divin do you have any ideas on how this could be fixed? |
Hi @magauran 👋 I have recently encountered this kind of issue when a protocol has a Here is an example that fails to compile: protocol AProtocol {}
// sourcery: AutoMockable
protocol ProtocolWithAssociatedType {
associatedtype Foo
func foo(param: Foo)
} The generated mock looks like this: class ProtocolWithAssociatedTypeMock<>: ProtocolWithAssociatedTypes { <= The generic parameters <> list is empty. This causes compilation failure
typealias Foo = Any
//MARK: - foo
var fooParamFooVoidCallsCount = 0
var fooParamFooVoidCalled: Bool {
return fooParamFooVoidCallsCount > 0
}
var fooParamFooVoidReceivedParam: (Foo)?
var fooParamFooVoidReceivedInvocations: [(Foo)] = []
var fooParamFooVoidClosure: ((Foo) -> Void)?
func foo(param: Foo) {
fooParamFooVoidCallsCount += 1
fooParamFooVoidReceivedParam = param
fooParamFooVoidReceivedInvocations.append(param)
fooParamFooVoidClosure?(param)
}
} Potential fix:
|
@rokridi I think the issue is the same as #1357 The problem is not with the stencil, but with Sourcery itself. Sourcery fails to read the annotation. Whenever an AutoMockable protocol is used as generic constraint to another protocol, in 50% of the cases, Sourcery fails to read the annotation hence it does not generate mocks for it. |
What @magauran found seems sensible to me, composer is the pretty complicated and makes a bunch of assumptions and simplification and it's all written by hand (no apple support). The earliest I could look at it is after next week since I'm at NSSpain, if anyone has ideas how to fix this bit or even improve composer that would be amazing contribution 🙇 |
I've investigated this as far as I could. Bisecting shows that it was introduced by this PR. In Line 58 of
Maybe given these insights, @krzysztofzablocki or @art-divin could pick it up from here? 🙏 This is currently blocking us from updating to the latest version. Edit: Added a PR with an attempted fix here. |
@krzysztofzablocki when are you going to release this? We need it sir! 🙏 |
Hey 👋, thank you for this fantastic tool! Sourcery has significantly improved our workflow.
Issue
I've encountered an issue that first appeared in Sourcery version 2.2.5. The problem occurs during mock generation with
AutoMockable
for protocols that containassociatedtype
. The generated mocks are inconsistent — sometimes the mock is generated correctly, and other times it isn’t generated at all.Below, you can found a small code example that illustrates this behavior.
Sometimes, the mock is generated correctly:
but in about 50% of runs (without any changes to the code) mock is not generated.
Investigation
This issue appeared after commit 7153768, specifically due to changes in
ParserResultsComposed.swift
These lines insert associated types to
typeMap
in a random order, as the order inDictionary
is not guaranteed.And it seems likely that this code conflicts with
unifyTypes
function, which removes types with duplicateglobalName
from thetypeMap
.If the original type is listed before the associated type in the
typeMap
, the generation works correctly:However, if the associated type is listed before the original type, the original type gets removed from the
typeMap
, and the mock is not generated.The text was updated successfully, but these errors were encountered: