From f9935e7f2e40d1345e4ad4363a7fbfacec2c391f Mon Sep 17 00:00:00 2001 From: Mathias Elle Date: Thu, 12 Nov 2026 23:44:22 +0100 Subject: [PATCH 1/3] fix(magento-standard): resolve Node/Grunt paths from the Magento root Use the Magento root directory instead of the current working directory for Node setup detection, auto repair and watch mode, and also detect a capitalised Gruntfile.js, which is the conventional name on case-sensitive filesystems. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../ThemeBuilder/MagentoStandard/Builder.php | 23 +++++++- .../MagentoStandard/BuilderTest.php | 59 ++++++++++++++----- 2 files changed, 65 insertions(+), 17 deletions(-) diff --git a/src/Service/ThemeBuilder/MagentoStandard/Builder.php b/src/Service/ThemeBuilder/MagentoStandard/Builder.php index bf18a2bf..7e20b359 100644 --- a/src/Service/ThemeBuilder/MagentoStandard/Builder.php +++ b/src/Service/ThemeBuilder/MagentoStandard/Builder.php @@ -4,6 +4,7 @@ namespace OpenForgeProject\MageForge\Service\ThemeBuilder\MagentoStandard; +use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Filesystem\Driver\File; use Magento\Framework\Shell; use OpenForgeProject\MageForge\Service\CacheCleaner; @@ -29,6 +30,7 @@ class Builder implements BuilderInterface * @param SymlinkCleaner $symlinkCleaner * @param NodePackageManager $nodePackageManager * @param GruntTaskRunner $gruntTaskRunner + * @param DirectoryList $directoryList */ public function __construct( private readonly Shell $shell, @@ -39,6 +41,7 @@ public function __construct( private readonly SymlinkCleaner $symlinkCleaner, private readonly NodePackageManager $nodePackageManager, private readonly GruntTaskRunner $gruntTaskRunner, + private readonly DirectoryList $directoryList, ) { } @@ -154,7 +157,7 @@ private function processNodeSetup( */ public function autoRepair(string $themePath, SymfonyStyle $io, OutputInterface $output, bool $isVerbose): bool { - $rootPath = '.'; + $rootPath = $this->getRootPath(); // Check if node_modules is in sync with package-lock.json if (!$this->nodePackageManager->isNodeModulesInSync($rootPath)) { @@ -286,8 +289,10 @@ public function watch( } $exitCode = 0; + // Run from the Magento root so grunt finds its config regardless of the current working directory + $command = 'cd ' . escapeshellarg($this->getRootPath()) . ' && node_modules/.bin/grunt watch'; // phpcs:ignore Magento2.Security.InsecureFunction.Found -- passthru required for interactive watch mode - passthru('node_modules/.bin/grunt watch', $exitCode); + passthru($command, $exitCode); // Check if the command failed if ($exitCode !== 0) { @@ -321,11 +326,13 @@ public function getName(): string */ private function hasNodeSetup(): bool { - $rootPath = '.'; + $rootPath = $this->getRootPath(); + // Magento ships Gruntfile.js.sample; the copy is conventionally named Gruntfile.js $files = [ 'package.json', 'package-lock.json', + 'Gruntfile.js', 'gruntfile.js', 'grunt-config.json', ]; @@ -339,6 +346,16 @@ private function hasNodeSetup(): bool return false; } + /** + * Get the Magento root path without trailing slash + * + * @return string + */ + private function getRootPath(): string + { + return rtrim($this->directoryList->getRoot(), '/'); + } + /** * Check if theme is from vendor directory * diff --git a/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php b/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php index 0f1514d2..d23783db 100644 --- a/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php +++ b/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php @@ -4,6 +4,7 @@ namespace OpenForgeProject\MageForge\Test\Unit\Service\ThemeBuilder\MagentoStandard; +use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Filesystem\Driver\File; use Magento\Framework\Shell; use OpenForgeProject\MageForge\Service\CacheCleaner; @@ -52,6 +53,10 @@ class BuilderTest extends TestCase * @var GruntTaskRunner&Stub */ private $gruntTaskRunner; + /** + * @var DirectoryList&Stub + */ + private $directoryList; /** * @var SymfonyStyle&Stub */ @@ -79,6 +84,8 @@ protected function setUp(): void $this->symlinkCleaner = $this->createStub(SymlinkCleaner::class); $this->nodePackageManager = $this->createStub(NodePackageManager::class); $this->gruntTaskRunner = $this->createStub(GruntTaskRunner::class); + $this->directoryList = $this->createStub(DirectoryList::class); + $this->directoryList->method('getRoot')->willReturn('/magento-root'); $this->io = $this->createStub(SymfonyStyle::class); $this->output = $this->createStub(OutputInterface::class); @@ -94,6 +101,7 @@ private function createBuilder( ?SymlinkCleaner $symlinkCleaner = null, ?NodePackageManager $nodePackageManager = null, ?GruntTaskRunner $gruntTaskRunner = null, + ?DirectoryList $directoryList = null, ): Builder { return new Builder( $shell ?? $this->shell, @@ -104,6 +112,7 @@ private function createBuilder( $symlinkCleaner ?? $this->symlinkCleaner, $nodePackageManager ?? $this->nodePackageManager, $gruntTaskRunner ?? $this->gruntTaskRunner, + $directoryList ?? $this->directoryList, ); } @@ -194,10 +203,10 @@ public function testBuildSkipsGruntStepsWhenNoNodeSetupDetected(): void $this->fileDriver->method('isExists')->willReturnMap([ [$this->themePath . '/theme.xml', true], [$this->themePath . '/web/tailwind', false], - ['./package.json', false], - ['./package-lock.json', false], - ['./gruntfile.js', false], - ['./grunt-config.json', false], + ['/magento-root/package.json', false], + ['/magento-root/package-lock.json', false], + ['/magento-root/gruntfile.js', false], + ['/magento-root/grunt-config.json', false], ]); $nodePackageManager = $this->createMock(NodePackageManager::class); $nodePackageManager->expects($this->never())->method('isNodeModulesInSync'); @@ -208,12 +217,34 @@ public function testBuildSkipsGruntStepsWhenNoNodeSetupDetected(): void $this->assertTrue($builder->build('Vendor/theme', $this->themePath, $this->io, $this->output, true)); } + public function testBuildDetectsCapitalisedGruntfileInMagentoRoot(): void + { + $this->fileDriver->method('isExists')->willReturnMap([ + [$this->themePath . '/theme.xml', true], + [$this->themePath . '/web/tailwind', false], + ['/magento-root/Gruntfile.js', true], + ]); + $this->staticContentCleaner->method('cleanIfNeeded')->willReturn(true); + $this->staticContentDeployer->method('deploy')->willReturn(true); + $this->cacheCleaner->method('clean')->willReturn(true); + $this->symlinkCleaner->method('cleanSymlinks')->willReturn(true); + $this->gruntTaskRunner->method('runTasks')->willReturn(true); + $nodePackageManager = $this->createMock(NodePackageManager::class); + $nodePackageManager->expects($this->once()) + ->method('isNodeModulesInSync') + ->with('/magento-root') + ->willReturn(true); + $builder = $this->createBuilder(nodePackageManager: $nodePackageManager); + + $this->assertTrue($builder->build('Vendor/theme', $this->themePath, $this->io, $this->output, false)); + } + public function testBuildRunsNodeSetupWhenDetected(): void { $this->fileDriver->method('isExists')->willReturnMap([ [$this->themePath . '/theme.xml', true], [$this->themePath . '/web/tailwind', false], - ['./package.json', true], + ['/magento-root/package.json', true], ]); $this->staticContentCleaner->method('cleanIfNeeded')->willReturn(true); $this->nodePackageManager->method('isNodeModulesInSync')->willReturn(true); @@ -245,7 +276,7 @@ public function testBuildReturnsFalseWhenNodeSetupProcessingFails(): void $this->fileDriver->method('isExists')->willReturnMap([ [$this->themePath . '/theme.xml', true], [$this->themePath . '/web/tailwind', false], - ['./package.json', true], + ['/magento-root/package.json', true], ]); $this->staticContentCleaner->method('cleanIfNeeded')->willReturn(true); $this->nodePackageManager->method('isNodeModulesInSync')->willReturn(true); @@ -340,10 +371,10 @@ public function testWatchReturnsFalseWhenNoNodeSetupDetected(): void $this->fileDriver->method('isExists')->willReturnMap([ [$this->themePath . '/theme.xml', true], [$this->themePath . '/web/tailwind', false], - ['./package.json', false], - ['./package-lock.json', false], - ['./gruntfile.js', false], - ['./grunt-config.json', false], + ['/magento-root/package.json', false], + ['/magento-root/package-lock.json', false], + ['/magento-root/gruntfile.js', false], + ['/magento-root/grunt-config.json', false], ]); $this->assertFalse($this->builder->watch('Vendor/theme', $this->themePath, $this->io, $this->output, false)); @@ -354,7 +385,7 @@ public function testWatchReturnsFalseWhenStaticContentCleanFails(): void $this->fileDriver->method('isExists')->willReturnMap([ [$this->themePath . '/theme.xml', true], [$this->themePath . '/web/tailwind', false], - ['./package.json', true], + ['/magento-root/package.json', true], ]); $this->staticContentCleaner->method('cleanIfNeeded')->willReturn(false); @@ -366,7 +397,7 @@ public function testWatchReturnsFalseWhenAutoRepairFails(): void $this->fileDriver->method('isExists')->willReturnMap([ [$this->themePath . '/theme.xml', true], [$this->themePath . '/web/tailwind', false], - ['./package.json', true], + ['/magento-root/package.json', true], ]); $this->staticContentCleaner->method('cleanIfNeeded')->willReturn(true); $this->nodePackageManager->method('isNodeModulesInSync')->willReturn(false); @@ -467,7 +498,7 @@ public function testBuildFailsWhenSymlinkCleaningFails(): void public function testAutoRepairInstallsWithExactWarningWhenOutOfSync(): void { $nodePackageManager = $this->createMock(NodePackageManager::class); - $nodePackageManager->expects($this->once())->method('isNodeModulesInSync')->with('.')->willReturn(false); + $nodePackageManager->expects($this->once())->method('isNodeModulesInSync')->with('/magento-root')->willReturn(false); $nodePackageManager->expects($this->once())->method('installNodeModules')->willReturn(true); $io = $this->createMock(SymfonyStyle::class); $io @@ -603,7 +634,7 @@ private function givenFilesystem(File&Stub $fileDriver, string $themePath, bool if ($path === $themePath . '/web/tailwind') { return false; } - return $nodeSetup && $path === './package.json'; + return $nodeSetup && $path === '/magento-root/package.json'; }); } } From 0aab7ccd98153aa96080521e22648869786eba76 Mon Sep 17 00:00:00 2001 From: Mathias Elle Date: Thu, 12 Nov 2026 23:53:32 +0100 Subject: [PATCH 2/3] test(magento-standard): cover watch working directory and path quoting Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../MagentoStandard/BuilderTest.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php b/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php index d23783db..89cd8f47 100644 --- a/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php +++ b/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php @@ -14,6 +14,7 @@ use OpenForgeProject\MageForge\Service\StaticContentDeployer; use OpenForgeProject\MageForge\Service\SymlinkCleaner; use OpenForgeProject\MageForge\Service\ThemeBuilder\MagentoStandard\Builder; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\Stub; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Output\OutputInterface; @@ -392,6 +393,63 @@ public function testWatchReturnsFalseWhenStaticContentCleanFails(): void $this->assertFalse($this->builder->watch('Vendor/theme', $this->themePath, $this->io, $this->output, false)); } + /** + * @return array + */ + public static function watchExitCodeProvider(): array + { + return [ + 'successful exit' => [0, true], + 'non-zero exit' => [3, false], + ]; + } + + #[DataProvider('watchExitCodeProvider')] + public function testWatchRunsGruntFromMagentoRootWithQuotedPath(int $exitCode, bool $expected): void + { + $root = sys_get_temp_dir() . "/mage forge's root " . uniqid(); + $binDir = $root . '/node_modules/.bin'; + mkdir($binDir, 0o777, true); + $cwdFile = $root . '/cwd.txt'; + file_put_contents( + $binDir . '/grunt', + sprintf("#!/bin/sh\npwd > \"%s\"\necho \"$@\" >> \"%s\"\nexit %d\n", $cwdFile, $cwdFile, $exitCode), + ); + chmod($binDir . '/grunt', 0o755); + + $directoryList = $this->createStub(DirectoryList::class); + $directoryList->method('getRoot')->willReturn($root); + $this->fileDriver->method('isExists')->willReturnMap([ + [$this->themePath . '/theme.xml', true], + [$this->themePath . '/web/tailwind', false], + [$root . '/package.json', true], + ]); + $this->staticContentCleaner->method('cleanIfNeeded')->willReturn(true); + $this->nodePackageManager->method('isNodeModulesInSync')->willReturn(true); + $this->shell->method('execute')->willReturn(''); + $builder = $this->createBuilder(directoryList: $directoryList); + + $originalCwd = getcwd(); + try { + ob_start(); + $result = $builder->watch('Vendor/theme', $this->themePath, $this->io, $this->output, false); + ob_end_clean(); + $recorded = (string) file_get_contents($cwdFile); + } finally { + unlink($binDir . '/grunt'); + if (is_file($cwdFile)) { + unlink($cwdFile); + } + rmdir($binDir); + rmdir($root . '/node_modules'); + rmdir($root); + } + + $this->assertNotSame($root, $originalCwd); + $this->assertSame($expected, $result); + $this->assertSame([$root, 'watch'], array_map('trim', explode("\n", trim($recorded)))); + } + public function testWatchReturnsFalseWhenAutoRepairFails(): void { $this->fileDriver->method('isExists')->willReturnMap([ From ba5a28af72a5ddb18c2962a30ed0d074b102ebef Mon Sep 17 00:00:00 2001 From: Mathias Elle Date: Thu, 12 Nov 2026 23:55:12 +0100 Subject: [PATCH 3/3] style(magento-standard): fix PHPCS warnings Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/Service/ThemeBuilder/MagentoStandard/Builder.php | 1 + .../Service/ThemeBuilder/MagentoStandard/BuilderTest.php | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Service/ThemeBuilder/MagentoStandard/Builder.php b/src/Service/ThemeBuilder/MagentoStandard/Builder.php index 7e20b359..089f079c 100644 --- a/src/Service/ThemeBuilder/MagentoStandard/Builder.php +++ b/src/Service/ThemeBuilder/MagentoStandard/Builder.php @@ -290,6 +290,7 @@ public function watch( $exitCode = 0; // Run from the Magento root so grunt finds its config regardless of the current working directory + // phpcs:ignore Magento2.Functions.DiscouragedFunction.Discouraged -- quotes the root path for the shell $command = 'cd ' . escapeshellarg($this->getRootPath()) . ' && node_modules/.bin/grunt watch'; // phpcs:ignore Magento2.Security.InsecureFunction.Found -- passthru required for interactive watch mode passthru($command, $exitCode); diff --git a/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php b/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php index 89cd8f47..ba04755d 100644 --- a/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php +++ b/tests/Unit/Service/ThemeBuilder/MagentoStandard/BuilderTest.php @@ -556,7 +556,11 @@ public function testBuildFailsWhenSymlinkCleaningFails(): void public function testAutoRepairInstallsWithExactWarningWhenOutOfSync(): void { $nodePackageManager = $this->createMock(NodePackageManager::class); - $nodePackageManager->expects($this->once())->method('isNodeModulesInSync')->with('/magento-root')->willReturn(false); + $nodePackageManager + ->expects($this->once()) + ->method('isNodeModulesInSync') + ->with('/magento-root') + ->willReturn(false); $nodePackageManager->expects($this->once())->method('installNodeModules')->willReturn(true); $io = $this->createMock(SymfonyStyle::class); $io