Skip to content

Commit

Permalink
Fix linting issues on job_environment_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-flinn committed Jan 12, 2024
1 parent fc9cbf7 commit 2131363
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
8 changes: 4 additions & 4 deletions lint-workflow/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ tasks:
silent: true
cmds:
- pipenv run pytype src

test:unit:
cmds:
- pipenv run pytest tests

test:unit:
test:unit:single:
cmds:
- pipenv run pytest tests
- pipenv run pytest {{.CLI_ARGS}}

test:cov:
cmds:
Expand All @@ -44,4 +44,4 @@ tasks:

test:e2e:actions:update:
cmds:
- pipenv run python cli.py -v actions update --output test.json
- pipenv run python cli.py -v actions update --output test.json
18 changes: 15 additions & 3 deletions lint-workflow/src/rules/job_environment_prefix.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Union, Tuple
"""A Rule to enforce prefixes environment variables."""
from typing import Union, Tuple, List

from ..rule import Rule
from ..models.job import Job
Expand All @@ -8,8 +9,19 @@


class RuleJobEnvironmentPrefix(Rule):
"""Rule to enforce specific prefixes for environemnt variables.
Maintaining l
"""
def __init__(self, settings: Settings = None) -> None:
self.message: str = f"Job Environment vars should start with and underscore:"
"""RuleJobEnvironmentPrefix constructor.
Args:
settings:
A Settings object that contains any default, overriden, or custom settings
required anywhere in the application.
"""
self.message: str = "Job Environment vars should start with and underscore:"
self.on_fail: LintLevels = LintLevels.ERROR
self.compatibility: List[Union[Workflow, Job, Step]] = [Job]
self.settings: Settings = settings
Expand Down Expand Up @@ -39,7 +51,7 @@ def fn(self, obj: Job) -> Tuple[bool, str]:
correct = True

offending_keys = []
for key, value in obj.env.items():
for key in obj.env.keys():
if key[0] != "_":
offending_keys.append(key)
correct = False
Expand Down
20 changes: 9 additions & 11 deletions lint-workflow/tests/rules/test_job_environment_prefix.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""Test src/rules/job_environment_prefix."""
import pytest

from ruamel.yaml import YAML

from ..conftest import FIXTURE_DIR
from ..context import src

from src.load import WorkflowBuilder
from src.rules.job_environment_prefix import RuleJobEnvironmentPrefix

yaml = YAML()


@pytest.fixture
def correct_workflow():
@pytest.fixture(name="correct_workflow")
def fixture_correct_workflow():
workflow = """\
---
on:
Expand All @@ -29,8 +27,8 @@ def correct_workflow():
return WorkflowBuilder.build(workflow=yaml.load(workflow), from_file=False)


@pytest.fixture
def incorrect_workflow():
@pytest.fixture(name="incorrect_workflow")
def fixture_incorrect_workflow():
workflow = """\
---
on:
Expand All @@ -47,16 +45,16 @@ def incorrect_workflow():
return WorkflowBuilder.build(workflow=yaml.load(workflow), from_file=False)


@pytest.fixture
def rule():
@pytest.fixture(name="rule")
def fixture_rule():
return RuleJobEnvironmentPrefix()


def test_rule_on_correct_workflow(rule, correct_workflow):
obj = correct_workflow.jobs["job-key"]

result, message = rule.fn(correct_workflow.jobs["job-key"])
assert result == True
assert result is True
assert message == ""

finding = rule.execute(obj)
Expand All @@ -67,7 +65,7 @@ def test_rule_on_incorrect_workflow(rule, incorrect_workflow):
obj = incorrect_workflow.jobs["job-key"]

result, message = rule.fn(obj)
assert result == False
assert result is False
assert "TEST_ENV" in message

finding = rule.execute(obj)
Expand Down

0 comments on commit 2131363

Please sign in to comment.