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 to check for irregular spaces #76

Merged
merged 1 commit into from
Oct 25, 2019
Merged
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
43 changes: 43 additions & 0 deletions saltlint/rules/NoIrregularSpacesRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-

from saltlint.linter import SaltLintRule


class NoIrregularSpacesRule(SaltLintRule):
id = '212'
shortdesc = 'Most files should not contain irregular spaces'
description = 'Irregular spaces can cause unexpected display issues, use spaces'
severity = 'LOW'
tags = ['formatting']
version_added = 'develop'

irregular_spaces = [
u"\u000B", # Line Tabulation (\v) - <VT>
u"\u000C", # Form Feed (\f) - <FF>
u"\u00A0", # No-Break Space - <NBSP>
u"\u0085", # Next Line
u"\u1680", # Ogham Space Mark
u"\u180E", # Mongolian Vowel Separator - <MVS>
u"\uFEFF", # Zero Width No-Break Space - <BOM>
u"\u2000", # En Quad
u"\u2001", # Em Quad
u"\u2002", # En Space - <ENSP>
u"\u2003", # Em Space - <EMSP>
u"\u2004", # Tree-Per-Em
u"\u2005", # Four-Per-Em
u"\u2006", # Six-Per-Em
u"\u2007", # Figure Space
u"\u2008", # Punctuation Space - <PUNCSP>
u"\u2009", # Thin Space
u"\u200A", # Hair Space
u"\u200B", # Zero Width Space - <ZWSP>
u"\u2028", # Line Separator
u"\u2029", # Paragraph Separator
u"\u202F", # Narrow No-Break Space
u"\u205F", # Medium Mathematical Space
u"\u3000", # Ideographic Space
]

def match(self, file, line):
res = [i for i in self.irregular_spaces if i in line]
return len(res) != 0
31 changes: 31 additions & 0 deletions tests/unit/TestNoIrregularSpacesRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-

import unittest

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


LINE = '''
/tmp/testfile:
file.managed:
- content:{space}"foobar"
'''


class TestNoIrregularSpacesRule(unittest.TestCase):
collection = RulesCollection()
collection.register(NoIrregularSpacesRule())

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

def test_with_irregular_spaces(self):
for irregular in NoIrregularSpacesRule.irregular_spaces:
results = self.runner.run_state(LINE.format(space=irregular))
self.assertEqual(1, len(results))

def test_without_irregular_spaces(self):
results = self.runner.run_state(LINE.format(space=" "))
self.assertEqual(0, len(results))