-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add gherkin_lint task #1144
base: v2.x
Are you sure you want to change the base?
Add gherkin_lint task #1144
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Gherkin Lint | ||
|
||
The Gherkin Lint task will lint your Gherkin feature files. | ||
It lives under the `gherkin_lint` namespace and has following configurable parameters: | ||
|
||
```yaml | ||
# grumphp.yml | ||
grumphp: | ||
tasks: | ||
gherkin_lint: | ||
directory: 'features' | ||
config: ~ | ||
``` | ||
|
||
**directory** | ||
|
||
*Default: 'features'* | ||
|
||
This option will specify the location of your Gherkin feature files. | ||
By default, the Behat preferred `features` folder is chosen. | ||
|
||
**config** | ||
|
||
*Default: null* | ||
|
||
By default, all rules are enabled. To customize or disable them, create a configuration file named `gherkinlint.json` in the current directory. You need to set the path value in the configuration parameters. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GrumPHP\Task; | ||
|
||
use GrumPHP\Formatter\ProcessFormatterInterface; | ||
use GrumPHP\Runner\TaskResult; | ||
use GrumPHP\Runner\TaskResultInterface; | ||
use GrumPHP\Task\Config\ConfigOptionsResolver; | ||
use GrumPHP\Task\Context\ContextInterface; | ||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* @extends AbstractExternalTask<ProcessFormatterInterface> | ||
*/ | ||
class GherkinLint extends AbstractExternalTask | ||
{ | ||
public static function getConfigurableOptions(): ConfigOptionsResolver | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefaults([ | ||
'directory' => 'features', | ||
'config' => null, | ||
]); | ||
|
||
$resolver->addAllowedTypes('directory', ['string']); | ||
$resolver->addAllowedTypes('config', ['null', 'string']); | ||
$resolver->addAllowedValues('config', [null, 'text']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't config be any string? Now you are limiting it to the literal On second notice: there doesn't seem a way to specify the configuration file. It assumes a hardcoded path instead:
So I'dd say its better to remove the config option alltogether. |
||
|
||
return ConfigOptionsResolver::fromOptionsResolver($resolver); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function canRunInContext(ContextInterface $context): bool | ||
{ | ||
return $context instanceof GitPreCommitContext || $context instanceof RunContext; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function run(ContextInterface $context): TaskResultInterface | ||
{ | ||
$config = $this->getConfig()->getOptions(); | ||
$files = $context->getFiles()->extensions(['feature']); | ||
if (0 === \count($files)) { | ||
return TaskResult::createSkipped($this, $context); | ||
} | ||
|
||
$arguments = $this->processBuilder->createArgumentsForCommand('gherkinlint'); | ||
$arguments->add('lint'); | ||
$arguments->addOptionalArgument('--config=%s', $config['config']); | ||
$arguments->add($config['directory']); | ||
|
||
$process = $this->processBuilder->buildProcess($arguments); | ||
$process->run(); | ||
|
||
if (!$process->isSuccessful()) { | ||
return TaskResult::createFailed($this, $context, $this->formatter->format($process)); | ||
} | ||
|
||
return TaskResult::createPassed($this, $context); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GrumPHPTest\Unit\Task; | ||
|
||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use GrumPHP\Task\GherkinLint; | ||
use GrumPHP\Task\TaskInterface; | ||
use GrumPHP\Test\Task\AbstractExternalTaskTestCase; | ||
|
||
class GherkinLintTest extends AbstractExternalTaskTestCase | ||
{ | ||
protected function provideTask(): TaskInterface | ||
{ | ||
return new GherkinLint( | ||
$this->processBuilder->reveal(), | ||
$this->formatter->reveal() | ||
); | ||
} | ||
|
||
public function provideConfigurableOptions(): iterable | ||
{ | ||
yield 'defaults' => [ | ||
[], | ||
[ | ||
'directory' => 'features', | ||
'config' => null, | ||
] | ||
]; | ||
} | ||
|
||
public function provideRunContexts(): iterable | ||
{ | ||
yield 'run-context' => [ | ||
true, | ||
$this->mockContext(RunContext::class) | ||
]; | ||
|
||
yield 'pre-commit-context' => [ | ||
true, | ||
$this->mockContext(GitPreCommitContext::class) | ||
]; | ||
|
||
yield 'other' => [ | ||
false, | ||
$this->mockContext() | ||
]; | ||
} | ||
|
||
public function provideFailsOnStuff(): iterable | ||
{ | ||
yield 'exitCode1' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['hello.feature']), | ||
function () { | ||
$this->mockProcessBuilder('gherkinlint', $process = $this->mockProcess(1)); | ||
$this->formatter->format($process)->willReturn('nope'); | ||
}, | ||
'nope' | ||
]; | ||
} | ||
|
||
public function providePassesOnStuff(): iterable | ||
{ | ||
yield 'exitCode0' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['hello.feature']), | ||
function () { | ||
$this->mockProcessBuilder('gherkinlint', $this->mockProcess(0)); | ||
} | ||
]; | ||
} | ||
|
||
public function provideSkipsOnStuff(): iterable | ||
{ | ||
yield 'no-files' => [ | ||
[], | ||
$this->mockContext(RunContext::class), | ||
function () {} | ||
]; | ||
yield 'no-files-after-triggered-by' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['notafeaturefile.txt']), | ||
function () {} | ||
]; | ||
} | ||
|
||
public function provideExternalTaskRuns(): iterable | ||
{ | ||
yield 'defaults' => [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this provider should contain a test for every configurable option to make sure it behaves the correct way. |
||
[], | ||
$this->mockContext(RunContext::class, ['hello.feature', 'hello2.feature']), | ||
'gherkinlint', | ||
[ | ||
'lint', | ||
'features', | ||
] | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that if the specified directory doesn't exist, it fails with:
Is this expected ? or should we skip in such cases?
How are other tasks handling these cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's expected. For example src/Task/Gherkin.php has the same configuration, and returns the same error.