from __future__ import annotations
import typing as typ
from pathlib import Path
from codescene_environment import (
MISSING,
NO_UPLOADER,
REACHABLE,
STRAY,
environment_faults,
)
from test_coverage_shape_probes import CI, PUBLISHER, repository
from workflow_reader import load_workflows
REPOSITORY_ROOT = Path(__file__).resolve().parents[2]
PUBLISHER_JOB: typ.Final = " upload:\n runs-on: ubuntu-latest\n"
PLACED: typ.Final = PUBLISHER.replace(
PUBLISHER_JOB, PUBLISHER_JOB + " environment: codescene\n"
)
def faults_with(publisher: str = PLACED, ci: str = CI) -> list[str]:
return environment_faults(repository(ci=ci, publisher=publisher))
def test_this_repository_places_the_environment() -> None:
found = environment_faults(load_workflows(REPOSITORY_ROOT))
assert found == [], found
def test_the_base_places_the_environment() -> None:
assert PUBLISHER_JOB in PUBLISHER, "the probe's anchor is not in the base"
found = faults_with()
assert found == [], found
def test_the_publisher_cannot_drop_the_environment() -> None:
found = faults_with(publisher=PUBLISHER)
assert any(MISSING in fault for fault in found), found
def test_the_publisher_cannot_name_another_environment() -> None:
found = faults_with(publisher=PLACED.replace("codescene\n", "production\n", 1))
assert any(MISSING in fault for fault in found), found
def test_the_mapping_form_is_accepted() -> None:
found = faults_with(
publisher=PLACED.replace(
"environment: codescene\n", "environment: {name: codescene}\n"
)
)
assert found == [], found
def test_no_other_job_may_declare_it() -> None:
other = " other:\n runs-on: ubuntu-latest\n environment: codescene\n"
found = faults_with(publisher=PLACED + other + " steps: [{run: 'true'}]\n")
assert any(STRAY in fault for fault in found), found
def test_no_pull_request_job_may_declare_it() -> None:
ci = CI.replace(
" runs-on: ubuntu-latest\n",
" runs-on: ubuntu-latest\n environment: {name: codescene}\n",
1,
)
assert ci != CI, "the pull-request probe's anchor is not in the base"
found = faults_with(ci=ci)
assert any(REACHABLE in fault for fault in found), found
def test_an_empty_reading_is_refused() -> None:
uploader = "leynos/shared-actions/.github/actions/upload-codescene-coverage@abc"
found = faults_with(publisher=PLACED.replace(uploader, "actions/checkout@abc"))
assert found == [NO_UPLOADER], found