Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeebru committed Jul 29, 2024
1 parent 71a3603 commit be3c1cc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/bitwarden_workflow_linter/rules/underscore_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ def fn(self, obj: Union[Workflow, Job, Step]) -> Tuple[bool, str]:

if isinstance(obj, Step):
if obj.run:
outputs.extend(re.findall(r"\b([a-zA-Z0-9_-]+)\s*=\s*[^=]*>>", obj.run))
outputs.extend(re.findall(
r"\b([a-zA-Z0-9_-]+)\s*=\s*[^=]*>>\s*\$GITHUB_OUTPUT",
obj.run))

for output_name in outputs:
if "-" in output_name:
return False, (
f"Output name: '{output_name}' in {obj.__class__.__name__}: '{obj.name}'\n"
"contains a hyphen. Please replace it with an underscore."
f"Hyphen found in {obj.__class__.__name__} output: {output_name}"
)

return True, ""
61 changes: 60 additions & 1 deletion tests/rules/test_underscore_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,51 @@ def fixture_incorrect_workflow():
return WorkflowBuilder.build(workflow=yaml.load(workflow), from_file=False)


@pytest.fixture(name="push_only_workflow")
def fixture_push_only_workflow():
workflow = """\
---
name: Push Only
on:
push: {}
jobs:
job-key:
name: Test
runs-on: ubuntu-latest
steps:
- run: echo test
"""
return WorkflowBuilder.build(workflow=yaml.load(workflow), from_file=False)

@pytest.fixture(name="misc_workflow")
def fixture_misc_workflow():
workflow = """\
---
name: Misc Workflow
on:
push: {}
jobs:
job-key:
runs-on: ubuntu-22.04
outputs:
test-key-1: ${{ steps.test_output_1.outputs.test_key }}
steps:
- name: Test Step
run: |
echo "test_value=$does_it_break" >> /tmp/test
- name: Test Step 2
run: |
TEST_FILE=/tmp/test2
echo "test_value=$does_it_break" >> $TEST_FILE
"""
return WorkflowBuilder.build(workflow=yaml.load(workflow), from_file=False)



@pytest.fixture(name="rule")
def fixture_rule():
return RuleUnderscoreOutputs()
Expand Down Expand Up @@ -169,11 +214,25 @@ def test_rule_on_incorrect_step(rule, incorrect_workflow):
result, _ = rule.fn(incorrect_workflow.jobs["job-key"].steps[0])
assert result is False

result, _ = rule.fn(incorrect_workflow.jobs["job-key"].steps[1])
result, message = rule.fn(incorrect_workflow.jobs["job-key"].steps[1])
assert result is False
assert message == "Hyphen found in Step output: test-key-2"

result, _ = rule.fn(incorrect_workflow.jobs["job-key"].steps[2])
assert result is True

result, _ = rule.fn(incorrect_workflow.jobs["job-key"].steps[3])
assert result is True


def test_rule_on_push_only_workflow(rule, push_only_workflow):
result, _ = rule.fn(push_only_workflow)
assert result is True


def test_rule_on_misc_workflow(rule, misc_workflow):
result, _ = rule.fn(misc_workflow.jobs["job-key"].steps[0])
assert result is True

result, _ = rule.fn(misc_workflow.jobs["job-key"].steps[1])
assert result is True

0 comments on commit be3c1cc

Please sign in to comment.