summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/tests/test_renamed_into_review.py
blob: 109f5efc0cb8a8d589acd9d1df7fa4e32914be19 (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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
"""A file renamed from a name the commit and push reviews leave out to one
they read is reviewed whole; every other rename is reviewed as before."""
import json
import os
import sys

import pytest

from conftest import (
    GIT_ENV, bash_payload, commit_file, git, make_repo, metrics_of, run_hook,
)

import gitutil
import security_reminder_hook as hook
from diffstate import _load_reviewed_shas

BODY = "import os\n" + "".join(f"value_{i} = {i}\n" for i in range(30)) + "os.system(input())\n"
MARK = "os.system(input())"
SECRET = "".join(f"KEY_{i}=SECRET_VALUE_{i}\n" for i in range(30))


@pytest.fixture(autouse=True)
def git_env(monkeypatch):
    for k, v in GIT_ENV.items():
        monkeypatch.setenv(k, v)


def _sent(stub_api):
    return "\n".join(json.dumps(b) for b, c in zip(stub_api.bodies, stub_api.calls)
                     if c.endswith("/v1/messages"))


def _move(repo, old, new):
    (repo / new).parent.mkdir(parents=True, exist_ok=True)
    git(repo, "mv", old, new)


def _commit(repo, env, command="git commit -m change", args=("-m", "change")):
    git(repo, "add", "-A")
    out = git(repo, "commit", *args)
    return metrics_of(run_hook(bash_payload(repo, command, stdout=out), env)[1])


def _push_repo(tmp_path, files):
    repo = make_repo(tmp_path / "repo", files)
    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")
    return repo, remote


def _push(repo, remote, env):
    old = git(repo, "rev-parse", "--short", "@{u}").strip()
    git(repo, "push", "-q")
    new = git(repo, "rev-parse", "--short", "HEAD").strip()
    out = f"To {remote}\n   {old}..{new}  main -> main\n"
    return metrics_of(run_hook(bash_payload(repo, "git push", out), env)[1])


@pytest.mark.parametrize("old,new", [
    ("notes.txt", "payload.py"),
    ("docs/setup.md", "bin/setup.sh"),
    ("docs/image.txt", "Dockerfile"),
    ("notes.txt", "a b/pay load.py"),
    ("notes.txt", "café.py"),
])
def test_commit_review_reads_the_renamed_file(tmp_path, hook_env, stub_api, old, new):
    repo = make_repo(tmp_path / "repo", {old: BODY, "app.py": "x = 1\n"})
    _move(repo, old, new)
    m = _commit(repo, hook_env)
    assert m.get("skip_reason") is None and m["files_reviewed"] == 1, m
    assert m["renamed_in"] == 1
    assert MARK in _sent(stub_api)


def test_push_review_reads_the_renamed_file(tmp_path, hook_env, stub_api):
    repo, remote = _push_repo(tmp_path, {"notes.txt": BODY})
    _move(repo, "notes.txt", "payload.py")
    git(repo, "commit", "-qm", "rename")
    m = _push(repo, remote, hook_env)
    assert m.get("skip_reason") is None and m["files_reviewed"] == 1, m
    assert MARK in _sent(stub_api)


def test_edit_in_the_rename_shows_the_whole_file_once(tmp_path, hook_env, stub_api):
    repo = make_repo(tmp_path / "repo", {"notes.txt": BODY})
    _move(repo, "notes.txt", "payload.py")
    text = (repo / "payload.py").read_text().replace("value_29 = 29", "value_29 = EDITED")
    (repo / "payload.py").write_text(text)
    m = _commit(repo, hook_env)
    assert m["renamed_in"] == 1 and m["files_reviewed"] == 1, m
    # Added, not context: the reviewer is told to leave context lines alone.
    assert "+" + MARK in _sent(stub_api) and "+value_29 = EDITED" in _sent(stub_api)
    assert "\\n " + MARK not in _sent(stub_api)
    # What the edit removed is still there to see.
    assert "-value_29 = 29" in _sent(stub_api)


def test_edit_hunk_stays_where_the_whole_file_does_not_fit(tmp_path, monkeypatch):
    repo = make_repo(tmp_path / "repo", {"notes.txt": BODY, "app.py": "x = 1\n"})
    _move(repo, "notes.txt", "payload.py")
    git(repo, "commit", "-qm", "c")
    renamed = [("payload.py", ("show", "--format=", "HEAD"), "HEAD")]
    edits = [("payload.py", "@@ -1 +1 @@\n-x\n+y"), ("app.py", "@@ -1 +1 @@\n-x\n+y")]
    files, failed = hook._with_renamed_in(str(repo), edits, renamed, 30)
    assert [n for n, _ in files] == ["payload.py", "app.py"] and not failed
    assert MARK in files[0][1] and files[1] == edits[1]
    for name, limit in (("DIFF_TOTAL_BYTES", 300), ("DIFF_PER_FILE_BYTES", 300)):
        with monkeypatch.context() as m:
            m.setattr(hook, name, limit)
            assert hook._with_renamed_in(str(repo), edits, renamed, 30)[0] == edits
    monkeypatch.setattr(hook, "RENAMED_IN_BYTES", 300)
    for before in (edits, edits[1:]):
        assert hook._with_renamed_in(str(repo), before, renamed, 30)[0] == before


def test_merge_hunks_are_left_alone(tmp_path):
    repo = make_repo(tmp_path / "repo", {"notes.txt": BODY})
    _move(repo, "notes.txt", "payload.py")
    git(repo, "commit", "-qm", "c")
    files = [("payload.py", "@@@ -1,1 -1,1 +1,1 @@@\n- x\n -y\n++z")]
    renamed = [("payload.py", ("show", "--format=", "HEAD"), "HEAD")]
    assert hook._with_renamed_in(str(repo), files, renamed, 30) == (files, False)


def test_rename_added_by_an_amend(tmp_path, hook_env, stub_api):
    repo = make_repo(tmp_path / "repo", {"notes.txt": BODY, "app.py": "x = 1\n"})
    commit_file(repo, "app.py", "x = 2\n")
    _move(repo, "notes.txt", "payload.py")
    m = _commit(repo, hook_env, "git commit --amend --no-edit", ("--amend", "--no-edit"))
    assert m.get("amend_delta_review") is True and m["files_reviewed"] == 1, m
    assert MARK in _sent(stub_api)


@pytest.mark.parametrize("old,new", [
    ("a.py", "pkg/b.py"), ("a.py", "notes.txt"), ("notes.txt", "other.txt"),
    ("a.py", "vendor/a.py"), ("vendor/a.py", "a.py"), ("server.pem", "server"),
    ("prod.env", "prod.sh"), (".htpasswd", "htpasswd"), ("dump.csv", "dump.sql"),
    ("credentials.txt", "credentials.sh"), ("notes.txt", "credentials.sh"), ("deploy_rsa.txt", "deploy.sh"),
    ("passwords.md", "list.py"), ("notes.txt", "notes"), ("data.txt", "data.json"), ("notes.txt", "vendor/a.py"),
])
def test_other_renames_are_not_sent(tmp_path, hook_env, stub_api, old, new):
    repo = make_repo(tmp_path / "repo", {old: BODY, "app.py": "x = 1\n"})
    _move(repo, old, new)
    m = _commit(repo, hook_env)
    assert m["skip_reason"] == 30 and "renamed_in" not in m
    assert _sent(stub_api) == ""


@pytest.mark.parametrize("name", """
    api_keys.txt tokens.txt apiKeysProd.md KEYS.md AWSkeys.txt GCPkey.md DBpass.txt stripekey.txt
    API_TOKEN.rst github-token-old.txt db_password.txt db_passwd.md my.creds.txt db-cred.txt
    root_pwd.txt site_logins.txt prod.env.txt env.production.txt server.pem.txt id_ed25519.md
    passphrase.md github-recovery-codes.txt gcp-service-account.txt service_account.md pgpass.txt
    npmrc.txt netrc.txt my-secret.txt app_credential.md
    secrets/prod.txt Credentials/prod.md prod-creds/a.txt api-keys/s.txt tokens/a/b.md .secrets/a.txt
    vault/a.txt passwd/a.txt .ssh/prod.txt a/.aws/prod.txt a/.gnupg/n.md .kube/n.txt a/Passwords/b.txt
    api_keys passwords secrets.yaml .env .env.py server.pem server.key deploy_rsa
""".split())
def test_name_of_a_secret(name):
    assert gitutil._is_secret_text(name)


@pytest.mark.parametrize("name", """
    notes.txt compassion.md environment.md keyboard.rst login.md tokenizer.md jwt.md author.md
    aws/setup.md ssh/setup.md password-manager/notes.txt secret-service/notes.txt keyboards/a.txt
    tokenizer/notes.txt
""".split())
def test_name_of_ordinary_text(name):
    assert not gitutil._is_secret_text(name)
    assert gitutil._is_text_turned_code(name, "config.py")


@pytest.mark.parametrize("name", [
    "tokens.py", "api_keys.py", "token_store.js", "vendor/tokens.go", "secrets/load.py",
    "vault/Dockerfile", "secrets/Makefile"])
def test_code_is_not_a_list_of_secrets(name):
    assert not gitutil._is_secret_text(name)


@pytest.mark.parametrize("old,new", [
    ("api_keys.txt", "api_keys.py"), ("tokens.txt", "config.py"), ("keys/prod.md", "config.sh")])
def test_list_of_secrets_is_not_sent(tmp_path, hook_env, stub_api, old, new):
    repo = make_repo(tmp_path / "repo", {old: SECRET, "app.py": "x = 1\n"})
    _move(repo, old, new)
    m = _commit(repo, hook_env)
    assert m["skip_reason"] == 30 and "renamed_in" not in m, m
    assert _sent(stub_api) == ""


@pytest.mark.parametrize("old", ["api_keys.txt", "passwords", "secrets.yaml", "server.pem"])
def test_list_of_secrets_renamed_in_two_steps_is_not_sent(tmp_path, hook_env, stub_api, old):
    repo = make_repo(tmp_path / "repo", {old: SECRET})
    _move(repo, old, "settings.txt")
    git(repo, "commit", "-qm", "one")
    _move(repo, "settings.txt", "settings.py")
    assert _commit(repo, hook_env)["skip_reason"] == 30
    assert _sent(stub_api) == ""


def test_other_renames_of_the_commit_are_still_read(tmp_path, hook_env, stub_api):
    repo = make_repo(tmp_path / "repo", {
        "api_keys.txt": SECRET, "docs/hotkeys.md": "press a\n" * 9, "notes.txt": BODY})
    _move(repo, "api_keys.txt", "settings.py")
    _move(repo, "docs/hotkeys.md", "hotkeys.py")
    _move(repo, "notes.txt", "payload.py")
    assert _commit(repo, hook_env)["renamed_in"] == 1
    assert MARK in _sent(stub_api) and "SECRET_VALUE" not in _sent(stub_api)


def test_list_of_secrets_is_not_listed():
    assert not gitutil._is_text_turned_code("api_keys.txt", "config.py")


def test_code_with_a_secret_word_in_its_name_is_not_a_secret_origin(
        tmp_path, hook_env, stub_api):
    repo = make_repo(tmp_path / "repo", {"token_store.py": BODY, "app.py": "x = 1\n"})
    _move(repo, "token_store.py", "notes.txt")
    git(repo, "commit", "-qm", "one")
    _move(repo, "notes.txt", "payload.py")
    assert _commit(repo, hook_env)["renamed_in"] == 1
    assert MARK in _sent(stub_api)


def test_rename_away_from_a_secret_shaped_name_stays_out(tmp_path, hook_env, stub_api):
    repo = make_repo(tmp_path / "repo", {"deploy_rsa": SECRET})
    _move(repo, "deploy_rsa", "settings.py")
    assert _commit(repo, hook_env)["skip_reason"] == 30
    assert _sent(stub_api) == ""


@pytest.mark.parametrize("secret_goes", ["deleted", "renamed"])
def test_not_sent_whole_when_a_secret_shaped_file_goes_too(
        tmp_path, hook_env, stub_api, secret_goes):
    # Same content under both names: git may pair either with the new file.
    repo = make_repo(tmp_path / "repo", {
        ".env": SECRET, "notes.txt": SECRET, "app.py": "x = 1\n"})
    _move(repo, "notes.txt", "settings.py")
    if secret_goes == "deleted":
        git(repo, "rm", "-q", ".env")
    else:
        _move(repo, ".env", "old_env.txt")
    (repo / "app.py").write_text("x = input()\n")
    m = _commit(repo, hook_env)
    assert m["files_reviewed"] == 1 and "renamed_in" not in m, m
    assert "SECRET_VALUE" not in _sent(stub_api)


@pytest.mark.parametrize("amend", [False, True])
def test_secret_shaped_file_renamed_in_two_steps_stays_out(tmp_path, hook_env, stub_api, amend):
    repo = make_repo(tmp_path / "repo", {".env": SECRET})
    _move(repo, ".env", "settings.txt")
    git(repo, "commit", "-qm", "one")
    _move(repo, "settings.txt", "settings.py")
    extra = ["--amend", "--no-edit"] if amend else ["-m", "two"]
    m = _commit(repo, hook_env, "git commit " + " ".join(extra), extra)
    assert "SECRET_VALUE" not in _sent(stub_api) and "renamed_in" not in m, m
    assert m["skip_reason"] == (35 if amend and m.get("amend_delta_review") else 30), m


def test_secret_shaped_file_renamed_after_edits_stays_out(tmp_path, hook_env, stub_api):
    repo = make_repo(tmp_path / "repo", {"deploy_rsa": SECRET})
    _move(repo, "deploy_rsa", "a.txt")
    with open(repo / "a.txt", "a") as f:
        f.write("MORE=1\n")
    git(repo, "commit", "-qam", "one")
    commit_file(repo, "other.py", "x = 1\n")
    _move(repo, "a.txt", "settings.py")
    m = _commit(repo, hook_env)
    assert "SECRET_VALUE" not in _sent(stub_api), m


@pytest.mark.parametrize("how", [
    "added lately", "in a merge", "in a merge, combined diffs", "on a side branch"])
def test_secret_shaped_origin_is_followed(tmp_path, hook_env, stub_api, how):
    repo = make_repo(tmp_path / "repo", {"app.py": "x = 1\n"})
    if how.endswith("combined diffs"):
        git(repo, "config", "log.diffMerges", "combined")
    if how == "on a side branch":
        commit_file(repo, "app.py", "x = 2\n")
        git(repo, "switch", "-qc", "side", "HEAD~1")
    commit_file(repo, ".env", SECRET)
    if how.startswith("in a merge"):
        git(repo, "switch", "-qc", "side")
        commit_file(repo, "side.py", "y = 1\n")
        git(repo, "switch", "-q", "main")
        git(repo, "merge", "-q", "--no-ff", "--no-commit", "side")
    _move(repo, ".env", "settings.txt")
    git(repo, "commit", "-qm", "one")
    if how == "on a side branch":
        git(repo, "switch", "-q", "main")
        git(repo, "merge", "-q", "--no-ff", "--no-edit", "side")
    for i in range(5):
        commit_file(repo, f"m{i}.py", "z = 1\n")
    _move(repo, "settings.txt", "settings.py")
    m = _commit(repo, hook_env)
    assert m["skip_reason"] == 30 and "SECRET_VALUE" not in _sent(stub_api), m


def test_unknown_origin_stays_out(tmp_path):
    repo = make_repo(tmp_path / "repo", {"notes.txt": BODY})
    _move(repo, "notes.txt", "payload.py")
    git(repo, "commit", "-qm", "c")
    show = ("show", "--format=", "HEAD")
    assert hook._with_renamed_in(str(repo), [], [("payload.py", show, "nope")], 30) == ([], True)
    assert hook._with_renamed_in(str(repo), [], [("payload.py", show, "HEAD")], 30)[0]
    assert gitutil.came_from_secret(str(repo), "HEAD", ["payload.py"], 10) == set()
    assert gitutil.came_from_secret(str(repo), "nope", ["payload.py"], 10) is None


def _two_quiet_commits(repo, env, second):
    _move(repo, "notes.txt", "payload.py")
    git(repo, "commit", "-qm", "one")
    second()
    git(repo, "add", "-A")
    git(repo, "commit", "-qm", "two")
    command = "git commit -qm one && git commit -qam two"
    return metrics_of(run_hook(bash_payload(repo, command), env)[1])


def test_file_is_shown_as_the_last_of_the_commits_left_it(tmp_path, hook_env, stub_api):
    repo = make_repo(tmp_path / "repo", {"notes.txt": BODY})
    m = _two_quiet_commits(repo, hook_env, lambda: (repo / "payload.py").write_text(
        BODY.replace(MARK, "print('fixed')")))
    assert m["reflog_shas_n"] > 1 and m["files_reviewed"] == 1 and m["renamed_in"] == 1, m
    assert "+value_5 = 5" in _sent(stub_api) and "+print('fixed')" in _sent(stub_api)
    assert "+" + MARK not in _sent(stub_api)


def _many(n):
    return {f"d/m{i:03}.txt": f"import os\nvalue = {i}\nkey_{i} = 'k{i}'\nname_{i} = {i}\n"
            for i in range(n)}


@pytest.mark.parametrize("edits,renames,renamed_in", [
    (28, 20, 2), (30, 5, None), (3, 12, hook.MAX_RENAMED_IN), (0, 40, hook.MAX_RENAMED_IN)])
def test_renamed_files_take_only_free_places(
        tmp_path, hook_env, stub_api, edits, renames, renamed_in):
    edited = {f"e{i:02}.py": "x = 1\n" for i in range(edits)}
    repo = make_repo(tmp_path / "repo", {**edited, **_many(renames)})
    for name in edited:
        (repo / name).write_text("x = 2\n")
    for name in _many(renames):
        _move(repo, name, name[:-4] + ".py")
    m = _commit(repo, hook_env)
    assert m["files_reviewed"] == edits + (renamed_in or 0), m
    assert m.get("renamed_in") == renamed_in and "diff_files_dropped" not in m
    assert all(n in _sent(stub_api) for n in edited)


def test_renames_do_not_count_towards_the_skip_for_huge_diffs(tmp_path, monkeypatch):
    many = [(f"e{i}.py", "@@ -1 +1 @@\n-x\n+y") for i in range(31)]
    monkeypatch.setattr(hook, "git_diff_unpaired", None)
    assert hook._with_renamed_in("/r", many, [("n.py", ("show", "x"), "x")], 30) == (many, False)


def test_a_directory_replaced_by_the_file_adds_one_file(tmp_path, hook_env, stub_api):
    under = {f"makefile/auth_handler{i:02}.py": f"import os\nx = {i}\n" for i in range(20)}
    edits = {f"e{i:02}.py": "x = 1\n" for i in range(9)}
    repo = make_repo(tmp_path / "repo", {"notes.txt": BODY, **under, **edits})
    git(repo, "rm", "-rq", "makefile")
    _move(repo, "notes.txt", "makefile")
    for name in edits:
        (repo / name).write_text("x = 2\n")
    m = _commit(repo, hook_env)
    assert m["renamed_in"] == 1 and m["files_reviewed"] == 30, m
    assert "diff_files_dropped" not in m


def test_push_with_an_unreadable_file_leaves_the_commits_unrecorded(tmp_path, hook_env, stub_api):
    repo, remote = _push_repo(tmp_path, {"notes.txt": BODY})
    blob = git(repo, "rev-parse", "HEAD:notes.txt").strip()
    _move(repo, "notes.txt", "payload.py")
    git(repo, "commit", "-qm", "rename")
    git(repo, "push", "-q")
    # git shows a rename of unchanged content without reading that content.
    obj = repo / ".git" / "objects" / blob[:2] / blob[2:]
    os.chmod(obj, 0o644)
    obj.unlink()
    out = f"To {remote}\n   {git(repo, 'rev-parse', '--short', 'HEAD~1').strip()}.."\
          f"{git(repo, 'rev-parse', '--short', 'HEAD').strip()}  main -> main\n"
    m = metrics_of(run_hook(bash_payload(repo, "git push", out), hook_env)[1])
    assert m["skip_reason"] == 45, m
    assert not _load_reviewed_shas(str(repo))
    assert _sent(stub_api) == ""


@pytest.mark.skipif(sys.platform in ("win32", "darwin"), reason="file names are Unicode there")
def test_push_of_a_name_that_is_not_utf8_is_not_recorded(tmp_path, hook_env, stub_api):
    repo, remote = _push_repo(tmp_path, {"notes.txt": BODY})
    os.rename(repo / "notes.txt", os.fsencode(repo) + b"/caf\xe9.py")
    git(repo, "add", "-A")
    git(repo, "commit", "-qm", "rename")
    assert _push(repo, remote, hook_env)["skip_reason"] == 45
    assert not _load_reviewed_shas(str(repo))


@pytest.mark.parametrize("name", ["GIT_ICASE_PATHSPECS", "GIT_GLOB_PATHSPECS",
                                  "GIT_NOGLOB_PATHSPECS", "GIT_LITERAL_PATHSPECS"])
def test_pathspec_setting_in_the_environment(tmp_path, hook_env, stub_api, name):
    repo = make_repo(tmp_path / "repo", {"notes.txt": BODY})
    _move(repo, "notes.txt", "payload.py")
    m = _commit(repo, {**hook_env, name: "1"})
    assert m.get("renamed_in") == 1, m


def test_new_name_is_a_file_name_not_a_pattern(tmp_path):
    repo = make_repo(tmp_path / "repo", {"notes.txt": BODY, "a.py": "x = 1\n"})
    _move(repo, "notes.txt", "[ab].py")
    (repo / "a.py").write_text("x = 2\n")
    git(repo, "add", "-A")
    git(repo, "commit", "-qm", "c")
    files = gitutil.git_diff_unpaired(
        str(repo), ("show", "--format=", "HEAD"), ["[ab].py"], 10)
    assert list(files) == ["[ab].py"]


def test_names_in_a_rename_with_an_edit_come_from_the_rename_lines():
    hunk = ["--- a/x", "+++ b/y", "@@ -1 +1 @@", "-a", "+b"]
    text = (_section("a/cfg b/.env b/cfg b/app.py", "rename from cfg b/.env",
                     "rename to cfg b/app.py", *hunk)
            + _section("a/a b/x.py b/a b/y.py", "rename from a b/x.py", "rename to a b/y.py", *hunk))
    assert [n for n, _ in gitutil.parse_diff_into_files(text)] == ["a b/y.py"]
    assert gitutil.extract_file_paths_from_diff(text) == ["a b/y.py"]


def test_name_with_the_header_separator_in_it(tmp_path, hook_env, stub_api):
    repo = make_repo(tmp_path / "repo", {"notes.txt": BODY, "cfg b/app.py": "x = 1\n"})
    _move(repo, "notes.txt", "pkg b/tool.py")
    (repo / "cfg b/app.py").write_text("x = input()\n")
    m = _commit(repo, hook_env)
    assert m["files_reviewed"] == 2 and m["renamed_in"] == 1, m
    assert "pkg b/tool.py" in _sent(stub_api) and "cfg b/app.py" in _sent(stub_api)
    assert "tool.py b/pkg" not in _sent(stub_api) and "app.py b/cfg" not in _sent(stub_api)


def _section(header, *lines):
    return "\n".join([f"diff --git {header}", *lines]) + "\n"


@pytest.mark.parametrize("header,old,new,expect", [
    ("a/n.txt b/n.py", "n.txt", "n.py", ["n.py"]),
    ('"a/we\\"ird.txt" "b/we\\"ird.py"', '"we\\"ird.txt"', '"we\\"ird.py"', ['we"ird.py']),
    ("a/x b/y.txt b/x b/y.py", "x b/y.txt", "x b/y.py", ["x b/y.py"]),
    ("a/x.py b/y.py b/x.py b/z.py", "x.py b/y.py", "x.py b/z.py", []),
    ("a/.env b/n.py", ".env", "n.py", []),
    ("a/n.txt b/deploy_rsa", "n.txt", "deploy_rsa", []),
])
@pytest.mark.parametrize("verb", ["rename", "copy"])
def test_names_come_from_the_rename_lines(verb, header, old, new, expect):
    text = _section(header, "similarity index 100%", f"{verb} from {old}", f"{verb} to {new}")
    assert gitutil.renamed_into_review(text) == expect


def test_lines_of_content_are_not_rename_lines():
    text = _section("a/a.py b/a.py", "--- a/a.py", "+++ b/a.py", "@@ -1 +1,3 @@",
                    " rename from n.txt", "rename from n.txt", "rename to n.py")
    assert gitutil.renamed_into_review(text) == []


@pytest.mark.parametrize("gone", [".env", "cfg b/.env", "deploy_rsa"])
def test_nothing_is_listed_when_a_secret_shaped_file_goes_too(gone):
    text = (_section(f"a/{gone} b/{gone}", "deleted file mode 100644", "@@ -1 +0,0 @@", "-x")
            + _section("a/n.txt b/n.py", "similarity index 100%", "rename from n.txt", "rename to n.py"))
    assert gitutil.renamed_into_review(text) == []