Repository navigation
Expand file tree
/
Copy pathtest_pending_head_params.py
More file actions
129 lines (93 loc) · 4.53 KB
/
Copy pathtest_pending_head_params.py
File metadata and controls
129 lines (93 loc) · 4.53 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
"""Which full scans ask the API to become their branch's head scan."""
import pytest
from socketsecurity.core import Core
from socketsecurity.core.scm.github import Github
from tests.core.test_strict_blocking_no_manifest_change import cli_runtime # noqa: F401
PULL_REQUEST_ENVIRONMENT = (
"GITHUB_EVENT_NAME",
"GITHUB_EVENT_PATH",
"GITHUB_REF",
"PR_NUMBER",
"EVENT_ACTION",
"CI_MERGE_REQUEST_IID",
"CI_PIPELINE_SOURCE",
"BITBUCKET_PR_ID",
"BUILDKITE_PULL_REQUEST",
"BUILD_REASON",
"SYSTEM_PULLREQUEST_PULLREQUESTID",
)
@pytest.fixture(autouse=True)
def branch_push_environment(monkeypatch):
# The suite itself runs on pull_request events in CI.
for name in PULL_REQUEST_ENVIRONMENT:
monkeypatch.delenv(name, raising=False)
def _scan_params(runtime):
runtime.full_scan.assert_called_once()
return runtime.full_scan.call_args.args[1]
def test_branch_scan_asks_to_be_the_branch_head(cli_runtime): # noqa: F811
assert cli_runtime.run(["--branch", "release/1.0"]) == 0
params = _scan_params(cli_runtime)
assert params.branch == "release/1.0"
assert params.set_as_pending_head is True
assert params.make_default_branch is False
def test_default_branch_scan_is_the_head_and_the_default_branch(cli_runtime): # noqa: F811
cli_runtime.git_repo.is_default_branch = True
assert cli_runtime.run() == 0
params = _scan_params(cli_runtime)
assert params.set_as_pending_head is True
assert params.make_default_branch is True
def test_pull_request_scan_of_a_non_default_branch_does_not_request_the_head(cli_runtime): # noqa: F811
assert cli_runtime.run(["--pr-number", "42"]) == 0
params = _scan_params(cli_runtime)
assert params.set_as_pending_head is False
assert params.make_default_branch is False
def test_pull_request_event_without_a_pr_number_does_not_request_the_head(
cli_runtime, monkeypatch # noqa: F811
):
monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request")
monkeypatch.setenv("GITHUB_REF", "refs/pull/7/merge")
assert cli_runtime.run(["--pr-number", "0"]) == 0
params = _scan_params(cli_runtime)
assert params.pull_request == 0
assert params.set_as_pending_head is False
def test_scm_comparison_without_a_pr_number_does_not_request_the_head(
cli_runtime, monkeypatch, mocker # noqa: F811
):
monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request")
monkeypatch.setenv("EVENT_ACTION", "synchronize")
monkeypatch.setenv("GITHUB_REPOSITORY", "example/widgets")
monkeypatch.setenv("GH_API_TOKEN", "test")
mocker.patch.object(Github, "get_comments_for_pr", return_value={})
mocker.patch.object(Github, "add_socket_comments")
mocker.patch.object(Github, "set_commit_status", create=True)
# A manifest change keeps the run on the SCM comparison path.
monkeypatch.setattr(Core, "has_manifest_files", lambda self, files: True)
assert cli_runtime.run(["--scm", "github", "--pr-number", "0"]) == 0
cli_runtime.full_scan.assert_not_called()
cli_runtime.compare.assert_called_once()
assert cli_runtime.compare.call_args.args[1].set_as_pending_head is False
def test_explicit_default_branch_on_a_pull_request_still_requests_the_head(cli_runtime): # noqa: F811
assert cli_runtime.run(["--pr-number", "42", "--default-branch"]) == 0
params = _scan_params(cli_runtime)
assert params.set_as_pending_head is True
assert params.make_default_branch is True
@pytest.mark.parametrize("detected_default", [True, False], ids=["default-branch", "tracked-branch"])
def test_no_default_branch_does_not_request_the_head(cli_runtime, detected_default): # noqa: F811
cli_runtime.git_repo.is_default_branch = detected_default
assert cli_runtime.run(["--branch", "release/1.0", "--no-default-branch"]) == 0
params = _scan_params(cli_runtime)
assert params.set_as_pending_head is False
assert params.make_default_branch is False
@pytest.mark.parametrize("force_default", [False, True], ids=["automatic", "explicit"])
def test_missing_explicit_commit_requires_a_default_branch_override_to_request_the_head(
cli_runtime, force_default # noqa: F811
):
cli_runtime.git_repo.explicit_commit_is_missing = True
cli_runtime.git_repo.explicit_ref_allows_default_branch = lambda: False
flags = ["--branch", "release/1.0"]
if force_default:
flags.append("--default-branch")
assert cli_runtime.run(flags) == 0
params = _scan_params(cli_runtime)
assert params.make_default_branch is force_default
assert params.set_as_pending_head is force_default