summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/tests/test_line_continuation.py
blob: 794fa78942465110a0146b58ff483296611332e4 (plain)
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
"""A git command continued on the next line is still a commit or a push."""
import time

import pytest

from conftest import bash_payload, commit_file, git, make_repo, metrics_of, run_hook

import security_reminder_hook as hook
from diffstate import _load_reviewed_shas


@pytest.mark.parametrize("command", [
    "git \\\n  commit -m x",
    "git \\\r\n  commit -m x",
    "git `\r\n  commit -m x",
    "git -C \\\n . commit",
    "git \\\n  -c user.name='A B' \\\n  --no-pager \\\n  commit -m x",
    "git --git-dir \\\n .git commit",
    "gt \\\n  create x",
    "# kept in C:\\temp\\\ngit commit -m x",
    "git commit \\\n  -m x",
])
def test_is_a_commit(command):
    assert hook._GIT_COMMIT_RE.search(command)


@pytest.mark.parametrize("command", [
    "git \\\n  push origin main", "git -C x `\n push", "gt \\\n submit", "echo a\\\ngit push",
])
def test_is_a_push(command):
    assert hook._GIT_PUSH_RE.search(command)


@pytest.mark.parametrize("command", [
    "git log \\\n  commit", "git \\ commit", "git \\\n  --help commit", "git \\\n  commit-graph write",
    "git log `date`\ncommit", "legit\\\ncommit",
])
def test_is_neither(command):
    assert not hook._GIT_COMMIT_RE.search(command)
    assert not hook._GIT_PUSH_RE.search(command)


def test_continued_gt_modify_is_an_amend():
    assert hook._GIT_AMEND_RE.search("gt \\\n  modify -m x")


def test_long_command_is_matched_quickly():
    for part in (" \\\n", " -c \\\n", " -c a=b \\\n", " --x \\\n", "\\\n", " `\n-p"):
        start = time.monotonic()
        hook._GIT_COMMIT_RE.search("git" + part * 3000 + " x")
        assert time.monotonic() - start < 2


@pytest.mark.parametrize("tool, command", [
    ("Bash", "git \\\n  commit -m change"),
    ("Bash", "git -c core.abbrev=12 \\\n  commit -m change"),
    ("PowerShell", "git `\n  commit -m change"),
    ("Bash", "cd .. && git -C \\\n  repo \\\n  commit -m change"),
])
def test_continued_commit_is_reviewed(tmp_path, hook_env, stub_api, tool, command):
    repo = make_repo(tmp_path / "repo")
    sha, out = commit_file(repo, "a.py", "print('a')\n")
    payload = {**bash_payload(str(repo), command, out), "tool_name": tool}
    _, so, _ = run_hook(payload, hook_env)
    assert metrics_of(so).get("commit_review") is True
    assert sha in _load_reviewed_shas(str(repo))


def test_continued_push_is_reviewed(tmp_path, hook_env, stub_api):
    repo = make_repo(tmp_path / "repo")
    remote = tmp_path / "remote.git"
    git(tmp_path, "init", "-q", "--bare", str(remote))
    git(repo, "remote", "add", "origin", str(remote))
    git(repo, "push", "-q", "-u", "origin", "main")
    base = git(repo, "rev-parse", "HEAD").strip()
    sha, _ = commit_file(repo, "a.py", "print('a')\n")
    git(repo, "push", "-q", "origin", "main")
    out = f"To {remote}\n   {base[:7]}..{sha[:7]}  main -> main\n"
    _, so, _ = run_hook(bash_payload(str(repo), "git \\\n  push origin main", out), hook_env)
    assert metrics_of(so).get("push_sweep") is True
    assert sha in _load_reviewed_shas(str(repo))