Skip to content

Commit

Permalink
Add stdIn placeholder
Browse files Browse the repository at this point in the history
In order to forward hooks that require stdIn like pre-push it is
necessary to allow access to the original hook stdIn data.

You can now use {$STDIN} in action configurations like this:

  echo {$STDIN} | hooks/git-lfs/pre-push

Fixes issue #256
  • Loading branch information
sebastianfeldmann committed Sep 5, 2024
1 parent 454d537 commit 90deb99
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Runner/Action/Cli/Command/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Formatter
'staged_files' => Placeholder\StagedFiles::class,
'changed_files' => Placeholder\ChangedFiles::class,
'branch_files' => Placeholder\BranchFiles::class,
'stdin' => Placeholder\StdIn::class,
];

/**
Expand Down
36 changes: 36 additions & 0 deletions src/Runner/Action/Cli/Command/Placeholder/StdIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of CaptainHook.
*
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CaptainHook\App\Runner\Action\Cli\Command\Placeholder;

/**
* Class StdIn
*
* @package CaptainHook
* @author Sebastian Feldmann <sf@sebastian-feldmann.info>
* @link https://github.com/captainhookphp/captainhook
* @since Class available since Release 5.23.5
*/
class StdIn extends Foundation
{
/**
* Return the original hook stdIn (shell escaped)
*
* Returns at least ''
*
* @param array<string, mixed> $options
* @return string
*/
public function replacement(array $options): string
{
return escapeshellarg(implode(PHP_EOL, $this->io->getStandardInput()));
}
}
4 changes: 3 additions & 1 deletion tests/unit/Runner/Action/Cli/Command/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ public function testCachedPlaceholder(): void
$index = $this->createGitIndexOperator(['foo/file1.php', 'bar/file2.php', 'baz/file3.php']);
$io->method('getArguments')->willReturn([]);
$config->method('getGitDirectory')->willReturn('./');
$repo->expects($this->atLeast(1))->method('getIndexOperator')->willReturn($index);
$repo->expects($this->exactly(3))->method('getIndexOperator')->willReturn($index);

$formatter = new Formatter($io, $config, $repo);
$command1 = $formatter->format('cmd1 argument {$STAGED_FILES|in-dir:foo} {$STAGED_FILES|in-dir:baz}');
$command2 = $formatter->format('cmd2 argument {$STAGED_FILES}');
$command3 = $formatter->format('cmd3 argument {$STAGED_FILES}');

$this->assertEquals('cmd1 argument foo/file1.php baz/file3.php', $command1);
$this->assertEquals('cmd2 argument foo/file1.php bar/file2.php baz/file3.php', $command2);
$this->assertEquals('cmd3 argument foo/file1.php bar/file2.php baz/file3.php', $command3);
}


Expand Down
58 changes: 58 additions & 0 deletions tests/unit/Runner/Action/Cli/Command/Placeholder/StdInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* This file is part of CaptainHook.
*
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CaptainHook\App\Runner\Action\Cli\Command\Placeholder;

use CaptainHook\App\Console\IO\Mockery as IOMockery;
use CaptainHook\App\Config\Mockery as ConfigMockery;
use CaptainHook\App\Mockery as AppMockery;
use PHPUnit\Framework\TestCase;

class StdInTest extends TestCase
{
use IOMockery;
use AppMockery;
use ConfigMockery;

/**
* Tests StdIn::replacement
*/
public function testStdInValue(): void
{
$expected = 'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409'
. ' refs/heads/main 8309f6e16097754469c485e604900c573bf2c5d8';

$io = $this->createIOMock();
$repo = $this->createRepositoryMock();
$config = $this->createConfigMock();
$io->expects($this->once())->method('getStandardInput')->willReturn([$expected]);

$placeholder = new StdIn($io, $config, $repo);
$result = $placeholder->replacement([]);

$this->assertEquals(escapeshellarg($expected), $result);
}

/**
* Tests StdIn::replacement
*/
public function testEmptyStdIn(): void
{
$io = $this->createIOMock();
$repo = $this->createRepositoryMock();
$config = $this->createConfigMock();
$io->method('getStandardInput')->willReturn([]);

$placeholder = new StdIn($io, $config, $repo);
$result = $placeholder->replacement([]);
$this->assertEquals("''", $result);
}
}

0 comments on commit 90deb99

Please sign in to comment.