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