mirror of
https://github.com/andres-montanez/Magallanes
synced 2024-05-07 00:06:42 +02:00
[Galactica] V5 - improve exec task
This commit is contained in:
parent
23b1794321
commit
e6ec044230
|
@ -6,3 +6,4 @@ CHANGELOG for 5.X
|
||||||
* Refactored for Symfony 6 and PHP 8
|
* Refactored for Symfony 6 and PHP 8
|
||||||
* Added strong types
|
* Added strong types
|
||||||
* Removed task `composer/self-update`
|
* Removed task `composer/self-update`
|
||||||
|
* Allow `exec` task to interpolate `%environment%` and `%release%`
|
||||||
|
|
|
@ -57,8 +57,22 @@ class ExecTask extends AbstractTask
|
||||||
throw new ErrorException('Parameter "cmd" is not defined');
|
throw new ErrorException('Parameter "cmd" is not defined');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$mapping = [
|
||||||
|
'%environment%' => $this->runtime->getEnvironment(),
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($this->runtime->getReleaseId() !== null) {
|
||||||
|
$mapping['%release%'] = $this->runtime->getReleaseId();
|
||||||
|
}
|
||||||
|
|
||||||
|
$cmd = str_replace(
|
||||||
|
array_keys($mapping),
|
||||||
|
array_values($mapping),
|
||||||
|
strval($options['cmd'])
|
||||||
|
);
|
||||||
|
|
||||||
/** @var Process $process */
|
/** @var Process $process */
|
||||||
$process = $this->runtime->runCommand(strval($options['cmd']), intval($options['timeout']));
|
$process = $this->runtime->runCommand($cmd, intval($options['timeout']));
|
||||||
return $process->isSuccessful();
|
return $process->isSuccessful();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,10 +25,10 @@ class ExecTest extends TestCase
|
||||||
$runtime->setEnvironment('test');
|
$runtime->setEnvironment('test');
|
||||||
|
|
||||||
$task = new ExecTask();
|
$task = new ExecTask();
|
||||||
$task->setOptions(['cmd' => 'ls -l', 'desc' => 'Loading docker']);
|
$task->setOptions(['cmd' => 'ls -l', 'desc' => 'Command description']);
|
||||||
$task->setRuntime($runtime);
|
$task->setRuntime($runtime);
|
||||||
|
|
||||||
$this->assertStringContainsString('[Exec] Loading docker', $task->getDescription());
|
$this->assertStringContainsString('[Exec] Command description', $task->getDescription());
|
||||||
$task->execute();
|
$task->execute();
|
||||||
|
|
||||||
$ranCommands = $runtime->getRanCommands();
|
$ranCommands = $runtime->getRanCommands();
|
||||||
|
@ -46,6 +46,35 @@ class ExecTest extends TestCase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSimpleCommandWithInterpolation()
|
||||||
|
{
|
||||||
|
$runtime = new RuntimeMockup();
|
||||||
|
$runtime->setConfiguration(['environments' => ['test' => []]]);
|
||||||
|
$runtime->setEnvironment('test');
|
||||||
|
$runtime->setReleaseId('1234');
|
||||||
|
|
||||||
|
$task = new ExecTask();
|
||||||
|
$task->setOptions(['cmd' => 'cp %environment%.env /app/%release%/.env', 'desc' => 'Copy config']);
|
||||||
|
$task->setRuntime($runtime);
|
||||||
|
|
||||||
|
$this->assertStringContainsString('[Exec] Copy config', $task->getDescription());
|
||||||
|
$task->execute();
|
||||||
|
|
||||||
|
$ranCommands = $runtime->getRanCommands();
|
||||||
|
|
||||||
|
$testCase = array(
|
||||||
|
0 => 'cp test.env /app/1234/.env',
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check total of Executed Commands
|
||||||
|
$this->assertEquals(count($testCase), count($ranCommands));
|
||||||
|
|
||||||
|
// Check Generated Commands
|
||||||
|
foreach ($testCase as $index => $command) {
|
||||||
|
$this->assertEquals($command, $ranCommands[$index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function testCommandWithoutDescription()
|
public function testCommandWithoutDescription()
|
||||||
{
|
{
|
||||||
$runtime = new RuntimeMockup();
|
$runtime = new RuntimeMockup();
|
||||||
|
|
Loading…
Reference in a new issue