|
39 | 39 | use Symfony\Component\Config\FileLocatorInterface; |
40 | 40 | use Symfony\Component\Console\Input\InputInterface; |
41 | 41 | use Symfony\Component\Console\Output\OutputInterface; |
| 42 | +use Symfony\Component\Filesystem\Exception\IOException; |
42 | 43 | use Symfony\Component\Finder\Finder; |
43 | 44 |
|
44 | 45 | use function Safe\mkdir; |
|
48 | 49 |
|
49 | 50 | #[CoversClass(GitHooksCommand::class)] |
50 | 51 | #[UsesClass(FileDiff::class)] |
| 52 | +#[UsesClass(IOException::class)] |
51 | 53 | #[UsesTrait(LogsCommandResults::class)] |
52 | 54 | final class GitHooksCommandTest extends TestCase |
53 | 55 | { |
@@ -164,9 +166,9 @@ public function executeWillCopyPackagedHooks(): void |
164 | 166 | ->willReturn('/app/.git/hooks'); |
165 | 167 | $this->filesystem->exists('/app/.git/hooks/post-merge') |
166 | 168 | ->willReturn(false); |
167 | | - $this->filesystem->copy(Argument::containingString('/post-merge'), '/app/.git/hooks/post-merge', true) |
| 169 | + $this->filesystem->copy(Argument::containingString('/post-merge'), '/app/.git/hooks/post-merge', false) |
168 | 170 | ->shouldBeCalledOnce(); |
169 | | - $this->filesystem->chmod('/app/.git/hooks/post-merge', 755, 0o755) |
| 171 | + $this->filesystem->chmod('/app/.git/hooks/post-merge', 0o755) |
170 | 172 | ->shouldBeCalledOnce(); |
171 | 173 | $this->logger->log('info', 'Installed {hook_name} hook.', Argument::type('array')) |
172 | 174 | ->shouldBeCalledOnce(); |
@@ -327,6 +329,97 @@ public function executeWillSkipReplacingHookWhenInteractiveConfirmationIsDecline |
327 | 329 | self::assertSame(GitHooksCommand::SUCCESS, $this->executeCommand()); |
328 | 330 | } |
329 | 331 |
|
| 332 | + /** |
| 333 | + * @return void |
| 334 | + */ |
| 335 | + #[Test] |
| 336 | + public function executeWillRemoveDriftedHookBeforeReplacingIt(): void |
| 337 | + { |
| 338 | + $this->input->getOption('source') |
| 339 | + ->willReturn('resources/git-hooks'); |
| 340 | + $this->input->getOption('target') |
| 341 | + ->willReturn('.git/hooks'); |
| 342 | + $this->input->getOption('no-overwrite') |
| 343 | + ->willReturn(false); |
| 344 | + |
| 345 | + $this->fileLocator->locate('resources/git-hooks') |
| 346 | + ->willReturn($this->sourceDirectory); |
| 347 | + $this->finderFactory->create() |
| 348 | + ->willReturn(new Finder()) |
| 349 | + ->shouldBeCalledOnce(); |
| 350 | + $this->filesystem->getAbsolutePath('.git/hooks') |
| 351 | + ->willReturn('/app/.git/hooks'); |
| 352 | + $this->filesystem->exists('/app/.git/hooks/post-merge') |
| 353 | + ->willReturn(true); |
| 354 | + $this->fileDiffer->diff(Argument::containingString('/post-merge'), '/app/.git/hooks/post-merge') |
| 355 | + ->willReturn(new FileDiff(FileDiff::STATUS_CHANGED, 'Changed summary', null)) |
| 356 | + ->shouldBeCalledOnce(); |
| 357 | + $this->filesystem->remove('/app/.git/hooks/post-merge') |
| 358 | + ->shouldBeCalledOnce(); |
| 359 | + $this->filesystem->copy(Argument::containingString('/post-merge'), '/app/.git/hooks/post-merge', false) |
| 360 | + ->shouldBeCalledOnce(); |
| 361 | + $this->filesystem->chmod('/app/.git/hooks/post-merge', 0o755) |
| 362 | + ->shouldBeCalledOnce(); |
| 363 | + $this->logger->log('info', 'Installed {hook_name} hook.', Argument::type('array')) |
| 364 | + ->shouldBeCalledOnce(); |
| 365 | + $this->logger->log('info', 'Git hook synchronization completed successfully.', Argument::type('array')) |
| 366 | + ->shouldBeCalledOnce(); |
| 367 | + |
| 368 | + self::assertSame(GitHooksCommand::SUCCESS, $this->executeCommand()); |
| 369 | + } |
| 370 | + |
| 371 | + /** |
| 372 | + * @return void |
| 373 | + */ |
| 374 | + #[Test] |
| 375 | + public function executeWillReportInstallFailureWhenReplacementStillCannotBeWritten(): void |
| 376 | + { |
| 377 | + $this->input->getOption('source') |
| 378 | + ->willReturn('resources/git-hooks'); |
| 379 | + $this->input->getOption('target') |
| 380 | + ->willReturn('.git/hooks'); |
| 381 | + $this->input->getOption('no-overwrite') |
| 382 | + ->willReturn(false); |
| 383 | + |
| 384 | + $this->fileLocator->locate('resources/git-hooks') |
| 385 | + ->willReturn($this->sourceDirectory); |
| 386 | + $this->finderFactory->create() |
| 387 | + ->willReturn(new Finder()) |
| 388 | + ->shouldBeCalledOnce(); |
| 389 | + $this->filesystem->getAbsolutePath('.git/hooks') |
| 390 | + ->willReturn('/app/.git/hooks'); |
| 391 | + $this->filesystem->exists('/app/.git/hooks/post-merge') |
| 392 | + ->willReturn(true); |
| 393 | + $this->fileDiffer->diff(Argument::containingString('/post-merge'), '/app/.git/hooks/post-merge') |
| 394 | + ->willReturn(new FileDiff(FileDiff::STATUS_CHANGED, 'Changed summary', null)) |
| 395 | + ->shouldBeCalledOnce(); |
| 396 | + $this->filesystem->remove('/app/.git/hooks/post-merge') |
| 397 | + ->shouldBeCalledOnce(); |
| 398 | + $this->filesystem->copy(Argument::containingString('/post-merge'), '/app/.git/hooks/post-merge', false) |
| 399 | + ->willThrow(new IOException('Target file could not be opened for writing.', 0, null, '/app/.git/hooks/post-merge')) |
| 400 | + ->shouldBeCalledOnce(); |
| 401 | + $this->filesystem->basename('/app/.git/hooks/post-merge') |
| 402 | + ->willReturn('post-merge') |
| 403 | + ->shouldBeCalledOnce(); |
| 404 | + $this->logger->error( |
| 405 | + 'Failed to install {hook_name} hook automatically. Remove or unlock {hook_path} and rerun git-hooks.', |
| 406 | + Argument::that( |
| 407 | + static fn(array $context): bool => $context['input'] instanceof InputInterface |
| 408 | + && 'post-merge' === $context['hook_name'] |
| 409 | + && '/app/.git/hooks/post-merge' === $context['hook_path'] |
| 410 | + && '/app/.git/hooks/post-merge' === $context['file'] |
| 411 | + && null === $context['line'] |
| 412 | + && str_contains($context['error'], 'Target file could not be opened for writing.') |
| 413 | + ), |
| 414 | + )->shouldBeCalledOnce(); |
| 415 | + $this->logger->error('One or more Git hooks could not be installed automatically.', Argument::type('array')) |
| 416 | + ->shouldBeCalledOnce(); |
| 417 | + $this->filesystem->chmod(Argument::cetera()) |
| 418 | + ->shouldNotBeCalled(); |
| 419 | + |
| 420 | + self::assertSame(GitHooksCommand::FAILURE, $this->executeCommand()); |
| 421 | + } |
| 422 | + |
330 | 423 | /** |
331 | 424 | * @return int |
332 | 425 | */ |
|
0 commit comments