From 70544eef95b65a1c24667d6e0127ae752e708b19 Mon Sep 17 00:00:00 2001 From: Tomasz Kulka Date: Mon, 13 Nov 2023 21:04:05 +0100 Subject: [PATCH] Resolve env placeholders in configuration --- doc/parameters.md | 16 ++++++++++ src/Configuration/ContainerBuilder.php | 2 +- test/E2E/AbstractE2ETestCase.php | 43 +++++++++++++++----------- test/E2E/ConfigurationTest.php | 40 ++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 19 deletions(-) create mode 100644 test/E2E/ConfigurationTest.php diff --git a/doc/parameters.md b/doc/parameters.md index 76a55d7e..8460f203 100644 --- a/doc/parameters.md +++ b/doc/parameters.md @@ -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) diff --git a/src/Configuration/ContainerBuilder.php b/src/Configuration/ContainerBuilder.php index 9eef2c0b..e7c21db9 100644 --- a/src/Configuration/ContainerBuilder.php +++ b/src/Configuration/ContainerBuilder.php @@ -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; } diff --git a/test/E2E/AbstractE2ETestCase.php b/test/E2E/AbstractE2ETestCase.php index a4b89220..d05b38dc 100644 --- a/test/E2E/AbstractE2ETestCase.php +++ b/test/E2E/AbstractE2ETestCase.php @@ -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; @@ -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') diff --git a/test/E2E/ConfigurationTest.php b/test/E2E/ConfigurationTest.php new file mode 100644 index 00000000..ea2a170d --- /dev/null +++ b/test/E2E/ConfigurationTest.php @@ -0,0 +1,40 @@ +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(), + ); + } +}