Skip to content

Commit

Permalink
Add support for macOS AArch64 (M1) binary
Browse files Browse the repository at this point in the history
So much for the comments in #11
as Geckodriver soon after added an ARM binary download. It still doesn't
offer an Intel-specific binary so Laravel Dusk's OperatingSystem::id()
method still requires a userland (packageland?) replacement to exclude
OS detection for 'mac-intel'.
  • Loading branch information
derekmd committed Apr 14, 2021
1 parent 3370153 commit dad10f6
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/Console/FirefoxDriverCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FirefoxDriverCommand extends Command
*
* @var string
*/
protected $latestVersion = 'v0.29.0';
protected $latestVersion = 'v0.29.1';

/**
* URL to discover latest release version.
Expand Down Expand Up @@ -130,9 +130,10 @@ protected function binaryStubNames()
return collect([
'linux' => 'linux64.tar.gz',
'mac' => 'macos.tar.gz',
'mac-arm' => 'macos-aarch64.tar.gz',
'win' => 'win64.zip',
])->unless($this->option('all'), function ($items) {
return $items->only(OperatingSystem::parentId());
return $items->only(OperatingSystem::geckodriverId());
})->all();
}

Expand Down
23 changes: 17 additions & 6 deletions src/Firefox/FirefoxProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Derekmd\Dusk\Firefox;

use Laravel\Dusk\OperatingSystem;
use Derekmd\Dusk\OperatingSystem;
use RuntimeException;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -38,12 +38,13 @@ public function toProcess(array $arguments = [])
{
if ($this->driver) {
$driver = $this->driver;
} elseif ($this->onWindows()) {
$driver = __DIR__.'/../../bin/geckodriver-win.exe';
} elseif ($this->onMac()) {
$driver = __DIR__.'/../../bin/geckodriver-mac';
} else {
$driver = __DIR__.'/../../bin/geckodriver-linux';
$driver = __DIR__.'/../../bin/'.[
'linux' => 'geckodriver-linux',
'mac' => 'geckodriver-mac',
'mac-arm' => 'geckodriver-mac-arm',
'win' => 'geckodriver-win.exe',
][$this->operatingSystemId()];
}

$this->driver = realpath($driver);
Expand Down Expand Up @@ -104,4 +105,14 @@ protected function onMac()
{
return OperatingSystem::onMac();
}

/**
* Determine OS ID.
*
* @return string
*/
protected function operatingSystemId()
{
return OperatingSystem::geckodriverId();
}
}
21 changes: 17 additions & 4 deletions src/OperatingSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,26 @@
class OperatingSystem extends BaseOperatingSystem
{
/**
* Get the identifier of the current operating system, exclusive of
* the architecture that OperatingSystem::id() returns.
* Get the identifier of the current operating system for Geckodriver
* binary discovery. This excludes 'mac-intel' that OperatorSystem::id()
* returns for Chromedriver.
*
* @return string
*/
public static function parentId()
public static function geckodriverId()
{
return static::onWindows() ? 'win' : (static::onMac() ? 'mac' : 'linux');
if (static::onWindows()) {
return 'win';
}

if (static::onMac()) {
if (php_uname('m') === 'arm64') {
return 'mac-arm';
}

return 'mac';
}

return 'linux';
}
}
26 changes: 26 additions & 0 deletions tests/FirefoxDriverCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ protected function archiveFilename()
case 'Windows':
return 'geckodriver-'.static::VERSION.'-win64.zip';
case 'Darwin':
if (php_uname('m') === 'arm64') {
return 'geckodriver-'.static::VERSION.'-macos-aarch64.tar.gz';
}

return 'geckodriver-'.static::VERSION.'-macos.tar.gz';
default:
return 'geckodriver-'.static::VERSION.'-linux64.tar.gz';
Expand All @@ -63,6 +67,10 @@ protected function binaryFilename()
case 'Windows':
return 'geckodriver-win.exe';
case 'Darwin':
if (php_uname('m') === 'arm64') {
return 'geckodriver-mac-arm';
}

return 'geckodriver-mac';
default:
return 'geckodriver-linux';
Expand All @@ -75,6 +83,10 @@ protected function os()
case 'Windows':
return 'win';
case 'Darwin':
if (php_uname('m') === 'arm64') {
return 'mac-arm';
}

return 'mac';
default:
return 'linux';
Expand Down Expand Up @@ -185,6 +197,17 @@ public function test_it_can_download_geckodriver_for_linux_mac_and_windows()
return $this->copyMockBinary('geckodriver-'.static::VERSION.'-macos.tar.gz');
});

$http->shouldReceive('request')->with(
'GET',
vsprintf('https://github.com/mozilla/geckodriver/releases/download/%s/geckodriver-%s-macos-aarch64.tar.gz', [
static::VERSION,
static::VERSION,
]),
['sink' => $this->tempDir.'/geckodriver-'.static::VERSION.'-macos-aarch64.tar.gz']
)->andReturnUsing(function () {
return $this->copyMockBinary('geckodriver-'.static::VERSION.'-macos-aarch64.tar.gz');
});

$http->shouldReceive('request')->with(
'GET',
vsprintf('https://github.com/mozilla/geckodriver/releases/download/%s/geckodriver-%s-linux64.tar.gz', [
Expand All @@ -209,6 +232,9 @@ public function test_it_can_download_geckodriver_for_linux_mac_and_windows()
$this->assertFileDoesNotExist($this->tempDir.'/geckodriver-'.static::VERSION.'-macos.tar.gz');
$this->assertStringEqualsFile($this->tempDir.'/geckodriver-mac', 'foo');

$this->assertFileDoesNotExist($this->tempDir.'/geckodriver-'.static::VERSION.'-macos-aarch64.tar.gz');
$this->assertStringEqualsFile($this->tempDir.'/geckodriver-mac-arm', 'foo');

$this->assertFileDoesNotExist($this->tempDir.'/geckodriver-'.static::VERSION.'-linux64.tar.gz');
$this->assertStringEqualsFile($this->tempDir.'/geckodriver-linux', 'foo');
}
Expand Down
41 changes: 41 additions & 0 deletions tests/FirefoxProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function test_build_process_for_darwin()
$this->assertStringContainsString('geckodriver-mac', $process->getCommandLine());
}

public function test_build_process_for_mac_arm_m1()
{
$process = (new FirefoxProcessM1)->toProcess();

$this->assertInstanceOf(Process::class, $process);
$this->assertStringContainsString('geckodriver-mac-arm', $process->getCommandLine());
}

public function test_build_process_for_linux()
{
$process = (new FirefoxProcessLinux)->toProcess();
Expand All @@ -61,6 +69,11 @@ protected function onWindows()
{
return true;
}

protected function operatingSystemId()
{
return 'win';
}
}

class FirefoxProcessDarwin extends FirefoxProcess
Expand All @@ -74,6 +87,29 @@ protected function onWindows()
{
return false;
}

protected function operatingSystemId()
{
return 'mac';
}
}

class FirefoxProcessM1 extends FirefoxProcess
{
protected function onMac()
{
return true;
}

protected function onWindows()
{
return false;
}

protected function operatingSystemId()
{
return 'mac-arm';
}
}

class FirefoxProcessLinux extends FirefoxProcess
Expand All @@ -87,4 +123,9 @@ protected function onWindows()
{
return false;
}

protected function operatingSystemId()
{
return 'linux';
}
}
Binary file not shown.
Binary file not shown.

0 comments on commit dad10f6

Please sign in to comment.