From 8be21876e00ccd7191d79347292fd12d54123b2a Mon Sep 17 00:00:00 2001 From: Mathias Elle Date: Fri, 13 Nov 2026 00:06:41 +0100 Subject: [PATCH] fix(node): report outdated npm packages correctly The outdated check treated the '{}' output of npm as outdated packages and ignored that npm exits non-zero when packages are outdated. The standard builder additionally ran the check in the current working directory. Reuse the normalised getOutdatedPackages() result for all builders and run the standard builder check in the Magento root. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/Service/NodePackageManager.php | 22 +++++++++----- .../ThemeBuilder/MagentoStandard/Builder.php | 23 +-------------- tests/Unit/Service/NodePackageManagerTest.php | 29 +++++++++++-------- .../MagentoStandard/BuilderTest.php | 17 ++++------- 4 files changed, 38 insertions(+), 53 deletions(-) diff --git a/src/Service/NodePackageManager.php b/src/Service/NodePackageManager.php index 2fc88b3d..99b02cbb 100644 --- a/src/Service/NodePackageManager.php +++ b/src/Service/NodePackageManager.php @@ -234,16 +234,22 @@ public function installPackageVersions( */ public function checkOutdatedPackages(string $path, SymfonyStyle $io): void { - try { - $outdated = $this->shell->execute('cd %s && npm outdated --json', [$path]); - if ($outdated) { - $io->warning('Outdated packages found:'); - $io->writeln($outdated); - } - } catch (\Exception $e) { + $outdated = $this->getOutdatedPackages($path); + + if ($outdated === null) { if ($io->isVerbose()) { - $io->warning('Failed to check outdated packages: ' . $e->getMessage()); + $io->warning('Failed to check outdated packages.'); } + return; + } + + if ($outdated === []) { + return; + } + + $io->warning('Outdated packages found:'); + foreach ($outdated as $package) { + $io->writeln(sprintf(' %s: %s -> %s', $package['name'], $package['current'], $package['latest'])); } } diff --git a/src/Service/ThemeBuilder/MagentoStandard/Builder.php b/src/Service/ThemeBuilder/MagentoStandard/Builder.php index 089f079c..bc1c876a 100644 --- a/src/Service/ThemeBuilder/MagentoStandard/Builder.php +++ b/src/Service/ThemeBuilder/MagentoStandard/Builder.php @@ -176,7 +176,7 @@ public function autoRepair(string $themePath, SymfonyStyle $io, OutputInterface // Check for outdated packages if ($isVerbose) { - $this->checkOutdatedPackages($io); + $this->nodePackageManager->checkOutdatedPackages($rootPath, $io); } return true; @@ -214,27 +214,6 @@ private function installGruntIfMissing(SymfonyStyle $io, bool $isVerbose): bool } } - /** - * Check for outdated packages and report them - * - * @param SymfonyStyle $io - * @return void - */ - private function checkOutdatedPackages(SymfonyStyle $io): void - { - try { - $outdated = $this->shell->execute('npm outdated --json'); - if ($outdated) { - $io->warning('Outdated packages found:'); - $io->writeln($outdated); - } - } catch (\Exception $e) { - if ($io->isVerbose()) { - $io->warning('Failed to check outdated packages: ' . $e->getMessage()); - } - } - } - /** * Run watch mode for Magento standard theme assets. * diff --git a/tests/Unit/Service/NodePackageManagerTest.php b/tests/Unit/Service/NodePackageManagerTest.php index d01619b9..86afb6db 100644 --- a/tests/Unit/Service/NodePackageManagerTest.php +++ b/tests/Unit/Service/NodePackageManagerTest.php @@ -207,25 +207,23 @@ public function testWarnsAboutOutdatedPackages(): void $shell ->expects($this->once()) ->method('execute') - ->with('cd %s && npm outdated --json', ['/theme']) - ->willReturn('{"tailwindcss": {"current": "3.0.0", "latest": "4.0.0"}}'); + ->with('cd %s && npm outdated --json --long 2>/dev/null; true', ['/theme']) + ->willReturn('{"tailwindcss": {"current": "3.0.0", "wanted": "3.4.0", "latest": "4.0.0"}}'); $io = $this->createMock(SymfonyStyle::class); $io->expects($this->once())->method('warning')->with('Outdated packages found:'); - $io - ->expects($this->once()) - ->method('writeln') - ->with('{"tailwindcss": {"current": "3.0.0", "latest": "4.0.0"}}'); + $io->expects($this->once())->method('writeln')->with(' tailwindcss: 3.0.0 -> 4.0.0'); $packageManager = $this->createPackageManager(shell: $shell); $packageManager->checkOutdatedPackages('/theme', $io); } - public function testStaysSilentWhenEverythingIsUpToDate(): void + public function testStaysSilentWhenNpmReportsEmptyObject(): void { - $this->shell->method('execute')->willReturn(''); + $this->shell->method('execute')->willReturn('{}'); $io = $this->createMock(SymfonyStyle::class); $io->expects($this->never())->method('warning'); + $io->expects($this->never())->method('writeln'); $this->packageManager->checkOutdatedPackages('/theme', $io); } @@ -235,10 +233,17 @@ public function testReportsCheckFailureOnlyInVerboseMode(): void $this->shell->method('execute')->willThrowException(new \RuntimeException('registry unreachable')); $io = $this->createMock(SymfonyStyle::class); $io->method('isVerbose')->willReturn(true); - $io - ->expects($this->once()) - ->method('warning') - ->with('Failed to check outdated packages: registry unreachable'); + $io->expects($this->once())->method('warning')->with('Failed to check outdated packages.'); + + $this->packageManager->checkOutdatedPackages('/theme', $io); + } + + public function testStaysSilentOnCheckFailureWhenNotVerbose(): void + { + $this->shell->method('execute')->willThrowException(new \RuntimeException('registry unreachable')); + $io = $this->createMock(SymfonyStyle::class); + $io->method('isVerbose')->willReturn(false); + $io->expects($this->never())->method('warning'); $this->packageManager->checkOutdatedPackages('/theme', $io); } diff --git a/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php b/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php index ba04755d..aa11fced 100644 --- a/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php +++ b/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php @@ -593,18 +593,13 @@ public function testAutoRepairSkipsInstallAndOutdatedCheckWhenQuietAndInSync(): public function testAutoRepairChecksOutdatedPackagesInVerboseMode(): void { - $this->nodePackageManager->method('isNodeModulesInSync')->willReturn(true); - - $executedCommands = []; - $this->shell - ->method('execute') - ->willReturnCallback(function (string $command) use (&$executedCommands): string { - $executedCommands[] = $command; - return ''; - }); + $nodePackageManager = $this->createMock(NodePackageManager::class); + $nodePackageManager->method('isNodeModulesInSync')->willReturn(true); + $nodePackageManager->expects($this->once())->method('checkOutdatedPackages')->with('/magento-root', $this->io); + $this->shell->method('execute')->willReturn(''); + $builder = $this->createBuilder(nodePackageManager: $nodePackageManager); - $this->assertTrue($this->builder->autoRepair($this->themePath, $this->io, $this->output, true)); - $this->assertSame(['which grunt', 'npm outdated --json'], $executedCommands); + $this->assertTrue($builder->autoRepair($this->themePath, $this->io, $this->output, true)); } public function testInstallsGruntWithExactMessagesWhenMissing(): void