Skip to content

Commit c6e3878

Browse files
authored
Switch back to raising bad path errors (#3862)
* Go to standard posix failures
1 parent b1ddd33 commit c6e3878

File tree

8 files changed

+67
-6
lines changed

8 files changed

+67
-6
lines changed

src/cfnlint/config.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -796,10 +796,10 @@ def _glob_filenames(self, filenames: Sequence[str]) -> list[str]:
796796
for filename in filenames:
797797
add_filenames = glob.glob(filename, recursive=True)
798798

799-
if isinstance(add_filenames, list):
800-
all_filenames.extend(add_filenames)
801-
else:
802-
LOGGER.error(f"{filename} could not be processed by glob.glob")
799+
if not add_filenames and not self.ignore_bad_template:
800+
raise ValueError(f"{filename} could not be processed by glob.glob")
801+
802+
all_filenames.extend(add_filenames)
803803

804804
return sorted(list(map(str, map(Path, all_filenames))))
805805

src/cfnlint/data/CfnLintCli/config/schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
"description": "custom rule file to use",
5959
"type": "string"
6060
},
61+
"ignore_bad_template": {
62+
"description": "Ignore bad templates",
63+
"type": "boolean"
64+
},
6165
"ignore_checks": {
6266
"description": "List of checks to ignore",
6367
"items": {

src/cfnlint/rules/errors/__init__.py

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

6+
from cfnlint.rules.errors.config import ConfigError
67
from cfnlint.rules.errors.parse import ParseError
78
from cfnlint.rules.errors.rule import RuleError
89
from cfnlint.rules.errors.transform import TransformError

src/cfnlint/rules/errors/config.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
6+
from cfnlint.rules._rule import CloudFormationLintRule
7+
8+
9+
class ConfigError(CloudFormationLintRule):
10+
11+
id = "E0003"
12+
shortdesc = "Error with cfn-lint configuration"
13+
description = "Error as a result of the cfn-lint configuration"
14+
source_url = "https://github.com/aws-cloudformation/cfn-lint"
15+
tags = ["base", "rule"]

src/cfnlint/runner.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from cfnlint.decode.decode import decode
1818
from cfnlint.helpers import REGIONS
1919
from cfnlint.rules import Match, Rules
20-
from cfnlint.rules.errors import ParseError, TransformError
20+
from cfnlint.rules.errors import ConfigError, ParseError, TransformError
2121
from cfnlint.schema import PROVIDER_SCHEMA_MANAGER
2222
from cfnlint.template.template import Template
2323

@@ -223,10 +223,13 @@ def __init__(self, config: ConfigMixIn) -> None:
223223
settings for the template scan.
224224
"""
225225
self.config = config
226-
self.config.templates
227226
self.formatter = get_formatter(self.config)
228227
self.rules: Rules = Rules()
229228
self._get_rules()
229+
try:
230+
self.config.templates
231+
except ValueError as e:
232+
self._cli_output([Match(str(e), ConfigError(), None)])
230233
# load registry schemas before patching
231234
if self.config.registry_schemas:
232235
for path in self.config.registry_schemas:

test/unit/module/config/test_config_mixin.py

+14
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ def test_config_expand_paths_nomatch(self, yaml_mock):
208208
]
209209
config = cfnlint.config.ConfigMixIn([])
210210

211+
with self.assertRaises(ValueError):
212+
self.assertEqual(len(config.templates), 1)
213+
214+
@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
215+
def test_config_expand_paths_nomatch_ignore_bad_template(self, yaml_mock):
216+
"""Test precedence in"""
217+
218+
filename = "test/fixtures/templates/nonexistant/*.yaml"
219+
yaml_mock.side_effect = [
220+
{"templates": [filename], "ignore_bad_template": True},
221+
{},
222+
]
223+
config = cfnlint.config.ConfigMixIn([])
224+
211225
# test defaults
212226
self.assertEqual(config.templates, [])
213227

test/unit/module/maintenance/test_update_documentation.py

+8
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ def test_update_docs(self):
7777
" [Source](https://github.com/aws-cloudformation/cfn-lint) |"
7878
" `base`,`rule` |\n"
7979
),
80+
call(
81+
'| [E0003<a name="E0003"></a>]'
82+
"(../src/cfnlint/rules/errors/config.py) | "
83+
"Error with cfn-lint configuration | "
84+
"Error as a result of the cfn-lint configuration | | "
85+
"[Source](https://github.com/aws-cloudformation/cfn-lint) "
86+
"| `base`,`rule` |\n"
87+
),
8088
call("\n\\* experimental rules\n"),
8189
]
8290
mock_builtin_open.return_value.write.assert_has_calls(expected_calls)

test/unit/module/runner/test_runner.py

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

6+
from io import StringIO
67
from unittest.mock import patch
78

89
import pytest
@@ -93,3 +94,18 @@ def test_init_schemas(name, registry_path, patch_path, expected):
9394

9495
PROVIDER_SCHEMA_MANAGER._registry_schemas = {}
9596
PROVIDER_SCHEMA_MANAGER.reset()
97+
98+
99+
def test_no_templates():
100+
params = ["--template", "does-not-exist.yaml"]
101+
102+
config = ConfigMixIn(params)
103+
with patch("sys.exit") as exit:
104+
with patch("sys.stdout", new=StringIO()) as out:
105+
exit.assert_not_called()
106+
Runner(config)
107+
assert out.getvalue().strip() == (
108+
"E0003 does-not-exist.yaml could not "
109+
"be processed by glob.glob\nNone:1:1"
110+
)
111+
exit.assert_called_once_with(2)

0 commit comments

Comments
 (0)