Skip to content

Commit

Permalink
Use grunt stylelint by default.
Browse files Browse the repository at this point in the history
So that core grunt task will decide whether css or scss linter needs to be run (or both).
  • Loading branch information
kabalin committed Sep 10, 2024
1 parent 360562c commit f6681d3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Command/GruntCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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;
}
Expand Down
35 changes: 34 additions & 1 deletion tests/Command/GruntCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +39,7 @@ protected function executeCommand()
$commandTester->execute([
'plugin' => $this->pluginDir,
'--moodle' => $this->moodleDir,
'--tasks' => ['css'],
'--tasks' => ['stylelint'],
]);

return $commandTester;
Expand Down Expand Up @@ -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);
Expand All @@ -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'));
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Fake/Bridge/DummyMoodleThemePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Moodle Plugin CI package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2024 Ruslan Kabalin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodlePluginCI\Tests\Fake\Bridge;

use MoodlePluginCI\Bridge\MoodlePlugin;

class DummyMoodleThemePlugin extends MoodlePlugin
{
public string $component = 'theme_ci';
}

0 comments on commit f6681d3

Please sign in to comment.