|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CaptainHook. |
| 5 | + * |
| 6 | + * (c) Sebastian Feldmann <sf@sebastian-feldmann.info> |
| 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\HookInstaller; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use RuntimeException; |
| 17 | + |
| 18 | +class GitDirParseTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @link https://github.com/captainhook-git/hook-installer/pull/5#discussion_r3729235534 |
| 22 | + */ |
| 23 | + public static function gitDirFormatsInvalidData(): array |
| 24 | + { |
| 25 | + return [ |
| 26 | + 'no space after colon' => [ |
| 27 | + "gitdir:/foo/.bare\r", |
| 28 | + ], |
| 29 | + |
| 30 | + 'invalid whitespace after colon' => [ |
| 31 | + "gitdir:\n/foo/.bare\n\n", |
| 32 | + ], |
| 33 | + |
| 34 | + 'text before gitdir' => [ |
| 35 | + "header\ngitdir: /foo/.bare", |
| 36 | + ], |
| 37 | + ]; |
| 38 | + } |
| 39 | + |
| 40 | + #[DataProvider('gitDirFormatsInvalidData')] |
| 41 | + public function testParseGitDirInvalid(string $input): void |
| 42 | + { |
| 43 | + $this->expectException(RuntimeException::class); |
| 44 | + $this->expectExceptionMessage('invalid .git file'); |
| 45 | + |
| 46 | + $path = $this->writeTempDotGit($input); |
| 47 | + |
| 48 | + DotGit::searchInPath($path); |
| 49 | + } |
| 50 | + |
| 51 | + private function writeTempDotGit(string $content): string |
| 52 | + { |
| 53 | + $dir = $this->createTempDir(); |
| 54 | + |
| 55 | + if (file_put_contents("$dir/.git", $content) === false) { |
| 56 | + throw new RuntimeException('Failed to create temporary git path'); |
| 57 | + } |
| 58 | + |
| 59 | + return $dir; |
| 60 | + } |
| 61 | + |
| 62 | + private function createTempDir(): string |
| 63 | + { |
| 64 | + $path = tempnam(sys_get_temp_dir(), 'captainhook-hook-installer-'); |
| 65 | + |
| 66 | + if ($path === false) { |
| 67 | + throw new RuntimeException('Failed to create temporary path'); |
| 68 | + } |
| 69 | + |
| 70 | + if (unlink($path) === false) { |
| 71 | + throw new RuntimeException('Failed to delete temporary path'); |
| 72 | + } |
| 73 | + |
| 74 | + if (mkdir($path, 0700) === false) { |
| 75 | + throw new RuntimeException('Failed to create temporary directory'); |
| 76 | + } |
| 77 | + |
| 78 | + return $path; |
| 79 | + } |
| 80 | +} |
0 commit comments