Skip to content

Commit e3c3ed9

Browse files
committed
WIP - forbid key types
1 parent 95e17b3 commit e3c3ed9

File tree

12 files changed

+400
-0
lines changed

12 files changed

+400
-0
lines changed

tests/rules/test_forbid_bool.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from tests.common import RuleTestCase
2+
3+
4+
class ForbidBoolTestCase(RuleTestCase):
5+
rule_id = 'forbid-bool'
6+
7+
def test_forbid_null_disabled(self):
8+
conf = 'forbid-bool: disable'
9+
self.check('---\n'
10+
'bool:\n'
11+
'valid: "somevalue"\n', conf)
12+
13+
def test_forbid_null_enabled(self):
14+
conf = 'forbid-bool: enable\n'
15+
self.check('---\n'
16+
'bool:\n'
17+
'valid: "somevalue"\n', conf, problem=(2, 1))
18+
19+
def test_forbid_null_with_valid_array_keys(self):
20+
conf = 'forbid-bool: enable'
21+
self.check('---\n'
22+
'valid_key: value\n'
23+
'another_key:\n'
24+
' - item0\n'
25+
' - "bool"\n'
26+
' - item2\n', conf)
27+
28+
def test_forbid_null_with_nested_null(self):
29+
conf = 'forbid-bool: enable'
30+
self.check('---\n'
31+
'outer_key:\n'
32+
' bool:\n'
33+
' inner_key: value\n', conf, problem=(3, 3))
34+
35+
def test_forbid_null_with_nested_null_disabled(self):
36+
conf = 'forbid-bool: disable'
37+
self.check('---\n'
38+
'outer_key:\n'
39+
' bool:\n'
40+
' inner_key: value\n', conf)
41+
42+
def test_forbid_null_with_null_value(self):
43+
conf = 'forbid-bool: enable'
44+
self.check('---\n'
45+
'key: "bool"\n', conf)
46+
47+
def test_forbid_null_with_empty_mapping(self):
48+
conf = 'forbid-bool: enable'
49+
self.check('---\n'
50+
'{}\n', conf)

tests/rules/test_forbid_float.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from tests.common import RuleTestCase
2+
3+
4+
class ForbidFloatTestCase(RuleTestCase):
5+
rule_id = 'forbid-float'
6+
7+
def test_forbid_null_disabled(self):
8+
conf = 'forbid-float: disable'
9+
self.check('---\n'
10+
'float:\n'
11+
'valid: "somevalue"\n', conf)
12+
13+
def test_forbid_null_enabled(self):
14+
conf = 'forbid-float: enable\n'
15+
self.check('---\n'
16+
'float:\n'
17+
'valid: "somevalue"\n', conf, problem=(2, 1))
18+
19+
def test_forbid_null_with_valid_array_keys(self):
20+
conf = 'forbid-float: enable'
21+
self.check('---\n'
22+
'valid_key: value\n'
23+
'another_key:\n'
24+
' - item0\n'
25+
' - "float"\n'
26+
' - item2\n', conf)
27+
28+
def test_forbid_null_with_nested_null(self):
29+
conf = 'forbid-float: enable'
30+
self.check('---\n'
31+
'outer_key:\n'
32+
' float:\n'
33+
' inner_key: value\n', conf, problem=(3, 3))
34+
35+
def test_forbid_null_with_nested_null_disabled(self):
36+
conf = 'forbid-float: disable'
37+
self.check('---\n'
38+
'outer_key:\n'
39+
' float:\n'
40+
' inner_key: value\n', conf)
41+
42+
def test_forbid_null_with_null_value(self):
43+
conf = 'forbid-float: enable'
44+
self.check('---\n'
45+
'key: "float"\n', conf)
46+
47+
def test_forbid_null_with_empty_mapping(self):
48+
conf = 'forbid-float: enable'
49+
self.check('---\n'
50+
'{}\n', conf)

tests/rules/test_forbid_int.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from tests.common import RuleTestCase
2+
3+
4+
class ForbidIntTestCase(RuleTestCase):
5+
rule_id = 'forbid-int'
6+
7+
def test_forbid_null_disabled(self):
8+
conf = 'forbid-int: disable'
9+
self.check('---\n'
10+
'int:\n'
11+
'valid: "somevalue"\n', conf)
12+
13+
def test_forbid_null_enabled(self):
14+
conf = 'forbid-int: enable\n'
15+
self.check('---\n'
16+
'int:\n'
17+
'valid: "somevalue"\n', conf, problem=(2, 1))
18+
19+
def test_forbid_null_with_valid_array_keys(self):
20+
conf = 'forbid-int: enable'
21+
self.check('---\n'
22+
'valid_key: value\n'
23+
'another_key:\n'
24+
' - item0\n'
25+
' - "int"\n'
26+
' - item2\n', conf)
27+
28+
def test_forbid_null_with_nested_null(self):
29+
conf = 'forbid-int: enable'
30+
self.check('---\n'
31+
'outer_key:\n'
32+
' int:\n'
33+
' inner_key: value\n', conf, problem=(3, 3))
34+
35+
def test_forbid_null_with_nested_null_disabled(self):
36+
conf = 'forbid-int: disable'
37+
self.check('---\n'
38+
'outer_key:\n'
39+
' int:\n'
40+
' inner_key: value\n', conf)
41+
42+
def test_forbid_null_with_null_value(self):
43+
conf = 'forbid-int: enable'
44+
self.check('---\n'
45+
'key: "int"\n', conf)
46+
47+
def test_forbid_null_with_empty_mapping(self):
48+
conf = 'forbid-int: enable'
49+
self.check('---\n'
50+
'{}\n', conf)

tests/rules/test_forbid_null.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from tests.common import RuleTestCase
2+
3+
4+
class ForbidNullTestCase(RuleTestCase):
5+
rule_id = 'forbid-null'
6+
7+
def test_forbid_null_disabled(self):
8+
conf = 'forbid-null: disable'
9+
self.check('---\n'
10+
'null:\n'
11+
'valid: "somevalue"\n', conf)
12+
13+
def test_forbid_null_enabled(self):
14+
conf = 'forbid-null: enable\n'
15+
self.check('---\n'
16+
'null:\n'
17+
'valid: "somevalue"\n', conf, problem=(2, 1))
18+
19+
def test_forbid_null_with_valid_array_keys(self):
20+
conf = 'forbid-null: enable'
21+
self.check('---\n'
22+
'valid_key: value\n'
23+
'another_key:\n'
24+
' - item0\n'
25+
' - "null"\n'
26+
' - item2\n', conf)
27+
28+
def test_forbid_null_with_nested_null(self):
29+
conf = 'forbid-null: enable'
30+
self.check('---\n'
31+
'outer_key:\n'
32+
' null:\n'
33+
' inner_key: value\n', conf, problem=(3, 3))
34+
35+
def test_forbid_null_with_nested_null_disabled(self):
36+
conf = 'forbid-null: disable'
37+
self.check('---\n'
38+
'outer_key:\n'
39+
' null:\n'
40+
' inner_key: value\n', conf)
41+
42+
def test_forbid_null_with_null_value(self):
43+
conf = 'forbid-null: enable'
44+
self.check('---\n'
45+
'key: "null"\n', conf)
46+
47+
def test_forbid_null_with_empty_mapping(self):
48+
conf = 'forbid-null: enable'
49+
self.check('---\n'
50+
'{}\n', conf)

tests/rules/test_forbid_str.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from tests.common import RuleTestCase
2+
3+
4+
class ForbidStrTestCase(RuleTestCase):
5+
rule_id = 'forbid-str'
6+
7+
def test_forbid_null_disabled(self):
8+
conf = 'forbid-str: disable'
9+
self.check('---\n'
10+
'str:\n'
11+
'valid: "somevalue"\n', conf)
12+
13+
def test_forbid_null_enabled(self):
14+
conf = 'forbid-str: enable\n'
15+
self.check('---\n'
16+
'str:\n'
17+
'valid: "somevalue"\n', conf, problem=(2, 1))
18+
19+
def test_forbid_null_with_valid_array_keys(self):
20+
conf = 'forbid-str: enable'
21+
self.check('---\n'
22+
'valid_key: value\n'
23+
'another_key:\n'
24+
' - item0\n'
25+
' - "str"\n'
26+
' - item2\n', conf)
27+
28+
def test_forbid_null_with_nested_null(self):
29+
conf = 'forbid-str: enable'
30+
self.check('---\n'
31+
'outer_key:\n'
32+
' str:\n'
33+
' inner_key: value\n', conf, problem=(3, 3))
34+
35+
def test_forbid_null_with_nested_null_disabled(self):
36+
conf = 'forbid-str: disable'
37+
self.check('---\n'
38+
'outer_key:\n'
39+
' str:\n'
40+
' inner_key: value\n', conf)
41+
42+
def test_forbid_null_with_null_value(self):
43+
conf = 'forbid-str: enable'
44+
self.check('---\n'
45+
'key: "str"\n', conf)
46+
47+
def test_forbid_null_with_empty_mapping(self):
48+
conf = 'forbid-str: enable'
49+
self.check('---\n'
50+
'{}\n', conf)

yamllint/conf/default.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ rules:
3333
trailing-spaces: enable
3434
truthy:
3535
level: warning
36+
forbid-null: disable
37+
forbid-bool: disable
38+
forbid-int: disable
39+
forbid-float: disable
40+
forbid-str: disable

yamllint/rules/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
quoted_strings,
3838
trailing_spaces,
3939
truthy,
40+
forbid_null,
41+
forbid_bool,
42+
forbid_int,
43+
forbid_float,
44+
forbid_str,
4045
)
4146

4247
_RULES = {
@@ -63,6 +68,11 @@
6368
quoted_strings.ID: quoted_strings,
6469
trailing_spaces.ID: trailing_spaces,
6570
truthy.ID: truthy,
71+
forbid_null.ID: forbid_null,
72+
forbid_bool.ID: forbid_bool,
73+
forbid_int.ID: forbid_int,
74+
forbid_float.ID: forbid_float,
75+
forbid_str.ID: forbid_str,
6676
}
6777

6878

yamllint/rules/forbid_bool.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Use this rule to ......
3+
4+
"""
5+
6+
import yaml
7+
from yamllint.linter import LintProblem
8+
9+
ID = 'forbid-bool'
10+
TYPE = 'token'
11+
CONF = {'forbid-bool': bool}
12+
DEFAULT = {'forbid-bool': False}
13+
14+
15+
def check(conf, token, prev, next, nextnext, context):
16+
if not isinstance(token, yaml.tokens.ScalarToken):
17+
return
18+
if token.style:
19+
return
20+
val = token.value
21+
22+
if isinstance(token, yaml.tokens.ScalarToken):
23+
if val == "bool":
24+
yield LintProblem(
25+
token.start_mark.line + 1,
26+
token.start_mark.column + 1,
27+
'Found key with value "bool"')

yamllint/rules/forbid_float.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Use this rule to ......
3+
4+
"""
5+
6+
import yaml
7+
from yamllint.linter import LintProblem
8+
9+
ID = 'forbid-float'
10+
TYPE = 'token'
11+
CONF = {'forbid-float': bool}
12+
DEFAULT = {'forbid-float': False}
13+
14+
15+
def check(conf, token, prev, next, nextnext, context):
16+
if not isinstance(token, yaml.tokens.ScalarToken):
17+
return
18+
if token.style:
19+
return
20+
val = token.value
21+
22+
if isinstance(token, yaml.tokens.ScalarToken):
23+
if val == "float":
24+
yield LintProblem(
25+
token.start_mark.line + 1,
26+
token.start_mark.column + 1,
27+
'Found key with value "float"')

yamllint/rules/forbid_int.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Use this rule to ......
3+
4+
"""
5+
6+
import yaml
7+
from yamllint.linter import LintProblem
8+
9+
ID = 'forbid-int'
10+
TYPE = 'token'
11+
CONF = {'forbid-int': bool}
12+
DEFAULT = {'forbid-int': False}
13+
14+
15+
def check(conf, token, prev, next, nextnext, context):
16+
if not isinstance(token, yaml.tokens.ScalarToken):
17+
return
18+
if token.style:
19+
return
20+
val = token.value
21+
22+
if isinstance(token, yaml.tokens.ScalarToken):
23+
if val == "int":
24+
yield LintProblem(
25+
token.start_mark.line + 1,
26+
token.start_mark.column + 1,
27+
'Found key with value "int"')

0 commit comments

Comments
 (0)