Skip to content

Commit

Permalink
Fix 210 to skip non-number values
Browse files Browse the repository at this point in the history
Fix rule 210 to skip non-number values (e.g. `000-default`) as they
aren't octal values but strings.

This commit also allows optional whitespace around the unquoted octal
value, and the value followed by a YAML or JINJA comment (e.g.
`0700 {# JINJA COMMENT #}`).

Fixes #68.

Signed-off-by: Roald Nefs <info@roaldnefs.com>
  • Loading branch information
roaldnefs committed Oct 21, 2019
1 parent 10554fe commit 8dab577
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion saltlint/rules/YamlHasOctalValueRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class YamlHasOctalValueRule(SaltLintRule):
tags = ['formatting']
version_added = 'v0.0.6'

bracket_regex = re.compile(r": (?<!['\"])0+[0-9]\d*(?!['\"])")
bracket_regex = re.compile(r"(?<=:)\s{0,}0[0-9]{1,}\s{0,}((?={#)|(?=#)|(?=$))")

def match(self, file, line):
return self.bracket_regex.search(line)
34 changes: 24 additions & 10 deletions tests/unit/TestYamlHasOctalValueRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from tests import RunFromText


GOOD_NUMBER_LINE = '''
GOOD_NUMBER_STATE = '''
testdirectory:
file.recurse:
- name: /tmp/directory
Expand All @@ -22,29 +22,43 @@
- name: /tmp/directory02
- file_mode: 0
- dir_mode: "0775"
# Non-number values should be skipped, for more information see:
# https://github.com/warpnet/salt-lint/issues/68
apache_disable_default_site:
apache_site.disabled:
- name: 000-default
'''

BAD_NUMBER_LINE = '''
BAD_NUMBER_STATE = '''
# Unquoted octal values with leading zero's.
testdirectory:
file.recurse:
- name: /tmp/directory
- file_mode: 00
- dir_mode: 0700
# Unquoted octal values with leading zero's followed by a YAML of Jinja
# comment.
testdirectory:
file.recurse:
- name: /tmp/directory001 # shouldn't fail
- mode: 0 # shouldn't fail
- file_mode: 00 # should fail
- dir_mode: 0700 # should fail
- name: /tmp/directory
- file_mode: 00 # COMMENT
- dir_mode:0700{# JINJA COMMENT #}
'''

class TestFileModeLeadingZeroRule(unittest.TestCase):
class TestYamlHasOctalValueRule(unittest.TestCase):
collection = RulesCollection()

def setUp(self):
self.collection.register(YamlHasOctalValueRule())

def test_statement_positive(self):
runner = RunFromText(self.collection)
results = runner.run_state(GOOD_NUMBER_LINE)
results = runner.run_state(GOOD_NUMBER_STATE)
self.assertEqual(0, len(results))

def test_statement_negative(self):
runner = RunFromText(self.collection)
results = runner.run_state(BAD_NUMBER_LINE)
self.assertEqual(2, len(results))
results = runner.run_state(BAD_NUMBER_STATE)
self.assertEqual(4, len(results))

0 comments on commit 8dab577

Please sign in to comment.