Added env test

This commit is contained in:
Anton Medvedev 2020-10-11 16:04:44 +02:00
parent 91c1d5fbec
commit 5bd53b8fc9
5 changed files with 59 additions and 12 deletions

View File

@ -69,7 +69,21 @@ set('use_atomic_symlink', function () {
return commandSupportsOption('mv', '--no-target-directory');
});
set('env', []); // Run command environment (for example, SYMFONY_ENV=prod)
/**
* Remote environment variables.
* ```php
* set('env', [
* 'KEY' => 'something',
* ]);
* ```
*
* It is possible to override it per `run()` call.
*
* ```php
* run('echo $KEY', ['env' => ['KEY' => 'over']]
* ```
*/
set('env', []);
/**
* Return current release path.

View File

@ -88,15 +88,9 @@ function starts_with(string $string, string $startString)
}
/**
* Take array of key/value and create string of it.
*
* This function used for create environment string.
*
* @param array $array
*
* @return string
*/
function array_to_string(array $array): string
function env_strinfigy(array $array): string
{
return implode(' ', array_map(
function ($key, $value) {

View File

@ -25,7 +25,7 @@ use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use function Deployer\Support\array_merge_alternate;
use function Deployer\Support\array_to_string;
use function Deployer\Support\env_strinfigy;
use function Deployer\Support\str_contains;
/**
@ -297,7 +297,8 @@ function within($path, $callback)
* Options:
* - `timeout` - Sets the process timeout (max. runtime). The timeout in seconds (default: 300 sec).
* - `secret` - Placeholder `%secret%` can be used in command. Placeholder will be replaced with this value and will not appear in any logs.
* - `vars` - Array of placeholders to replace in command: `run('echo %key%', ['vars' => ['key' => 'anything does here']])`;
* - `vars` - Array of placeholders to replace in command: `run('echo %key%', ['vars' => ['key' => 'anything does here']]);`
* - `env` - Array of environment variables: `run('echo $KEY', ['env' => ['key' => 'value']]);`
*
* Examples:
*
@ -330,7 +331,7 @@ function run($command, $options = [])
$env = array_merge_alternate(get('env', []), $options['env'] ?? []);
if (!empty($env)) {
$env = array_to_string($env);
$env = env_strinfigy($env);
$command = "export $env; $command";
}
@ -380,7 +381,7 @@ function runLocally($command, $options = [])
$env = array_merge_alternate(get('env', []), $options['env'] ?? []);
if (!empty($env)) {
$env = array_to_string($env);
$env = env_strinfigy($env);
$command = "export $env; $command";
}

24
test/joy/EnvTest.php Normal file
View File

@ -0,0 +1,24 @@
<?php
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer;
class EnvTest extends AbstractTest
{
const RECIPE = __DIR__ . '/recipe/env.php';
public function testOnce()
{
$this->init(self::RECIPE);
$this->tester->run(['test', '-f' => self::RECIPE]);
$display = $this->tester->getDisplay();
self::assertEquals(0, $this->tester->getStatusCode(), $display);
self::assertStringContainsString('global=global', $display);
self::assertStringContainsString('local=local', $display);
}
}

14
test/joy/recipe/env.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace Deployer;
localhost('prod');
set('env', [
'VAR' => 'global',
]);
task('test', function () {
info('global=' . run('echo $VAR'));
info('local=' . run('echo $VAR', ['env' => ['VAR' => 'local']]));
});