Skip to content

Commit 88c0d7e

Browse files
committed
Create relative instead of absolute symlinks
1 parent 8edaeb1 commit 88c0d7e

File tree

2 files changed

+53
-51
lines changed

2 files changed

+53
-51
lines changed

src/Script/Helper/Filesystem.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@
22

33
namespace Tooly\Script\Helper;
44

5+
use Composer\Util\Filesystem as ComposerFileSystem;
6+
57
/**
68
* @package Tooly\Script\Helper
79
*/
810
class Filesystem
911
{
12+
/**
13+
* @var ComposerFileSystem
14+
*/
15+
private $filesystem;
16+
17+
/**
18+
* @param ComposerFileSystem|null $filesystem
19+
*/
20+
public function __construct(ComposerFileSystem $filesystem = null)
21+
{
22+
$this->filesystem = $filesystem ?: new ComposerFileSystem();
23+
}
24+
1025
/**
1126
* @param string $filename
1227
*
@@ -51,7 +66,17 @@ public function symlinkFile($sourceFile, $file)
5166
return true;
5267
}
5368

54-
return symlink($sourceFile, $file);
69+
return $this->filesystem->relativeSymlink($sourceFile, $file);
70+
}
71+
72+
/**
73+
* @param string $directory
74+
*
75+
* @return bool
76+
*/
77+
public function removeDirectory($directory)
78+
{
79+
return $this->filesystem->removeDirectoryPhp($directory);
5580
}
5681

5782
/**

tests/Script/Helper/FilesystemTest.php

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,40 @@
1111
*/
1212
class FilesystemTest extends \PHPUnit_Framework_TestCase
1313
{
14-
use PHPMock;
15-
16-
public function testCanSymlinkAFile()
14+
/**
15+
* @var Filesystem
16+
*/
17+
private $filesystem;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $testDirectory;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $testFile;
28+
29+
public function setUp()
1730
{
18-
vfsStream::setup();
19-
20-
file_put_contents('vfs://root/foo', 'test');
21-
22-
$symlink = $this
23-
->getFunctionMock('Tooly\Script\Helper', 'symlink')
24-
->expects($this->once())
25-
->willReturn(true);
26-
27-
$filesystem = new Filesystem;
28-
$filesystem->symlinkFile('vfs://root/foo', 'vfs://root/bar');
31+
$this->filesystem = new Filesystem;
32+
$this->testDirectory = sys_get_temp_dir();
33+
$this->testFile = $this->testDirectory . DIRECTORY_SEPARATOR . 'file';
2934
}
3035

31-
public function testSymlinkFileToNonExistingDirectoryWorks()
36+
public function tearDown()
3237
{
33-
$root = vfsStream::setup();
34-
35-
$mkdir = $this
36-
->getFunctionMock('Tooly\Script\Helper', 'mkdir')
37-
->expects($this->once())
38-
->willReturnCallback(function() use ($root) {
39-
$root->addChild(vfsStream::newDirectory('bar'));
40-
41-
return true;
42-
});
43-
44-
$symlink = $this
45-
->getFunctionMock('Tooly\Script\Helper', 'symlink')
46-
->expects($this->once());
47-
48-
file_put_contents('vfs://root/foo.txt', 'foo');
49-
50-
$filesystem = new Filesystem;
51-
$filesystem->symlinkFile('vfs://root/foo.txt', 'vfs://root/bar/bar.txt');
38+
if (is_dir($this->testDirectory)) {
39+
$this->filesystem->removeDirectory($this->testDirectory);
40+
}
5241
}
5342

54-
public function testCannotCreateDirectoryReturnsFalse()
43+
public function testCanRelativeSymlinkAFile()
5544
{
56-
$root = vfsStream::setup();
57-
$filesystem = new Filesystem;
58-
59-
$mkdir = $this
60-
->getFunctionMock('Tooly\Script\Helper', 'mkdir')
61-
->expects($this->exactly(2))
62-
->willReturnCallback(function() {
63-
return false;
64-
});
65-
66-
$symlink = $this
67-
->getFunctionMock('Tooly\Script\Helper', 'symlink')
68-
->expects($this->never());
45+
$symlink = $this->testDirectory . DIRECTORY_SEPARATOR . '/foo/symlink';
6946

70-
$this->assertFalse($filesystem->createFile('vfs://root/foo/bar.txt', 'test'));
71-
$this->assertFalse($filesystem->symlinkFile('vfs://root/foo/bar.txt', 'vfs://root/foo/baz.txt'));
47+
$this->assertTrue($this->filesystem->symlinkFile($this->testFile, $symlink));
48+
$this->assertNotEquals('/', substr(readlink($symlink), '0', 1));
7249
}
7350
}

0 commit comments

Comments
 (0)