Skip to content

Commit

Permalink
WIP - forbid key types
Browse files Browse the repository at this point in the history
  • Loading branch information
AvnerCohen committed Oct 29, 2024
1 parent 95e17b3 commit e3c3ed9
Show file tree
Hide file tree
Showing 12 changed files with 400 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/rules/test_forbid_bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from tests.common import RuleTestCase


class ForbidBoolTestCase(RuleTestCase):
rule_id = 'forbid-bool'

def test_forbid_null_disabled(self):
conf = 'forbid-bool: disable'
self.check('---\n'
'bool:\n'
'valid: "somevalue"\n', conf)

def test_forbid_null_enabled(self):
conf = 'forbid-bool: enable\n'
self.check('---\n'
'bool:\n'
'valid: "somevalue"\n', conf, problem=(2, 1))

def test_forbid_null_with_valid_array_keys(self):
conf = 'forbid-bool: enable'
self.check('---\n'
'valid_key: value\n'
'another_key:\n'
' - item0\n'
' - "bool"\n'
' - item2\n', conf)

def test_forbid_null_with_nested_null(self):
conf = 'forbid-bool: enable'
self.check('---\n'
'outer_key:\n'
' bool:\n'
' inner_key: value\n', conf, problem=(3, 3))

def test_forbid_null_with_nested_null_disabled(self):
conf = 'forbid-bool: disable'
self.check('---\n'
'outer_key:\n'
' bool:\n'
' inner_key: value\n', conf)

def test_forbid_null_with_null_value(self):
conf = 'forbid-bool: enable'
self.check('---\n'
'key: "bool"\n', conf)

def test_forbid_null_with_empty_mapping(self):
conf = 'forbid-bool: enable'
self.check('---\n'
'{}\n', conf)
50 changes: 50 additions & 0 deletions tests/rules/test_forbid_float.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from tests.common import RuleTestCase


class ForbidFloatTestCase(RuleTestCase):
rule_id = 'forbid-float'

def test_forbid_null_disabled(self):
conf = 'forbid-float: disable'
self.check('---\n'
'float:\n'
'valid: "somevalue"\n', conf)

def test_forbid_null_enabled(self):
conf = 'forbid-float: enable\n'
self.check('---\n'
'float:\n'
'valid: "somevalue"\n', conf, problem=(2, 1))

def test_forbid_null_with_valid_array_keys(self):
conf = 'forbid-float: enable'
self.check('---\n'
'valid_key: value\n'
'another_key:\n'
' - item0\n'
' - "float"\n'
' - item2\n', conf)

def test_forbid_null_with_nested_null(self):
conf = 'forbid-float: enable'
self.check('---\n'
'outer_key:\n'
' float:\n'
' inner_key: value\n', conf, problem=(3, 3))

def test_forbid_null_with_nested_null_disabled(self):
conf = 'forbid-float: disable'
self.check('---\n'
'outer_key:\n'
' float:\n'
' inner_key: value\n', conf)

def test_forbid_null_with_null_value(self):
conf = 'forbid-float: enable'
self.check('---\n'
'key: "float"\n', conf)

def test_forbid_null_with_empty_mapping(self):
conf = 'forbid-float: enable'
self.check('---\n'
'{}\n', conf)
50 changes: 50 additions & 0 deletions tests/rules/test_forbid_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from tests.common import RuleTestCase


class ForbidIntTestCase(RuleTestCase):
rule_id = 'forbid-int'

def test_forbid_null_disabled(self):
conf = 'forbid-int: disable'
self.check('---\n'
'int:\n'
'valid: "somevalue"\n', conf)

def test_forbid_null_enabled(self):
conf = 'forbid-int: enable\n'
self.check('---\n'
'int:\n'
'valid: "somevalue"\n', conf, problem=(2, 1))

def test_forbid_null_with_valid_array_keys(self):
conf = 'forbid-int: enable'
self.check('---\n'
'valid_key: value\n'
'another_key:\n'
' - item0\n'
' - "int"\n'
' - item2\n', conf)

def test_forbid_null_with_nested_null(self):
conf = 'forbid-int: enable'
self.check('---\n'
'outer_key:\n'
' int:\n'
' inner_key: value\n', conf, problem=(3, 3))

def test_forbid_null_with_nested_null_disabled(self):
conf = 'forbid-int: disable'
self.check('---\n'
'outer_key:\n'
' int:\n'
' inner_key: value\n', conf)

def test_forbid_null_with_null_value(self):
conf = 'forbid-int: enable'
self.check('---\n'
'key: "int"\n', conf)

def test_forbid_null_with_empty_mapping(self):
conf = 'forbid-int: enable'
self.check('---\n'
'{}\n', conf)
50 changes: 50 additions & 0 deletions tests/rules/test_forbid_null.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from tests.common import RuleTestCase


class ForbidNullTestCase(RuleTestCase):
rule_id = 'forbid-null'

def test_forbid_null_disabled(self):
conf = 'forbid-null: disable'
self.check('---\n'
'null:\n'
'valid: "somevalue"\n', conf)

def test_forbid_null_enabled(self):
conf = 'forbid-null: enable\n'
self.check('---\n'
'null:\n'
'valid: "somevalue"\n', conf, problem=(2, 1))

def test_forbid_null_with_valid_array_keys(self):
conf = 'forbid-null: enable'
self.check('---\n'
'valid_key: value\n'
'another_key:\n'
' - item0\n'
' - "null"\n'
' - item2\n', conf)

def test_forbid_null_with_nested_null(self):
conf = 'forbid-null: enable'
self.check('---\n'
'outer_key:\n'
' null:\n'
' inner_key: value\n', conf, problem=(3, 3))

def test_forbid_null_with_nested_null_disabled(self):
conf = 'forbid-null: disable'
self.check('---\n'
'outer_key:\n'
' null:\n'
' inner_key: value\n', conf)

def test_forbid_null_with_null_value(self):
conf = 'forbid-null: enable'
self.check('---\n'
'key: "null"\n', conf)

def test_forbid_null_with_empty_mapping(self):
conf = 'forbid-null: enable'
self.check('---\n'
'{}\n', conf)
50 changes: 50 additions & 0 deletions tests/rules/test_forbid_str.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from tests.common import RuleTestCase


class ForbidStrTestCase(RuleTestCase):
rule_id = 'forbid-str'

def test_forbid_null_disabled(self):
conf = 'forbid-str: disable'
self.check('---\n'
'str:\n'
'valid: "somevalue"\n', conf)

def test_forbid_null_enabled(self):
conf = 'forbid-str: enable\n'
self.check('---\n'
'str:\n'
'valid: "somevalue"\n', conf, problem=(2, 1))

def test_forbid_null_with_valid_array_keys(self):
conf = 'forbid-str: enable'
self.check('---\n'
'valid_key: value\n'
'another_key:\n'
' - item0\n'
' - "str"\n'
' - item2\n', conf)

def test_forbid_null_with_nested_null(self):
conf = 'forbid-str: enable'
self.check('---\n'
'outer_key:\n'
' str:\n'
' inner_key: value\n', conf, problem=(3, 3))

def test_forbid_null_with_nested_null_disabled(self):
conf = 'forbid-str: disable'
self.check('---\n'
'outer_key:\n'
' str:\n'
' inner_key: value\n', conf)

def test_forbid_null_with_null_value(self):
conf = 'forbid-str: enable'
self.check('---\n'
'key: "str"\n', conf)

def test_forbid_null_with_empty_mapping(self):
conf = 'forbid-str: enable'
self.check('---\n'
'{}\n', conf)
5 changes: 5 additions & 0 deletions yamllint/conf/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ rules:
trailing-spaces: enable
truthy:
level: warning
forbid-null: disable
forbid-bool: disable
forbid-int: disable
forbid-float: disable
forbid-str: disable
10 changes: 10 additions & 0 deletions yamllint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
quoted_strings,
trailing_spaces,
truthy,
forbid_null,
forbid_bool,
forbid_int,
forbid_float,
forbid_str,
)

_RULES = {
Expand All @@ -63,6 +68,11 @@
quoted_strings.ID: quoted_strings,
trailing_spaces.ID: trailing_spaces,
truthy.ID: truthy,
forbid_null.ID: forbid_null,
forbid_bool.ID: forbid_bool,
forbid_int.ID: forbid_int,
forbid_float.ID: forbid_float,
forbid_str.ID: forbid_str,
}


Expand Down
27 changes: 27 additions & 0 deletions yamllint/rules/forbid_bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Use this rule to ......
"""

import yaml
from yamllint.linter import LintProblem

ID = 'forbid-bool'
TYPE = 'token'
CONF = {'forbid-bool': bool}
DEFAULT = {'forbid-bool': False}


def check(conf, token, prev, next, nextnext, context):
if not isinstance(token, yaml.tokens.ScalarToken):
return
if token.style:
return
val = token.value

if isinstance(token, yaml.tokens.ScalarToken):
if val == "bool":
yield LintProblem(
token.start_mark.line + 1,
token.start_mark.column + 1,
'Found key with value "bool"')
27 changes: 27 additions & 0 deletions yamllint/rules/forbid_float.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Use this rule to ......
"""

import yaml
from yamllint.linter import LintProblem

ID = 'forbid-float'
TYPE = 'token'
CONF = {'forbid-float': bool}
DEFAULT = {'forbid-float': False}


def check(conf, token, prev, next, nextnext, context):
if not isinstance(token, yaml.tokens.ScalarToken):
return
if token.style:
return
val = token.value

if isinstance(token, yaml.tokens.ScalarToken):
if val == "float":
yield LintProblem(
token.start_mark.line + 1,
token.start_mark.column + 1,
'Found key with value "float"')
27 changes: 27 additions & 0 deletions yamllint/rules/forbid_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Use this rule to ......
"""

import yaml
from yamllint.linter import LintProblem

ID = 'forbid-int'
TYPE = 'token'
CONF = {'forbid-int': bool}
DEFAULT = {'forbid-int': False}


def check(conf, token, prev, next, nextnext, context):
if not isinstance(token, yaml.tokens.ScalarToken):
return
if token.style:
return
val = token.value

if isinstance(token, yaml.tokens.ScalarToken):
if val == "int":
yield LintProblem(
token.start_mark.line + 1,
token.start_mark.column + 1,
'Found key with value "int"')
27 changes: 27 additions & 0 deletions yamllint/rules/forbid_null.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Use this rule to ......
"""

import yaml
from yamllint.linter import LintProblem

ID = 'forbid-null'
TYPE = 'token'
CONF = {'forbid-null': bool}
DEFAULT = {'forbid-null': False}


def check(conf, token, prev, next, nextnext, context):
if not isinstance(token, yaml.tokens.ScalarToken):
return
if token.style:
return
val = token.value

if isinstance(token, yaml.tokens.ScalarToken):
if val == "null":
yield LintProblem(
token.start_mark.line + 1,
token.start_mark.column + 1,
'Found key with value "null"')
Loading

0 comments on commit e3c3ed9

Please sign in to comment.