|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +# |
| 19 | +import pytest |
| 20 | + |
| 21 | +from verify_action_build.action_ref import ( |
| 22 | + parse_action_ref, |
| 23 | + extract_composite_uses, |
| 24 | + detect_action_type_from_yml, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class TestParseActionRef: |
| 29 | + def test_simple_ref(self): |
| 30 | + org, repo, sub, hash_ = parse_action_ref("dorny/test-reporter@abc123def456789012345678901234567890abcd") |
| 31 | + assert org == "dorny" |
| 32 | + assert repo == "test-reporter" |
| 33 | + assert sub == "" |
| 34 | + assert hash_ == "abc123def456789012345678901234567890abcd" |
| 35 | + |
| 36 | + def test_monorepo_sub_path(self): |
| 37 | + org, repo, sub, hash_ = parse_action_ref("gradle/actions/setup-gradle@abc123def456789012345678901234567890abcd") |
| 38 | + assert org == "gradle" |
| 39 | + assert repo == "actions" |
| 40 | + assert sub == "setup-gradle" |
| 41 | + assert hash_ == "abc123def456789012345678901234567890abcd" |
| 42 | + |
| 43 | + def test_deep_sub_path(self): |
| 44 | + org, repo, sub, hash_ = parse_action_ref("org/repo/a/b/c@deadbeef" * 1 + "org/repo/a/b/c@" + "a" * 40) |
| 45 | + # Reset: test clean |
| 46 | + org, repo, sub, hash_ = parse_action_ref("org/repo/a/b/c@" + "a" * 40) |
| 47 | + assert org == "org" |
| 48 | + assert repo == "repo" |
| 49 | + assert sub == "a/b/c" |
| 50 | + assert hash_ == "a" * 40 |
| 51 | + |
| 52 | + def test_missing_at_sign_exits(self): |
| 53 | + with pytest.raises(SystemExit): |
| 54 | + parse_action_ref("dorny/test-reporter") |
| 55 | + |
| 56 | + def test_missing_org_repo_exits(self): |
| 57 | + with pytest.raises(SystemExit): |
| 58 | + parse_action_ref("singlepart@abc123") |
| 59 | + |
| 60 | + |
| 61 | +class TestExtractCompositeUses: |
| 62 | + def test_standard_action_ref(self): |
| 63 | + yml = """ |
| 64 | +steps: |
| 65 | + - uses: actions/checkout@abc123def456789012345678901234567890abcd |
| 66 | +""" |
| 67 | + results = extract_composite_uses(yml) |
| 68 | + assert len(results) == 1 |
| 69 | + assert results[0]["org"] == "actions" |
| 70 | + assert results[0]["repo"] == "checkout" |
| 71 | + assert results[0]["is_hash_pinned"] is True |
| 72 | + assert results[0]["is_local"] is False |
| 73 | + |
| 74 | + def test_tag_ref_not_hash_pinned(self): |
| 75 | + yml = """ |
| 76 | +steps: |
| 77 | + - uses: actions/checkout@v4 |
| 78 | +""" |
| 79 | + results = extract_composite_uses(yml) |
| 80 | + assert len(results) == 1 |
| 81 | + assert results[0]["is_hash_pinned"] is False |
| 82 | + assert results[0]["ref"] == "v4" |
| 83 | + |
| 84 | + def test_local_action(self): |
| 85 | + yml = """ |
| 86 | +steps: |
| 87 | + - uses: ./.github/actions/my-action |
| 88 | +""" |
| 89 | + results = extract_composite_uses(yml) |
| 90 | + assert len(results) == 1 |
| 91 | + assert results[0]["is_local"] is True |
| 92 | + assert results[0]["raw"] == "./.github/actions/my-action" |
| 93 | + |
| 94 | + def test_docker_reference(self): |
| 95 | + yml = """ |
| 96 | +steps: |
| 97 | + - uses: docker://alpine:3.18 |
| 98 | +""" |
| 99 | + results = extract_composite_uses(yml) |
| 100 | + assert len(results) == 1 |
| 101 | + assert results[0].get("is_docker") is True |
| 102 | + |
| 103 | + def test_monorepo_sub_action(self): |
| 104 | + yml = """ |
| 105 | +steps: |
| 106 | + - uses: gradle/actions/setup-gradle@abc123def456789012345678901234567890abcd |
| 107 | +""" |
| 108 | + results = extract_composite_uses(yml) |
| 109 | + assert len(results) == 1 |
| 110 | + assert results[0]["org"] == "gradle" |
| 111 | + assert results[0]["repo"] == "actions" |
| 112 | + assert results[0]["sub_path"] == "setup-gradle" |
| 113 | + |
| 114 | + def test_comment_stripped(self): |
| 115 | + yml = """ |
| 116 | +steps: |
| 117 | + - uses: actions/checkout@abc123def456789012345678901234567890abcd # v4 |
| 118 | +""" |
| 119 | + results = extract_composite_uses(yml) |
| 120 | + assert len(results) == 1 |
| 121 | + assert results[0]["ref"] == "abc123def456789012345678901234567890abcd" |
| 122 | + |
| 123 | + def test_multiple_uses(self): |
| 124 | + yml = """ |
| 125 | +steps: |
| 126 | + - uses: actions/checkout@abc123def456789012345678901234567890abcd |
| 127 | + - uses: actions/setup-node@def456789012345678901234567890abcd123456 |
| 128 | +""" |
| 129 | + results = extract_composite_uses(yml) |
| 130 | + assert len(results) == 2 |
| 131 | + |
| 132 | + def test_no_uses(self): |
| 133 | + yml = """ |
| 134 | +steps: |
| 135 | + - run: echo hello |
| 136 | +""" |
| 137 | + results = extract_composite_uses(yml) |
| 138 | + assert len(results) == 0 |
| 139 | + |
| 140 | + def test_quoted_uses(self): |
| 141 | + yml = """ |
| 142 | +steps: |
| 143 | + - uses: 'actions/checkout@abc123def456789012345678901234567890abcd' |
| 144 | +""" |
| 145 | + results = extract_composite_uses(yml) |
| 146 | + assert len(results) == 1 |
| 147 | + assert results[0]["org"] == "actions" |
| 148 | + |
| 149 | + def test_line_numbers(self): |
| 150 | + yml = """line1 |
| 151 | +line2 |
| 152 | + - uses: actions/checkout@abc123def456789012345678901234567890abcd |
| 153 | +line4 |
| 154 | + - uses: actions/setup-node@def456789012345678901234567890abcd123456 |
| 155 | +""" |
| 156 | + results = extract_composite_uses(yml) |
| 157 | + assert results[0]["line_num"] == 3 |
| 158 | + assert results[1]["line_num"] == 5 |
| 159 | + |
| 160 | + |
| 161 | +class TestDetectActionTypeFromYml: |
| 162 | + def test_node20(self): |
| 163 | + yml = """ |
| 164 | +name: Test |
| 165 | +runs: |
| 166 | + using: node20 |
| 167 | + main: dist/index.js |
| 168 | +""" |
| 169 | + assert detect_action_type_from_yml(yml) == "node20" |
| 170 | + |
| 171 | + def test_composite(self): |
| 172 | + yml = """ |
| 173 | +name: Test |
| 174 | +runs: |
| 175 | + using: composite |
| 176 | + steps: [] |
| 177 | +""" |
| 178 | + assert detect_action_type_from_yml(yml) == "composite" |
| 179 | + |
| 180 | + def test_docker(self): |
| 181 | + yml = """ |
| 182 | +name: Test |
| 183 | +runs: |
| 184 | + using: docker |
| 185 | + image: Dockerfile |
| 186 | +""" |
| 187 | + assert detect_action_type_from_yml(yml) == "docker" |
| 188 | + |
| 189 | + def test_quoted(self): |
| 190 | + yml = """ |
| 191 | +runs: |
| 192 | + using: 'node16' |
| 193 | +""" |
| 194 | + assert detect_action_type_from_yml(yml) == "node16" |
| 195 | + |
| 196 | + def test_unknown_when_missing(self): |
| 197 | + yml = """ |
| 198 | +name: Test |
| 199 | +""" |
| 200 | + assert detect_action_type_from_yml(yml) == "unknown" |
0 commit comments