From 0622a19533306ca152bdca705e91e219231c0818 Mon Sep 17 00:00:00 2001 From: Samriddhi Singh Date: Thu, 11 Jul 2024 09:30:44 +0530 Subject: [PATCH 1/2] Lint using isort --- setup.py | 11 +++-------- src/tirith/core/core.py | 6 +++--- src/tirith/core/evaluators/__init__.py | 8 ++++---- src/tirith/core/evaluators/contained_in.py | 3 ++- src/tirith/core/evaluators/contains.py | 3 ++- src/tirith/core/evaluators/equals.py | 3 ++- src/tirith/core/evaluators/greater_than.py | 1 - src/tirith/core/evaluators/not_contained_in.py | 3 ++- src/tirith/core/evaluators/not_contains.py | 3 ++- src/tirith/core/evaluators/not_equals.py | 3 ++- src/tirith/core/evaluators/regex_match.py | 3 ++- src/tirith/providers/__init__.py | 6 +++--- src/tirith/providers/json/handler.py | 5 +++-- src/tirith/providers/kubernetes/handler.py | 5 +++-- src/tirith/providers/terraform_plan/handler.py | 1 + tests/core/evaluators/test_contained_in.py | 2 +- tests/core/evaluators/test_contains.py | 2 +- tests/core/evaluators/test_equals.py | 3 ++- tests/core/evaluators/test_greater_than.py | 3 ++- tests/core/evaluators/test_greater_than_equal_to.py | 3 ++- tests/core/evaluators/test_is_empty.py | 3 ++- tests/core/evaluators/test_is_not_empty.py | 3 ++- tests/core/evaluators/test_less_than.py | 3 ++- tests/core/evaluators/test_less_than_equal_to.py | 3 ++- tests/core/evaluators/test_not_contained_in.py | 2 +- tests/core/evaluators/test_not_contains.py | 2 +- tests/core/evaluators/test_not_equals.py | 3 ++- tests/core/evaluators/test_regex_match.py | 3 ++- tests/providers/infracost/tests_infracost.py | 1 + tests/providers/sg_workflow/tests_sg_workflow.py | 2 ++ tests/providers/terraform_plan/test_action.py | 3 ++- .../terraform_plan/test_direct_references.py | 7 ++++--- tests/providers/terraform_plan/test_dot_star_attr.py | 3 ++- .../providers/terraform_plan/test_provider_config.py | 3 ++- tests/providers/terraform_plan/test_terraform_plan.py | 3 +-- 35 files changed, 70 insertions(+), 51 deletions(-) diff --git a/setup.py b/setup.py index b80589cb..e068a7a7 100644 --- a/setup.py +++ b/setup.py @@ -1,18 +1,13 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from __future__ import absolute_import -from __future__ import print_function +from __future__ import absolute_import, print_function import io import re from glob import glob -from os.path import basename -from os.path import dirname -from os.path import join -from os.path import splitext +from os.path import basename, dirname, join, splitext -from setuptools import find_packages -from setuptools import setup +from setuptools import find_packages, setup def read(*names, **kwargs): diff --git a/src/tirith/core/core.py b/src/tirith/core/core.py index c4816c81..c67a9e55 100644 --- a/src/tirith/core/core.py +++ b/src/tirith/core/core.py @@ -2,16 +2,16 @@ import json import logging import re -import yaml from types import CodeType +from typing import Any, Dict, List, Optional, Tuple -from typing import Any, Dict, List, Tuple, Optional +import yaml from tirith.providers.common import ProviderError + from ..providers import PROVIDERS_DICT from .evaluators import EVALUATORS_DICT - # TODO: Use __name__ for the logger name instead of using the root logger logger = logging.getLogger() diff --git a/src/tirith/core/evaluators/__init__.py b/src/tirith/core/evaluators/__init__.py index 5666af0e..42d8f850 100644 --- a/src/tirith/core/evaluators/__init__.py +++ b/src/tirith/core/evaluators/__init__.py @@ -4,16 +4,16 @@ from .contained_in import ContainedIn from .contains import Contains from .equals import Equals -from .greater_than_equal_to import GreaterThanEqualTo from .greater_than import GreaterThan +from .greater_than_equal_to import GreaterThanEqualTo from .is_empty import IsEmpty from .is_not_empty import IsNotEmpty -from .less_than_equal_to import LessThanEqualTo from .less_than import LessThan -from .regex_match import RegexMatch -from .not_equals import NotEquals +from .less_than_equal_to import LessThanEqualTo from .not_contained_in import NotContainedIn from .not_contains import NotContains +from .not_equals import NotEquals +from .regex_match import RegexMatch EVALUATORS_DICT: Dict[str, Type[BaseEvaluator]] = { "ContainedIn": ContainedIn, diff --git a/src/tirith/core/evaluators/contained_in.py b/src/tirith/core/evaluators/contained_in.py index da24a90f..e85880cd 100644 --- a/src/tirith/core/evaluators/contained_in.py +++ b/src/tirith/core/evaluators/contained_in.py @@ -1,8 +1,9 @@ import logging -from .base_evaluator import BaseEvaluator from tirith.utils import sort_collections +from .base_evaluator import BaseEvaluator + # TODO: At least add __name__ as the name for the logger logger = logging.getLogger() diff --git a/src/tirith/core/evaluators/contains.py b/src/tirith/core/evaluators/contains.py index 8313fc81..f7f1aeb2 100644 --- a/src/tirith/core/evaluators/contains.py +++ b/src/tirith/core/evaluators/contains.py @@ -1,8 +1,9 @@ import logging -from .base_evaluator import BaseEvaluator from tirith.utils import sort_collections +from .base_evaluator import BaseEvaluator + # TODO: At least add __name__ as the name for the logger logger = logging.getLogger() diff --git a/src/tirith/core/evaluators/equals.py b/src/tirith/core/evaluators/equals.py index 193747ad..d7690b98 100644 --- a/src/tirith/core/evaluators/equals.py +++ b/src/tirith/core/evaluators/equals.py @@ -1,8 +1,9 @@ import logging -from .base_evaluator import BaseEvaluator from tirith.utils import sort_collections +from .base_evaluator import BaseEvaluator + # TODO: Use __name__ for the logger name instead of using the root logger logger = logging.getLogger() diff --git a/src/tirith/core/evaluators/greater_than.py b/src/tirith/core/evaluators/greater_than.py index 9baba884..c8929c53 100644 --- a/src/tirith/core/evaluators/greater_than.py +++ b/src/tirith/core/evaluators/greater_than.py @@ -1,6 +1,5 @@ from .base_evaluator import BaseEvaluator - # Checks if :attr:`value` is more then :attr:`other`. Automatically casts values to the same type if possible. # Args: diff --git a/src/tirith/core/evaluators/not_contained_in.py b/src/tirith/core/evaluators/not_contained_in.py index 216bc222..7fad76e2 100644 --- a/src/tirith/core/evaluators/not_contained_in.py +++ b/src/tirith/core/evaluators/not_contained_in.py @@ -1,8 +1,9 @@ import logging -from .base_evaluator import BaseEvaluator from tirith.utils import sort_collections +from .base_evaluator import BaseEvaluator + # TODO: At least add __name__ as the name for the logger logger = logging.getLogger() diff --git a/src/tirith/core/evaluators/not_contains.py b/src/tirith/core/evaluators/not_contains.py index f696f535..eba2ace3 100644 --- a/src/tirith/core/evaluators/not_contains.py +++ b/src/tirith/core/evaluators/not_contains.py @@ -1,8 +1,9 @@ import logging -from .base_evaluator import BaseEvaluator from tirith.utils import sort_collections +from .base_evaluator import BaseEvaluator + # TODO: At least add __name__ as the name for the logger logger = logging.getLogger() diff --git a/src/tirith/core/evaluators/not_equals.py b/src/tirith/core/evaluators/not_equals.py index d8d57b43..854db545 100644 --- a/src/tirith/core/evaluators/not_equals.py +++ b/src/tirith/core/evaluators/not_equals.py @@ -1,8 +1,9 @@ import logging -from .base_evaluator import BaseEvaluator from tirith.utils import sort_collections +from .base_evaluator import BaseEvaluator + # TODO: Use __name__ for the logger name instead of using the root logger logger = logging.getLogger() diff --git a/src/tirith/core/evaluators/regex_match.py b/src/tirith/core/evaluators/regex_match.py index 73de6043..78552ca9 100644 --- a/src/tirith/core/evaluators/regex_match.py +++ b/src/tirith/core/evaluators/regex_match.py @@ -1,6 +1,7 @@ -from .base_evaluator import BaseEvaluator import re +from .base_evaluator import BaseEvaluator + class RegexMatch(BaseEvaluator): def evaluate(self, evaluator_input, evaluator_data): diff --git a/src/tirith/providers/__init__.py b/src/tirith/providers/__init__.py index f46cd79e..f409e82f 100644 --- a/src/tirith/providers/__init__.py +++ b/src/tirith/providers/__init__.py @@ -1,11 +1,11 @@ from typing import Callable, Dict -from .terraform_plan.handler import * from .infracost import provide as infracost_provider -from .sg_workflow import provide as sg_wf_provider -from .terraform_plan import provide as terraform_provider from .json import provide as json_provider from .kubernetes import provide as kubernetes_provider +from .sg_workflow import provide as sg_wf_provider +from .terraform_plan import provide as terraform_provider +from .terraform_plan.handler import * PROVIDERS_DICT: Dict[str, Callable] = { "stackguardian/terraform_plan": terraform_provider, diff --git a/src/tirith/providers/json/handler.py b/src/tirith/providers/json/handler.py index 173e56d2..01b181fd 100644 --- a/src/tirith/providers/json/handler.py +++ b/src/tirith/providers/json/handler.py @@ -1,7 +1,8 @@ +from typing import Callable, Dict, List + import pydash -from typing import Callable, Dict, List -from ..common import create_result_dict, ProviderError +from ..common import ProviderError, create_result_dict class PydashPathNotFound: diff --git a/src/tirith/providers/kubernetes/handler.py b/src/tirith/providers/kubernetes/handler.py index 62ac576e..b0d137d5 100644 --- a/src/tirith/providers/kubernetes/handler.py +++ b/src/tirith/providers/kubernetes/handler.py @@ -1,7 +1,8 @@ +from typing import Callable, Dict, List + import pydash -from typing import Callable, Dict, List -from ..common import create_result_dict, ProviderError +from ..common import ProviderError, create_result_dict class PydashPathNotFound: diff --git a/src/tirith/providers/terraform_plan/handler.py b/src/tirith/providers/terraform_plan/handler.py index 3b7a3166..049d1984 100644 --- a/src/tirith/providers/terraform_plan/handler.py +++ b/src/tirith/providers/terraform_plan/handler.py @@ -9,6 +9,7 @@ # input->(list ["a.b","c", "d"],value of resource) # returns->[any, any, any] from typing import Iterable, Tuple + import pydash from ..common import ProviderError diff --git a/tests/core/evaluators/test_contained_in.py b/tests/core/evaluators/test_contained_in.py index d255c797..c16e963e 100644 --- a/tests/core/evaluators/test_contained_in.py +++ b/tests/core/evaluators/test_contained_in.py @@ -1,6 +1,6 @@ -from tirith.core.evaluators import ContainedIn from pytest import mark +from tirith.core.evaluators import ContainedIn checks_passing = [ ("a", ["a", "b", "c", "d"]), diff --git a/tests/core/evaluators/test_contains.py b/tests/core/evaluators/test_contains.py index b73a2291..6eea2df3 100644 --- a/tests/core/evaluators/test_contains.py +++ b/tests/core/evaluators/test_contains.py @@ -1,6 +1,6 @@ -from tirith.core.evaluators import Contains from pytest import mark +from tirith.core.evaluators import Contains checks_passing = [ (["a", "b", "c", "d"], "a"), diff --git a/tests/core/evaluators/test_equals.py b/tests/core/evaluators/test_equals.py index 57ea9883..80a01bd9 100644 --- a/tests/core/evaluators/test_equals.py +++ b/tests/core/evaluators/test_equals.py @@ -1,6 +1,7 @@ -from tirith.core.evaluators import Equals from pytest import mark +from tirith.core.evaluators import Equals + checks_passing = [({1, 2, 3, 4}, {1, 2, 3, 4}), ([2, 4, 3], [2, 3, 4])] checks_failing = [({1, 2, 3, 4}, {2, 3, 4}), ({2, 3, 4}, [2, 3, 4])] diff --git a/tests/core/evaluators/test_greater_than.py b/tests/core/evaluators/test_greater_than.py index 7c7e27e7..99bdbcd5 100644 --- a/tests/core/evaluators/test_greater_than.py +++ b/tests/core/evaluators/test_greater_than.py @@ -1,6 +1,7 @@ -from tirith.core.evaluators import GreaterThan from pytest import mark +from tirith.core.evaluators import GreaterThan + evaluator_input1 = 5 evaluator_data1 = 2 diff --git a/tests/core/evaluators/test_greater_than_equal_to.py b/tests/core/evaluators/test_greater_than_equal_to.py index 112b347f..053e9211 100644 --- a/tests/core/evaluators/test_greater_than_equal_to.py +++ b/tests/core/evaluators/test_greater_than_equal_to.py @@ -1,6 +1,7 @@ -from tirith.core.evaluators import GreaterThanEqualTo from pytest import mark +from tirith.core.evaluators import GreaterThanEqualTo + evaluator_input1 = 27 evaluator_data1 = 26 diff --git a/tests/core/evaluators/test_is_empty.py b/tests/core/evaluators/test_is_empty.py index b03c5a32..3af3a149 100644 --- a/tests/core/evaluators/test_is_empty.py +++ b/tests/core/evaluators/test_is_empty.py @@ -1,6 +1,7 @@ -from tirith.core.evaluators import IsEmpty from pytest import mark +from tirith.core.evaluators import IsEmpty + checks_passing = ("", None, [], dict()) checks_failing = ("stackguardian", 1, [None], dict(a=1)) diff --git a/tests/core/evaluators/test_is_not_empty.py b/tests/core/evaluators/test_is_not_empty.py index d708e62f..e2107ddd 100644 --- a/tests/core/evaluators/test_is_not_empty.py +++ b/tests/core/evaluators/test_is_not_empty.py @@ -1,6 +1,7 @@ -from tirith.core.evaluators import IsNotEmpty from pytest import mark +from tirith.core.evaluators import IsNotEmpty + evaluator_input1 = "abc" evaluator_input2 = "" diff --git a/tests/core/evaluators/test_less_than.py b/tests/core/evaluators/test_less_than.py index 881b5ca0..534d882a 100644 --- a/tests/core/evaluators/test_less_than.py +++ b/tests/core/evaluators/test_less_than.py @@ -1,6 +1,7 @@ -from tirith.core.evaluators import LessThan from pytest import mark +from tirith.core.evaluators import LessThan + evaluator_input1 = 5 evaluator_data1 = 10 diff --git a/tests/core/evaluators/test_less_than_equal_to.py b/tests/core/evaluators/test_less_than_equal_to.py index 5d9ff7a8..524d27d5 100644 --- a/tests/core/evaluators/test_less_than_equal_to.py +++ b/tests/core/evaluators/test_less_than_equal_to.py @@ -1,6 +1,7 @@ -from tirith.core.evaluators import LessThanEqualTo from pytest import mark +from tirith.core.evaluators import LessThanEqualTo + evaluator_input1 = 26 evaluator_data1 = 27 diff --git a/tests/core/evaluators/test_not_contained_in.py b/tests/core/evaluators/test_not_contained_in.py index 9718197e..1af80d09 100644 --- a/tests/core/evaluators/test_not_contained_in.py +++ b/tests/core/evaluators/test_not_contained_in.py @@ -1,6 +1,6 @@ -from tirith.core.evaluators import NotContainedIn from pytest import mark +from tirith.core.evaluators import NotContainedIn checks_failing = [ ("a", ["a", "b", "c", "d"]), diff --git a/tests/core/evaluators/test_not_contains.py b/tests/core/evaluators/test_not_contains.py index 60e647ee..05e522d6 100644 --- a/tests/core/evaluators/test_not_contains.py +++ b/tests/core/evaluators/test_not_contains.py @@ -1,6 +1,6 @@ -from tirith.core.evaluators import NotContains from pytest import mark +from tirith.core.evaluators import NotContains checks_failing = [ (["a", "b", "c", "d"], "a"), diff --git a/tests/core/evaluators/test_not_equals.py b/tests/core/evaluators/test_not_equals.py index d76707c3..bd5a95a3 100644 --- a/tests/core/evaluators/test_not_equals.py +++ b/tests/core/evaluators/test_not_equals.py @@ -1,6 +1,7 @@ -from tirith.core.evaluators import NotEquals from pytest import mark +from tirith.core.evaluators import NotEquals + checks_failing = [({1, 2, 3, 4}, {1, 2, 3, 4}), ([2, 4, 3], [2, 3, 4])] checks_passing = [({1, 2, 3, 4}, {2, 3, 4}), ({2, 3, 4}, [2, 3, 4])] diff --git a/tests/core/evaluators/test_regex_match.py b/tests/core/evaluators/test_regex_match.py index 3b4e1297..259c09d6 100644 --- a/tests/core/evaluators/test_regex_match.py +++ b/tests/core/evaluators/test_regex_match.py @@ -1,6 +1,7 @@ -from tirith.core.evaluators import RegexMatch from pytest import mark +from tirith.core.evaluators import RegexMatch + evaluator_data1 = "^(?=[a-zA-Z0-9._]{8,20}$)(?!.*[_.]{2})[^_.].*[^_.]$" evaluator_input1 = "amitrakshar01" diff --git a/tests/providers/infracost/tests_infracost.py b/tests/providers/infracost/tests_infracost.py index 3e9ba421..d4b25b8a 100644 --- a/tests/providers/infracost/tests_infracost.py +++ b/tests/providers/infracost/tests_infracost.py @@ -1,5 +1,6 @@ import json import os + import pytest from tirith.providers.infracost import handler diff --git a/tests/providers/sg_workflow/tests_sg_workflow.py b/tests/providers/sg_workflow/tests_sg_workflow.py index b4ef03fd..6648e7f4 100644 --- a/tests/providers/sg_workflow/tests_sg_workflow.py +++ b/tests/providers/sg_workflow/tests_sg_workflow.py @@ -1,5 +1,6 @@ import json import os + import pytest from tirith.providers.sg_workflow import handler @@ -13,6 +14,7 @@ def load_terraform_plan_json(json_path): input_data = load_terraform_plan_json("input.json") import json import os + import pytest from tirith.providers.sg_workflow import handler diff --git a/tests/providers/terraform_plan/test_action.py b/tests/providers/terraform_plan/test_action.py index 56092421..f57dac7f 100644 --- a/tests/providers/terraform_plan/test_action.py +++ b/tests/providers/terraform_plan/test_action.py @@ -1,6 +1,7 @@ -from tirith.providers.terraform_plan import handler from utils import load_terraform_plan_json +from tirith.providers.terraform_plan import handler + def test_action_star_resource_type_should_include_every_resource_types(): provider_args_dict = {"operation_type": "action", "terraform_resource_type": "*"} diff --git a/tests/providers/terraform_plan/test_direct_references.py b/tests/providers/terraform_plan/test_direct_references.py index 293d63a6..444a5667 100644 --- a/tests/providers/terraform_plan/test_direct_references.py +++ b/tests/providers/terraform_plan/test_direct_references.py @@ -1,12 +1,13 @@ import json import os -import pytest import shutil - from subprocess import Popen + +import pytest +from utils import load_terraform_plan_json + from tirith.core.core import start_policy_evaluation_from_dict from tirith.providers.terraform_plan import handler -from utils import load_terraform_plan_json # TODO: Move these helper functions to a utils file diff --git a/tests/providers/terraform_plan/test_dot_star_attr.py b/tests/providers/terraform_plan/test_dot_star_attr.py index 05fa65f1..3a06032f 100644 --- a/tests/providers/terraform_plan/test_dot_star_attr.py +++ b/tests/providers/terraform_plan/test_dot_star_attr.py @@ -1,6 +1,7 @@ -from tirith.providers.terraform_plan import handler from pytest import mark +from tirith.providers.terraform_plan import handler + checks_passing = [ ( "a.*.b.c.*", diff --git a/tests/providers/terraform_plan/test_provider_config.py b/tests/providers/terraform_plan/test_provider_config.py index 8cbdc610..12edd40d 100644 --- a/tests/providers/terraform_plan/test_provider_config.py +++ b/tests/providers/terraform_plan/test_provider_config.py @@ -1,6 +1,7 @@ +from utils import load_terraform_plan_json + from tirith.providers.common import ProviderError from tirith.providers.terraform_plan import handler -from utils import load_terraform_plan_json def test_get_terraform_provider_get_region(): diff --git a/tests/providers/terraform_plan/test_terraform_plan.py b/tests/providers/terraform_plan/test_terraform_plan.py index c55f72ba..493798e7 100644 --- a/tests/providers/terraform_plan/test_terraform_plan.py +++ b/tests/providers/terraform_plan/test_terraform_plan.py @@ -1,8 +1,7 @@ import pytest - -from tirith.providers.terraform_plan import handler from utils import load_terraform_plan_json +from tirith.providers.terraform_plan import handler input_data = load_terraform_plan_json("input.json") From 1eca62529187114ce285bf4a6518b2ba1a5fc5ac Mon Sep 17 00:00:00 2001 From: Samriddhi Singh Date: Thu, 11 Jul 2024 13:11:19 +0530 Subject: [PATCH 2/2] Add isort linting to workflows --- .github/workflows/lint.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index c759fc0b..4c6541b6 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -12,3 +12,17 @@ jobs: with: options: "--check" src: "." + + isort: + runs-on: ubuntu-latest + name: isort linting + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install isort + run: pip install isort + - name: Run isort + run: isort --check-only . \ No newline at end of file