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

Resolve env placeholders in configuration #1113

Merged
merged 1 commit into from
Nov 14, 2023
Merged
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
16 changes: 16 additions & 0 deletions doc/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,19 @@ grumphp:
paths:
- tools
```

## Configuration

**Environment variables:**

```yaml
# grumphp.yml
parameters:
env(FIXER_ENABLED): 'true'

grumphp:
fixer:
enabled: '%env(bool:FIXER_ENABLED)%'
```

More about [Environment Variable Processors](https://symfony.com/doc/current/configuration/env_var_processors.html)
2 changes: 1 addition & 1 deletion src/Configuration/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function buildFromConfiguration(string $path): SymfonyContainerBui
$container->setParameter('config_file', $path);

// Compile configuration to make sure that tasks are added to the taskrunner
$container->compile();
$container->compile(true);

return $container;
}
Expand Down
43 changes: 25 additions & 18 deletions test/E2E/AbstractE2ETestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,27 @@ protected function ensureHooksExist(string $gitPath = null, string $containsPatt
}
}

protected function initializeGrumphpConfig(string $path, string $fileName = 'grumphp.yml'): string
protected function initializeGrumphpConfig(string $path, string $fileName = 'grumphp.yml', array $customConfig = []): string
{
$grumphpFile = $this->useCorrectDirectorySeparator($path.'/'.$fileName);
$config = [
'grumphp' => [
// Don't run E2E tests in parallel.
// This causes a deep nesting of parallel running tasks - which is causing some CI issues.
'parallel' => [
'enabled' => false,
],
'tasks' => [],
],
];

if ($customConfig) {
$config = array_merge_recursive($config, $customConfig);
}

$this->filesystem->dumpFile(
$grumphpFile,
Yaml::dump([
'grumphp' => [
// Don't run E2E tests in parallel.
// This causes a deep nesting of parallel running tasks - which is causing some CI issues.
'parallel' => [
'enabled' => false,
],
'tasks' => []
]
])
Yaml::dump($config),
);

return $grumphpFile;
Expand Down Expand Up @@ -373,15 +378,17 @@ protected function gitAddPath(string $path)
$this->runCommand('add files to git', new Process([$git, 'add', '-A'], $path));
}

protected function runGrumphp(string $projectPath, $vendorPath = './vendor', $environment = [])
protected function runGrumphp(string $projectPath, string $vendorPath = './vendor', array $environment = []): Process
{
$projectPath = $this->relativeRootPath($projectPath);
$this->runCommand('grumphp run', (
new Process(
[$vendorPath.'/bin/grumphp', 'run', '-vvv'],
$projectPath
)
)->setEnv($environment));
$process = new Process(
[$vendorPath.'/bin/grumphp', 'run', '-vvv'],
$projectPath
);

$this->runCommand('grumphp run', $process->setEnv($environment));

return $process;
}

protected function runGrumphpWithConfig(string $projectPath, string $grumphpFile, $vendorPath = './vendor')
Expand Down
40 changes: 40 additions & 0 deletions test/E2E/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace GrumPHPTest\E2E;

class ConfigurationTest extends AbstractE2ETestCase
{
/** @test */
function it_should_be_able_to_resolve_env_variable_in_configuration()
{
$this->initializeGitInRootDir();
$this->initializeComposer($this->rootDir);
$grumphpFile = $this->initializeGrumphpConfig(path: $this->rootDir, customConfig: [
'parameters' => [
'env(GRUMPHP_PARAMETER_TEST)' => '~'
],
'grumphp' => [
'ascii' => [
'succeeded' => '%env(string:GRUMPHP_PARAMETER_TEST)%',
],
],
]);

$this->installComposer($this->rootDir);
$this->ensureHooksExist();

$this->enableValidatePathsTask($grumphpFile, $this->rootDir);

$this->commitAll();
$process = $this->runGrumphp(projectPath: $this->rootDir, environment: [
'GRUMPHP_PARAMETER_TEST' => 'succeeded.txt',
]);

$this->assertStringContainsString(
file_get_contents(PROJECT_BASE_PATH . '/resources/ascii/succeeded.txt'),
$process->getOutput(),
);
}
}