Skip to content

Commit

Permalink
Merge pull request #432 from pierredup/yaml-lint
Browse files Browse the repository at this point in the history
Add parse_constant and parse_custom_tags options to yamllint task
  • Loading branch information
veewee authored Nov 24, 2017
2 parents a4c1328 + a375508 commit 4c485fd
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 2 deletions.
18 changes: 18 additions & 0 deletions doc/tasks/yamllint.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ parameters:
ignore_patterns: []
object_support: false
exception_on_invalid_type: false
parse_constant: false
parse_custom_tags: false
```
**ignore_patterns**
Expand All @@ -34,3 +36,19 @@ This option indicates if the Yaml parser supports serialized PHP objects.
By enabling this option, the types of the yaml values are validated.
When the value has an incorrect type, a lint error will be triggered.
**parse_constant**
*Default: false*
By enabling this option, constants defined by the special `!php/const:` syntax is parsed and validated.
When this option is not set, the constant syntax will trigger an error


**parse_custom_tags**

*Default: false*

By enabling this option, custom tags in the yaml file will be parsed and validated (E.G `!my_tag { foo: bar }`).
When this option is not set, using custom tags will trigger an error
6 changes: 6 additions & 0 deletions spec/Task/YamlLintSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function it_should_have_configurable_options()
$options->shouldBeAnInstanceOf(OptionsResolver::class);
$options->getDefinedOptions()->shouldContain('object_support');
$options->getDefinedOptions()->shouldContain('exception_on_invalid_type');
$options->getDefinedOptions()->shouldContain('parse_custom_tags');
$options->getDefinedOptions()->shouldContain('parse_constant');
}

function it_should_run_in_git_pre_commit_context(GitPreCommitContext $context)
Expand Down Expand Up @@ -70,6 +72,8 @@ function it_runs_the_suite(YamlLinter $linter, ContextInterface $context)
$linter->isInstalled()->willReturn(true);
$linter->setObjectSupport(false)->shouldBeCalled();
$linter->setExceptionOnInvalidType(false)->shouldBeCalled();
$linter->setParseCustomTags(false)->shouldBeCalled();
$linter->setParseConstants(false)->shouldBeCalled();
$linter->lint(Argument::type('SplFileInfo'))->willReturn(new LintErrorsCollection());

$context->getFiles()->willReturn(new FilesCollection([
Expand All @@ -86,6 +90,8 @@ function it_throws_exception_if_the_process_fails(YamlLinter $linter, ContextInt
$linter->isInstalled()->willReturn(true);
$linter->setObjectSupport(false)->shouldBeCalled();
$linter->setExceptionOnInvalidType(false)->shouldBeCalled();
$linter->setParseCustomTags(false)->shouldBeCalled();
$linter->setParseConstants(false)->shouldBeCalled();
$linter->lint(Argument::type('SplFileInfo'))->willReturn(new LintErrorsCollection([
new YamlLintError(LintError::TYPE_ERROR, 0, 'error', 'file.yaml', 1, 1)
]));
Expand Down
38 changes: 36 additions & 2 deletions src/Linter/Yaml/YamlLinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ class YamlLinter implements LinterInterface
*/
private $exceptionOnInvalidType = false;

/**
* True if custom tags needs to be parsed
*
* @var bool
*/
private $parseCustomTags = false;

/**
* True if PHP constants needs to be parsed
*
* @var bool
*/
private $parseConstants = false;

/**
* @var Filesystem
*/
Expand Down Expand Up @@ -92,8 +106,10 @@ private function parseYaml($content)

// Lint on Symfony Yaml >= 3.1
$flags = 0;
$flags += $this->objectSupport ? Yaml::PARSE_OBJECT : 0;
$flags += $this->exceptionOnInvalidType ? Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE : 0;
$flags |= $this->objectSupport ? Yaml::PARSE_OBJECT : 0;
$flags |= $this->exceptionOnInvalidType ? Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE : 0;
$flags |= $this->parseConstants ? Yaml::PARSE_CONSTANT : 0;
$flags |= $this->parseCustomTags ? Yaml::PARSE_CUSTOM_TAGS : 0;
Yaml::parse($content, $flags);
}

Expand All @@ -120,4 +136,22 @@ public function setExceptionOnInvalidType($exceptionOnInvalidType)
{
$this->exceptionOnInvalidType = $exceptionOnInvalidType;
}

/**
* @param bool $parseCustomTags
*/
public function setParseCustomTags($parseCustomTags)
{
// Yaml::PARSE_CONSTANT is only available in Symfony Yaml >= 3.2
$this->parseCustomTags = $parseCustomTags && defined('Symfony\Component\Yaml\Yaml::PARSE_CONSTANT');
}

/**
* @param bool $parseConstants
*/
public function setParseConstants($parseConstants)
{
// Yaml::PARSE_CUSTOM_TAGS is only available in Symfony Yaml >= 3.3
$this->parseConstants = $parseConstants && defined('Symfony\Component\Yaml\Yaml::PARSE_CUSTOM_TAGS');
}
}
6 changes: 6 additions & 0 deletions src/Task/YamlLint.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ public function getConfigurableOptions()
$resolver->setDefaults([
'object_support' => false,
'exception_on_invalid_type' => false,
'parse_constant' => false,
'parse_custom_tags' => false,
]);

$resolver->addAllowedTypes('object_support', ['bool']);
$resolver->addAllowedTypes('exception_on_invalid_type', ['bool']);
$resolver->addAllowedTypes('parse_constant', ['bool']);
$resolver->addAllowedTypes('parse_custom_tags', ['bool']);

return $resolver;
}
Expand All @@ -60,6 +64,8 @@ public function run(ContextInterface $context)
$config = $this->getConfiguration();
$this->linter->setObjectSupport($config['object_support']);
$this->linter->setExceptionOnInvalidType($config['exception_on_invalid_type']);
$this->linter->setParseCustomTags($config['parse_custom_tags']);
$this->linter->setParseConstants($config['parse_constant']);

try {
$lintErrors = $this->lint($files);
Expand Down
58 changes: 58 additions & 0 deletions test/Linter/Yaml/YamlLinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,64 @@ function it_should_handle_exceptions_on_invalid_type()
$this->validateFixture($fixture, 1);
}

/**
* @test
*/
function it_should_handle_exceptions_on_constants()
{
if (!YamlLinter::supportsFlags()) {
$this->markTestSkipped('Parsing constants is not supported by the current version of symfony/yaml');
}

$this->linter->setExceptionOnInvalidType(true);
$fixture = 'constant-support.yml';
$this->validateFixture($fixture, 1);
}

/**
* @test
*/
function it_should_validate_constants()
{
if (!YamlLinter::supportsFlags()) {
$this->markTestSkipped('Parsing constants is not supported by the current version of symfony/yaml');
}

$this->linter->setExceptionOnInvalidType(true);
$this->linter->setParseConstants(true);
$fixture = 'constant-support.yml';
$this->validateFixture($fixture, 0);
}

/**
* @test
*/
function it_should_handle_exceptions_on_custom_tags()
{
if (!YamlLinter::supportsFlags()) {
$this->markTestSkipped('Parsing custom tags is not supported by the current version of symfony/yaml');
}

$this->linter->setExceptionOnInvalidType(true);
$fixture = 'tags-support.yml';
$this->validateFixture($fixture, 1);
}

/**
* @test
*/
function it_should_validate_custom_tags()
{
if (!YamlLinter::supportsFlags()) {
$this->markTestSkipped('Parsing custom tags is not supported by the current version of symfony/yaml');
}

$this->linter->setExceptionOnInvalidType(true);
$this->linter->setParseCustomTags(true);
$fixture = 'tags-support.yml';
$this->validateFixture($fixture, 0);
}

/**
* @return array
*/
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/linters/yaml/constant-support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: !php/const:PHP_EOL
1 change: 1 addition & 0 deletions test/fixtures/linters/yaml/tags-support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: !my_tag {foo: bar}

0 comments on commit 4c485fd

Please sign in to comment.