Skip to content

Commit a23bb00

Browse files
authored
Stdin overrides .cfnlintrc when determining templates (#4138)
* Stdin overrides .cfnlintrc when determining templates
1 parent f36d646 commit a23bb00

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

src/cfnlint/config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,12 @@ def templates(self):
775775

776776
if cli_alt_args:
777777
filenames = cli_alt_args
778-
elif file_args:
779-
filenames = file_args
780778
elif cli_args:
781779
filenames = cli_args
780+
elif not sys.stdin.isatty():
781+
return []
782+
elif file_args:
783+
filenames = file_args
782784
else:
783785
# No filenames found, could be piped in or be using the api.
784786
return None

test/unit/module/config/test_config_mixin.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ def test_config_force_with_update_specs(self, yaml_mock):
167167
self.assertEqual(config.update_specs, True)
168168

169169
@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
170-
def test_config_expand_paths(self, yaml_mock):
170+
@patch("sys.stdin.isatty", return_va=True)
171+
def test_config_expand_paths(self, isatty_mock, yaml_mock):
171172
"""Test precedence in"""
172-
173+
isatty_mock.return_value = True
173174
yaml_mock.side_effect = [
174175
{"templates": ["test/fixtures/templates/public/*.yaml"]},
175176
{},
@@ -198,10 +199,12 @@ def test_config_expand_paths(self, yaml_mock):
198199
)
199200

200201
@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
201-
def test_config_expand_paths_nomatch(self, yaml_mock):
202+
@patch("sys.stdin.isatty", return_va=True)
203+
def test_config_expand_paths_nomatch(self, isatty_mock, yaml_mock):
202204
"""Test precedence in"""
203205

204206
filename = "test/fixtures/templates/nonexistant/*.yaml"
207+
isatty_mock.return_value = True
205208
yaml_mock.side_effect = [
206209
{"templates": [filename]},
207210
{},
@@ -212,10 +215,14 @@ def test_config_expand_paths_nomatch(self, yaml_mock):
212215
self.assertEqual(len(config.templates), 1)
213216

214217
@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
215-
def test_config_expand_paths_nomatch_ignore_bad_template(self, yaml_mock):
218+
@patch("sys.stdin.isatty", return_va=True)
219+
def test_config_expand_paths_nomatch_ignore_bad_template(
220+
self, isatty_mock, yaml_mock
221+
):
216222
"""Test precedence in"""
217223

218224
filename = "test/fixtures/templates/nonexistant/*.yaml"
225+
isatty_mock.return_value = True
219226
yaml_mock.side_effect = [
220227
{"templates": [filename], "ignore_bad_template": True},
221228
{},
@@ -226,9 +233,10 @@ def test_config_expand_paths_nomatch_ignore_bad_template(self, yaml_mock):
226233
self.assertEqual(config.templates, [])
227234

228235
@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
229-
def test_config_expand_ignore_templates(self, yaml_mock):
236+
@patch("sys.stdin.isatty", return_va=True)
237+
def test_config_expand_ignore_templates(self, isatty_mock, yaml_mock):
230238
"""Test ignore templates"""
231-
239+
isatty_mock.return_value = True
232240
yaml_mock.side_effect = [
233241
{
234242
"templates": ["test/fixtures/templates/bad/resources/iam/*.yaml"],
@@ -248,9 +256,13 @@ def test_config_expand_ignore_templates(self, yaml_mock):
248256
self.assertEqual(len(config.templates), 3)
249257

250258
@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
251-
def test_config_expand_and_ignore_templates_with_bad_path(self, yaml_mock):
259+
@patch("sys.stdin.isatty", return_va=True)
260+
def test_config_expand_and_ignore_templates_with_bad_path(
261+
self, isatty_mock, yaml_mock
262+
):
252263
"""Test ignore templates"""
253264

265+
isatty_mock.return_value = True
254266
yaml_mock.side_effect = [
255267
{
256268
"templates": ["test/fixtures/templates/bad/resources/iam/*.yaml"],
@@ -270,9 +282,11 @@ def test_config_expand_and_ignore_templates_with_bad_path(self, yaml_mock):
270282
self.assertEqual(len(config.templates), 4)
271283

272284
@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
273-
def test_config_merge(self, yaml_mock):
285+
@patch("sys.stdin.isatty", return_va=True)
286+
def test_config_merge(self, isatty_mock, yaml_mock):
274287
"""Test merging lists"""
275288

289+
isatty_mock.return_value = True
276290
yaml_mock.side_effect = [
277291
{
278292
"include_checks": ["I"],

test/unit/module/formatters/test_formatters.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
from pathlib import Path
99
from test.testlib.testcase import BaseTestCase
10+
from unittest.mock import patch
1011

1112
import defusedxml.ElementTree as ET
1213

@@ -214,8 +215,10 @@ def test_pretty_formatter(self):
214215
),
215216
)
216217

217-
def test_pretty_formatter_pipe(self):
218+
@patch("sys.stdin.isatty", return_va=True)
219+
def test_pretty_formatter_pipe(self, isatty_mock):
218220
"""Test pretty formatter"""
221+
isatty_mock.return_value = True
219222
formatter = PrettyFormatter()
220223
self.config.cli_args.templates = None
221224
results = formatter.print_matches(

test/unit/module/runner/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_print_help(self, mock_isatty, mock_print_help):
7979

8080
self.assertEqual(e.exception.code, 1)
8181
mock_print_help.assert_called_once()
82-
mock_isatty.assert_called_once()
82+
self.assertEqual(mock_isatty.call_count, 2)
8383

8484
def test_bad_regions(self):
8585
config = ConfigMixIn(

0 commit comments

Comments
 (0)