From 3d53d2c5ed0374549d48bb85897a0fc171dbc798 Mon Sep 17 00:00:00 2001 From: Herve Donner Date: Wed, 12 Jun 2024 15:37:58 +0200 Subject: [PATCH] Trigger composer task also when composer.lock changes. --- src/Task/Composer.php | 17 +++++++++++++++-- test/Unit/Task/ComposerTest.php | 9 +++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Task/Composer.php b/src/Task/Composer.php index 9290e0ce..e812ee8a 100644 --- a/src/Task/Composer.php +++ b/src/Task/Composer.php @@ -67,9 +67,12 @@ public function canRunInContext(ContextInterface $context): bool public function run(ContextInterface $context): TaskResultInterface { $config = $this->getConfig()->getOptions(); + $composerDir = pathinfo($config['file'], PATHINFO_DIRNAME); + $composerFile = pathinfo($config['file'], PATHINFO_BASENAME); + $composerLockFile = $this->getLockFile($composerFile); $files = $context->getFiles() - ->path(pathinfo($config['file'], PATHINFO_DIRNAME)) - ->name(pathinfo($config['file'], PATHINFO_BASENAME)); + ->path($composerDir) + ->names([$composerFile, $composerLockFile]); if (0 === \count($files)) { return TaskResult::createSkipped($this, $context); } @@ -116,4 +119,14 @@ private function hasLocalRepository(SplFileInfo $composerFile): bool return false; } + + /** + * Verbatim copy from \Composer\Factory::getLockFile. + */ + private static function getLockFile(string $composerFile): string + { + return 'json' === pathinfo($composerFile, PATHINFO_EXTENSION) + ? substr($composerFile, 0, -4) . 'lock' + : $composerFile . '.lock'; + } } diff --git a/test/Unit/Task/ComposerTest.php b/test/Unit/Task/ComposerTest.php index b320645b..1b0d6f1a 100644 --- a/test/Unit/Task/ComposerTest.php +++ b/test/Unit/Task/ComposerTest.php @@ -195,6 +195,15 @@ public function provideExternalTaskRuns(): iterable './composer.json', ] ]; + yield 'lock-only' => [ + [], + $this->mockContext(RunContext::class, ['composer.lock']), + 'composer', + [ + 'validate', + './composer.json', + ] + ]; yield 'no-check-all' => [ [ 'no_check_all' => true,