Skip to content

Commit 381aa50

Browse files
authored
fix: ignore non file mode lines (#274)
Ignore non file mode lines in rule 207 and 208. Closes #255 Signed-off-by: Roald Nefs <info@roaldnefs.com>
1 parent a233236 commit 381aa50

File tree

6 files changed

+74
-13
lines changed

6 files changed

+74
-13
lines changed

.pylintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ disable=
1616
duplicate-code,
1717
dangerous-default-value,
1818
import-error,
19+
fixme,

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
77
### Fixed
88
- False positive when detecting missing spaces in Jinja variables when the Jinja statement is nested in literal braces ([#272](https://github.com/warpnet/salt-lint/pull/272)).
99
- Ensure a single missing quote in the file mode is also detected as incorrect quotation of a file mode ([#273](https://github.com/warpnet/salt-lint/pull/273)).
10+
- Ignore non file mode arguments for the file mode quotation and leading zero checks ([#274](https://github.com/warpnet/salt-lint/pull/274)).
1011

1112
## [0.7.0] (2021-11-01)
1213
### Added

saltlint/rules/FileModeLeadingZeroRule.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
# Copyright (c) 2016 Will Thames and contributors
33
# Copyright (c) 2018 Ansible Project
4-
# Modified work Copyright (c) 2020 Warpnet B.V.
4+
# Modified work Copyright (c) 2020-2021 Warpnet B.V.
55

66
import re
77
from saltlint.linter.rule import Rule
@@ -17,7 +17,19 @@ class FileModeLeadingZeroRule(Rule):
1717
tags = ['formatting']
1818
version_added = 'v0.0.3'
1919

20-
regex = re.compile(r"^\s+- ((dir_)|(file_))?mode: ((')|(\"))?[0-9]{3}([\D]|$)")
20+
regex = re.compile(
21+
r"""^\s+ # line starting with whitespace
22+
-\ # whitespace escaped due to re.VERBOSE
23+
(?:dir_|file_)?mode # file_mode, dir_mode or mode
24+
:\ # whitespace escaped due to re.VERBOSE
25+
['"]? # optional quotation character
26+
([0-9]{3}) # three number digit
27+
(?:['"\s]|$) # followed by whitespace, quoation or EOL
28+
""",
29+
re.VERBOSE
30+
)
2131

2232
def match(self, file, line):
33+
# TODO: when salt-lint becomes state aware it should ignore the mode
34+
# argument in the network.managed state.
2335
return self.regex.search(line)

saltlint/rules/FileModeQuotationRule.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,23 @@ class FileModeQuotationRule(Rule):
2424
:\ # whitespace escaped duo to re.VERBOSE
2525
(?:
2626
(\d{3,4}) # mode without quotation
27+
(?:
28+
['"] # followed by a quotation character
29+
|
30+
\s # followed by a whitespace
31+
|
32+
$ # followed by EOL
33+
)
2734
|
2835
(['"]\d{3,4} # mode prefixed with quotation
29-
(?:
30-
$ # end of line
31-
| # or
32-
[^\d'"] # ending quotation missing
33-
)
36+
(?:[^\d'"]|$) # not followed by digit, quoation or EOL
3437
)
3538
)
3639
""",
3740
re.VERBOSE
3841
)
3942

4043
def match(self, file, line):
44+
# TODO: when salt-lint becomes state aware it should ignore the mode
45+
# argument in the network.managed state.
4146
return self.regex.search(line)

tests/unit/TestFileModeLeadingZeroRule.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
# Copyright (c) 2013-2018 Will Thames <will@thames.id.au>
33
# Copyright (c) 2018 Ansible by Red Hat
4-
# Modified work Copyright (c) 2020 Warpnet B.V.
4+
# Modified work Copyright (c) 2020-2021 Warpnet B.V.
55

66
import unittest
77

@@ -25,23 +25,44 @@
2525
- name: /tmp/badfile
2626
- user: root
2727
- group: root
28-
- mode: 700
28+
- mode: 600
2929
- file_mode: '660'
3030
- dir_mode: '775'
3131
'''
3232

33+
NETWORK_MANAGED_MODE = '''
34+
bond0:
35+
network.managed:
36+
- type: bond
37+
- mode: 802.3ad
38+
- proto: static
39+
- ipaddr: 10.1.1.77
40+
- netmask: 255.255.255.0
41+
- gateway: 10.1.1.1
42+
- dns: 10.1.1.1
43+
'''
44+
45+
3346
class TestFileModeLeadingZeroRule(unittest.TestCase):
3447
collection = RulesCollection()
3548

3649
def setUp(self):
3750
self.collection.register(FileModeLeadingZeroRule())
51+
self.runner = RunFromText(self.collection)
3852

3953
def test_statement_positive(self):
40-
runner = RunFromText(self.collection)
41-
results = runner.run_state(GOOD_MODE_LEADING_ZERO_LINE)
54+
results = self.runner.run_state(GOOD_MODE_LEADING_ZERO_LINE)
4255
self.assertEqual(0, len(results))
4356

4457
def test_statement_negative(self):
45-
runner = RunFromText(self.collection)
46-
results = runner.run_state(BAD_MODE_LEADING_ZERO_LINE)
58+
results = self.runner.run_state(BAD_MODE_LEADING_ZERO_LINE)
4759
self.assertEqual(3, len(results))
60+
61+
def test_network_mode(self):
62+
"""
63+
Ensure the mode argument in the network.managed state gets ignored. See
64+
related GitHub issue:
65+
https://github.com/warpnet/salt-lint/issues/255
66+
"""
67+
results = self.runner.run_state(NETWORK_MANAGED_MODE)
68+
self.assertEqual(0, len(results))

tests/unit/TestFileModeQuotationRule.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@
4141
- dir_mode: 0775"
4242
'''
4343

44+
NETWORK_MANAGED_MODE = '''
45+
bond0:
46+
network.managed:
47+
- type: bond
48+
- mode: 802.3ad
49+
- proto: static
50+
- ipaddr: 10.1.1.77
51+
- netmask: 255.255.255.0
52+
- gateway: 10.1.1.1
53+
- dns: 10.1.1.1
54+
'''
55+
4456

4557
class TestModeQuotationRule(unittest.TestCase):
4658
collection = RulesCollection()
@@ -60,3 +72,12 @@ def test_statement_negative(self):
6072
def test_missing_quotes(self):
6173
results = self.runner.run_state(MODE_MISSING_QUOTATION_LINE)
6274
self.assertEqual(3, len(results))
75+
76+
def test_network_mode(self):
77+
"""
78+
Ensure the mode argument in the network.managed state gets ignored. See
79+
related GitHub issue:
80+
https://github.com/warpnet/salt-lint/issues/255
81+
"""
82+
results = self.runner.run_state(NETWORK_MANAGED_MODE)
83+
self.assertEqual(0, len(results))

0 commit comments

Comments
 (0)