Skip to content

Commit b3cec6a

Browse files
authored
Change filenames to be OS specific paths (#3170)
* Change filenames to be OS specific paths
1 parent eaf23ca commit b3cec6a

File tree

7 files changed

+84
-48
lines changed

7 files changed

+84
-48
lines changed

src/cfnlint/config.py

+8-17
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import os
1212
import sys
1313
from pathlib import Path
14-
from typing import Dict
14+
from typing import Dict, List, Sequence
1515

1616
import jsonschema
1717

@@ -692,23 +692,10 @@ def templates(self):
692692
if isinstance(filenames, str):
693693
filenames = [filenames]
694694

695-
# handle different shells and Config files
696-
# some shells don't expand * and configparser won't expand wildcards
697-
all_filenames = []
698695
ignore_templates = self._ignore_templates()
699-
for filename in filenames:
700-
add_filenames = glob.glob(filename, recursive=True)
701-
# only way to know of the glob failed is to test it
702-
# then add the filename as requested
703-
if not add_filenames:
704-
if filename not in ignore_templates:
705-
all_filenames.append(filename)
706-
else:
707-
for add_filename in add_filenames:
708-
if add_filename not in ignore_templates:
709-
all_filenames.append(add_filename)
696+
all_filenames = self._glob_filenames(filenames)
710697

711-
return sorted(all_filenames)
698+
return [i for i in all_filenames if i not in ignore_templates]
712699

713700
def _ignore_templates(self):
714701
ignore_template_args = self._get_argument_value("ignore_templates", False, True)
@@ -721,9 +708,13 @@ def _ignore_templates(self):
721708
if isinstance(filenames, str):
722709
filenames = [filenames]
723710

711+
return self._glob_filenames(filenames)
712+
713+
def _glob_filenames(self, filenames: Sequence[str]) -> List[str]:
724714
# handle different shells and Config files
725715
# some shells don't expand * and configparser won't expand wildcards
726716
all_filenames = []
717+
727718
for filename in filenames:
728719
add_filenames = glob.glob(filename, recursive=True)
729720
# only way to know of the glob failed is to test it
@@ -733,7 +724,7 @@ def _ignore_templates(self):
733724
else:
734725
all_filenames.extend(add_filenames)
735726

736-
return all_filenames
727+
return sorted(list(map(str, map(Path, all_filenames))))
737728

738729
@property
739730
def append_rules(self):

test/integration/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import subprocess
88
import sys
9+
from pathlib import Path
910
from test.testlib.testcase import BaseTestCase
1011

1112
import cfnlint.core
@@ -33,6 +34,9 @@ def run_scenarios(self, extra_params=None):
3334
with open(results_filename, encoding="utf-8") as json_data:
3435
expected_results = json.load(json_data)
3536

37+
for result in expected_results:
38+
result["Filename"] = str(Path(result.get("Filename")))
39+
3640
try:
3741
result = subprocess.check_output(
3842
["cfn-lint"] + extra_params + ["--format", "json", "--", filename]
@@ -93,6 +97,9 @@ def run_module_integration_scenarios(self, rules):
9397
with open(results_filename, encoding="utf-8") as json_data:
9498
expected_results = json.load(json_data)
9599

100+
for result in expected_results:
101+
result["Filename"] = str(Path(result.get("Filename")))
102+
96103
template = cfnlint.decode.cfn_yaml.load(filename)
97104

98105
matches = cfnlint.core.run_checks(filename, template, rules, regions)

test/integration/test_directives.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from pathlib import Path
67
from test.integration import BaseCliTestCase
78

89

@@ -11,16 +12,18 @@ class TestDirectives(BaseCliTestCase):
1112

1213
scenarios = [
1314
{
14-
"filename": "test/fixtures/templates/good/core/directives.yaml",
15+
"filename": str(Path("test/fixtures/templates/good/core/directives.yaml")),
1516
"exit_code": 0,
1617
"results": [],
1718
},
1819
{
19-
"filename": "test/fixtures/templates/bad/core/directives.yaml",
20+
"filename": str(Path("test/fixtures/templates/bad/core/directives.yaml")),
2021
"exit_code": 2,
2122
"results": [
2223
{
23-
"Filename": "test/fixtures/templates/bad/core/directives.yaml",
24+
"Filename": str(
25+
Path("test/fixtures/templates/bad/core/directives.yaml")
26+
),
2427
"Level": "Error",
2528
"Location": {
2629
"End": {"ColumnNumber": 18, "LineNumber": 17},
@@ -41,7 +44,9 @@ class TestDirectives(BaseCliTestCase):
4144
},
4245
},
4346
{
44-
"Filename": "test/fixtures/templates/bad/core/directives.yaml",
47+
"Filename": str(
48+
Path("test/fixtures/templates/bad/core/directives.yaml")
49+
),
4550
"Level": "Error",
4651
"Location": {
4752
"End": {"ColumnNumber": 13, "LineNumber": 28},
@@ -62,7 +67,9 @@ class TestDirectives(BaseCliTestCase):
6267
},
6368
},
6469
{
65-
"Filename": "test/fixtures/templates/bad/core/directives.yaml",
70+
"Filename": str(
71+
Path("test/fixtures/templates/bad/core/directives.yaml")
72+
),
6673
"Level": "Error",
6774
"Location": {
6875
"End": {"ColumnNumber": 16, "LineNumber": 32},
@@ -82,7 +89,9 @@ class TestDirectives(BaseCliTestCase):
8289
},
8390
},
8491
{
85-
"Filename": "test/fixtures/templates/bad/core/directives.yaml",
92+
"Filename": str(
93+
Path("test/fixtures/templates/bad/core/directives.yaml")
94+
),
8695
"Level": "Error",
8796
"Location": {
8897
"End": {"ColumnNumber": 13, "LineNumber": 35},
@@ -103,7 +112,9 @@ class TestDirectives(BaseCliTestCase):
103112
},
104113
},
105114
{
106-
"Filename": "test/fixtures/templates/bad/core/directives.yaml",
115+
"Filename": str(
116+
Path("test/fixtures/templates/bad/core/directives.yaml")
117+
),
107118
"Level": "Error",
108119
"Location": {
109120
"End": {"ColumnNumber": 15, "LineNumber": 37},

test/integration/test_mandatory_checks.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from pathlib import Path
67
from test.integration import BaseCliTestCase
78

89

@@ -11,11 +12,15 @@ class TestDirectives(BaseCliTestCase):
1112

1213
scenarios = [
1314
{
14-
"filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml",
15+
"filename": str(
16+
Path("test/fixtures/templates/bad/core/mandatory_checks.yaml")
17+
),
1518
"exit_code": 2,
1619
"results": [
1720
{
18-
"Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml",
21+
"Filename": str(
22+
Path("test/fixtures/templates/bad/core/mandatory_checks.yaml")
23+
),
1924
"Level": "Error",
2025
"Location": {
2126
"End": {"ColumnNumber": 18, "LineNumber": 17},
@@ -36,7 +41,9 @@ class TestDirectives(BaseCliTestCase):
3641
},
3742
},
3843
{
39-
"Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml",
44+
"Filename": str(
45+
Path("test/fixtures/templates/bad/core/mandatory_checks.yaml")
46+
),
4047
"Level": "Error",
4148
"Location": {
4249
"End": {"ColumnNumber": 13, "LineNumber": 28},
@@ -57,7 +64,9 @@ class TestDirectives(BaseCliTestCase):
5764
},
5865
},
5966
{
60-
"Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml",
67+
"Filename": str(
68+
Path("test/fixtures/templates/bad/core/mandatory_checks.yaml")
69+
),
6170
"Level": "Error",
6271
"Location": {
6372
"End": {"ColumnNumber": 16, "LineNumber": 32},
@@ -77,7 +86,9 @@ class TestDirectives(BaseCliTestCase):
7786
},
7887
},
7988
{
80-
"Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml",
89+
"Filename": str(
90+
Path("test/fixtures/templates/bad/core/mandatory_checks.yaml")
91+
),
8192
"Level": "Error",
8293
"Location": {
8394
"End": {"ColumnNumber": 13, "LineNumber": 35},
@@ -98,7 +109,9 @@ class TestDirectives(BaseCliTestCase):
98109
},
99110
},
100111
{
101-
"Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml",
112+
"Filename": str(
113+
Path("test/fixtures/templates/bad/core/mandatory_checks.yaml")
114+
),
102115
"Level": "Error",
103116
"Location": {
104117
"End": {"ColumnNumber": 15, "LineNumber": 37},
@@ -120,7 +133,9 @@ class TestDirectives(BaseCliTestCase):
120133
},
121134
},
122135
{
123-
"Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml",
136+
"Filename": str(
137+
Path("test/fixtures/templates/bad/core/mandatory_checks.yaml")
138+
),
124139
"Level": "Error",
125140
"Location": {
126141
"End": {"ColumnNumber": 18, "LineNumber": 13},
@@ -141,7 +156,9 @@ class TestDirectives(BaseCliTestCase):
141156
},
142157
},
143158
{
144-
"Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml",
159+
"Filename": str(
160+
Path("test/fixtures/templates/bad/core/mandatory_checks.yaml")
161+
),
145162
"Level": "Error",
146163
"Location": {
147164
"End": {"ColumnNumber": 16, "LineNumber": 19},

test/unit/module/config/test_config_mixin.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import logging
77
import os
8+
from pathlib import Path
89
from test.testlib.testcase import BaseTestCase
910
from unittest.mock import patch
1011

@@ -179,23 +180,36 @@ def test_config_expand_paths(self, yaml_mock):
179180
self.assertEqual(
180181
config.templates,
181182
[
182-
"test/fixtures/templates/public" + os.path.sep + "lambda-poller.yaml",
183-
"test/fixtures/templates/public" + os.path.sep + "rds-cluster.yaml",
183+
str(
184+
Path(
185+
"test/fixtures/templates/public"
186+
+ os.path.sep
187+
+ "lambda-poller.yaml"
188+
)
189+
),
190+
str(
191+
Path(
192+
"test/fixtures/templates/public"
193+
+ os.path.sep
194+
+ "rds-cluster.yaml"
195+
)
196+
),
184197
],
185198
)
186199

187200
@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
188201
def test_config_expand_paths_failure(self, yaml_mock):
189202
"""Test precedence in"""
190203

204+
filename = "test/fixtures/templates/badpath/*.yaml"
191205
yaml_mock.side_effect = [
192206
{"templates": ["test/fixtures/templates/badpath/*.yaml"]},
193207
{},
194208
]
195209
config = cfnlint.config.ConfigMixIn([])
196210

197211
# test defaults
198-
self.assertEqual(config.templates, ["test/fixtures/templates/badpath/*.yaml"])
212+
self.assertEqual(config.templates, [str(Path(filename))])
199213

200214
@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
201215
def test_config_expand_ignore_templates(self, yaml_mock):
@@ -214,7 +228,7 @@ def test_config_expand_ignore_templates(self, yaml_mock):
214228

215229
# test defaults
216230
self.assertNotIn(
217-
"test/fixtures/templates/bad/resources/iam/resource_policy.yaml",
231+
str(Path("test/fixtures/templates/bad/resources/iam/resource_policy.yaml")),
218232
config.templates,
219233
)
220234
self.assertEqual(len(config.templates), 5)

test/unit/module/core/test_run_cli.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import logging
7+
from pathlib import Path
78
from test.testlib.testcase import BaseTestCase
89
from unittest.mock import patch
910

@@ -119,7 +120,7 @@ def test_template_via_stdin(self):
119120
(_, filenames, _) = cfnlint.core.get_args_filenames(
120121
["--template", filename]
121122
)
122-
assert filenames == [filename]
123+
assert filenames == [str(Path(filename))]
123124

124125
@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
125126
def test_template_config(self, yaml_mock):
@@ -146,9 +147,7 @@ def test_template_config(self, yaml_mock):
146147
self.assertEqual(args.override_spec, None)
147148
self.assertEqual(args.custom_rules, None)
148149
self.assertEqual(args.regions, ["us-east-1"])
149-
self.assertEqual(
150-
args.templates, ["test/fixtures/templates/good/core/config_parameters.yaml"]
151-
)
150+
self.assertEqual(args.templates, [str(Path(filename))])
152151
self.assertEqual(args.update_documentation, False)
153152
self.assertEqual(args.update_specs, False)
154153
self.assertEqual(args.output_file, None)
@@ -185,9 +184,7 @@ def test_positional_template_parameters(self, yaml_mock):
185184
self.assertEqual(args.override_spec, None)
186185
self.assertEqual(args.custom_rules, None)
187186
self.assertEqual(args.regions, ["us-east-1"])
188-
self.assertEqual(
189-
args.templates, ["test/fixtures/templates/good/core/config_parameters.yaml"]
190-
)
187+
self.assertEqual(args.templates, [str(Path(filename))])
191188
self.assertEqual(args.update_documentation, False)
192189
self.assertEqual(args.update_specs, False)
193190

@@ -217,9 +214,7 @@ def test_override_parameters(self, yaml_mock):
217214
self.assertEqual(args.override_spec, None)
218215
self.assertEqual(args.custom_rules, None)
219216
self.assertEqual(args.regions, ["us-east-1"])
220-
self.assertEqual(
221-
args.templates, ["test/fixtures/templates/good/core/config_parameters.yaml"]
222-
)
217+
self.assertEqual(args.templates, [str(Path(filename))])
223218
self.assertEqual(args.update_documentation, False)
224219
self.assertEqual(args.update_specs, False)
225220

@@ -243,6 +238,6 @@ def test_bad_config(self, yaml_mock):
243238
self.assertEqual(args.override_spec, None)
244239
self.assertEqual(args.custom_rules, None)
245240
self.assertEqual(args.regions, ["us-east-1"])
246-
self.assertEqual(args.templates, [filename])
241+
self.assertEqual(args.templates, [str(Path(filename))])
247242
self.assertEqual(args.update_documentation, False)
248243
self.assertEqual(args.update_specs, False)

test/unit/module/formatters/test_formatters.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import sys
88
import xml.etree.ElementTree as ET
9+
from pathlib import Path
910
from test.testlib.testcase import BaseTestCase
1011

1112
import jsonschema
@@ -27,7 +28,7 @@ class TestFormatters(BaseTestCase):
2728
def setUp(self) -> None:
2829
super().setUp()
2930
cfnlint.core._reset_rule_cache()
30-
self.filename = "test/fixtures/templates/bad/formatters.yaml"
31+
self.filename = str(Path("test/fixtures/templates/bad/formatters.yaml"))
3132
self.results = [
3233
Match(
3334
6,

0 commit comments

Comments
 (0)