From 757970784913c0745ece1b217b6c57ef5c252cc2 Mon Sep 17 00:00:00 2001 From: Martin Le Guillou Date: Sat, 26 Aug 2017 17:25:30 +0200 Subject: [PATCH] [feature/disableWithNoDev] feat: allow to disable a file in production mode --- Processor.php | 7 ++++++- README.md | 18 +++++++++++++++++ ScriptHandler.php | 2 +- Tests/ProcessorTest.php | 30 ++++++++++++++++++++++++++++ Tests/ScriptHandlerTest.php | 1 + Tests/fixtures/dev_only/dist.yml | 3 +++ Tests/fixtures/dev_only/existing.yml | 3 +++ Tests/fixtures/dev_only/expected.yml | 3 +++ Tests/fixtures/dev_only/setup.yml | 4 ++++ 9 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 Tests/fixtures/dev_only/dist.yml create mode 100644 Tests/fixtures/dev_only/existing.yml create mode 100644 Tests/fixtures/dev_only/expected.yml create mode 100644 Tests/fixtures/dev_only/setup.yml diff --git a/Processor.php b/Processor.php index 6dc208f..e0f770c 100644 --- a/Processor.php +++ b/Processor.php @@ -16,13 +16,18 @@ public function __construct(IOInterface $io) $this->io = $io; } - public function processFile(array $config) + public function processFile(array $config, $devMode = true) { $config = $this->processConfig($config); $realFile = $config['file']; $parameterKey = $config['parameter-key']; + if ($devMode !== true && !empty($config['dev-only']) && true === $config['dev-only']) { + $this->io->write(sprintf('Skipping the "%s" file', $realFile)); + return; + } + $exists = is_file($realFile); $yamlParser = new Parser(); diff --git a/README.md b/README.md index 0f42600..437b41f 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,24 @@ If the old parameter is no longer present (maybe because it has been renamed and removed already), no parameters are overwritten. You don't need to remove obsolete parameters from the rename map once they have been renamed. +### Skipping file in production mode + +You can enable the parameter handler only in development mode: + +```json +{ + "extra": { + "incenteev-parameters": { + "dev-only": true + } + } +} + +``` + +Doing that, calling composer with `--no-dev` flag will disable the parameter +handler. + ### Managing multiple ignored files The parameter handler can manage multiple ignored files. To use this feature, diff --git a/ScriptHandler.php b/ScriptHandler.php index 165064a..52bc6d7 100644 --- a/ScriptHandler.php +++ b/ScriptHandler.php @@ -31,7 +31,7 @@ public static function buildParameters(Event $event) throw new \InvalidArgumentException('The extra.incenteev-parameters setting must be an array of configuration objects.'); } - $processor->processFile($config); + $processor->processFile($config, $event->isDevMode()); } } } diff --git a/Tests/ProcessorTest.php b/Tests/ProcessorTest.php index babf44a..dcb93d0 100644 --- a/Tests/ProcessorTest.php +++ b/Tests/ProcessorTest.php @@ -171,4 +171,34 @@ public function provideParameterHandlingTestCases() return $tests; } + + public function testNoDev() + { + $dataDir = __DIR__.'/fixtures/dev_only'; + + $testCase = array_replace_recursive( + array( + 'title' => 'unknown test', + 'config' => array( + 'file' => 'parameters.yml', + ), + 'dist-file' => 'parameters.yml.dist', + 'environment' => array(), + 'interactive' => false, + ), + (array) Yaml::parse(file_get_contents($dataDir.'/setup.yml')) + ); + + $workingDir = sys_get_temp_dir() . '/incenteev_parameter_handler'; + $this->initializeTestCase($testCase, $dataDir, $workingDir); + + $message = sprintf('Skipping the "%s" file', $testCase['config']['file']); + $this->io->write($message)->shouldBeCalled(); + + $this->setInteractionExpectations($testCase); + + $this->processor->processFile($testCase['config'], false); + + $this->assertFileEquals($dataDir.'/expected.yml', $workingDir.'/'.$testCase['config']['file'], $testCase['title']); + } } diff --git a/Tests/ScriptHandlerTest.php b/Tests/ScriptHandlerTest.php index b77e76a..faf3e9e 100644 --- a/Tests/ScriptHandlerTest.php +++ b/Tests/ScriptHandlerTest.php @@ -23,6 +23,7 @@ protected function setUp() $composer->getPackage()->willReturn($this->package); $this->event->getComposer()->willReturn($composer); $this->event->getIO()->willReturn($this->io); + $this->event->isDevMode()->willReturn(true); } /** diff --git a/Tests/fixtures/dev_only/dist.yml b/Tests/fixtures/dev_only/dist.yml new file mode 100644 index 0000000..5e50535 --- /dev/null +++ b/Tests/fixtures/dev_only/dist.yml @@ -0,0 +1,3 @@ +parameters: + foo: bar + foo2: bar2 diff --git a/Tests/fixtures/dev_only/existing.yml b/Tests/fixtures/dev_only/existing.yml new file mode 100644 index 0000000..367f8eb --- /dev/null +++ b/Tests/fixtures/dev_only/existing.yml @@ -0,0 +1,3 @@ +# This file is auto-generated during the composer install +parameters: + foo: bar diff --git a/Tests/fixtures/dev_only/expected.yml b/Tests/fixtures/dev_only/expected.yml new file mode 100644 index 0000000..367f8eb --- /dev/null +++ b/Tests/fixtures/dev_only/expected.yml @@ -0,0 +1,3 @@ +# This file is auto-generated during the composer install +parameters: + foo: bar diff --git a/Tests/fixtures/dev_only/setup.yml b/Tests/fixtures/dev_only/setup.yml new file mode 100644 index 0000000..46ab9a4 --- /dev/null +++ b/Tests/fixtures/dev_only/setup.yml @@ -0,0 +1,4 @@ +title: Skip file in production mode + +config: + dev-only: true