Skip to content

Commit

Permalink
Fix yarn outdated lockfile with the upgrade command
Browse files Browse the repository at this point in the history
Issue #26
  • Loading branch information
francoispluchino committed Apr 10, 2019
1 parent b756042 commit 518f39c
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 28 deletions.
15 changes: 12 additions & 3 deletions Asset/AbstractAssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,15 @@ public function setUpdatable($updatable)
*/
public function isUpdatable()
{
return $this->updatable && $this->isInstalled();
return $this->updatable && $this->isInstalled() && $this->isValidForUpdate();
}

/**
* {@inheritdoc}
*/
public function isValidForUpdate()
{
return true;
}

/**
Expand Down Expand Up @@ -183,12 +191,13 @@ public function run()
return 0;
}

$info = sprintf('<info>%s %s dependencies</info>', $this->isUpdatable() ? 'Updating' : 'Installing', $this->getName());
$updatable = $this->isUpdatable();
$info = sprintf('<info>%s %s dependencies</info>', $updatable ? 'Updating' : 'Installing', $this->getName());
$this->io->write($info);

$timeout = ProcessExecutor::getTimeout();
ProcessExecutor::setTimeout($this->config->get('manager-timeout'));
$cmd = $this->isUpdatable() ? $this->getUpdateCommand() : $this->getInstallCommand();
$cmd = $updatable ? $this->getUpdateCommand() : $this->getInstallCommand();
$res = (int) $this->executor->execute($cmd);
ProcessExecutor::setTimeout($timeout);

Expand Down
7 changes: 7 additions & 0 deletions Asset/AssetManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public function setUpdatable($updatable);
*/
public function isUpdatable();

/**
* Check if the asset package is valid for the update.
*
* @return bool
*/
public function isValidForUpdate();

/**
* Get the filename of the lock file.
*
Expand Down
10 changes: 10 additions & 0 deletions Asset/YarnManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,14 @@ public function isInstalled()
{
return parent::isInstalled() && file_exists($this->getLockPackageName());
}

/**
* {@inheritdoc}
*/
public function isValidForUpdate()
{
$cmd = $this->buildCommand('yarn', 'check', 'check --non-interactive');

return 0 === $this->executor->execute($cmd);
}
}
26 changes: 21 additions & 5 deletions Tests/Asset/AbstractAssetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function testValidateWithInstalledManagerAndWithoutValidVersion()
));
$this->manager = $this->getManager();

$this->executor->mockExecuteOutputValue = '42.0.0';
$this->executor->addExpectedValues(0, '42.0.0');

$this->manager->validate();
}
Expand All @@ -205,15 +205,15 @@ public function testValidateWithInstalledManagerAndWithValidVersion()
));
$this->manager = $this->getManager();

$this->executor->mockExecuteOutputValue = '42.0.0';
$this->executor->addExpectedValues(0, '42.0.0');

$this->manager->validate();
$this->assertSame('>=41.0', $this->config->get('manager-version'));
}

public function testValidateWithInstalledManagerAndWithoutValidationVersion()
{
$this->executor->mockExecuteOutputValue = '42.0.0';
$this->executor->addExpectedValues(0, '42.0.0');

$this->manager->validate();
$this->assertNull($this->config->get('manager-version'));
Expand Down Expand Up @@ -248,6 +248,8 @@ public function testAddDependenciesForInstallCommand()

public function testAddDependenciesForUpdateCommand()
{
$this->actionForTestAddDependenciesForUpdateCommand();

$expectedPackage = array(
'dependencies' => array(
'@composer-asset/foo--bar' => 'file:./path/foo/bar',
Expand Down Expand Up @@ -315,6 +317,8 @@ public function getRunData()
*/
public function testRunForInstallCommand($expectedRes, $action)
{
$this->actionForTestRunForInstallCommand($action);

$this->config = new Config(array(), array(
'run-asset-manager' => true,
'fallback-asset' => true,
Expand Down Expand Up @@ -344,11 +348,23 @@ public function testRunForInstallCommand($expectedRes, $action)
->method('restore');
}

$this->executor->mockExecuteReturnValue = $expectedRes;
$this->executor->mockExecuteOutputValue = 'ASSET MANAGER OUTPUT';
$this->executor->addExpectedValues($expectedRes, 'ASSET MANAGER OUTPUT');

$this->assertSame($expectedRes, $this->getManager()->run());
$this->assertSame($expectedCommand, $this->executor->getLastCommand());
$this->assertSame('ASSET MANAGER OUTPUT', $this->executor->getLastOutput());
}

protected function actionForTestAddDependenciesForUpdateCommand()
{
// do nothing by default
}

/**
* @param string $action The action
*/
protected function actionForTestRunForInstallCommand($action)
{
// do nothing by default
}
}
19 changes: 19 additions & 0 deletions Tests/Asset/YarnAssetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,23 @@ protected function getValidUpdateCommand()
{
return 'yarn upgrade --non-interactive';
}

/**
* {@inheritdoc}
*/
protected function actionForTestAddDependenciesForUpdateCommand()
{
$this->executor->addExpectedValues(0, 'CHECK OUTPUT');
}

/**
* {@inheritdoc}
*/
public function actionForTestRunForInstallCommand($action)
{
if ('update' === $action) {
$this->executor->addExpectedValues(0, 'CHECK OUTPUT');
$this->executor->addExpectedValues(0, 'CHECK OUTPUT');
}
}
}
107 changes: 87 additions & 20 deletions Tests/Fixtures/Util/ProcessExecutorMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,83 @@
class ProcessExecutorMock extends ProcessExecutor
{
/**
* @var int
* @var array
*/
public $mockExecuteReturnValue = 0;
private $expectedValues = array();

/**
* @var string|null
* @var array
*/
public $mockExecuteOutputValue = null;
private $executedCommands = array();

/**
* @var string|null
* @var int
*/
private $lastCommand;
private $position = 0;

/**
* @var string|null
* {@inheritdoc}
*/
private $lastOutput;
public function execute($command, &$output = null, $cwd = null)
{
$expected = isset($this->expectedValues[$this->position])
? $this->expectedValues[$this->position]
: array(null, $output);

list($returnedCode, $output) = $expected;
$this->executedCommands[] = array($command, $returnedCode, $output);
++$this->position;

return $returnedCode;
}

/**
* {@inheritdoc}
* @param int $returnedCode The returned code
* @param null $output The output
*
* @return self
*/
public function execute($command, &$output = null, $cwd = null)
public function addExpectedValues($returnedCode = 0, $output = null)
{
$output = $this->mockExecuteOutputValue;
$res = $this->mockExecuteReturnValue;
$this->expectedValues[] = array($returnedCode, $output);

$this->lastCommand = $command;
$this->lastOutput = $output;
return $this;
}

// reset
$this->mockExecuteReturnValue = 0;
$this->mockExecuteOutputValue = null;
/**
* Get the executed command.
*
* @param int $position The position of executed command
*
* @return string|null
*/
public function getExecutedCommand($position)
{
return $this->getExecutedValue($position, 0);
}

return $res;
/**
* Get the executed returned code.
*
* @param int $position The position of executed command
*
* @return int|null
*/
public function getExecutedReturnedCode($position)
{
return $this->getExecutedValue($position, 1);
}

/**
* Get the executed command.
*
* @param int $position The position of executed command
*
* @return string|null
*/
public function getExecutedOutput($position)
{
return $this->getExecutedValue($position, 2);
}

/**
Expand All @@ -65,7 +107,17 @@ public function execute($command, &$output = null, $cwd = null)
*/
public function getLastCommand()
{
return $this->lastCommand;
return $this->getExecutedCommand(\count($this->executedCommands) - 1);
}

/**
* Get the last executed returned code.
*
* @return int|null
*/
public function getLastReturnedCode()
{
return $this->getExecutedReturnedCode(\count($this->executedCommands) - 1);
}

/**
Expand All @@ -75,6 +127,21 @@ public function getLastCommand()
*/
public function getLastOutput()
{
return $this->lastOutput;
return $this->getExecutedOutput(\count($this->executedCommands) - 1);
}

/**
* Get the value of the executed command.
*
* @param int $position The position
* @param int $index The index of value
*
* @return string|int|null
*/
private function getExecutedValue($position, $index)
{
return isset($this->executedCommands[$position])
? $this->executedCommands[$position][$index]
: null;
}
}
73 changes: 73 additions & 0 deletions Tests/Fixtures/Util/ProcessExecutorMockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the Foxy package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Foxy\Tests\Json;

use Foxy\Tests\Fixtures\Util\ProcessExecutorMock;

/**
* Tests for the process executor mock.
*
* @author François Pluchino <[email protected]>
*/
class ProcessExecutorMockTest extends \PHPUnit_Framework_TestCase
{
public function testExecuteWithoutExpectedValues()
{
$executor = new ProcessExecutorMock();

$executor->execute('run', $output);

$this->assertSame('run', $executor->getExecutedCommand(0));
$this->assertNull($executor->getExecutedReturnedCode(0));
$this->assertNull($executor->getExecutedOutput(0));

$this->assertNull($executor->getExecutedCommand(1));
$this->assertNull($executor->getExecutedReturnedCode(1));
$this->assertNull($executor->getExecutedOutput(1));

$this->assertSame('run', $executor->getLastCommand());
$this->assertNull($executor->getLastReturnedCode());
$this->assertNull($executor->getLastOutput());

$this->assertNull($output);
}

public function testExecuteWithExpectedValues()
{
$executor = new ProcessExecutorMock();

$executor->addExpectedValues(0, 'TEST');
$executor->addExpectedValues(42, 'TEST 2');

$executor->execute('run', $output);
$executor->execute('run2', $output2);

$this->assertSame('run', $executor->getExecutedCommand(0));
$this->assertSame(0, $executor->getExecutedReturnedCode(0));
$this->assertSame('TEST', $executor->getExecutedOutput(0));

$this->assertSame('run2', $executor->getExecutedCommand(1));
$this->assertSame(42, $executor->getExecutedReturnedCode(1));
$this->assertSame('TEST 2', $executor->getExecutedOutput(1));

$this->assertNull($executor->getExecutedCommand(2));
$this->assertNull($executor->getExecutedReturnedCode(2));
$this->assertNull($executor->getExecutedOutput(2));

$this->assertSame('run2', $executor->getLastCommand());
$this->assertSame(42, $executor->getLastReturnedCode());
$this->assertSame('TEST 2', $executor->getLastOutput());

$this->assertSame('TEST', $output);
$this->assertSame('TEST 2', $output2);
}
}

0 comments on commit 518f39c

Please sign in to comment.