Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/skip asset update #278

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 23 additions & 0 deletions Repository/Vcs/GitDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Fxp\Composer\AssetPlugin\Repository\Vcs;

use Composer\Cache;
use Composer\IO\IOInterface;
use Composer\Repository\Vcs\GitDriver as BaseGitDriver;

/**
Expand All @@ -35,4 +36,26 @@ public function getComposerInformation($identifier)

return ProcessUtil::getComposerInformation($this->cache, $this->infoCache, $this->repoConfig['asset-type'], $this->process, $identifier, $resource, sprintf('git show %s', $resource), sprintf('git log -1 --format=%%at %s', escapeshellarg($identifier)), $this->repoDir, '@');
}

/**
* {@inheritDoc}
*/
public function initialize()
{
/* @var AssetRepositoryManager $arm */
$arm = $this->repoConfig['asset-repository-manager'];

if (null !== ($skip = $arm->getConfig()->get('git-skip-update'))) {
$localUrl = $this->config->get('cache-vcs-dir') . '/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/';

// check if local copy exists and if it is a git repository and that modification time is within threshold
if (is_dir($localUrl) && is_file($localUrl.'/config') && filemtime($localUrl) > strtotime('-'.$skip)) {
$this->io->write('(<comment>local</comment>) ', false, IOInterface::VERBOSE);
$this->url = $localUrl;
} else {
$this->io->write('(<info>remote</info>) ', false, IOInterface::VERBOSE);
}
}
parent::initialize();
}
}
10 changes: 10 additions & 0 deletions Resources/doc/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ The plugin can override the main file definitions of the Bower packages. To over
definitions specify the packages and their main file array as name/value pairs. For an example
see the [usage informations](index.md#override-the-main-files-for-bower).

##### config.fxp-asset.git-skip-update (root-only)

The plugin can skip updating meta-data in git repositories for given amount of time, i.e. `6 hours`, `3 days` or `1 week`.

"config": {
"fxp-asset": {
"git-skip-update": "2 days"
}
}

### Mapping asset file to composer package

##### NPM mapping
Expand Down
82 changes: 82 additions & 0 deletions Tests/Repository/Vcs/GitDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Composer\IO\IOInterface;
use Composer\Util\Filesystem;
use Composer\Util\ProcessExecutor;
use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager;
use Fxp\Composer\AssetPlugin\Repository\Vcs\GitDriver;

/**
Expand All @@ -29,15 +30,34 @@ class GitDriverTest extends \PHPUnit_Framework_TestCase
*/
private $config;

/**
* @var AssetRepositoryManager|\PHPUnit_Framework_MockObject_MockObject
*/
private $assetRepositoryManager;

public function setUp()
{
$assetConfig = new \Fxp\Composer\AssetPlugin\Config\Config(array('git-skip-update' => '1 hour'));

/* @var AssetRepositoryManager|\PHPUnit_Framework_MockObject_MockObject $arm */
$this->assetRepositoryManager = $this->getMockBuilder(AssetRepositoryManager::class)->disableOriginalConstructor()->getMock();
$this->assetRepositoryManager->expects($this->any())
->method('getConfig')
->willReturn($assetConfig);

$this->config = new Config();
$this->config->merge(array(
'config' => array(
'home' => sys_get_temp_dir().'/composer-test',
'cache-repo-dir' => sys_get_temp_dir().'/composer-test-cache',
'cache-vcs-dir' => sys_get_temp_dir().'/composer-test-cache',
),
));

// Mock for skip asset
$fs = new Filesystem();
$fs->ensureDirectoryExists(sys_get_temp_dir().'/composer-test-cache/https---github.com-fxpio-composer-asset-plugin.git');
file_put_contents(sys_get_temp_dir().'/composer-test-cache/https---github.com-fxpio-composer-asset-plugin.git/config', '');
}

public function tearDown()
Expand Down Expand Up @@ -71,6 +91,7 @@ public function testPublicRepositoryWithEmptyComposer($type, $filename)
'url' => $repoUrl,
'asset-type' => $type,
'filename' => $filename,
'asset-repository-manager' => $this->assetRepositoryManager,
);

$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
Expand All @@ -96,6 +117,65 @@ public function testPublicRepositoryWithEmptyComposer($type, $filename)
$this->assertSame($validEmpty, $gitDriver->getComposerInformation($identifier));
}

/**
* @dataProvider getAssetTypes
*
* @param string $type
* @param string $filename
*/
public function testPublicRepositoryWithSkipUpdate($type, $filename)
{
$repoUrl = 'https://github.com/fxpio/composer-asset-plugin.git';
$identifier = '92bebbfdcde75ef2368317830e54b605bc938123';
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add:

$assetConfig = new \Fxp\Composer\AssetPlugin\Config\Config(array(
    'git-skip-update' => true,
));
/* @var AssetRepositoryManager|\PHPUnit_Framework_MockObject_MockObject $arm */
$arm = $this->getMockBuilder(AssetRepositoryManager::class)->disableOriginalConstructor()->getMock();
$arm->expects($this->any())
            ->method('getConfig')
            ->willReturn($config);

$repoConfig = array(
'url' => $repoUrl,
'asset-type' => $type,
'filename' => $filename,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add:

'asset-repository-manager' => $arm,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I do this in setUp() in the test for all tests?
When using the RepositoryManager in GitDriver, most of the tests fail with...

6) Fxp\Composer\AssetPlugin\Tests\Repository\Vcs\GitDriverTest::testPublicRepositoryWithFilesystemCache with data set #1 ('bower', 'bower.json')
Undefined index: asset-repository-manager

/app/Repository/Vcs/GitDriver.php:46
/app/Tests/Repository/Vcs/GitDriverTest.php:249

7) Fxp\Composer\AssetPlugin\Tests\Repository\Vcs\GitHubDriverTest::testPrivateRepositoryNoInteraction with data set #0 ('npm', 'package.json')
Error: Call to a member function get() on null

But the actual code work, this is just an issue during testing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes of course, you can.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a look at: e676a76

I am getting...

8) Fxp\Composer\AssetPlugin\Tests\Repository\Vcs\GitDriverTest::testPublicRepositoryWithFilesystemCache with data set #1 ('bower', 'bower.json')
Trying to configure method "getConfig" which cannot be configured because it does not exist, has not been specified, is final, or is static

I thought we'd avoid that by extending from AssetRepositoryManager - I am getting confused :)

Also the GitHubDriver test now complains about

18) Fxp\Composer\AssetPlugin\Tests\Repository\Vcs\GitHubDriverTest::testNoApi with data set #5 ('bower', 'bower.json', array('0123456789abcdef0123456789abc...234567'), array('master 0123456789abcdef012345...omment'))
Error: Call to a member function get() on null

I am unsure if I'd need to mock the config or add actual checks to the GitDriver.php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must add the asset-repository-manager key in each repoConfig of failed tests.

Copy link
Member

@francoispluchino francoispluchino Mar 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For GithubDriverTest, you must create a asset config in the setUp() method, and mock the AssetRepositoryManager::getConfig() method returning the instance of asset config.

'asset-repository-manager' => $this->assetRepositoryManager,
);

$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$process->expects($this->any())
->method('splitLines')
->will($this->returnValue(array()));
$process->expects($this->any())
->method('execute')
->will($this->returnCallback(function ($command, &$output = null) use ($identifier, $repoConfig) {
if ($command === sprintf('git show %s', sprintf('%s:%s', escapeshellarg($identifier), $repoConfig['filename']))) {
$output = '{"name": "foo"}';
} elseif (false !== strpos($command, 'git log')) {
$date = new \DateTime(null, new \DateTimeZone('UTC'));
$output = $date->getTimestamp();
}

return 0;
}));

/* @var IOInterface $io */
/* @var ProcessExecutor $process */

$gitDriver1 = new GitDriver($repoConfig, $io, $this->config, $process, null);
$gitDriver1->initialize();

$gitDriver2 = new GitDriver($repoConfig, $io, $this->config, $process, null);
$gitDriver2->initialize();

$validEmpty = array(
'_nonexistent_package' => true,
);

$composer1 = $gitDriver1->getComposerInformation($identifier);
$composer2 = $gitDriver2->getComposerInformation($identifier);

$this->assertNotNull($composer1);
$this->assertNotNull($composer2);
$this->assertSame($composer1, $composer2);
$this->assertNotSame($validEmpty, $composer1);
$this->assertNotSame($validEmpty, $composer2);
}

/**
* @dataProvider getAssetTypes
*
Expand All @@ -110,6 +190,7 @@ public function testPublicRepositoryWithCodeCache($type, $filename)
'url' => $repoUrl,
'asset-type' => $type,
'filename' => $filename,
'asset-repository-manager' => $this->assetRepositoryManager,
);
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
Expand Down Expand Up @@ -156,6 +237,7 @@ public function testPublicRepositoryWithFilesystemCache($type, $filename)
'url' => $repoUrl,
'asset-type' => $type,
'filename' => $filename,
'asset-repository-manager' => $this->assetRepositoryManager,
);
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
Expand Down
5 changes: 5 additions & 0 deletions Tests/Repository/Vcs/GitHubDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ public function setUp()
),
));

$assetConfig = new \Fxp\Composer\AssetPlugin\Config\Config(array());

$this->assetRepositoryManager = $this->getMockBuilder(AssetRepositoryManager::class)
->disableOriginalConstructor()->getMock();
$this->assetRepositoryManager->expects($this->any())
->method('getConfig')
->willReturn($assetConfig);
}

public function tearDown()
Expand Down