Skip to content

Commit 90deb99

Browse files
Add stdIn placeholder
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
1 parent 454d537 commit 90deb99

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

src/Runner/Action/Cli/Command/Formatter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Formatter
6060
'staged_files' => Placeholder\StagedFiles::class,
6161
'changed_files' => Placeholder\ChangedFiles::class,
6262
'branch_files' => Placeholder\BranchFiles::class,
63+
'stdin' => Placeholder\StdIn::class,
6364
];
6465

6566
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook.
5+
*
6+
* (c) Sebastian Feldmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Runner\Action\Cli\Command\Placeholder;
13+
14+
/**
15+
* Class StdIn
16+
*
17+
* @package CaptainHook
18+
* @author Sebastian Feldmann <[email protected]>
19+
* @link https://github.com/captainhookphp/captainhook
20+
* @since Class available since Release 5.23.5
21+
*/
22+
class StdIn extends Foundation
23+
{
24+
/**
25+
* Return the original hook stdIn (shell escaped)
26+
*
27+
* Returns at least ''
28+
*
29+
* @param array<string, mixed> $options
30+
* @return string
31+
*/
32+
public function replacement(array $options): string
33+
{
34+
return escapeshellarg(implode(PHP_EOL, $this->io->getStandardInput()));
35+
}
36+
}

tests/unit/Runner/Action/Cli/Command/FormatterTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,16 @@ public function testCachedPlaceholder(): void
6565
$index = $this->createGitIndexOperator(['foo/file1.php', 'bar/file2.php', 'baz/file3.php']);
6666
$io->method('getArguments')->willReturn([]);
6767
$config->method('getGitDirectory')->willReturn('./');
68-
$repo->expects($this->atLeast(1))->method('getIndexOperator')->willReturn($index);
68+
$repo->expects($this->exactly(3))->method('getIndexOperator')->willReturn($index);
6969

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

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

7880

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook.
5+
*
6+
* (c) Sebastian Feldmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Runner\Action\Cli\Command\Placeholder;
13+
14+
use CaptainHook\App\Console\IO\Mockery as IOMockery;
15+
use CaptainHook\App\Config\Mockery as ConfigMockery;
16+
use CaptainHook\App\Mockery as AppMockery;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class StdInTest extends TestCase
20+
{
21+
use IOMockery;
22+
use AppMockery;
23+
use ConfigMockery;
24+
25+
/**
26+
* Tests StdIn::replacement
27+
*/
28+
public function testStdInValue(): void
29+
{
30+
$expected = 'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409'
31+
. ' refs/heads/main 8309f6e16097754469c485e604900c573bf2c5d8';
32+
33+
$io = $this->createIOMock();
34+
$repo = $this->createRepositoryMock();
35+
$config = $this->createConfigMock();
36+
$io->expects($this->once())->method('getStandardInput')->willReturn([$expected]);
37+
38+
$placeholder = new StdIn($io, $config, $repo);
39+
$result = $placeholder->replacement([]);
40+
41+
$this->assertEquals(escapeshellarg($expected), $result);
42+
}
43+
44+
/**
45+
* Tests StdIn::replacement
46+
*/
47+
public function testEmptyStdIn(): void
48+
{
49+
$io = $this->createIOMock();
50+
$repo = $this->createRepositoryMock();
51+
$config = $this->createConfigMock();
52+
$io->method('getStandardInput')->willReturn([]);
53+
54+
$placeholder = new StdIn($io, $config, $repo);
55+
$result = $placeholder->replacement([]);
56+
$this->assertEquals("''", $result);
57+
}
58+
}

0 commit comments

Comments
 (0)