Skip to content

Fix non-deterministic output when using mockfiles - #365

Merged
sidepelican merged 2 commits into
uber:masterfrom
tokizuoh:fix-parent-mock-lookup-by-shared-type
Nov 14, 2026
Merged

sidepelican merged 2 commits into
uber:masterfrom
tokizuoh:fix-parent-mock-lookup-by-shared-type

Conversation

@tokizuoh

Copy link
Copy Markdown
Contributor

When --mockfiles is used, the generated output can differ between runs even though the inputs are the same.
In the following case, TargetMock gets the members of either FooMock or BarMock, depending on the run.

  • source
/// @mockable
public protocol Target: Sendable {
    func run()
}
  • mockfiles
public final class FooMock: Foo, @unchecked Sendable {
    public init() { }

    public private(set) var fooCallCount = 0
    public var fooHandler: (() -> ())?
    public func foo() {
        fooCallCount += 1
        if let fooHandler = fooHandler {
            fooHandler()
        }
    }
}

public final class BarMock: Bar, @unchecked Sendable {
    public init() { }

    public private(set) var barCallCount = 0
    public var barHandler: (() -> ())?
    public func bar() {
        barCallCount += 1
        if let barHandler = barHandler {
            barHandler()
        }
    }
}
  • actual
public final class TargetMock: Target, @unchecked Sendable {
    public init() { }

    // ...
    public func run() {
        // ...
    }
    public private(set) var barCallCount = 0
    public var barHandler: (() -> ())?
    public func bar() {
        barCallCount += 1
        if let barHandler = barHandler {
            barHandler()
        }
    }
}

On another run, foo is added instead of bar. Neither should be there in the first place.

Sendable is not in the sources, so it is looked up in parentMocksByInheritedType (added in #338). Every mock conforming to Sendable matches there, and the table keeps whichever comes last while iterating parentMocks, which is a dictionary.

I changed two things:

  • Sendable and Error are no longer looked up as a parent protocol.
  • A type inherited by more than one mock is not registered in parentMocksByInheritedType. The same problem happens with other types such as CustomStringConvertible, so this is not limited to Sendable.

One behavior change: if mockfiles have two or more mocks for the same protocol and none of them is named <Protocol>Mock, none of them is used as the parent mock. Before this change one of them was picked depending on the run.

@sidepelican

Copy link
Copy Markdown
Contributor

This change looks good, but is it still a draft because you plan to add more implementation yet?

@tokizuoh
tokizuoh marked this pull request as ready for review November 12, 2026 00:00
@tokizuoh

Copy link
Copy Markdown
Contributor Author

@sidepelican
Thanks! I was just reviewing it once more myself. Nothing more to add, so it's ready for review now.

@sidepelican sidepelican left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, good catch. Thank you.

@cla-assistant

cla-assistant Bot commented Nov 14, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ sidepelican
❌ tokizuoh
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@cla-assistant

cla-assistant Bot commented Nov 14, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ sidepelican
❌ tokizuoh
You have signed the CLA already but the status is still pending? Let us recheck it.

@sidepelican
sidepelican merged commit e86e4eb into uber:master Nov 14, 2026
11 of 12 checks passed
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