MakeAWishFoundation / SwiftyMocky

Framework for automatic mock generation. Adds a set of handy methods, simplifying testing. One of the best and most complete solutions, including generics support and much more.
MIT License
1.03k stars 104 forks source link

Parallel execution of SwiftyMocky not supported? #229

Open remkoseelig opened 4 years ago

remkoseelig commented 4 years ago

TL;DR When running an Xcode build which target/scheme has parallel build enabled, when targets concurrently run swiftymocky generate, the output of these builds become unpredictable. Disabling parallel build seems to resolve the issue.

Background As I prefer to develop small libraries/frameworks, my projects regularly have multiple dependencies. As part of my TDD journey with Swift, for each library/framework I develop, my base framework target contains protocols of all public classes, and I include a separate target that generates mocked classed (using SwiftyMocky) for each of those protocols. This way, projects that need to mock my framework's classes, only need to include the target that includes the mocks projects and can start mocking right away. (This is a bit what was discussed in #228)

Now I ran into the following issue of having a project's test target that needed to include multiple of those static (mock) libraries, and as all these targets are listed in 'Link Binaries With Libraries', Xcode may decide to start building these targets in parallel.

When this happens, I discovered that the output of these builds (the generated mock classes) are similar.

E.g.

When running swiftymocky generate directly from the command-line, the output still is fine for both targets, but when you run the build again (CMD+U) the output file for BarMocks is being replaces with the contents of class FooMock.

After a long evening of trying to fix this (and not being able to discover what caused this black magic), driving to my work this morning, it suddenly became something obvious that it must have to do with concurrency. This evening I checked the settings of parallel build and when disabling this for the build scheme, I finally found my target building correctly. :)

Don't know the internals of SwiftyMocky, but it sounds like (haven't checked) that the temp location of SwiftyMocky's internal files might be fixed, at least for a project or something that leads to this issue.

Disabling parallel build is not an issue for me, just sharing this to validate my findings.

ajacquelin commented 1 year ago

👋 Is there any plan regarding this issue ? I am currently working on a project and each target is responsable of generating its mocks.

Sometimes the build fail because of concurrency, I was able to reproduce it by triggering 3 generate mock in parallel:

./generate_mocks.sh --foo & ./generate_mocks.sh --bar & ./generate_mocks.sh --foobar

I wonder if we could have 1 path cache per Mockfile by using --cacheBasePath from Sourcery https://github.com/krzysztofzablocki/Sourcery/releases/tag/1.9.0

Reproductible on 4.2.0.

@remkoseelig Were you able to resolve this issue by disabling parallel build ?

choulepoka commented 1 year ago

@ajacquelin

I know this will sound like heresy, but I side-stepped this particular problem:

This may not be what you wanted, but until someone fix this particular issue, perhaps this is a way you could use.