Skip to content

Commit bd9c559

Browse files
GromNaNnicolas-grekas
authored andcommitted
Add SYMFONY_ALLOW_CONTRIB variable
1 parent f356aa3 commit bd9c559

2 files changed

Lines changed: 65 additions & 5 deletions

File tree

src/Flex.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,12 @@ public function install(Event $event)
386386
}
387387

388388
$this->io->writeError(\sprintf('<info>Symfony operations: %d recipe%s (%s)</>', \count($recipes), \count($recipes) > 1 ? 's' : '', $this->downloader->getSessionId()));
389-
$installContribs = $this->composer->getPackage()->getExtra()['symfony']['allow-contrib'] ?? false;
389+
if (false === $installContribs = getenv('SYMFONY_ALLOW_CONTRIB')) {
390+
$installContribs = $this->composer->getPackage()->getExtra()['symfony']['allow-contrib'] ?? false;
391+
}
392+
if (!\is_bool($installContribs)) {
393+
$installContribs = filter_var($installContribs, \FILTER_VALIDATE_BOOL);
394+
}
390395
$manifest = null;
391396
$originalComposerJsonHash = $this->getComposerJsonHash();
392397
$postInstallRecipes = [];

tests/FlexTest.php

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testPostInstall()
7070
$package = new Package('dummy/dummy', '1.0.0', '1.0.0');
7171
$recipe = new Recipe($package, 'dummy/dummy', 'install', $data['manifests']['dummy/dummy'], $data['locks']['dummy/dummy']);
7272

73-
$rootPackage = $this->mockRootPackage(['symfony' => ['allow-contrib' => true]]);
73+
$rootPackage = $this->mockRootPackage();
7474
$flex = $this->mockFlex($io, $rootPackage, $recipe, $data);
7575
$flex->record($this->mockPackageEvent($package));
7676
$flex->install($this->mockFlexEvent());
@@ -110,7 +110,7 @@ public function testActivateLoadsClasses()
110110
{
111111
$io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE);
112112

113-
$package = $this->mockRootPackage(['symfony' => ['allow-contrib' => true]]);
113+
$package = $this->mockRootPackage();
114114
$package->method('getRequires')->willReturn([new Link('dummy', 'symfony/flex', class_exists(MatchAllConstraint::class) ? new MatchAllConstraint() : null)]);
115115

116116
$composer = $this->mockComposer($this->mockLocker(), $package, Factory::createConfig($io));
@@ -194,7 +194,7 @@ public function testFetchRecipesOrder()
194194
];
195195

196196
$io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE);
197-
$rootPackage = $this->mockRootPackage(['symfony' => ['allow-contrib' => true]]);
197+
$rootPackage = $this->mockRootPackage();
198198

199199
$flex = $this->mockFlex($io, $rootPackage, null, [
200200
'manifests' => array_reduce($packages, static function (array $manifests, array $packageInfo) {
@@ -271,7 +271,7 @@ public function testFetchRecipesWithConflicts()
271271
];
272272

273273
$io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE);
274-
$rootPackage = $this->mockRootPackage(['symfony' => ['allow-contrib' => true]]);
274+
$rootPackage = $this->mockRootPackage();
275275

276276
$downloader = $this->getMockBuilder(Downloader::class)->disableOriginalConstructor()->getMock();
277277
$downloader->expects($this->exactly(2))
@@ -313,6 +313,61 @@ class_exists(LockArrayRepository::class) ? LockArrayRepository::class : Reposito
313313
$this->assertSame('2.3', $recipes['doctrine/doctrine-bundle']->getVersion());
314314
}
315315

316+
public function testInstallWithSymfonyAllowContribEnvVar()
317+
{
318+
$data = [
319+
'manifests' => [
320+
'dummy/contrib-package' => [
321+
'manifest' => [
322+
'bundles' => [
323+
'Dummy\\ContribBundle\\ContribBundle' => ['all'],
324+
],
325+
],
326+
'origin' => 'dummy/contrib-package:1.0@github.com/symfony/recipes-contrib:main',
327+
'is_contrib' => true,
328+
],
329+
],
330+
'locks' => [
331+
'dummy/contrib-package' => [
332+
'recipe' => [],
333+
'version' => '1.0',
334+
],
335+
],
336+
];
337+
338+
$package = new Package('dummy/contrib-package', '1.0.0', '1.0.0');
339+
340+
// SYMFONY_ALLOW_CONTRIB=1 should install contrib recipes even without config
341+
putenv('SYMFONY_ALLOW_CONTRIB=1');
342+
$io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE);
343+
$recipe = new Recipe($package, 'dummy/contrib-package', 'install', $data['manifests']['dummy/contrib-package'], $data['locks']['dummy/contrib-package']);
344+
$rootPackage = $this->mockRootPackage(['symfony' => []]);
345+
$flex = $this->mockFlex($io, $rootPackage, $recipe, $data);
346+
$flex->record($this->mockPackageEvent($package));
347+
$flex->install($this->mockFlexEvent());
348+
349+
$output = $io->getOutput();
350+
$this->assertStringContainsString('Configuring dummy/contrib-package', $output);
351+
$this->assertStringNotContainsString('IGNORING', $output);
352+
353+
// SYMFONY_ALLOW_CONTRIB=0 should skip contrib recipes even with config allow-contrib=true
354+
putenv('SYMFONY_ALLOW_CONTRIB=0');
355+
$io = new BufferIO('', OutputInterface::VERBOSITY_NORMAL);
356+
$recipe = new Recipe($package, 'dummy/contrib-package', 'install', $data['manifests']['dummy/contrib-package'], $data['locks']['dummy/contrib-package']);
357+
$rootPackage = $this->mockRootPackage(['symfony' => ['allow-contrib' => true]]);
358+
// For this test, we expect the recipe NOT to be installed, so pass null as recipe to configurator
359+
$flex = $this->mockFlex($io, $rootPackage, null, $data);
360+
$flex->record($this->mockPackageEvent($package));
361+
$flex->install($this->mockFlexEvent());
362+
363+
$output = $io->getOutput();
364+
$this->assertStringContainsString('IGNORING', $output);
365+
$this->assertStringNotContainsString('Configuring dummy/contrib-package', $output);
366+
367+
// Cleanup
368+
putenv('SYMFONY_ALLOW_CONTRIB');
369+
}
370+
316371
public function testInstallWithPackageJsonToSynchronizeSkipped()
317372
{
318373
$io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE);

0 commit comments

Comments
 (0)