Skip to content

Commit

Permalink
Support chromedriver archive with license file (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: Yves Blatti <[email protected]>
  • Loading branch information
dbrekelmans and Yves Blatti authored Jan 16, 2023
1 parent e66de2b commit ca229af
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
1 change: 0 additions & 1 deletion src/Archive/TarGzExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public function extract(string $archive, string $destination): array
{
$tarGzData = new PharData($archive);
$tarData = $tarGzData->decompress();
/** @phpstan-ignore-next-line */
$tarData->extractTo($destination, null, true);

$extractedFilenames = [];
Expand Down
45 changes: 32 additions & 13 deletions src/Driver/ChromeDriver/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;
use UnexpectedValueException;

use function count;
use function array_map;
use function in_array;
use function Safe\fclose;
use function Safe\fopen;
use function Safe\fwrite;
use function Safe\sprintf;
use function str_replace;
use function sys_get_temp_dir;

use const DIRECTORY_SEPARATOR;
Expand All @@ -43,11 +45,15 @@ final class Downloader implements DownloaderInterface
/** @var Extractor */
private $archiveExtractor;

/** @var string */
private $tempDir;

public function __construct(Filesystem $filesystem, HttpClientInterface $httpClient, Extractor $archiveExtractor)
{
$this->filesystem = $filesystem;
$this->httpClient = $httpClient;
$this->archiveExtractor = $archiveExtractor;
$this->tempDir = sys_get_temp_dir();
}

public function supports(Driver $driver): bool
Expand All @@ -67,7 +73,7 @@ public function download(Driver $driver, string $location): string
}

try {
$binary = $this->extractArchive($archive);
$binary = $this->extractArchive($archive, $driver->operatingSystem());
} catch (IOException | RuntimeException $exception) {
throw new RuntimeException('Something went wrong extracting the chromedriver archive.', 0, $exception);
}
Expand Down Expand Up @@ -110,7 +116,7 @@ public function download(Driver $driver, string $location): string
*/
private function downloadArchive(Driver $driver): string
{
$temporaryFile = $this->filesystem->tempnam(sys_get_temp_dir(), 'chromedriver', '.zip');
$temporaryFile = $this->filesystem->tempnam($this->tempDir, 'chromedriver', '.zip');

$response = $this->httpClient->request(
'GET',
Expand Down Expand Up @@ -165,19 +171,27 @@ private function getBinaryName(Driver $driver): string
* @throws RuntimeException
* @throws IOException
*/
private function extractArchive(string $archive): string
private function extractArchive(string $archive, OperatingSystem $operatingSystem): string
{
$unzipLocation = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'chromedriver';
$unzipLocation = $this->tempDir . DIRECTORY_SEPARATOR . 'chromedriver';
$extractedFiles = $this->archiveExtractor->extract($archive, $unzipLocation);

$count = count($extractedFiles);
if ($count !== 1) {
throw new UnexpectedValueException(sprintf('Expected exactly one file in the archive. Found %d', $count));
$filePath = $this->getFilePath($unzipLocation, $operatingSystem);
if (
in_array(
$this->getFileName($operatingSystem),
array_map(function ($file): string {
return str_replace($this->tempDir . DIRECTORY_SEPARATOR, '', $file);
}, $extractedFiles),
true
)
) {
throw new UnexpectedValueException(sprintf('Could not find "%s" in the extracted files.', $filePath));
}

$file = $this->filesystem->readlink($extractedFiles[0], true);
$file = $this->filesystem->readlink($filePath, true);
if ($file === null) {
throw new RuntimeException(sprintf('Could not read link %s', $extractedFiles[0]));
throw new RuntimeException(sprintf('Could not read link %s', $filePath));
}

$this->filesystem->remove($archive);
Expand All @@ -187,12 +201,17 @@ private function extractArchive(string $archive): string

private function getFilePath(string $location, OperatingSystem $operatingSystem): string
{
$filePath = $location . DIRECTORY_SEPARATOR . 'chromedriver';
return $location . DIRECTORY_SEPARATOR . $this->getFileName($operatingSystem);
}

private function getFileName(OperatingSystem $operatingSystem): string
{
$fileName = 'chromedriver';

if ($operatingSystem->equals(OperatingSystem::WINDOWS())) {
$filePath .= '.exe';
$fileName .= '.exe';
}

return $filePath;
return $fileName;
}
}
4 changes: 2 additions & 2 deletions tests/Driver/ChromeDriver/DownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use DBrekelmans\BrowserDriverInstaller\Driver\DriverName;
use DBrekelmans\BrowserDriverInstaller\OperatingSystem\OperatingSystem;
use DBrekelmans\BrowserDriverInstaller\Version;
use PHPStan\Testing\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Contracts\HttpClient\HttpClientInterface;

Expand Down Expand Up @@ -113,6 +113,6 @@ private function mockFsAndArchiveExtractorForSuccessfulDownload(): void

$this->archiveExtractor
->method('extract')
->willReturn(['./chromedriver']);
->willReturn(['./chromedriver', './LICENSE.chromedriver']);
}
}

0 comments on commit ca229af

Please sign in to comment.