from __future__ import annotations
import typing as typ
from pathlib import Path
import pytest
from test_coverage_shape_probes import PUBLISHER, repository
from uploader_pin import (
APPROVED_PIN,
DEPRECATED,
NO_UPLOADER,
deprecated_mentions,
pin_faults,
refresh_workflows,
)
from workflow_reader import Workflow, load_workflows
REPOSITORY_ROOT = Path(__file__).resolve().parents[2]
BASE_USES: typ.Final = "upload-codescene-coverage@abc\n"
APPROVED_USES: typ.Final = f"upload-codescene-coverage@{APPROVED_PIN}\n"
PINNED: typ.Final = PUBLISHER.replace(BASE_USES, APPROVED_USES)
SECOND_JOB: typ.Final = (
" second:\n runs-on: ubuntu-latest\n steps:\n"
" - uses: Leynos/Shared-Actions/.github/actions/"
"upload-codescene-coverage@main\n"
)
LAST_INPUT: typ.Final = " access-token: ${{ secrets.CS_ACCESS_TOKEN }}\n"
@pytest.fixture(scope="module")
def workflows() -> list[Workflow]:
return load_workflows(REPOSITORY_ROOT)
def test_every_uploader_step_is_pinned_to_the_approved_sha(
workflows: list[Workflow],
) -> None:
found = pin_faults(workflows)
assert found == [], found
@pytest.mark.parametrize("name", DEPRECATED, ids=["input", "variable"])
def test_no_workflow_mentions_a_deprecated_name(
workflows: list[Workflow], name: str
) -> None:
found = deprecated_mentions(workflows, name)
assert found == [], f"{name} is still mentioned in {found}"
def test_the_checksum_refresh_workflow_is_absent(workflows: list[Workflow]) -> None:
found = refresh_workflows(workflows)
assert found == [], f"delete {found}; nothing reads the variable it wrote"
def test_the_pinned_base_is_clean() -> None:
assert BASE_USES in PUBLISHER, "the probe's anchor is not in the base"
found = pin_faults(repository(publisher=PINNED))
assert found == [], found
def test_an_unapproved_pin_is_named() -> None:
found = pin_faults(repository())
assert any("pinned to 'abc'" in fault for fault in found), found
def test_the_owner_is_matched_without_regard_to_case() -> None:
found = pin_faults(repository(publisher=PINNED + SECOND_JOB))
assert any("pinned to 'main'" in fault for fault in found), found
def test_the_action_path_is_matched_as_written() -> None:
recased = PINNED.replace(
"/upload-codescene-coverage@", "/Upload-CodeScene-Coverage@"
)
assert recased != PINNED, "the probe's anchor is not in the base"
found = pin_faults(repository(publisher=recased))
assert found == [NO_UPLOADER], found
def test_a_commented_out_step_is_not_an_uploader() -> None:
commented = PINNED.replace(
f" uses: leynos/shared-actions/.github/actions/{APPROVED_USES}",
f" # uses: leynos/shared-actions/.github/actions/{APPROVED_USES}"
" uses: actions/checkout@abc\n",
)
assert commented != PINNED, "the probe's anchor is not in the base"
found = pin_faults(repository(publisher=commented))
assert found == [NO_UPLOADER], found
@pytest.mark.parametrize(
("line", "name"),
[
("installer-checksum: abc\n", "installer-checksum"),
(
"archive-checksum: ${{ vars.CODESCENE_CLI_SHA256 }}\n",
"codescene_cli_sha256",
),
],
ids=["input", "variable"],
)
def test_a_deprecated_name_is_found_and_a_comment_is_not(line: str, name: str) -> None:
added = PINNED.replace(LAST_INPUT, f"{LAST_INPUT} {line}")
commented = PINNED.replace(LAST_INPUT, f"{LAST_INPUT} # {line}")
assert added != PINNED, "the probe's anchor is not in the base"
found = deprecated_mentions(repository(publisher=added), name)
assert found == [".github/workflows/coverage-main.yml"], found
found = deprecated_mentions(repository(publisher=commented), name)
assert found == [], found
def test_a_refresh_workflow_is_found_in_any_case() -> None:
extra = {"Get-CodeScene-SHA.yml": "on: workflow_dispatch\njobs: {}\n"}
found = refresh_workflows(repository(publisher=PINNED, extra=extra))
assert found == [".github/workflows/Get-CodeScene-SHA.yml"], found