diff --git a/src/Service/ThemeBuilder/MagentoStandard/Builder.php b/src/Service/ThemeBuilder/MagentoStandard/Builder.php index bf18a2bf..089f079c 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,11 @@ 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('node_modules/.bin/grunt watch', $exitCode); + passthru($command, $exitCode); // Check if the command failed if ($exitCode !== 0) { @@ -321,11 +327,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 +347,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..ba04755d 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; @@ -13,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; @@ -52,6 +54,10 @@ class BuilderTest extends TestCase * @var GruntTaskRunner&Stub */ private $gruntTaskRunner; + /** + * @var DirectoryList&Stub + */ + private $directoryList; /** * @var SymfonyStyle&Stub */ @@ -79,6 +85,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 +102,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 +113,7 @@ private function createBuilder( $symlinkCleaner ?? $this->symlinkCleaner, $nodePackageManager ?? $this->nodePackageManager, $gruntTaskRunner ?? $this->gruntTaskRunner, + $directoryList ?? $this->directoryList, ); } @@ -194,10 +204,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 +218,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 +277,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 +372,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,19 +386,76 @@ 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); $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([ [$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 +556,11 @@ 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 +696,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'; }); } }