diff --git a/tests/rules/test_forbid_bool.py b/tests/rules/test_forbid_bool.py new file mode 100644 index 00000000..1fbb9404 --- /dev/null +++ b/tests/rules/test_forbid_bool.py @@ -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) diff --git a/tests/rules/test_forbid_float.py b/tests/rules/test_forbid_float.py new file mode 100644 index 00000000..8b6b9e93 --- /dev/null +++ b/tests/rules/test_forbid_float.py @@ -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) diff --git a/tests/rules/test_forbid_int.py b/tests/rules/test_forbid_int.py new file mode 100644 index 00000000..b51d8358 --- /dev/null +++ b/tests/rules/test_forbid_int.py @@ -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) diff --git a/tests/rules/test_forbid_null.py b/tests/rules/test_forbid_null.py new file mode 100644 index 00000000..2017ea65 --- /dev/null +++ b/tests/rules/test_forbid_null.py @@ -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) diff --git a/tests/rules/test_forbid_str.py b/tests/rules/test_forbid_str.py new file mode 100644 index 00000000..18e2dea7 --- /dev/null +++ b/tests/rules/test_forbid_str.py @@ -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) diff --git a/yamllint/conf/default.yaml b/yamllint/conf/default.yaml index b082e228..1a00e06e 100644 --- a/yamllint/conf/default.yaml +++ b/yamllint/conf/default.yaml @@ -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 diff --git a/yamllint/rules/__init__.py b/yamllint/rules/__init__.py index 815d4bcf..d15f313f 100644 --- a/yamllint/rules/__init__.py +++ b/yamllint/rules/__init__.py @@ -37,6 +37,11 @@ quoted_strings, trailing_spaces, truthy, + forbid_null, + forbid_bool, + forbid_int, + forbid_float, + forbid_str, ) _RULES = { @@ -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, } diff --git a/yamllint/rules/forbid_bool.py b/yamllint/rules/forbid_bool.py new file mode 100644 index 00000000..04a67857 --- /dev/null +++ b/yamllint/rules/forbid_bool.py @@ -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"') diff --git a/yamllint/rules/forbid_float.py b/yamllint/rules/forbid_float.py new file mode 100644 index 00000000..353fd0da --- /dev/null +++ b/yamllint/rules/forbid_float.py @@ -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"') diff --git a/yamllint/rules/forbid_int.py b/yamllint/rules/forbid_int.py new file mode 100644 index 00000000..10ec82dd --- /dev/null +++ b/yamllint/rules/forbid_int.py @@ -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"') diff --git a/yamllint/rules/forbid_null.py b/yamllint/rules/forbid_null.py new file mode 100644 index 00000000..5a59d67f --- /dev/null +++ b/yamllint/rules/forbid_null.py @@ -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"') diff --git a/yamllint/rules/forbid_str.py b/yamllint/rules/forbid_str.py new file mode 100644 index 00000000..27ae9721 --- /dev/null +++ b/yamllint/rules/forbid_str.py @@ -0,0 +1,27 @@ +""" +Use this rule to ...... + +""" + +import yaml +from yamllint.linter import LintProblem + +ID = 'forbid-str' +TYPE = 'token' +CONF = {'forbid-str': bool} +DEFAULT = {'forbid-str': 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 == "str": + yield LintProblem( + token.start_mark.line + 1, + token.start_mark.column + 1, + 'Found key with value "str"')