Skip to content
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

Open
wants to merge 4 commits into
base: v2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"brianium/paratest": "Lets GrumPHP run PHPUnit in parallel.",
"codeception/codeception": "Lets GrumPHP run your project's full stack tests",
"consolidation/robo": "Lets GrumPHP run your automated PHP tasks.",
"dantleech/gherkin-lint": "Lets GrumPHP lint your Gherkin files.",
"designsecurity/progpilot": "Lets GrumPHP be sure that there are no vulnerabilities in your code.",
"doctrine/orm": "Lets GrumPHP validate your Doctrine mapping files.",
"enlightn/security-checker": "Lets GrumPHP be sure that there are no known security issues.",
Expand All @@ -60,7 +61,7 @@
"friendsoftwig/twigcs": "Lets GrumPHP check Twig coding standard.",
"infection/infection": "Lets GrumPHP evaluate the quality your unit tests",
"maglnet/composer-require-checker": "Lets GrumPHP analyze composer dependencies.",
"malukenho/kawaii-gherkin": "Lets GrumPHP lint your Gherkin files.",
"malukenho/kawaii-gherkin": "Lets GrumPHP lint/fix your Gherkin files.",
"nette/tester": "Lets GrumPHP run your unit tests with nette tester.",
"nikic/php-parser": "Lets GrumPHP run static analyses through your PHP files.",
"pestphp/pest": "Lets GrumPHP run your unit test with Pest PHP",
Expand Down
1 change: 1 addition & 0 deletions doc/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ grumphp:
eslint: ~
file_size: ~
gherkin: ~
gherkin_lint: ~
git_blacklist: ~
git_branch_name: ~
git_commit_message: ~
Expand Down
26 changes: 26 additions & 0 deletions doc/tasks/gherkin_lint.md
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.
7 changes: 7 additions & 0 deletions resources/config/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ services:
tags:
- {name: grumphp.task, task: gherkin}

GrumPHP\Task\GherkinLint:
arguments:
- '@process_builder'
- '@formatter.raw_process'
tags:
- { name: grumphp.task, task: gherkin_lint }

GrumPHP\Task\Git\Blacklist:
arguments:
- '@process_builder'
Expand Down
69 changes: 69 additions & 0 deletions src/Task/GherkinLint.php
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',
Copy link
Contributor

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:

In FeatureFinder.php line 25:
  Provided path "/var/www/html/features" does not exist

lint <path>
Failed to run grumphp run --tasks=gherkin_lint: exit status 1

Is this expected ? or should we skip in such cases?
How are other tasks handling these cases?

Copy link
Author

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.

'config' => null,
]);

$resolver->addAllowedTypes('directory', ['string']);
$resolver->addAllowedTypes('config', ['null', 'string']);
$resolver->addAllowedValues('config', [null, 'text']);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 text here ?

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);
}
}
102 changes: 102 additions & 0 deletions test/Unit/Task/GherkinLintTest.php
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' => [
Copy link
Contributor

Choose a reason for hiding this comment

The 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',
]
];
}
}