Brightify / Cuckoo

Boilerplate-free mocking framework for Swift!
MIT License
1.67k stars 174 forks source link

Provide option to skip generating mocks for parent protocols outside package #409

Closed juyan closed 6 months ago

juyan commented 2 years ago

Overview

It looks like the generated mock file will contain mocks for inherited protocols by design. Can we improve the generator to exclude them? Or is there a way to do this that I'm unaware of.

Example

Swift Package "Base":

public protocol BaseProtocol {
    func getBar() -> Int
}

Swift Package "Middle":

public protocol MiddleProtocol: BaseProtocol {
    func getFoo() -> Int
}

Now run the generator using the following command:

cuckoo_generator generate --glob Base/Sources/Base/* --glob Middle/Sources/Middle/* --output Middle/Tests/MiddleTests/MiddleTestMocks.swift --testable Middle

We would get both MockMiddleProtocol and MockBaseProtocol inside the file MiddleTestMocks.swift.

Expected Behavior

Using the above example, I'm looking for a way to not contain MockBaseProtocol inside BaseMiddleMocks. This can probably be configurable via a new command line argument like supportGlob. It indicates the file necessary to generate the correct mocks that contains parent methods but does not generate mock for parent protocols themselves.

cuckoo_generator generate --glob Middle/Sources/Middle/* --supportGlob Base/Sources/Base/* --output Middle/Tests/MiddleTests/MiddleTestMocks.swift --testable Middle

Looking forward to see other's thoughts. Thanks.

MatyasKriz commented 2 years ago

Hey there, @juyan. Does excluding the MiddleProtocol on its own do what you need? I'm not against adding support for this case, but I'd refrain from adding more CLI flags if possible.

juyan commented 2 years ago

@MatyasKriz sorry there was something wrong in my example and I fixed it. Please take a look again, hope it makes more sense now.

MatyasKriz commented 6 months ago

Hey, in Cuckoo 2.0 the regex exclusion is applied after inheritance is resolved, so it is possible to do the thing you're proposing here. 🙂 Thanks for the issue!