|
5 | 5 |
|
6 | 6 | from __future__ import annotations
|
7 | 7 |
|
| 8 | +import os |
| 9 | +import tempfile |
| 10 | +from pathlib import Path |
8 | 11 | from unittest import TestCase
|
9 | 12 |
|
10 |
| -from cfnlint import lint, lint_all |
| 13 | +from cfnlint import lint, lint_all, lint_file |
11 | 14 | from cfnlint.config import ManualArgs
|
12 | 15 | from cfnlint.core import get_rules
|
13 | 16 | from cfnlint.helpers import REGIONS
|
@@ -167,3 +170,113 @@ def test_sam_template(self):
|
167 | 170 | filename = "test/fixtures/templates/good/transform/list_transform_many.yaml"
|
168 | 171 | matches = self.helper_lint_string_from_file(filename)
|
169 | 172 | self.assertEqual([], matches, f"Got matches: {matches!r}")
|
| 173 | + |
| 174 | + |
| 175 | +class TestLintFile(TestCase): |
| 176 | + """Test the lint_file API function""" |
| 177 | + |
| 178 | + def test_nonexistent_file(self): |
| 179 | + """Test linting a file that doesn't exist""" |
| 180 | + matches = lint_file(Path("nonexistent_file.yaml")) |
| 181 | + self.assertEqual(1, len(matches)) |
| 182 | + self.assertEqual("E0000", matches[0].rule.id) |
| 183 | + self.assertIn("Template file not found", matches[0].message) |
| 184 | + |
| 185 | + def test_noecho_yaml_template(self): |
| 186 | + """Test linting a template with NoEcho issues""" |
| 187 | + filename = Path("test/fixtures/templates/bad/noecho.yaml") |
| 188 | + matches = lint_file( |
| 189 | + filename, |
| 190 | + config=ManualArgs(regions=["us-east-1", "us-west-2", "eu-west-1"]), |
| 191 | + ) |
| 192 | + self.assertEqual( |
| 193 | + ["W2010", "W2010"], |
| 194 | + [match.rule.id for match in matches], |
| 195 | + f"Got matches: {matches!r}", |
| 196 | + ) |
| 197 | + |
| 198 | + def test_noecho_yaml_template_warnings_ignored(self): |
| 199 | + """Test linting with warnings ignored""" |
| 200 | + filename = Path("test/fixtures/templates/bad/noecho.yaml") |
| 201 | + matches = lint_file( |
| 202 | + filename, |
| 203 | + config=ManualArgs( |
| 204 | + ignore_checks=["W", "I"], |
| 205 | + ), |
| 206 | + ) |
| 207 | + self.assertListEqual([], matches, f"Got matches: {matches!r}") |
| 208 | + |
| 209 | + def test_duplicate_json_template(self): |
| 210 | + """Test linting a template with duplicate keys""" |
| 211 | + filename = Path("test/fixtures/templates/bad/duplicate.json") |
| 212 | + matches = lint_file( |
| 213 | + filename, |
| 214 | + config=ManualArgs( |
| 215 | + regions=["us-east-1", "us-west-2", "eu-west-1"], |
| 216 | + ), |
| 217 | + ) |
| 218 | + self.assertEqual( |
| 219 | + ["E0000", "E0000", "E0000"], |
| 220 | + [match.rule.id for match in matches], |
| 221 | + f"Got matches: {matches!r}", |
| 222 | + ) |
| 223 | + |
| 224 | + def test_invalid_yaml_template(self): |
| 225 | + """Test linting an invalid YAML template""" |
| 226 | + filename = Path("test/fixtures/templates/bad/core/config_invalid_yaml.yaml") |
| 227 | + matches = lint_file( |
| 228 | + filename, |
| 229 | + config=ManualArgs(regions=["us-east-1", "us-west-2", "eu-west-1"]), |
| 230 | + ) |
| 231 | + self.assertEqual( |
| 232 | + ["E0000"], [match.rule.id for match in matches], f"Got matches: {matches!r}" |
| 233 | + ) |
| 234 | + |
| 235 | + def test_invalid_json_template(self): |
| 236 | + """Test linting an invalid JSON template""" |
| 237 | + filename = Path("test/fixtures/templates/bad/core/config_invalid_json.json") |
| 238 | + matches = lint_file( |
| 239 | + filename, |
| 240 | + config=ManualArgs(regions=["us-east-1", "us-west-2", "eu-west-1"]), |
| 241 | + ) |
| 242 | + self.assertEqual( |
| 243 | + ["E0000"], [match.rule.id for match in matches], f"Got matches: {matches!r}" |
| 244 | + ) |
| 245 | + |
| 246 | + def test_issues_template(self): |
| 247 | + """Test linting a template with issues""" |
| 248 | + filename = Path("test/fixtures/templates/bad/issues.yaml") |
| 249 | + matches = lint_file( |
| 250 | + filename, |
| 251 | + config=ManualArgs(regions=["us-east-1", "us-west-2", "eu-west-1"]), |
| 252 | + ) |
| 253 | + self.assertEqual( |
| 254 | + ["E1020"], [match.rule.id for match in matches], f"Got matches: {matches!r}" |
| 255 | + ) |
| 256 | + |
| 257 | + def test_sam_template(self): |
| 258 | + """Test linting a SAM template""" |
| 259 | + filename = Path( |
| 260 | + "test/fixtures/templates/good/transform/list_transform_many.yaml" |
| 261 | + ) |
| 262 | + matches = lint_file(filename) |
| 263 | + self.assertEqual([], matches, f"Got matches: {matches!r}") |
| 264 | + |
| 265 | + def test_empty_file(self): |
| 266 | + """Test linting an empty file""" |
| 267 | + with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
| 268 | + temp_file.write(b"") |
| 269 | + temp_file_path = Path(temp_file.name) |
| 270 | + |
| 271 | + try: |
| 272 | + matches = lint_file(temp_file_path) |
| 273 | + self.assertEqual(1, len(matches)) |
| 274 | + self.assertEqual("E1001", matches[0].rule.id) |
| 275 | + finally: |
| 276 | + os.unlink(temp_file_path) |
| 277 | + |
| 278 | + def test_good_template(self): |
| 279 | + """Test linting a good template""" |
| 280 | + filename = Path("test/fixtures/templates/good/generic.yaml") |
| 281 | + matches = lint_file(filename) |
| 282 | + self.assertEqual([], matches, f"Got matches: {matches!r}") |
0 commit comments