Skip to content

Commit 8975275

Browse files
authored
Fix template check when using cli (#4183)
1 parent c6cb545 commit 8975275

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

src/cfnlint/runner/cli.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,7 @@ def cli(self) -> None:
294294
self.config.parser.print_help()
295295
sys.exit(1)
296296

297-
# Special case: if no templates/deployment files and stdin is a tty, show help
298-
raw_templates = self.config._get_argument_value("templates", True, False) or []
299-
raw_deployment_files = (
300-
self.config._get_argument_value("deployment_files", False, False) or []
301-
)
302-
if not raw_templates and not raw_deployment_files:
297+
if not self.config.templates and not self.config.deployment_files:
303298
if sys.stdin.isatty():
304299
self.config.parser.print_help()
305300
sys.exit(1)

test/unit/module/runner/test_cli.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,66 @@ def test_print_help(self, mock_isatty, mock_print_help):
9393

9494
self.assertEqual(e.exception.code, 1)
9595
mock_print_help.assert_called_once()
96-
mock_isatty.assert_called_once()
96+
# isatty should be called at least once
97+
self.assertTrue(mock_isatty.call_count >= 1)
9798
# self.assertEqual(mock_isatty.call_count, 2)
9899

100+
@patch("argparse.ArgumentParser.print_help")
101+
@patch("sys.stdin.isatty")
102+
def test_no_templates_no_deployment_files(self, mock_isatty, mock_print_help):
103+
"""Test that help is printed when no templates or deployment
104+
files are provided and stdin is a tty"""
105+
# Create a config with no templates or deployment files
106+
config = ConfigMixIn([])
107+
108+
# Ensure templates and deployment_files are empty or None
109+
self.assertTrue(not config.templates or config.templates == [])
110+
self.assertTrue(not config.deployment_files or config.deployment_files == [])
111+
112+
runner = Runner(config)
113+
mock_isatty.return_value = True
114+
115+
# Should exit with code 1 and print help
116+
with self.assertRaises(SystemExit) as e:
117+
runner.cli()
118+
119+
self.assertEqual(e.exception.code, 1)
120+
mock_print_help.assert_called_once()
121+
# isatty should be called at least once
122+
self.assertTrue(mock_isatty.call_count >= 1)
123+
124+
@patch("argparse.ArgumentParser.print_help")
125+
@patch("sys.stdin.isatty")
126+
@patch("cfnlint.runner.Runner._cli_output")
127+
def test_no_templates_no_deployment_files_with_stdin(
128+
self, mock_cli_output, mock_isatty, mock_print_help
129+
):
130+
"""Test that when no templates or deployment files are
131+
provided but stdin is not a tty, the program continues
132+
execution without printing help"""
133+
# Create a config with no templates or deployment files
134+
config = ConfigMixIn([])
135+
136+
# Ensure templates and deployment_files are empty or None
137+
self.assertTrue(not config.templates or config.templates == [])
138+
self.assertTrue(not config.deployment_files or config.deployment_files == [])
139+
140+
# Mock _cli_output to avoid actual processing
141+
mock_cli_output.return_value = None
142+
143+
runner = Runner(config)
144+
mock_isatty.return_value = False
145+
146+
# Should not exit and not print help
147+
runner.cli()
148+
149+
# Help should not be printed
150+
mock_print_help.assert_not_called()
151+
# isatty should be called at least once
152+
self.assertTrue(mock_isatty.call_count >= 1)
153+
# _cli_output should be called once
154+
mock_cli_output.assert_called_once()
155+
99156
def test_bad_regions(self):
100157
config = ConfigMixIn(
101158
[

0 commit comments

Comments
 (0)