Skip to content

Commit

Permalink
Prevent PHAR from crashing unnecessarily
Browse files Browse the repository at this point in the history
If no autoloader is set the PHAR file does not need a bootstrap file.
As long as you are only using Cap'n functionality you are good to go.
This fix makes sure the Cap'n only returns an error if a bootstrap
file is not available that is set explicitly.

Fix issue #248
  • Loading branch information
sebastianfeldmann committed Jul 7, 2024
1 parent dcadeb2 commit 04676c9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,14 @@ public function getGitDirectory(): string
/**
* Return bootstrap file if configured, CWD/vendor/autoload.php by default
*
* @param string $default
* @return string
*/
public function getBootstrap(): string
public function getBootstrap(string $default = 'vendor/autoload.php'): string
{
return !empty($this->settings[self::SETTING_BOOTSTRAP])
? $this->settings[self::SETTING_BOOTSTRAP]
: 'vendor/autoload.php';
: $default;
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/Console/Command/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
private function handleBootstrap(Config $config): void
{
// we only have to care about bootstrapping PHAR builds because for
// Composer installs the bootstrapping is already done in the bin script
if ($this->resolver->isPharRelease()) {
// check the custom and default autoloader
$bootstrapFile = dirname($config->getPath()) . '/' . $config->getBootstrap();
if (!file_exists($bootstrapFile) && $config->getBootstrap() !== 'vendor/autoload.php') {
if (!file_exists($bootstrapFile)) {
// since the phar is doing its own autoloading we don't need to do anything
// if the bootstrap file is not actively set
if (empty($config->getBootstrap(''))) {
return;
}
throw new RuntimeException('bootstrap file not found');
}
// the bootstrap file exists so lets load it
try {
require $bootstrapFile;
} catch (Throwable $t) {
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/Console/Command/Hook/PreCommitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
use CaptainHook\App\Console\Runtime\Resolver;
use CaptainHook\App\Git\DummyRepo;
use Exception;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\NullOutput;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\OutputInterface;

class PreCommitTest extends TestCase
{
Expand Down Expand Up @@ -119,6 +122,27 @@ public function testExecutePharBootstrapNotFound(): void
$this->assertEquals(1, $cmd->run($input, $output));
}

/**
* Tests PreCommit::run
*/
public function testExecutePharBootstrapNotSet(): void
{
$resolver = $this->createMock(Resolver::class);
$resolver->expects($this->once())->method('isPharRelease')->willReturn(true);

$repo = new DummyRepo();
$output = new NullOutput();
$input = new ArrayInput(
[
'--configuration' => CH_PATH_FILES . '/config/empty.json',
'--git-directory' => $repo->getGitDir()
]
);

$cmd = new PreCommit($resolver);
$this->assertEquals(0, $cmd->run($input, $output));
}

/**
* Tests PreCommit::run
*
Expand Down

0 comments on commit 04676c9

Please sign in to comment.