@@ -36,6 +36,11 @@ abstract class AbstractRectorTestCase extends AbstractLazyTestCase implements Re
3636
3737 private ?string $ inputFilePath = null ;
3838
39+ /**
40+ * @var resource|null
41+ */
42+ private $ fixtureDirectoryLock ;
43+
3944 /**
4045 * @var array<string, true>
4146 */
@@ -133,6 +138,8 @@ protected function tearDown(): void
133138 FileSystem ::delete ($ this ->inputFilePath );
134139 }
135140
141+ $ this ->unlockFixtureDirectory ();
142+
136143 // a next test that does not set its own paths would still locate the deleted file
137144 $ this ->dynamicSourceLocatorProvider ->reset ();
138145 }
@@ -177,6 +184,10 @@ protected function doTestFile(string $fixtureFilePath, bool $includeFixtureDirec
177184 throw new ShouldNotHappenException ('Fixture file and input file cannot be the same: ' . $ fixtureFilePath );
178185 }
179186
187+ // test classes that share a fixture directory write the same temp files,
188+ // so in parallel processes one could delete the other's file
189+ $ this ->lockFixtureDirectory (dirname ($ inputFilePath ));
190+
180191 // write temp file
181192 FileSystem ::write ($ inputFilePath , $ inputFileContents , null );
182193
@@ -282,6 +293,31 @@ private function processFilePath(string $filePath, bool $includeFixtureDirectory
282293 return new RectorTestResult ($ changedFileContents , $ processResult );
283294 }
284295
296+ private function lockFixtureDirectory (string $ directory ): void
297+ {
298+ // a test can call doTestFile() more than once; a second lock from this process would wait for the first
299+ $ this ->unlockFixtureDirectory ();
300+
301+ $ fixtureDirectoryLock = fopen ($ directory . '/.fixture.lock ' , 'c ' );
302+ if ($ fixtureDirectoryLock === false ) {
303+ throw new ShouldNotHappenException ('Cannot open the lock file in ' . $ directory );
304+ }
305+
306+ flock ($ fixtureDirectoryLock , LOCK_EX );
307+ $ this ->fixtureDirectoryLock = $ fixtureDirectoryLock ;
308+ }
309+
310+ private function unlockFixtureDirectory (): void
311+ {
312+ if (! is_resource ($ this ->fixtureDirectoryLock )) {
313+ return ;
314+ }
315+
316+ flock ($ this ->fixtureDirectoryLock , LOCK_UN );
317+ fclose ($ this ->fixtureDirectoryLock );
318+ $ this ->fixtureDirectoryLock = null ;
319+ }
320+
285321 private function createInputFilePath (string $ fixtureFilePath ): string
286322 {
287323 $ inputFileDirectory = dirname ($ fixtureFilePath );
0 commit comments