Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule for colons similar to yamllint #116

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Rule | Description
[210](https://github.com/warpnet/salt-lint/wiki/210) | Numbers that start with `0` should always be encapsulated in quotation marks
[211](https://github.com/warpnet/salt-lint/wiki/211) | `pillar.get` or `grains.get` should be formatted differently
[212](https://github.com/warpnet/salt-lint/wiki/212) | Most files should not contain irregular spaces
[214](https://github.com/warpnet/salt-lint/wiki/214) | The line should have an proper colons formatting

## False Positives: Skipping Rules

Expand Down
19 changes: 19 additions & 0 deletions saltlint/rules/ColonFormatRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Dawid Malinowski <dawidmalina@gmail.com>

import re
from saltlint.linter import SaltLintRule


class ColonFormatRule(SaltLintRule):
id = '214'
shortdesc = 'The line should have an proper colons formatting.'
description = 'The line should have an proper colons formatting.'
severity = 'MEDIUM'
tags = ['formatting']
version_added = 'develop'

regex = re.compile(r"([\s]{1,}:)|((:\{)(?!\{))|(:\[)")

def match(self, _, line):
return self.regex.search(line)
96 changes: 96 additions & 0 deletions tests/unit/TestColonFormatRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Dawid Malinowski <dawidmalina@gmail.com>

import unittest

from saltlint.linter import RulesCollection
from saltlint.rules.ColonFormatRule import ColonFormatRule
from tests import RunFromText


BAD_EXTRA_SPACE = '''
# the following code snippet would fail:
/path/to/file1:
file.managed:
- contents : This is line 1
'''

BAD_NO_SPACE_BRACKET = '''
# the following code snippet would fail:
/path/to/file1:
file.managed:
- contents:[]
'''

BAD_NO_SPACE_CURLY_BRACKET = '''
# the following code snippet would fail:
/path/to/file1:
file.managed:
- contents:{}
'''

GOOD_SPACE_BRACKET = '''
# the following code snippet would fail:
/path/to/file1:
file.managed:
- contents: []
'''

GOOD_SPACE_CURLY_BRACKET = '''
# the following code snippet would fail:
/path/to/file1:
file.managed:
- contents: {}
'''

GOOD_NO_EXTRA_SPACE = '''
# the following code snippet would pass:
/path/to/file1:
file.managed:
- contents: This is line 1
'''

GOOD_CORNER_CASE = '''
# the following code snippet would pass:
example_file:
file.managed:
- name: /etc/example.txt
- contents: |
line:with:colons:without:spaces
'''


class TestColonFormatRule(unittest.TestCase):
collection = RulesCollection()

def setUp(self):
self.collection.register(ColonFormatRule())
self.runner = RunFromText(self.collection)

def test_bad_extra_space(self):
results = self.runner.run_state(BAD_EXTRA_SPACE)
self.assertEqual(1, len(results))

def test_bad_no_space_bracket(self):
results = self.runner.run_state(BAD_NO_SPACE_BRACKET)
self.assertEqual(2, len(results))

def test_bad_no_space_curly_bracket(self):
results = self.runner.run_state(BAD_NO_SPACE_CURLY_BRACKET)
self.assertEqual(3, len(results))

def test_good_space_bracket(self):
results = self.runner.run_state(GOOD_SPACE_BRACKET)
self.assertEqual(0, len(results))

def test_good_space_curly_bracket(self):
results = self.runner.run_state(GOOD_SPACE_CURLY_BRACKET)
self.assertEqual(0, len(results))

def test_good_no_extra_space(self):
results = self.runner.run_state(GOOD_NO_EXTRA_SPACE)
self.assertEqual(0, len(results))

def test_good_corner_case(self):
results = self.runner.run_state(GOOD_CORNER_CASE)
self.assertEqual(0, len(results))