-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
366 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
use Composer\Script\Event; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
class Google_Task_Composer | ||
{ | ||
/** | ||
* @param Event $event Composer event passed in for any script method | ||
* @param FilesystemInterface $filesystem Optional. Used for testing. | ||
*/ | ||
public static function cleanup( | ||
Event $event, | ||
Filesystem $filesystem = null | ||
) { | ||
$composer = $event->getComposer(); | ||
$extra = $composer->getPackage()->getExtra(); | ||
$servicesToKeep = isset($extra['google/apiclient-services']) ? | ||
$extra['google/apiclient-services'] : []; | ||
if ($servicesToKeep) { | ||
$serviceDir = sprintf( | ||
'%s/google/apiclient-services/src/Google/Service', | ||
$composer->getConfig()->get('vendor-dir') | ||
); | ||
self::verifyServicesToKeep($serviceDir, $servicesToKeep); | ||
$finder = self::getServicesToRemove($serviceDir, $servicesToKeep); | ||
$filesystem = $filesystem ?: new Filesystem(); | ||
if (0 !== $count = count($finder)) { | ||
$event->getIO()->write( | ||
sprintf( | ||
'Removing %s google services', | ||
$count | ||
) | ||
); | ||
foreach ($finder as $file) { | ||
$realpath = $file->getRealPath(); | ||
$filesystem->remove($realpath); | ||
$filesystem->remove($realpath . '.php'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException when the service doesn't exist | ||
*/ | ||
private static function verifyServicesToKeep( | ||
$serviceDir, | ||
array $servicesToKeep | ||
) { | ||
$finder = (new Finder()) | ||
->directories() | ||
->depth('== 0'); | ||
|
||
foreach ($servicesToKeep as $service) { | ||
if (!preg_match('/^[a-zA-Z0-9]*$/', $service)) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'Invalid Google service name "%s"', | ||
$service | ||
) | ||
); | ||
} | ||
try { | ||
$finder->in($serviceDir . '/' . $service); | ||
} catch (\InvalidArgumentException $e) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'Google service "%s" does not exist or was removed previously', | ||
$service | ||
) | ||
); | ||
} | ||
} | ||
} | ||
|
||
private static function getServicesToRemove( | ||
$serviceDir, | ||
array $servicesToKeep | ||
) { | ||
// find all files in the current directory | ||
return (new Finder()) | ||
->directories() | ||
->depth('== 0') | ||
->in($serviceDir) | ||
->exclude($servicesToKeep); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
<?php | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
class Google_Task_ComposerTest extends BaseTest | ||
{ | ||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedExceptionMessage Google service "Foo" does not exist | ||
*/ | ||
public function testInvalidServiceName() | ||
{ | ||
Google_Task_Composer::cleanup($this->createMockEvent(['Foo'])); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedExceptionMessage Invalid Google service name "../YouTube" | ||
*/ | ||
public function testRelatePathServiceName() | ||
{ | ||
Google_Task_Composer::cleanup($this->createMockEvent(['../YouTube'])); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedExceptionMessage Google service "" does not exist | ||
*/ | ||
public function testEmptyServiceName() | ||
{ | ||
Google_Task_Composer::cleanup($this->createMockEvent([''])); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedExceptionMessage Invalid Google service name "YouTube*" | ||
*/ | ||
public function testWildcardServiceName() | ||
{ | ||
Google_Task_Composer::cleanup($this->createMockEvent(['YouTube*'])); | ||
} | ||
|
||
public function testRemoveServices() | ||
{ | ||
$vendorDir = sys_get_temp_dir() . '/rand-' . rand(); | ||
$serviceDir = sprintf( | ||
'%s/google/apiclient-services/src/Google/Service/', | ||
$vendorDir | ||
); | ||
$dirs = [ | ||
'ServiceToKeep', | ||
'ServiceToDelete1', | ||
'ServiceToDelete2', | ||
]; | ||
$files = [ | ||
'ServiceToKeep/ServiceFoo.php', | ||
'ServiceToKeep.php', | ||
'SomeRandomFile.txt', | ||
'ServiceToDelete1/ServiceFoo.php', | ||
'ServiceToDelete1.php', | ||
'ServiceToDelete2/ServiceFoo.php', | ||
'ServiceToDelete2.php', | ||
]; | ||
foreach ($dirs as $dir) { | ||
@mkdir($serviceDir . $dir, 0777, true); | ||
} | ||
foreach ($files as $file) { | ||
touch($serviceDir . $file); | ||
} | ||
$print = 'Removing 2 google services'; | ||
Google_Task_Composer::cleanup( | ||
$this->createMockEvent(['ServiceToKeep'], $vendorDir, $print), | ||
$this->createMockFilesystem([ | ||
'ServiceToDelete2', | ||
'ServiceToDelete2.php', | ||
'ServiceToDelete1', | ||
'ServiceToDelete1.php', | ||
], $serviceDir) | ||
); | ||
} | ||
|
||
private function createMockFilesystem(array $files, $serviceDir) | ||
{ | ||
$mockFilesystem = $this->prophesize('Symfony\Component\Filesystem\Filesystem'); | ||
foreach ($files as $filename) { | ||
$file = new \SplFileInfo($serviceDir . $filename); | ||
$mockFilesystem->remove($file->getRealPath()) | ||
->shouldBeCalledTimes(1); | ||
} | ||
|
||
return $mockFilesystem->reveal(); | ||
} | ||
|
||
private function createMockEvent( | ||
array $servicesToKeep, | ||
$vendorDir = '', | ||
$print = null | ||
) { | ||
$mockPackage = $this->prophesize('Composer\Package\RootPackage'); | ||
$mockPackage->getExtra() | ||
->shouldBeCalledTimes(1) | ||
->willReturn(['google/apiclient-services' => $servicesToKeep]); | ||
|
||
$mockConfig = $this->prophesize('Composer\Config'); | ||
$mockConfig->get('vendor-dir') | ||
->shouldBeCalledTimes(1) | ||
->willReturn($vendorDir); | ||
|
||
$mockComposer = $this->prophesize('Composer\Composer'); | ||
$mockComposer->getPackage() | ||
->shouldBeCalledTimes(1) | ||
->willReturn($mockPackage->reveal()); | ||
$mockComposer->getConfig() | ||
->shouldBeCalledTimes(1) | ||
->willReturn($mockConfig->reveal()); | ||
|
||
$mockEvent = $this->prophesize('Composer\Script\Event'); | ||
$mockEvent->getComposer() | ||
->shouldBeCalledTimes(1) | ||
->willReturn($mockComposer); | ||
|
||
if ($print) { | ||
$mockIO = $this->prophesize('Composer\IO\ConsoleIO'); | ||
$mockIO->write($print) | ||
->shouldBeCalledTimes(1); | ||
|
||
$mockEvent->getIO() | ||
->shouldBeCalledTimes(1) | ||
->willReturn($mockIO->reveal()); | ||
} | ||
|
||
return $mockEvent->reveal(); | ||
} | ||
|
||
public function testE2E() | ||
{ | ||
$composerJson = json_encode([ | ||
'require' => [ | ||
'google/apiclient' => 'dev-add-composer-cleanup' | ||
], | ||
'scripts' => [ | ||
'post-update-cmd' => 'Google_Task_Composer::cleanup' | ||
], | ||
'extra' => [ | ||
'google/apiclient-services' => [ | ||
'Drive', | ||
'YouTube' | ||
] | ||
] | ||
]); | ||
|
||
$tmpDir = sys_get_temp_dir() . '/test-' . rand(); | ||
$serviceDir = $tmpDir . '/vendor/google/apiclient-services/src/Google/Service'; | ||
|
||
mkdir($tmpDir); | ||
file_put_contents($tmpDir . '/composer.json', $composerJson); | ||
passthru('composer install -d ' . $tmpDir); | ||
|
||
$this->assertFileExists($serviceDir . '/Drive.php'); | ||
$this->assertFileExists($serviceDir . '/Drive'); | ||
$this->assertFileExists($serviceDir . '/YouTube.php'); | ||
$this->assertFileExists($serviceDir . '/YouTube'); | ||
$this->assertFileNotExists($serviceDir . '/YouTubeReporting.php'); | ||
$this->assertFileNotExists($serviceDir . '/YouTubeReporting'); | ||
|
||
$composerJson = json_encode([ | ||
'require' => [ | ||
'google/apiclient' => 'dev-add-composer-cleanup' | ||
], | ||
'scripts' => [ | ||
'post-update-cmd' => 'Google_Task_Composer::cleanup' | ||
], | ||
'extra' => [ | ||
'google/apiclient-services' => [ | ||
'Drive', | ||
'YouTube', | ||
'YouTubeReporting', | ||
] | ||
] | ||
]); | ||
|
||
file_put_contents($tmpDir . '/composer.json', $composerJson); | ||
passthru('rm -r ' . $tmpDir . '/vendor/google/apiclient-services'); | ||
passthru('composer update -d ' . $tmpDir); | ||
|
||
$this->assertFileExists($serviceDir . '/Drive.php'); | ||
$this->assertFileExists($serviceDir . '/Drive'); | ||
$this->assertFileExists($serviceDir . '/YouTube.php'); | ||
$this->assertFileExists($serviceDir . '/YouTube'); | ||
$this->assertFileExists($serviceDir . '/YouTubeReporting.php'); | ||
$this->assertFileExists($serviceDir . '/YouTubeReporting'); | ||
} | ||
} |