From 5cc900f2a8323a6994765c9997618ea64d8b6117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Thu, 14 Jan 2016 17:09:25 +0100 Subject: [PATCH] Rules: document-start: Allow directives --- tests/rules/test_document_start.py | 20 +++++++++++++++++--- tests/rules/test_syntax_error.py | 6 ++++++ yamllint/rules/document_start.py | 13 ++++++------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/tests/rules/test_document_start.py b/tests/rules/test_document_start.py index 228e6fd..29a6dc0 100644 --- a/tests/rules/test_document_start.py +++ b/tests/rules/test_document_start.py @@ -85,6 +85,20 @@ class DocumentStartTestCase(RuleTestCase): 'third: document\n', conf, problem=(4, 1)) def test_directives(self): - # TODO - # %YAML 1.2 - pass + conf = 'document-start: {present: yes}' + self.check('%YAML 1.2\n' + '---\n' + 'doc: ument\n' + '...\n', conf) + self.check('%YAML 1.2\n' + '%TAG ! tag:clarkevans.com,2002:\n' + '---\n' + 'doc: ument\n' + '...\n', conf) + self.check('---\n' + 'doc: 1\n' + '...\n' + '%YAML 1.2\n' + '---\n' + 'doc: 2\n' + '...\n', conf) diff --git a/tests/rules/test_syntax_error.py b/tests/rules/test_syntax_error.py index d47eca2..c5ed79b 100644 --- a/tests/rules/test_syntax_error.py +++ b/tests/rules/test_syntax_error.py @@ -29,3 +29,9 @@ class YamlLintTestCase(RuleTestCase): 'this is an error: [\n' '\n' '...\n', None, problem=(6, 1)) + + def test_directives(self): + self.check('%YAML 1.2\n' + '%TAG ! tag:clarkevans.com,2002:\n' + 'doc: ument\n' + '...\n', None, problem=(3, 1)) diff --git a/yamllint/rules/document_start.py b/yamllint/rules/document_start.py index b0d3554..54cd418 100644 --- a/yamllint/rules/document_start.py +++ b/yamllint/rules/document_start.py @@ -24,15 +24,14 @@ TYPE = 'token' CONF = {'present': bool} -# TODO: Don't fail if document contains directives such as -# %YAML 1.2 - def check(conf, token, prev, next): if conf['present']: - if ((isinstance(prev, yaml.StreamStartToken) or - isinstance(prev, yaml.DocumentEndToken)) and - not (isinstance(token, yaml.DocumentStartToken) or - isinstance(token, yaml.StreamEndToken))): + if (isinstance(prev, (yaml.StreamStartToken, + yaml.DocumentEndToken, + yaml.DirectiveToken)) and + not isinstance(token, (yaml.DocumentStartToken, + yaml.DirectiveToken, + yaml.StreamEndToken))): yield LintProblem(token.start_mark.line + 1, 1, 'missing document start "---"')