Skip to content

Commit

Permalink
Merge pull request #6 from open-sausages/pulls/1.0/tests-n-windows
Browse files Browse the repository at this point in the history
BUG Fix windows support
  • Loading branch information
flamerohr authored Oct 20, 2017
2 parents 07d427d + 938dc7e commit 0401347
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/tests/ export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
LICENSE export-ignore
*.xml.dist export-ignore
README.md export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor/
composer.lock
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: php

dist: trusty

cache:
directories:
- $HOME/.composer/cache/files

matrix:
include:
- php: 5.6
- php: 7.2

fast_finish: true

before_script:
- phpenv rehash
- export PATH=~/.composer/vendor/bin:$PATH
- composer validate
- composer global require squizlabs/php_codesniffer:^3 --prefer-dist --no-interaction --no-progress --no-suggest -o
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile

script:
- vendor/bin/phpunit
- composer run-script lint
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"autoload": {
"psr-4": {
"SilverStripe\\VendorPlugin\\": "src/"
"SilverStripe\\VendorPlugin\\": "src/",
"SilverStripe\\VendorPlugin\\Tests\\": "tests/"
}
},
"extra": {
Expand All @@ -20,11 +21,16 @@
"dev-master": "1.0.x-dev"
}
},
"scripts": {
"lint": "phpcs src/ tests/",
"lint-clean": "phpcbf src/ tests/"
},
"minimum-stability": "dev",
"require": {
"composer-plugin-api": "^1.1"
},
"require-dev": {
"composer/composer": "^1.5"
"composer/composer": "^1.5",
"phpunit/phpunit": "^5.7"
}
}
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset><rule ref="PSR2" /></ruleset>
6 changes: 6 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuite name="Default">
<directory>tests/</directory>
</testsuite>
</phpunit>
1 change: 0 additions & 1 deletion src/Methods/ChainedMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function exposeDirectory($source, $target)
$failover->exposeDirectory($source, $target);
return; // Return on first success
} catch (RuntimeException $lastException) {

}
}
if ($lastException) {
Expand Down
3 changes: 2 additions & 1 deletion src/Methods/ExposeMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use RuntimeException;

interface ExposeMethod {
interface ExposeMethod
{

/**
* Exposes the directory with the given paths
Expand Down
19 changes: 18 additions & 1 deletion src/Methods/SymlinkMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\VendorPlugin\Methods;

use Composer\Util\Filesystem;
use Composer\Util\Platform;
use RuntimeException;

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

// Ensure symlink exists
if (!$this->filesystem->relativeSymlink($source, $target)) {
if (!$this->relativeSymlink($source, $target)) {
throw new RuntimeException("Could not create symlink at $target");
}
}

/**
* Create symlink
*
* @param string $source File source
* @param string $target Place to put symlink
* @return bool
*/
protected function relativeSymlink($source, $target)
{
if (Platform::isWindows()) {
$this->filesystem->junction($source, $target);
return true;
}
return $this->filesystem->relativeSymlink($source, $target);
}
}
4 changes: 3 additions & 1 deletion src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public static function joinPaths(...$parts)
{
$combined = null;
array_walk_recursive($parts, function ($part) use (&$combined) {
// Normalise path
$part = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $part);
$combined = $combined
? (rtrim($combined, '\\/') . DIRECTORY_SEPARATOR . $part)
? (rtrim($combined, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $part)
: $part;
});
return $combined;
Expand Down
76 changes: 76 additions & 0 deletions tests/Methods/ChainedMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace SilverStripe\VendorPlugin\Tests\Methods;

use Composer\Util\Filesystem;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use SilverStripe\VendorPlugin\Methods\ChainedMethod;
use SilverStripe\VendorPlugin\Methods\CopyMethod;
use SilverStripe\VendorPlugin\Methods\ExposeMethod;
use SilverStripe\VendorPlugin\Util;

class ChainedMethodTest extends TestCase
{
/**
* @var Filesystem
*/
protected $filesystem = null;

/**
* @var string app base path
*/
protected $root = null;

protected function setUp()
{
parent::setUp();

// Get temp dir
$this->root = Util::joinPaths(
sys_get_temp_dir(),
'ChainedMethodTest',
substr(sha1(uniqid()), 0, 10)
);

// Setup filesystem
$this->filesystem = new Filesystem();
$this->filesystem->ensureDirectoryExists($this->root);
}

protected function tearDown()
{
$this->filesystem->remove($this->root);
parent::tearDown();
}

public function testFailover()
{
$failingMethod = $this->createMock(CopyMethod::class);
$failingMethod
->method('exposeDirectory')
->willThrowException(new RuntimeException());

// Create eventually successful method
$method = new ChainedMethod($failingMethod, new CopyMethod());

// Expose
$target = Util::joinPaths($this->root, 'resources', 'client');
$method->exposeDirectory(
realpath(__DIR__.'/../fixtures/source/client'),
$target
);

// Ensure file exists
$this->assertFileExists(Util::joinPaths($this->root, 'resources', 'client', 'subfolder', 'somefile.txt'));

// Folder is a real folder and not a symlink
$this->assertFalse($this->filesystem->isSymlinkedDirectory($target));
$this->assertDirectoryExists($target);


// Parent folder is a real folder
$this->assertFalse($this->filesystem->isSymlinkedDirectory(dirname($target)));
$this->assertDirectoryExists(dirname($target));
}
}
79 changes: 79 additions & 0 deletions tests/Methods/CopyMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace SilverStripe\VendorPlugin\Tests\Methods;

use Composer\Util\Filesystem;
use Composer\Util\Platform;
use PHPUnit\Framework\TestCase;
use SilverStripe\VendorPlugin\Methods\CopyMethod;
use SilverStripe\VendorPlugin\Methods\SymlinkMethod;
use SilverStripe\VendorPlugin\Util;

class CopyMethodTest extends TestCase
{
/**
* @var Filesystem
*/
protected $filesystem = null;

/**
* @var string app base path
*/
protected $root = null;

protected function setUp()
{
parent::setUp();

// Get temp dir
$this->root = Util::joinPaths(
sys_get_temp_dir(),
'CopyMethodTest',
substr(sha1(uniqid()), 0, 10)
);

// Setup filesystem
$this->filesystem = new Filesystem();
$this->filesystem->ensureDirectoryExists($this->root);
}

protected function tearDown()
{
$this->filesystem->remove($this->root);
parent::tearDown();
}

public function testCopy()
{
$method = new CopyMethod();
$target = Util::joinPaths($this->root, 'resources', 'client');
$method->exposeDirectory(
realpath(__DIR__.'/../fixtures/source/client'),
$target
);

// Ensure file exists
$this->assertFileExists(Util::joinPaths($this->root, 'resources', 'client', 'subfolder', 'somefile.txt'));

// Folder is a real folder and not a symlink
$this->assertFalse($this->filesystem->isSymlinkedDirectory($target));
$this->assertDirectoryExists($target);


// Parent folder is a real folder
$this->assertFalse($this->filesystem->isSymlinkedDirectory(dirname($target)));
$this->assertDirectoryExists(dirname($target));
}

public function testRecoversFromSymlink()
{
$method = new SymlinkMethod();
$target = Util::joinPaths($this->root, 'resources', 'client');
$method->exposeDirectory(
realpath(__DIR__.'/../fixtures/source/client'),
$target
);

$this->testCopy();
}
}
81 changes: 81 additions & 0 deletions tests/Methods/SymlinkMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace SilverStripe\VendorPlugin\Tests\Methods;

use Composer\Util\Filesystem;
use Composer\Util\Platform;
use PHPUnit\Framework\TestCase;
use SilverStripe\VendorPlugin\Methods\CopyMethod;
use SilverStripe\VendorPlugin\Methods\SymlinkMethod;
use SilverStripe\VendorPlugin\Util;

class SymlinkMethodTest extends TestCase
{
/**
* @var Filesystem
*/
protected $filesystem = null;

/**
* @var string app base path
*/
protected $root = null;

protected function setUp()
{
parent::setUp();

// Get temp dir
$this->root = Util::joinPaths(
sys_get_temp_dir(),
'SymlinkMethodTest',
substr(sha1(uniqid()), 0, 10)
);

// Setup filesystem
$this->filesystem = new Filesystem();
$this->filesystem->ensureDirectoryExists($this->root);
}

protected function tearDown()
{
$this->filesystem->remove($this->root);
parent::tearDown();
}

public function testSymlink()
{
$method = new SymlinkMethod();
$target = Util::joinPaths($this->root, 'resources', 'client');
$method->exposeDirectory(
realpath(__DIR__.'/../fixtures/source/client'),
$target
);

// Ensure file exists
$this->assertFileExists(Util::joinPaths($this->root, 'resources', 'client', 'subfolder', 'somefile.txt'));

// Folder is NOT a real folder
if (Platform::isWindows()) {
$this->assertTrue($this->filesystem->isJunction($target));
} else {
$this->assertTrue($this->filesystem->isSymlinkedDirectory($target));
}

// Parent folder is a real folder
$this->assertDirectoryExists(dirname($target));
}

public function testRecoversFromCopy()
{
$method = new CopyMethod();
$target = Util::joinPaths($this->root, 'resources', 'client');
$method->exposeDirectory(
realpath(__DIR__.'/../fixtures/source/client'),
$target
);

// Repeat prior test
$this->testSymlink();
}
}
1 change: 1 addition & 0 deletions tests/fixtures/source/client/subfolder/somefile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Here is some content

0 comments on commit 0401347

Please sign in to comment.