|
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\Hook\Template\Local; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use SebastianFeldmann\Camino\Path\Directory; |
|
16
|
|
|
use SebastianFeldmann\Camino\Path\File; |
|
17
|
|
|
|
|
18
|
|
|
class ShellTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Tests Shell::getCode |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testTemplate(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$repo = new Directory('/foo/bar'); |
|
26
|
|
|
$config = new File('/foo/bar/captainhook.json'); |
|
27
|
|
|
$executable = new File('/foo/bar/vendor/bin/captainhook'); |
|
28
|
|
|
$bootstrap = 'vendor/autoload.php'; |
|
29
|
|
|
|
|
30
|
|
|
$template = new Shell($repo, $config, $executable, $bootstrap, false); |
|
31
|
|
|
$code = $template->getCode('commit-msg'); |
|
32
|
|
|
|
|
33
|
|
|
$this->assertStringContainsString('#!/bin/sh', $code); |
|
34
|
|
|
$this->assertStringContainsString('commit-msg', $code); |
|
35
|
|
|
$this->assertStringContainsString('vendor/bin/captainhook $INTERACTIVE', $code); |
|
36
|
|
|
$this->assertStringContainsString($this->getTtyRedirectionLines(), $code); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Tests Shell::getCode |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testTemplateExtExecutable(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$repo = new Directory('/foo/bar'); |
|
45
|
|
|
$config = new File('/foo/bar/captainhook.json'); |
|
46
|
|
|
$executable = new File('/usr/local/bin/captainhook'); |
|
47
|
|
|
$bootstrap = 'vendor/autoload.php'; |
|
48
|
|
|
|
|
49
|
|
|
$template = new Shell($repo, $config, $executable, $bootstrap, false); |
|
50
|
|
|
$code = $template->getCode('commit-msg'); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertStringContainsString('#!/bin/sh', $code); |
|
53
|
|
|
$this->assertStringContainsString('commit-msg', $code); |
|
54
|
|
|
$this->assertStringContainsString('/usr/local/bin/captainhook $INTERACTIVE', $code); |
|
55
|
|
|
$this->assertStringContainsString($this->getTtyRedirectionLines(), $code); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function getTtyRedirectionLines(): string |
|
59
|
|
|
{ |
|
60
|
|
|
return <<<'EOD' |
|
61
|
|
|
INTERACTIVE="--no-interaction" |
|
62
|
|
|
if [ -t 1 ]; then |
|
63
|
|
|
# If we're in a terminal, redirect stdout and stderr to /dev/tty and |
|
64
|
|
|
# read stdin from /dev/tty. Allow interactive mode for CaptainHook. |
|
65
|
|
|
exec >/dev/tty 2>/dev/tty </dev/tty |
|
66
|
|
|
INTERACTIVE="" |
|
67
|
|
|
fi |
|
68
|
|
|
EOD; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|