Skip to content

Commit

Permalink
anchors: Fix invalid YAML in aliases test cases
Browse files Browse the repository at this point in the history
Although accepted by PyYAML, `{*x: 4}` is not valid YAML: it should be
noted `{*x : 4}`. The reason is that a colon can be part of an anchor
name. See this comment from Tina Müller for more details:
#550 (comment)

Even if it's not a problem for yamllint, let's fix our tests to include
valid YAML snippets.
  • Loading branch information
adrienverge committed Apr 4, 2023
1 parent 6bfd675 commit 32ebee9
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions tests/rules/test_anchors.py
Expand Up @@ -20,7 +20,8 @@ class AnchorsTestCase(RuleTestCase):
rule_id = 'anchors'

def test_disabled(self):
conf = 'anchors: disable'
conf = ('anchors: disable\n'
'colons: disable\n')
self.check('---\n'
'- &b true\n'
'- &i 42\n'
Expand All @@ -46,7 +47,7 @@ def test_disabled(self):
' <<: *b_m\n'
' foo: bar\n'
'---\n'
'{a: 1, &x b: 2, c: &y 3, *x: 4, e: *y}\n'
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
'...\n', conf)
self.check('---\n'
'- &i 42\n'
Expand Down Expand Up @@ -74,13 +75,14 @@ def test_disabled(self):
' <<: *b_m\n'
' foo: bar\n'
'---\n'
'{a: 1, &x b: 2, c: &x 3, *x: 4, e: *y}\n'
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
'...\n', conf)

def test_forbid_undeclared_aliases(self):
conf = ('anchors:\n'
' forbid-undeclared-aliases: true\n'
' forbid-duplicated-anchors: false\n')
' forbid-duplicated-anchors: false\n'
'colons: disable\n')
self.check('---\n'
'- &b true\n'
'- &i 42\n'
Expand All @@ -106,7 +108,7 @@ def test_forbid_undeclared_aliases(self):
' <<: *b_m\n'
' foo: bar\n'
'---\n'
'{a: 1, &x b: 2, c: &y 3, *x: 4, e: *y}\n'
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
'...\n', conf)
self.check('---\n'
'- &i 42\n'
Expand Down Expand Up @@ -134,20 +136,21 @@ def test_forbid_undeclared_aliases(self):
' <<: *b_m\n'
' foo: bar\n'
'---\n'
'{a: 1, &x b: 2, c: &x 3, *x: 4, e: *y}\n'
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
'...\n', conf,
problem1=(9, 3),
problem2=(10, 3),
problem3=(11, 3),
problem4=(12, 3),
problem5=(13, 3),
problem6=(24, 7),
problem7=(27, 36))
problem7=(27, 37))

def test_forbid_duplicated_anchors(self):
conf = ('anchors:\n'
' forbid-undeclared-aliases: false\n'
' forbid-duplicated-anchors: true\n')
' forbid-duplicated-anchors: true\n'
'colons: disable\n')
self.check('---\n'
'- &b true\n'
'- &i 42\n'
Expand All @@ -173,7 +176,7 @@ def test_forbid_duplicated_anchors(self):
' <<: *b_m\n'
' foo: bar\n'
'---\n'
'{a: 1, &x b: 2, c: &y 3, *x: 4, e: *y}\n'
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
'...\n', conf)
self.check('---\n'
'- &i 42\n'
Expand Down Expand Up @@ -201,7 +204,7 @@ def test_forbid_duplicated_anchors(self):
' <<: *b_m\n'
' foo: bar\n'
'---\n'
'{a: 1, &x b: 2, c: &x 3, *x: 4, e: *y}\n'
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
'...\n', conf,
problem1=(5, 3),
problem2=(6, 3),
Expand Down

0 comments on commit 32ebee9

Please sign in to comment.