Skip to content

Commit 8f4a89e

Browse files
committed
Improve testing for param files
1 parent 98cd0bf commit 8f4a89e

File tree

5 files changed

+221
-6
lines changed

5 files changed

+221
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
6+
from __future__ import annotations
7+
8+
from cfnlint.rules.jsonschema.Base import BaseJsonSchema
9+
10+
11+
class Configuration(BaseJsonSchema):
12+
13+
id = "E0200"
14+
shortdesc = "Validate parameter file configuration"
15+
description = (
16+
"Validate if a parameter file has the correct syntax "
17+
"for one of the supported formats"
18+
)
19+
source_url = "https://github.com/aws-cloudformation/cfn-lint"
20+
tags = ["base"]

src/cfnlint/rules/parameter_files/__init__.py

Whitespace-only changes.

src/cfnlint/runner/parameter_file/runner.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from cfnlint.context.parameters import ParameterSet
1313
from cfnlint.decode.decode import decode
1414
from cfnlint.rules import Match
15-
from cfnlint.rules.deployment_files.Configuration import Configuration
15+
from cfnlint.rules.parameter_files.Configuration import Configuration
1616

1717
LOGGER = logging.getLogger(__name__)
1818

@@ -72,8 +72,9 @@ def expand_parameter_files(
7272

7373
parameters.append(ParameterSet(parameter_file, parameter_data))
7474

75-
parameters_config = config.evolve(
76-
parameters=parameters,
77-
deployment_files=[],
78-
)
79-
yield parameters_config, []
75+
if parameters:
76+
parameters_config = config.evolve(
77+
parameters=parameters,
78+
deployment_files=[],
79+
)
80+
yield parameters_config, []

test/unit/module/runner/parameter_file/__init__.py

Whitespace-only changes.
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
6+
from unittest.mock import MagicMock, PropertyMock, patch
7+
8+
import pytest
9+
10+
from cfnlint.config import ConfigMixIn
11+
from cfnlint.context import ParameterSet
12+
from cfnlint.rules import Match
13+
from cfnlint.rules.parameter_files.Configuration import Configuration
14+
from cfnlint.runner.parameter_file import expand_parameter_files
15+
16+
_filename_dev = "parameters-dev.json"
17+
_filename_prod = "parameters-prod.json"
18+
19+
20+
def mock_glob(value, recursive):
21+
return [value]
22+
23+
24+
@pytest.mark.parametrize(
25+
("name,parameter_files,expected"),
26+
[
27+
(
28+
"A standard parameter file",
29+
{
30+
_filename_dev: (
31+
[
32+
{
33+
"ParameterKey": "Foo",
34+
"ParameterValue": "Bar",
35+
}
36+
],
37+
[],
38+
),
39+
},
40+
[
41+
(
42+
ConfigMixIn(
43+
[],
44+
parameter_files=[_filename_dev],
45+
parameters=[
46+
ParameterSet(
47+
source=_filename_dev, parameters={"Foo": "Bar"}
48+
)
49+
],
50+
),
51+
[],
52+
)
53+
],
54+
),
55+
(
56+
"Multiple standard parameter files",
57+
{
58+
_filename_dev: (
59+
[
60+
{
61+
"ParameterKey": "Foo",
62+
"ParameterValue": "Bar",
63+
}
64+
],
65+
[],
66+
),
67+
_filename_prod: (
68+
[
69+
{
70+
"ParameterKey": "One",
71+
"ParameterValue": "Two",
72+
}
73+
],
74+
[],
75+
),
76+
},
77+
[
78+
(
79+
ConfigMixIn(
80+
[],
81+
parameter_files=[_filename_dev, _filename_prod],
82+
parameters=[
83+
ParameterSet(
84+
source=_filename_dev, parameters={"Foo": "Bar"}
85+
),
86+
ParameterSet(
87+
source=_filename_prod, parameters={"One": "Two"}
88+
),
89+
],
90+
),
91+
[],
92+
),
93+
],
94+
),
95+
(
96+
"Bad decode",
97+
{
98+
_filename_dev: (
99+
None,
100+
[
101+
Match(
102+
linenumber=1,
103+
columnnumber=1,
104+
linenumberend=1,
105+
columnnumberend=1,
106+
filename=_filename_dev,
107+
message=(
108+
"Parameter file 'parameters-dev.json' "
109+
"is not supported"
110+
),
111+
rule=Configuration(),
112+
)
113+
],
114+
),
115+
},
116+
[
117+
(
118+
None,
119+
[
120+
Match(
121+
linenumber=1,
122+
columnnumber=1,
123+
linenumberend=1,
124+
columnnumberend=1,
125+
filename=_filename_dev,
126+
message=(
127+
"Parameter file 'parameters-dev.json' "
128+
"is not supported"
129+
),
130+
rule=Configuration(),
131+
),
132+
],
133+
),
134+
],
135+
),
136+
(
137+
"Bad structure",
138+
{
139+
_filename_dev: (
140+
[
141+
{
142+
"Key": "Foo",
143+
"Value": "Bar",
144+
}
145+
],
146+
[],
147+
),
148+
},
149+
[
150+
(
151+
None,
152+
[
153+
Match(
154+
linenumber=1,
155+
columnnumber=1,
156+
linenumberend=1,
157+
columnnumberend=1,
158+
filename=_filename_dev,
159+
message=(
160+
"Parameter file 'parameters-dev.json' "
161+
"is not supported"
162+
),
163+
rule=Configuration(),
164+
),
165+
],
166+
),
167+
],
168+
),
169+
],
170+
)
171+
def test_runner(
172+
name,
173+
parameter_files,
174+
expected,
175+
):
176+
177+
decode_results = [v for _, v in parameter_files.items()]
178+
parameter_files = [k for k, _ in parameter_files.items()]
179+
with patch("glob.glob", MagicMock(side_effect=mock_glob)):
180+
181+
with patch(
182+
"cfnlint.config.ConfigMixIn.parameter_files", new_callable=PropertyMock
183+
) as mock_parameter_files:
184+
with patch("cfnlint.runner.parameter_file.runner.decode") as mock_decode:
185+
mock_parameter_files.return_value = parameter_files
186+
mock_decode.side_effect = decode_results
187+
config = ConfigMixIn([], parameter_files=parameter_files)
188+
189+
configs = list(expand_parameter_files(config))
190+
191+
for parameter_file in parameter_files:
192+
mock_decode.assert_any_call(parameter_file)
193+
194+
assert configs == expected, f"{name}: {configs} != {expected}"

0 commit comments

Comments
 (0)