Skip to content

Commit 0401347

Browse files
authored
Merge pull request #6 from open-sausages/pulls/1.0/tests-n-windows
BUG Fix windows support
2 parents 07d427d + 938dc7e commit 0401347

14 files changed

+309
-6
lines changed

.gitattributes

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/tests/ export-ignore
2+
.gitattributes export-ignore
3+
.gitignore export-ignore
4+
.travis.yml export-ignore
5+
LICENSE export-ignore
6+
*.xml.dist export-ignore
7+
README.md export-ignore

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor/
2+
composer.lock

.travis.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: php
2+
3+
dist: trusty
4+
5+
cache:
6+
directories:
7+
- $HOME/.composer/cache/files
8+
9+
matrix:
10+
include:
11+
- php: 5.6
12+
- php: 7.2
13+
14+
fast_finish: true
15+
16+
before_script:
17+
- phpenv rehash
18+
- export PATH=~/.composer/vendor/bin:$PATH
19+
- composer validate
20+
- composer global require squizlabs/php_codesniffer:^3 --prefer-dist --no-interaction --no-progress --no-suggest -o
21+
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile
22+
23+
script:
24+
- vendor/bin/phpunit
25+
- composer run-script lint

composer.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
],
1212
"autoload": {
1313
"psr-4": {
14-
"SilverStripe\\VendorPlugin\\": "src/"
14+
"SilverStripe\\VendorPlugin\\": "src/",
15+
"SilverStripe\\VendorPlugin\\Tests\\": "tests/"
1516
}
1617
},
1718
"extra": {
@@ -20,11 +21,16 @@
2021
"dev-master": "1.0.x-dev"
2122
}
2223
},
24+
"scripts": {
25+
"lint": "phpcs src/ tests/",
26+
"lint-clean": "phpcbf src/ tests/"
27+
},
2328
"minimum-stability": "dev",
2429
"require": {
2530
"composer-plugin-api": "^1.1"
2631
},
2732
"require-dev": {
28-
"composer/composer": "^1.5"
33+
"composer/composer": "^1.5",
34+
"phpunit/phpunit": "^5.7"
2935
}
3036
}

phpcs.xml.dist

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset><rule ref="PSR2" /></ruleset>

phpunit.xml.dist

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true">
3+
<testsuite name="Default">
4+
<directory>tests/</directory>
5+
</testsuite>
6+
</phpunit>

src/Methods/ChainedMethod.php

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function exposeDirectory($source, $target)
3838
$failover->exposeDirectory($source, $target);
3939
return; // Return on first success
4040
} catch (RuntimeException $lastException) {
41-
4241
}
4342
}
4443
if ($lastException) {

src/Methods/ExposeMethod.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use RuntimeException;
66

7-
interface ExposeMethod {
7+
interface ExposeMethod
8+
{
89

910
/**
1011
* Exposes the directory with the given paths

src/Methods/SymlinkMethod.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SilverStripe\VendorPlugin\Methods;
44

55
use Composer\Util\Filesystem;
6+
use Composer\Util\Platform;
67
use RuntimeException;
78

89
/**
@@ -32,8 +33,24 @@ public function exposeDirectory($source, $target)
3233
$this->filesystem->ensureDirectoryExists($parent);
3334

3435
// Ensure symlink exists
35-
if (!$this->filesystem->relativeSymlink($source, $target)) {
36+
if (!$this->relativeSymlink($source, $target)) {
3637
throw new RuntimeException("Could not create symlink at $target");
3738
}
3839
}
40+
41+
/**
42+
* Create symlink
43+
*
44+
* @param string $source File source
45+
* @param string $target Place to put symlink
46+
* @return bool
47+
*/
48+
protected function relativeSymlink($source, $target)
49+
{
50+
if (Platform::isWindows()) {
51+
$this->filesystem->junction($source, $target);
52+
return true;
53+
}
54+
return $this->filesystem->relativeSymlink($source, $target);
55+
}
3956
}

src/Util.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ public static function joinPaths(...$parts)
1414
{
1515
$combined = null;
1616
array_walk_recursive($parts, function ($part) use (&$combined) {
17+
// Normalise path
18+
$part = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $part);
1719
$combined = $combined
18-
? (rtrim($combined, '\\/') . DIRECTORY_SEPARATOR . $part)
20+
? (rtrim($combined, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $part)
1921
: $part;
2022
});
2123
return $combined;

tests/Methods/ChainedMethodTest.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace SilverStripe\VendorPlugin\Tests\Methods;
4+
5+
use Composer\Util\Filesystem;
6+
use PHPUnit\Framework\TestCase;
7+
use RuntimeException;
8+
use SilverStripe\VendorPlugin\Methods\ChainedMethod;
9+
use SilverStripe\VendorPlugin\Methods\CopyMethod;
10+
use SilverStripe\VendorPlugin\Methods\ExposeMethod;
11+
use SilverStripe\VendorPlugin\Util;
12+
13+
class ChainedMethodTest extends TestCase
14+
{
15+
/**
16+
* @var Filesystem
17+
*/
18+
protected $filesystem = null;
19+
20+
/**
21+
* @var string app base path
22+
*/
23+
protected $root = null;
24+
25+
protected function setUp()
26+
{
27+
parent::setUp();
28+
29+
// Get temp dir
30+
$this->root = Util::joinPaths(
31+
sys_get_temp_dir(),
32+
'ChainedMethodTest',
33+
substr(sha1(uniqid()), 0, 10)
34+
);
35+
36+
// Setup filesystem
37+
$this->filesystem = new Filesystem();
38+
$this->filesystem->ensureDirectoryExists($this->root);
39+
}
40+
41+
protected function tearDown()
42+
{
43+
$this->filesystem->remove($this->root);
44+
parent::tearDown();
45+
}
46+
47+
public function testFailover()
48+
{
49+
$failingMethod = $this->createMock(CopyMethod::class);
50+
$failingMethod
51+
->method('exposeDirectory')
52+
->willThrowException(new RuntimeException());
53+
54+
// Create eventually successful method
55+
$method = new ChainedMethod($failingMethod, new CopyMethod());
56+
57+
// Expose
58+
$target = Util::joinPaths($this->root, 'resources', 'client');
59+
$method->exposeDirectory(
60+
realpath(__DIR__.'/../fixtures/source/client'),
61+
$target
62+
);
63+
64+
// Ensure file exists
65+
$this->assertFileExists(Util::joinPaths($this->root, 'resources', 'client', 'subfolder', 'somefile.txt'));
66+
67+
// Folder is a real folder and not a symlink
68+
$this->assertFalse($this->filesystem->isSymlinkedDirectory($target));
69+
$this->assertDirectoryExists($target);
70+
71+
72+
// Parent folder is a real folder
73+
$this->assertFalse($this->filesystem->isSymlinkedDirectory(dirname($target)));
74+
$this->assertDirectoryExists(dirname($target));
75+
}
76+
}

tests/Methods/CopyMethodTest.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace SilverStripe\VendorPlugin\Tests\Methods;
4+
5+
use Composer\Util\Filesystem;
6+
use Composer\Util\Platform;
7+
use PHPUnit\Framework\TestCase;
8+
use SilverStripe\VendorPlugin\Methods\CopyMethod;
9+
use SilverStripe\VendorPlugin\Methods\SymlinkMethod;
10+
use SilverStripe\VendorPlugin\Util;
11+
12+
class CopyMethodTest extends TestCase
13+
{
14+
/**
15+
* @var Filesystem
16+
*/
17+
protected $filesystem = null;
18+
19+
/**
20+
* @var string app base path
21+
*/
22+
protected $root = null;
23+
24+
protected function setUp()
25+
{
26+
parent::setUp();
27+
28+
// Get temp dir
29+
$this->root = Util::joinPaths(
30+
sys_get_temp_dir(),
31+
'CopyMethodTest',
32+
substr(sha1(uniqid()), 0, 10)
33+
);
34+
35+
// Setup filesystem
36+
$this->filesystem = new Filesystem();
37+
$this->filesystem->ensureDirectoryExists($this->root);
38+
}
39+
40+
protected function tearDown()
41+
{
42+
$this->filesystem->remove($this->root);
43+
parent::tearDown();
44+
}
45+
46+
public function testCopy()
47+
{
48+
$method = new CopyMethod();
49+
$target = Util::joinPaths($this->root, 'resources', 'client');
50+
$method->exposeDirectory(
51+
realpath(__DIR__.'/../fixtures/source/client'),
52+
$target
53+
);
54+
55+
// Ensure file exists
56+
$this->assertFileExists(Util::joinPaths($this->root, 'resources', 'client', 'subfolder', 'somefile.txt'));
57+
58+
// Folder is a real folder and not a symlink
59+
$this->assertFalse($this->filesystem->isSymlinkedDirectory($target));
60+
$this->assertDirectoryExists($target);
61+
62+
63+
// Parent folder is a real folder
64+
$this->assertFalse($this->filesystem->isSymlinkedDirectory(dirname($target)));
65+
$this->assertDirectoryExists(dirname($target));
66+
}
67+
68+
public function testRecoversFromSymlink()
69+
{
70+
$method = new SymlinkMethod();
71+
$target = Util::joinPaths($this->root, 'resources', 'client');
72+
$method->exposeDirectory(
73+
realpath(__DIR__.'/../fixtures/source/client'),
74+
$target
75+
);
76+
77+
$this->testCopy();
78+
}
79+
}

tests/Methods/SymlinkMethodTest.php

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace SilverStripe\VendorPlugin\Tests\Methods;
4+
5+
use Composer\Util\Filesystem;
6+
use Composer\Util\Platform;
7+
use PHPUnit\Framework\TestCase;
8+
use SilverStripe\VendorPlugin\Methods\CopyMethod;
9+
use SilverStripe\VendorPlugin\Methods\SymlinkMethod;
10+
use SilverStripe\VendorPlugin\Util;
11+
12+
class SymlinkMethodTest extends TestCase
13+
{
14+
/**
15+
* @var Filesystem
16+
*/
17+
protected $filesystem = null;
18+
19+
/**
20+
* @var string app base path
21+
*/
22+
protected $root = null;
23+
24+
protected function setUp()
25+
{
26+
parent::setUp();
27+
28+
// Get temp dir
29+
$this->root = Util::joinPaths(
30+
sys_get_temp_dir(),
31+
'SymlinkMethodTest',
32+
substr(sha1(uniqid()), 0, 10)
33+
);
34+
35+
// Setup filesystem
36+
$this->filesystem = new Filesystem();
37+
$this->filesystem->ensureDirectoryExists($this->root);
38+
}
39+
40+
protected function tearDown()
41+
{
42+
$this->filesystem->remove($this->root);
43+
parent::tearDown();
44+
}
45+
46+
public function testSymlink()
47+
{
48+
$method = new SymlinkMethod();
49+
$target = Util::joinPaths($this->root, 'resources', 'client');
50+
$method->exposeDirectory(
51+
realpath(__DIR__.'/../fixtures/source/client'),
52+
$target
53+
);
54+
55+
// Ensure file exists
56+
$this->assertFileExists(Util::joinPaths($this->root, 'resources', 'client', 'subfolder', 'somefile.txt'));
57+
58+
// Folder is NOT a real folder
59+
if (Platform::isWindows()) {
60+
$this->assertTrue($this->filesystem->isJunction($target));
61+
} else {
62+
$this->assertTrue($this->filesystem->isSymlinkedDirectory($target));
63+
}
64+
65+
// Parent folder is a real folder
66+
$this->assertDirectoryExists(dirname($target));
67+
}
68+
69+
public function testRecoversFromCopy()
70+
{
71+
$method = new CopyMethod();
72+
$target = Util::joinPaths($this->root, 'resources', 'client');
73+
$method->exposeDirectory(
74+
realpath(__DIR__.'/../fixtures/source/client'),
75+
$target
76+
);
77+
78+
// Repeat prior test
79+
$this->testSymlink();
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Here is some content

0 commit comments

Comments
 (0)