Skip to content

Commit

Permalink
Merge pull request #31 from roaldnefs/add-octal-value-rule
Browse files Browse the repository at this point in the history
Add YamlHasOctalValueRule
  • Loading branch information
Jeffrey authored Oct 7, 2019
2 parents 6dd30a5 + 012fad4 commit e53f147
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/saltlint/rules/YamlHasOctalValueRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2016, Will Thames and contributors
# Copyright (c) 2018, Ansible Project
# Modified work Copyright (c) 2019 Roald Nefs

from saltlint import SaltLintRule
import re


class YamlHasOctalValueRule(SaltLintRule):
id = '210'
shortdesc = 'Numbers that start with `0` should always be encapsulated in quotation marks'
description = 'Numbers that start with `0` should always be encapsulated in quotation marks'
severity = 'HIGH'
tags = ['formatting']
version_added = 'develop'

bracket_regex = re.compile(r"(?<!['\"])0+[1-9]\d*(?!['\"])")

def match(self, file, line):
return self.bracket_regex.search(line)
48 changes: 48 additions & 0 deletions tests/unit/TestYamlHasOctalValueRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright (c) 2013-2018 Will Thames <will@thames.id.au>
# Copyright (c) 2018 Ansible by Red Hat
# Modified work Copyright (c) 2019 Roald Nefs

import unittest

from saltlint import RulesCollection
from saltlint.rules.YamlHasOctalValueRule import YamlHasOctalValueRule
from tests import RunFromText


GOOD_NUMBER_LINE = '''
testdirectory:
file.recurse:
- name: /tmp/directory
- file_mode: 700
- dir_mode: '0775'
testdirectory2:
file.recurse:
- name: /tmp/directory2
- file_mode: 0
- dir_mode: "0775"
'''

BAD_NUMBER_LINE = '''
testdirectory:
file.recurse:
- name: /tmp/directory
- file_mode: 0775
- dir_mode: 070
'''

class TestFileModeLeadingZeroRule(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)
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))

0 comments on commit e53f147

Please sign in to comment.