Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions src/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,37 +298,23 @@ private function lockFixtureDirectory(string $directory): void
// a test can call doTestFile() more than once; a second lock from this process would wait for the first
$this->unlockFixtureDirectory();

$lockFilePath = $this->createFixtureDirectoryLockFilePath($directory);
// PHP cannot open a directory on Windows
if ($this->isWindows()) {
return;
}

$fixtureDirectoryLock = fopen($lockFilePath, 'c');
// lock the directory itself: a lock file in sys_get_temp_dir() is not shared by test runners
// that give each worker its own TMPDIR, like fast-unit
$fixtureDirectoryLock = fopen($directory, 'r');

if ($fixtureDirectoryLock === false) {
throw new ShouldNotHappenException('Cannot open the lock file ' . $lockFilePath);
throw new ShouldNotHappenException('Cannot open the fixture directory ' . $directory);
}

flock($fixtureDirectoryLock, LOCK_EX);
$this->fixtureDirectoryLock = $fixtureDirectoryLock;
}

/**
* The lock file lives in a directory in the system temp directory, so a project that tests its own rules gets no file in its fixture directories.
* The lock file is never deleted: a process waiting on a deleted file would hold a lock no other process sees.
*/
private function createFixtureDirectoryLockFilePath(string $directory): string
{
$realDirectory = realpath($directory);

if ($realDirectory === false) {
$realDirectory = $directory;
}

$lockDirectory = sys_get_temp_dir() . '/rector_fixture_lock';

FileSystem::createDir($lockDirectory);

return $lockDirectory . '/' . sha1($realDirectory) . '.lock';
}

private function unlockFixtureDirectory(): void
{
if (! is_resource($this->fixtureDirectoryLock)) {
Expand Down
17 changes: 17 additions & 0 deletions tests/Testing/FixtureDirectoryLock/FixtureDirectoryLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ public function testDoesNotWriteLockFileIntoFixtureDirectory(): void
$this->assertFileDoesNotExist(__DIR__ . '/Fixture/.fixture.lock');
}

public function testLocksFixtureDirectoryWhateverTheTempDirectory(): void
{
if ($this->isWindows()) {
$this->markTestSkipped('PHP cannot open a directory on Windows');
}

$this->doTestFile(__DIR__ . '/Fixture/some_function.php.inc');

// a worker with its own TMPDIR opens the same directory, so it must see the lock
$otherProcessHandle = fopen(__DIR__ . '/Fixture', 'r');
$this->assertNotFalse($otherProcessHandle);

$this->assertFalse(flock($otherProcessHandle, LOCK_EX | LOCK_NB));

fclose($otherProcessHandle);
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
Expand Down
Loading