Repository navigation
Expand file tree
/
Copy pathtest_default_branch_flags.py
More file actions
165 lines (121 loc) · 5.78 KB
/
Copy pathtest_default_branch_flags.py
File metadata and controls
165 lines (121 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Which default-branch and head-scan parameters a full scan is created with."""
import pytest
from socketsecurity import socketcli
from socketsecurity.core import Core
from socketsecurity.core.git_interface import Git
from tests.core.test_strict_blocking_no_manifest_change import cli_runtime # noqa: F401
from tests.unit.test_git_interface import ( # noqa: F401
CI_ENVIRONMENT_VARIABLES,
release_tag_checkout,
)
REAL_APPLY_GIT_CONTEXT = socketcli.apply_git_context
def _scan_params(runtime):
runtime.full_scan.assert_called_once()
return runtime.full_scan.call_args.args[1]
def _flags(params):
return params.make_default_branch, params.set_as_pending_head
def test_detected_default_branch_makes_the_scan_the_head(cli_runtime): # noqa: F811
cli_runtime.git_repo.is_default_branch = True
assert cli_runtime.run() == 0
assert _flags(_scan_params(cli_runtime)) == (True, True)
@pytest.mark.parametrize("ci_says_default", [True, False])
def test_no_default_branch_wins_over_detection(cli_runtime, ci_says_default): # noqa: F811
cli_runtime.git_repo.is_default_branch = ci_says_default
assert cli_runtime.run(["--no-default-branch"]) == 0
assert _flags(_scan_params(cli_runtime)) == (False, False)
@pytest.fixture
def github_dispatch_on_develop(cli_runtime, release_tag_checkout, monkeypatch, mocker): # noqa: F811
"""
A --scm github run with DEFAULT_BRANCH=true, dispatched on develop (the default
branch) while the checkout is the v1.0 release tag on main. Git and the SCM
adapter both detect the default branch from the CI variables.
"""
for name in CI_ENVIRONMENT_VARIABLES:
monkeypatch.delenv(name, raising=False)
for name in ("PR_NUMBER", "EVENT_ACTION", "COMMIT_MESSAGE"):
monkeypatch.delenv(name, raising=False)
monkeypatch.setenv("GITHUB_EVENT_NAME", "workflow_dispatch")
monkeypatch.setenv("GITHUB_REF", "refs/heads/develop")
monkeypatch.setenv("GITHUB_SHA", release_tag_checkout.develop_sha)
monkeypatch.setenv("GITHUB_REPOSITORY", "example/widgets")
monkeypatch.setenv("DEFAULT_BRANCH", "true")
monkeypatch.setenv("GH_API_TOKEN", "test")
mocker.patch.object(Git, "ensure_safe_directory")
monkeypatch.setattr(socketcli, "apply_git_context", REAL_APPLY_GIT_CONTEXT)
# A manifest change keeps the run on the SCM full-scan path.
monkeypatch.setattr(Core, "has_manifest_files", lambda self, files: True)
def run(flags=()):
return cli_runtime.run(
[
"--scm", "github",
"--target-path", str(release_tag_checkout.path),
"--branch", "develop",
*flags,
]
)
return run
@pytest.mark.parametrize(
"explicit_ref",
[
pytest.param(["--branch", "main", "--commit-sha", "RELEASE"], id="branch-and-commit"),
pytest.param(["--branch", "main"], id="branch-only"),
pytest.param(["--branch", "develop", "--commit-sha", "RELEASE"], id="commit-off-default"),
],
)
def test_scm_detection_honors_an_explicit_ref_off_the_default_branch(
cli_runtime, github_dispatch_on_develop, release_tag_checkout, explicit_ref # noqa: F811
):
flags = [release_tag_checkout.release_sha if f == "RELEASE" else f for f in explicit_ref]
assert github_dispatch_on_develop(flags) == 0
# Not the default branch, but still a branch run that can update a tracked branch head.
assert _flags(_scan_params(cli_runtime)) == (False, True)
def test_scm_detection_honors_an_explicit_commit_when_the_ci_commit_is_not_cloned(
cli_runtime, github_dispatch_on_develop, release_tag_checkout, monkeypatch # noqa: F811
):
monkeypatch.setenv("GITHUB_SHA", "f" * 40)
assert github_dispatch_on_develop(["--commit-sha", release_tag_checkout.release_sha]) == 0
assert _flags(_scan_params(cli_runtime)) == (False, True)
def test_scm_detection_requires_explicit_ci_commit_in_checkout(
cli_runtime, github_dispatch_on_develop, monkeypatch # noqa: F811
):
missing_sha = "f" * 40
monkeypatch.setenv("GITHUB_SHA", missing_sha)
assert github_dispatch_on_develop(["--commit-sha", missing_sha]) == 0
params = _scan_params(cli_runtime)
assert params.commit_hash == missing_sha
assert _flags(params) == (False, False)
def test_scm_detection_still_marks_the_default_branch_run(
cli_runtime, github_dispatch_on_develop # noqa: F811
):
assert github_dispatch_on_develop() == 0
params = _scan_params(cli_runtime)
assert _flags(params) == (True, True)
assert params.commit_message.strip() == "dev work"
def test_explicit_default_branch_overrides_the_explicit_ref_check(
cli_runtime, github_dispatch_on_develop, release_tag_checkout # noqa: F811
):
assert github_dispatch_on_develop(
["--branch", "main", "--commit-sha", release_tag_checkout.release_sha, "--default-branch"]
) == 0
assert _flags(_scan_params(cli_runtime)) == (True, True)
def test_no_default_branch_overrides_scm_and_git_detection(
cli_runtime, github_dispatch_on_develop # noqa: F811
):
assert github_dispatch_on_develop(["--no-default-branch"]) == 0
assert _flags(_scan_params(cli_runtime)) == (False, False)
@pytest.mark.parametrize(
"actor, committer",
[(None, "release@example.com"), ("ci-actor", "ci-actor")],
)
def test_explicit_commit_supplies_message_and_committer_fallback(
cli_runtime, github_dispatch_on_develop, release_tag_checkout, # noqa: F811
monkeypatch, actor, committer,
):
if actor:
monkeypatch.setenv("GITHUB_ACTOR", actor)
assert github_dispatch_on_develop(
["--branch", "main", "--commit-sha", release_tag_checkout.release_sha]
) == 0
params = _scan_params(cli_runtime)
assert params.commit_message.strip() == "release 1.0"
assert params.committers == [committer]