[Galactica] V5 - improve exec task

This commit is contained in:
Andrés Montañez 2022-04-10 01:55:49 -03:00
parent 23b1794321
commit e6ec044230
No known key found for this signature in database
GPG key ID: 97E9F675F4C03DE2
3 changed files with 47 additions and 3 deletions

View file

@ -6,3 +6,4 @@ CHANGELOG for 5.X
* Refactored for Symfony 6 and PHP 8
* Added strong types
* Removed task `composer/self-update`
* Allow `exec` task to interpolate `%environment%` and `%release%`

View file

@ -57,8 +57,22 @@ class ExecTask extends AbstractTask
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 */
$process = $this->runtime->runCommand(strval($options['cmd']), intval($options['timeout']));
$process = $this->runtime->runCommand($cmd, intval($options['timeout']));
return $process->isSuccessful();
}

View file

@ -25,10 +25,10 @@ class ExecTest extends TestCase
$runtime->setEnvironment('test');
$task = new ExecTask();
$task->setOptions(['cmd' => 'ls -l', 'desc' => 'Loading docker']);
$task->setOptions(['cmd' => 'ls -l', 'desc' => 'Command description']);
$task->setRuntime($runtime);
$this->assertStringContainsString('[Exec] Loading docker', $task->getDescription());
$this->assertStringContainsString('[Exec] Command description', $task->getDescription());
$task->execute();
$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()
{
$runtime = new RuntimeMockup();