Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

3.3 KB
Raw Blame
import pin_actions

SHA = "a" * 40


def test_tags_are_replaced_by_sha_with_the_tag_kept_as_a_comment():
    text = "steps:\n  - uses: actions/checkout@v4\n  - uses: astral-sh/setup-uv@v6\n    with:\n      version: x\n"
    out = pin_actions.pin_text(text, lambda repo, ref: SHA)
    assert f"  - uses: actions/checkout@{SHA} # v4\n" in out
    assert f"  - uses: astral-sh/setup-uv@{SHA} # v6\n" in out


def test_already_pinned_lines_are_left_alone():
    text = f"  - uses: actions/checkout@{SHA} # v4\n"
    assert pin_actions.pin_text(text, lambda repo, ref: (_ for _ in ()).throw(AssertionError)) == text


def test_a_comment_already_on_the_line_is_kept_with_the_ref():
    # #1463: pin_text rebuilt the line from the lead, action, SHA and ref, so a note already on the line was lost.
    # The ref stays where it has always been, right after the SHA, and the existing comment follows it unchanged.
    text = "  - uses: actions/checkout@v4 # keep: checkout needs credentials\n"
    out = pin_actions.pin_text(text, lambda repo, ref: SHA)
    assert out == f"  - uses: actions/checkout@{SHA} # v4 # keep: checkout needs credentials\n"


def test_trailing_whitespace_after_the_ref_does_not_become_a_comment():
    # The no-comment line must still come out exactly as before, with no trailing space.
    text = "  - uses: actions/checkout@v4   \n"
    out = pin_actions.pin_text(text, lambda repo, ref: SHA)
    assert out == f"  - uses: actions/checkout@{SHA} # v4\n"


def test_quoted_uses_are_pinned_and_keep_their_quotes():
    for quote in ('"', "'"):
        text = f"  - uses: {quote}actions/checkout@v4{quote}\n"
        out = pin_actions.pin_text(text, lambda repo, ref: SHA)
        assert out == f"  - uses: {quote}actions/checkout@{SHA}{quote} # v4\n"


def test_main_pins_yaml_workflows(tmp_path, monkeypatch):
    workflow = tmp_path / ".github/workflows/check.yaml"
    workflow.parent.mkdir(parents=True)
    workflow.write_text("steps:\n  - uses: actions/checkout@v4\n", encoding="utf-8")
    monkeypatch.setattr(pin_actions, "ROOT", tmp_path)
    monkeypatch.setattr(pin_actions, "gh_resolve", lambda repo, ref: SHA)

    assert pin_actions.main() == 0
    assert workflow.read_text(encoding="utf-8") == f"steps:\n  - uses: actions/checkout@{SHA} # v4\n"


def test_a_quoted_uses_key_is_pinned():
    text = '  - "uses": actions/checkout@v4\n'
    assert pin_actions.pin_text(text, lambda repo, ref: SHA) == f'  - "uses": actions/checkout@{SHA} # v4\n'


def test_quoted_uses_keys_allow_separation_before_the_colon():
    for quote in ('"', "'"):
        text = f"  - {quote}uses{quote} : actions/checkout@v4\n"
        expected = f"  - {quote}uses{quote} : actions/checkout@{SHA} # v4\n"
        assert pin_actions.pin_text(text, lambda repo, ref: SHA) == expected


def test_block_scalar_uses_text_is_not_pinned_or_resolved():
    text = ("steps:\n"
            "  - uses: actions/checkout@v4\n"
            "  - run: |\n"
            "      cat <<'YAML'\n"
            "      uses: actions/checkout@v4\n"
            "      YAML\n")
    resolved = []

    def resolve(repo, ref):
        resolved.append((repo, ref))
        return SHA

    assert pin_actions.pin_text(text, resolve) == text.replace(
        "  - uses: actions/checkout@v4\n", f"  - uses: actions/checkout@{SHA} # v4\n")
    assert resolved == [("actions/checkout", "v4")]