From 0d7a4e77005c0bd6dfdcc901e6dceac9a08e6905 Mon Sep 17 00:00:00 2001 From: Kevin DeJong Date: Thu, 25 Apr 2024 12:02:23 -0700 Subject: [PATCH] Cleanup testing --- test/integration/__init__.py | 7 ++++ test/integration/test_directives.py | 25 ++++++++++---- test/integration/test_mandatory_checks.py | 33 ++++++++++++++----- test/unit/module/config/test_config_mixin.py | 6 ++-- test/unit/module/core/test_run_cli.py | 17 ++++------ .../unit/module/formatters/test_formatters.py | 3 +- 6 files changed, 62 insertions(+), 29 deletions(-) diff --git a/test/integration/__init__.py b/test/integration/__init__.py index 819d42a4ff..f6b303eb4f 100644 --- a/test/integration/__init__.py +++ b/test/integration/__init__.py @@ -6,6 +6,7 @@ import json import subprocess import sys +from pathlib import Path from test.testlib.testcase import BaseTestCase import cfnlint.core @@ -33,6 +34,9 @@ def run_scenarios(self, extra_params=None): with open(results_filename, encoding="utf-8") as json_data: expected_results = json.load(json_data) + for result in expected_results: + result["Filename"] = str(Path(result.get("Filename"))) + try: result = subprocess.check_output( ["cfn-lint"] + extra_params + ["--format", "json", "--", filename] @@ -93,6 +97,9 @@ def run_module_integration_scenarios(self, rules): with open(results_filename, encoding="utf-8") as json_data: expected_results = json.load(json_data) + for result in expected_results: + result["Filename"] = str(Path(result.get("Filename"))) + template = cfnlint.decode.cfn_yaml.load(filename) matches = cfnlint.core.run_checks(filename, template, rules, regions) diff --git a/test/integration/test_directives.py b/test/integration/test_directives.py index 4ba45b7fed..519a985b1f 100644 --- a/test/integration/test_directives.py +++ b/test/integration/test_directives.py @@ -3,6 +3,7 @@ SPDX-License-Identifier: MIT-0 """ +from pathlib import Path from test.integration import BaseCliTestCase @@ -11,16 +12,18 @@ class TestDirectives(BaseCliTestCase): scenarios = [ { - "filename": "test/fixtures/templates/good/core/directives.yaml", + "filename": str(Path("test/fixtures/templates/good/core/directives.yaml")), "exit_code": 0, "results": [], }, { - "filename": "test/fixtures/templates/bad/core/directives.yaml", + "filename": str(Path("test/fixtures/templates/bad/core/directives.yaml")), "exit_code": 2, "results": [ { - "Filename": "test/fixtures/templates/bad/core/directives.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/directives.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 18, "LineNumber": 17}, @@ -41,7 +44,9 @@ class TestDirectives(BaseCliTestCase): }, }, { - "Filename": "test/fixtures/templates/bad/core/directives.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/directives.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 13, "LineNumber": 28}, @@ -62,7 +67,9 @@ class TestDirectives(BaseCliTestCase): }, }, { - "Filename": "test/fixtures/templates/bad/core/directives.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/directives.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 16, "LineNumber": 32}, @@ -82,7 +89,9 @@ class TestDirectives(BaseCliTestCase): }, }, { - "Filename": "test/fixtures/templates/bad/core/directives.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/directives.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 13, "LineNumber": 35}, @@ -103,7 +112,9 @@ class TestDirectives(BaseCliTestCase): }, }, { - "Filename": "test/fixtures/templates/bad/core/directives.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/directives.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 15, "LineNumber": 37}, diff --git a/test/integration/test_mandatory_checks.py b/test/integration/test_mandatory_checks.py index 1a1371cc9a..4abd928e35 100644 --- a/test/integration/test_mandatory_checks.py +++ b/test/integration/test_mandatory_checks.py @@ -3,6 +3,7 @@ SPDX-License-Identifier: MIT-0 """ +from pathlib import Path from test.integration import BaseCliTestCase @@ -11,11 +12,15 @@ class TestDirectives(BaseCliTestCase): scenarios = [ { - "filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml", + "filename": str( + Path("test/fixtures/templates/bad/core/mandatory_checks.yaml") + ), "exit_code": 2, "results": [ { - "Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/mandatory_checks.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 18, "LineNumber": 17}, @@ -36,7 +41,9 @@ class TestDirectives(BaseCliTestCase): }, }, { - "Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/mandatory_checks.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 13, "LineNumber": 28}, @@ -57,7 +64,9 @@ class TestDirectives(BaseCliTestCase): }, }, { - "Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/mandatory_checks.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 16, "LineNumber": 32}, @@ -77,7 +86,9 @@ class TestDirectives(BaseCliTestCase): }, }, { - "Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/mandatory_checks.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 13, "LineNumber": 35}, @@ -98,7 +109,9 @@ class TestDirectives(BaseCliTestCase): }, }, { - "Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/mandatory_checks.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 15, "LineNumber": 37}, @@ -120,7 +133,9 @@ class TestDirectives(BaseCliTestCase): }, }, { - "Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/mandatory_checks.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 18, "LineNumber": 13}, @@ -141,7 +156,9 @@ class TestDirectives(BaseCliTestCase): }, }, { - "Filename": "test/fixtures/templates/bad/core/mandatory_checks.yaml", + "Filename": str( + Path("test/fixtures/templates/bad/core/mandatory_checks.yaml") + ), "Level": "Error", "Location": { "End": {"ColumnNumber": 16, "LineNumber": 19}, diff --git a/test/unit/module/config/test_config_mixin.py b/test/unit/module/config/test_config_mixin.py index 23521fcb10..abf6e33585 100644 --- a/test/unit/module/config/test_config_mixin.py +++ b/test/unit/module/config/test_config_mixin.py @@ -5,6 +5,7 @@ import logging import os +from pathlib import Path from test.testlib.testcase import BaseTestCase from unittest.mock import patch @@ -188,6 +189,7 @@ def test_config_expand_paths(self, yaml_mock): def test_config_expand_paths_failure(self, yaml_mock): """Test precedence in""" + filename = "test/fixtures/templates/badpath/*.yaml" yaml_mock.side_effect = [ {"templates": ["test/fixtures/templates/badpath/*.yaml"]}, {}, @@ -195,7 +197,7 @@ def test_config_expand_paths_failure(self, yaml_mock): config = cfnlint.config.ConfigMixIn([]) # test defaults - self.assertEqual(config.templates, ["test/fixtures/templates/badpath/*.yaml"]) + self.assertEqual(config.templates, [str(Path(filename))]) @patch("cfnlint.config.ConfigFileArgs._read_config", create=True) def test_config_expand_ignore_templates(self, yaml_mock): @@ -214,7 +216,7 @@ def test_config_expand_ignore_templates(self, yaml_mock): # test defaults self.assertNotIn( - "test/fixtures/templates/bad/resources/iam/resource_policy.yaml", + str(Path("test/fixtures/templates/bad/resources/iam/resource_policy.yaml")), config.templates, ) self.assertEqual(len(config.templates), 5) diff --git a/test/unit/module/core/test_run_cli.py b/test/unit/module/core/test_run_cli.py index 8c8db52eee..473404be30 100644 --- a/test/unit/module/core/test_run_cli.py +++ b/test/unit/module/core/test_run_cli.py @@ -4,6 +4,7 @@ """ import logging +from pathlib import Path from test.testlib.testcase import BaseTestCase from unittest.mock import patch @@ -119,7 +120,7 @@ def test_template_via_stdin(self): (_, filenames, _) = cfnlint.core.get_args_filenames( ["--template", filename] ) - assert filenames == [filename] + assert filenames == [str(Path(filename))] @patch("cfnlint.config.ConfigFileArgs._read_config", create=True) def test_template_config(self, yaml_mock): @@ -146,9 +147,7 @@ def test_template_config(self, yaml_mock): self.assertEqual(args.override_spec, None) self.assertEqual(args.custom_rules, None) self.assertEqual(args.regions, ["us-east-1"]) - self.assertEqual( - args.templates, ["test/fixtures/templates/good/core/config_parameters.yaml"] - ) + self.assertEqual(args.templates, [str(Path(filename))]) self.assertEqual(args.update_documentation, False) self.assertEqual(args.update_specs, False) self.assertEqual(args.output_file, None) @@ -185,9 +184,7 @@ def test_positional_template_parameters(self, yaml_mock): self.assertEqual(args.override_spec, None) self.assertEqual(args.custom_rules, None) self.assertEqual(args.regions, ["us-east-1"]) - self.assertEqual( - args.templates, ["test/fixtures/templates/good/core/config_parameters.yaml"] - ) + self.assertEqual(args.templates, [str(Path(filename))]) self.assertEqual(args.update_documentation, False) self.assertEqual(args.update_specs, False) @@ -217,9 +214,7 @@ def test_override_parameters(self, yaml_mock): self.assertEqual(args.override_spec, None) self.assertEqual(args.custom_rules, None) self.assertEqual(args.regions, ["us-east-1"]) - self.assertEqual( - args.templates, ["test/fixtures/templates/good/core/config_parameters.yaml"] - ) + self.assertEqual(args.templates, [str(Path(filename))]) self.assertEqual(args.update_documentation, False) self.assertEqual(args.update_specs, False) @@ -243,6 +238,6 @@ def test_bad_config(self, yaml_mock): self.assertEqual(args.override_spec, None) self.assertEqual(args.custom_rules, None) self.assertEqual(args.regions, ["us-east-1"]) - self.assertEqual(args.templates, [filename]) + self.assertEqual(args.templates, [str(Path(filename))]) self.assertEqual(args.update_documentation, False) self.assertEqual(args.update_specs, False) diff --git a/test/unit/module/formatters/test_formatters.py b/test/unit/module/formatters/test_formatters.py index 815e1123d7..41164f6dac 100644 --- a/test/unit/module/formatters/test_formatters.py +++ b/test/unit/module/formatters/test_formatters.py @@ -6,6 +6,7 @@ import json import sys import xml.etree.ElementTree as ET +from pathlib import Path from test.testlib.testcase import BaseTestCase import jsonschema @@ -27,7 +28,7 @@ class TestFormatters(BaseTestCase): def setUp(self) -> None: super().setUp() cfnlint.core._reset_rule_cache() - self.filename = "test/fixtures/templates/bad/formatters.yaml" + self.filename = str(Path("test/fixtures/templates/bad/formatters.yaml")) self.results = [ Match( 6,