From f6681d3770ad93b1ea9825027215afa9df362a64 Mon Sep 17 00:00:00 2001 From: Ruslan Kabalin Date: Tue, 10 Sep 2024 16:36:21 +0100 Subject: [PATCH] Use grunt stylelint by default. So that core grunt task will decide whether css or scss linter needs to be run (or both). --- src/Command/GruntCommand.php | 9 +++-- tests/Command/GruntCommandTest.php | 35 +++++++++++++++++++- tests/Fake/Bridge/DummyMoodleThemePlugin.php | 20 +++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tests/Fake/Bridge/DummyMoodleThemePlugin.php diff --git a/src/Command/GruntCommand.php b/src/Command/GruntCommand.php index 74f9a5a8..b7f65c3e 100644 --- a/src/Command/GruntCommand.php +++ b/src/Command/GruntCommand.php @@ -33,7 +33,7 @@ protected function configure(): void { parent::configure(); - $tasks = ['amd', 'yui', 'gherkinlint', 'stylelint:css', 'stylelint:scss']; + $tasks = ['amd', 'yui', 'gherkinlint', 'stylelint']; $this->setName('grunt') ->setDescription('Run Grunt task on a plugin') @@ -194,10 +194,15 @@ public function toGruntTask(string $task): ?GruntTaskModel } return new GruntTaskModel($task, $this->moodle->directory); + case 'stylelint': + // Let stylelint task logic to determine which type of linter to run. + return $this->plugin->hasFilesWithName('*.css') || $this->plugin->hasFilesWithName('*.scss') ? $defaultTaskPluginDir : null; case 'stylelint:css': return $this->plugin->hasFilesWithName('*.css') ? $defaultTaskPluginDir : null; case 'stylelint:scss': - return $this->plugin->hasFilesWithName('*.scss') ? $defaultTaskPluginDir : null; + $type = strtok($this->plugin->getComponent(), '_'); + + return ($type === 'theme') && $this->plugin->hasFilesWithName('*.scss') ? $defaultTaskPluginDir : null; default: return $defaultTask; } diff --git a/tests/Command/GruntCommandTest.php b/tests/Command/GruntCommandTest.php index 40dd79df..b210fdf4 100644 --- a/tests/Command/GruntCommandTest.php +++ b/tests/Command/GruntCommandTest.php @@ -16,6 +16,7 @@ use MoodlePluginCI\Model\GruntTaskModel; use MoodlePluginCI\Tests\Fake\Bridge\DummyMoodle; use MoodlePluginCI\Tests\Fake\Bridge\DummyMoodlePlugin; +use MoodlePluginCI\Tests\Fake\Bridge\DummyMoodleThemePlugin; use MoodlePluginCI\Tests\Fake\Process\DummyExecute; use MoodlePluginCI\Tests\MoodleTestCase; use Symfony\Component\Console\Application; @@ -38,7 +39,7 @@ protected function executeCommand() $commandTester->execute([ 'plugin' => $this->pluginDir, '--moodle' => $this->moodleDir, - '--tasks' => ['css'], + '--tasks' => ['stylelint'], ]); return $commandTester; @@ -136,6 +137,21 @@ public function testToGruntTaskWithStyles() { $command = $this->newCommand(); + $task = $command->toGruntTask('stylelint'); + $this->assertInstanceOf(GruntTaskModel::class, $task); + $this->assertSame('stylelint', $task->taskName); + $this->assertSame('', $task->buildDirectory); + $this->assertSame($this->pluginDir, $task->workingDirectory); + + $this->fs->remove($this->pluginDir . '/styles.css'); + + $this->assertNull($command->toGruntTask('stylelint')); + } + + public function testToGruntTaskWithStylesCss() + { + $command = $this->newCommand(); + $task = $command->toGruntTask('stylelint:css'); $this->assertInstanceOf(GruntTaskModel::class, $task); $this->assertSame('stylelint:css', $task->taskName); @@ -145,6 +161,23 @@ public function testToGruntTaskWithStyles() $this->fs->remove($this->pluginDir . '/styles.css'); $this->assertNull($command->toGruntTask('stylelint:css')); + } + + public function testToGruntTaskWithStylesScssTheme() + { + $command = $this->newCommand(); + $command->plugin = new DummyMoodleThemePlugin($this->pluginDir); + $this->fs->mkdir($this->pluginDir . '/scss'); + $this->fs->copy($this->pluginDir . '/styles.css', $this->pluginDir . '/scss/styles.scss'); + + $task = $command->toGruntTask('stylelint:scss'); + $this->assertInstanceOf(GruntTaskModel::class, $task); + $this->assertSame('stylelint:scss', $task->taskName); + $this->assertSame('', $task->buildDirectory); + $this->assertSame($this->pluginDir, $task->workingDirectory); + + $this->fs->remove($this->pluginDir . '/scss'); + $this->assertNull($command->toGruntTask('stylelint:scss')); } diff --git a/tests/Fake/Bridge/DummyMoodleThemePlugin.php b/tests/Fake/Bridge/DummyMoodleThemePlugin.php new file mode 100644 index 00000000..d1763e36 --- /dev/null +++ b/tests/Fake/Bridge/DummyMoodleThemePlugin.php @@ -0,0 +1,20 @@ +