Skip to content

Commit 0ef4547

Browse files
Handle relative symlink paths
1 parent 907dc92 commit 0ef4547

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/Runner/Installer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ private function getHookSourceCode(string $hook): string
268268
protected function checkForBrokenSymlink(File $file): void
269269
{
270270
if ($file->isLink()) {
271-
if (!is_dir(dirname($file->linkTarget()))) {
271+
$target = $file->linkTarget();
272+
if (!Check::isAbsolutePath($target)) {
273+
$target = dirname($file->getPath()) . DIRECTORY_SEPARATOR . $target;
274+
}
275+
if (!is_dir(dirname($target))) {
272276
throw new RuntimeException(
273277
'The hook at \'' . $file->getPath() . '\' is a broken symbolic link. ' . PHP_EOL .
274278
'Please remove the symbolic link and try again.'

tests/unit/Runner/InstallerTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function testBrokenSymlinkDetectionOnExistingFile(): void
198198
/**
199199
* Tests Installer::checkForBrokenSymlinks
200200
*/
201-
public function testBrokenSymlinkDetectionOnBrokenSymlink(): void
201+
public function testBrokenSymlinkDetectionOnBrokenAbsoluteSymlink(): void
202202
{
203203
$this->expectException(Exception::class);
204204

@@ -213,7 +213,30 @@ public function testBrokenSymlinkDetectionOnBrokenSymlink(): void
213213

214214
$runner = new FakeInstaller($io, $config, $repo, $template);
215215
$file->method('isLink')->willReturn(true);
216-
$file->method('linkTarget')->willReturn('./foo/bar/baz');
216+
$file->method('linkTarget')->willReturn('/foo/bar/baz');
217+
218+
$runner->checkSymlink($file);
219+
}
220+
221+
/**
222+
* Tests Installer::checkForBrokenSymlinks
223+
*/
224+
public function testBrokenSymlinkDetectionOnBrokenRelativeSymlink(): void
225+
{
226+
$this->expectException(Exception::class);
227+
228+
$io = $this->createIOMock();
229+
$config = $this->createConfigMock();
230+
$repo = $this->createRepositoryMock();
231+
$template = $this->createTemplateMock();
232+
233+
$file = $this->getMockBuilder(File::class)
234+
->disableOriginalConstructor()
235+
->getMock();
236+
237+
$runner = new FakeInstaller($io, $config, $repo, $template);
238+
$file->method('isLink')->willReturn(true);
239+
$file->method('linkTarget')->willReturn('../../foo/bar/baz');
217240

218241
$runner->checkSymlink($file);
219242
}

0 commit comments

Comments
 (0)