|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Tests for the copyright-year rule in toolshed/check_spdx.py. |
| 5 | +
|
| 6 | +The rule must give the same answer however the hook is invoked (``git commit``, |
| 7 | +``pre-commit run --all-files``, CI), so each test builds a throwaway git repository |
| 8 | +with controlled commit dates and runs the script against it. Years are derived from |
| 9 | +the current year so the tests do not expire. |
| 10 | +
|
| 11 | + python -m pytest ci/tools/tests/test_check_spdx.py -v --noconftest |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import datetime |
| 17 | +import os |
| 18 | +import shutil |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +pytest.importorskip("pathspec", reason="check_spdx.py requires pathspec") |
| 26 | + |
| 27 | +REPO = Path(__file__).resolve().parents[3] |
| 28 | +CHECKER = REPO / "toolshed" / "check_spdx.py" |
| 29 | +LICENSE = REPO / "LICENSE" |
| 30 | + |
| 31 | +CURRENT_YEAR = datetime.datetime.now(tz=datetime.UTC).year |
| 32 | +SAMPLE = "toolshed/sample.py" |
| 33 | + |
| 34 | +CONTENT = ( |
| 35 | + "# SPDX-FileCopyrightText: Copyright (c) {years} NVIDIA CORPORATION & AFFILIATES. All rights reserved.\n" |
| 36 | + "# SPDX-License-Identifier: Apache-2.0\n" |
| 37 | + "\n" |
| 38 | + "pass\n" |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +def git(repo, *args, year=None): |
| 43 | + env = dict(os.environ) |
| 44 | + env.update( |
| 45 | + GIT_AUTHOR_NAME="Test", |
| 46 | + GIT_AUTHOR_EMAIL="[email protected]", |
| 47 | + GIT_COMMITTER_NAME="Test", |
| 48 | + GIT_COMMITTER_EMAIL="[email protected]", |
| 49 | + ) |
| 50 | + if year is not None: |
| 51 | + date = f"{year}-06-15T12:00:00+0000" |
| 52 | + env.update(GIT_AUTHOR_DATE=date, GIT_COMMITTER_DATE=date) |
| 53 | + subprocess.run( # noqa: S603 |
| 54 | + ["git", "-c", "commit.gpgsign=false", *args], # noqa: S607 |
| 55 | + cwd=repo, |
| 56 | + env=env, |
| 57 | + check=True, |
| 58 | + capture_output=True, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def commit(repo, relpath, years, *, year): |
| 63 | + path = repo / relpath |
| 64 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 65 | + path.write_text(CONTENT.format(years=years), encoding="utf-8") |
| 66 | + git(repo, "add", relpath) |
| 67 | + git(repo, "commit", "-m", f"add {relpath}", year=year) |
| 68 | + return path |
| 69 | + |
| 70 | + |
| 71 | +def run_checker(repo, *args): |
| 72 | + # The script reads LICENSE and runs git relative to the working directory, |
| 73 | + # as pre-commit does from the repository root. |
| 74 | + return subprocess.run( # noqa: S603 |
| 75 | + [sys.executable, str(CHECKER), *args, SAMPLE], |
| 76 | + cwd=repo, |
| 77 | + capture_output=True, |
| 78 | + text=True, |
| 79 | + check=False, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture |
| 84 | +def repo(tmp_path): |
| 85 | + git(tmp_path, "init") |
| 86 | + shutil.copy(LICENSE, tmp_path / "LICENSE") |
| 87 | + git(tmp_path, "add", "LICENSE") |
| 88 | + git(tmp_path, "commit", "-m", "init", year=CURRENT_YEAR - 5) |
| 89 | + return tmp_path |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.agent_authored(model="claude-sonnet-5.5") |
| 93 | +@pytest.mark.parametrize( |
| 94 | + ("commit_age", "copyright_age", "passes"), |
| 95 | + [ |
| 96 | + # Not modified this year, so last year's copyright is still correct. |
| 97 | + pytest.param(1, 1, True, id="untouched-since-last-year"), |
| 98 | + # The bug in #2273: committed this year, nothing staged, stale copyright. |
| 99 | + pytest.param(0, 1, False, id="committed-this-year-stale-copyright"), |
| 100 | + ], |
| 101 | +) |
| 102 | +def test_committed_file_is_checked_against_its_last_commit_year(repo, commit_age, copyright_age, passes): |
| 103 | + commit(repo, SAMPLE, str(CURRENT_YEAR - copyright_age), year=CURRENT_YEAR - commit_age) |
| 104 | + |
| 105 | + result = run_checker(repo) |
| 106 | + |
| 107 | + assert (result.returncode == 0) is passes, result.stdout + result.stderr |
| 108 | + assert ("OUTDATED" in result.stdout) is (not passes) |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.agent_authored(model="claude-sonnet-5.5") |
| 112 | +def test_staged_edit_requires_current_year_and_fix_writes_it(repo): |
| 113 | + # The `git commit` path: the edited file is staged when the hook runs. |
| 114 | + path = commit(repo, SAMPLE, str(CURRENT_YEAR - 3), year=CURRENT_YEAR - 2) |
| 115 | + path.write_text(path.read_text(encoding="utf-8") + "# edited\n", encoding="utf-8") |
| 116 | + git(repo, "add", SAMPLE) |
| 117 | + |
| 118 | + check = run_checker(repo) |
| 119 | + assert check.returncode == 1, check.stdout + check.stderr |
| 120 | + assert f"(expected '{CURRENT_YEAR}')" in check.stdout |
| 121 | + |
| 122 | + # --fix reports that it changed something, as the hook always has. |
| 123 | + fix = run_checker(repo, "--fix") |
| 124 | + assert fix.returncode == 1, fix.stdout + fix.stderr |
| 125 | + assert f"Copyright (c) {CURRENT_YEAR - 3}-{CURRENT_YEAR} " in path.read_text(encoding="utf-8") |
| 126 | + |
| 127 | + # The rewrite is stable: a second run accepts the file. |
| 128 | + assert run_checker(repo).returncode == 0 |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.agent_authored(model="claude-sonnet-5.5") |
| 132 | +def test_fix_of_clean_stale_file_survives_being_committed(repo): |
| 133 | + # Stale relative to an old commit, with nothing staged (for example a manual |
| 134 | + # `pre-commit run --all-files`). |
| 135 | + path = commit(repo, SAMPLE, str(CURRENT_YEAR - 3), year=CURRENT_YEAR - 2) |
| 136 | + |
| 137 | + fix = run_checker(repo, "--fix") |
| 138 | + assert fix.returncode == 1, fix.stdout + fix.stderr |
| 139 | + # Not the older last-commit year: committing the fix is a change made this year. |
| 140 | + assert f"Copyright (c) {CURRENT_YEAR - 3}-{CURRENT_YEAR} " in path.read_text(encoding="utf-8") |
| 141 | + |
| 142 | + git(repo, "add", SAMPLE) |
| 143 | + git(repo, "commit", "-m", "fix copyright", year=CURRENT_YEAR) |
| 144 | + |
| 145 | + # The fix must not demand another fix once it is part of this year's history. |
| 146 | + assert run_checker(repo).returncode == 0 |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.parametrize( |
| 150 | + ("date", "passes"), |
| 151 | + [ |
| 152 | + pytest.param(f"{CURRENT_YEAR - 1}-12-31T23:30:00-0800", False, id="utc-year-after-recorded-year"), |
| 153 | + pytest.param(f"{CURRENT_YEAR}-01-01T00:30:00+1400", True, id="utc-year-before-recorded-year"), |
| 154 | + ], |
| 155 | +) |
| 156 | +@pytest.mark.agent_authored(model="gpt-6") |
| 157 | +def test_committed_file_uses_utc_year_across_new_year(repo, monkeypatch, date, passes): |
| 158 | + monkeypatch.setenv("GIT_AUTHOR_DATE", date) |
| 159 | + monkeypatch.setenv("GIT_COMMITTER_DATE", date) |
| 160 | + commit(repo, SAMPLE, str(CURRENT_YEAR - 1), year=None) |
| 161 | + |
| 162 | + result = run_checker(repo) |
| 163 | + |
| 164 | + assert (result.returncode == 0) is passes, result.stdout + result.stderr |
| 165 | + assert ("OUTDATED" in result.stdout) is (not passes) |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.agent_authored(model="gpt-6") |
| 169 | +def test_signed_last_commit_with_show_signature_enabled(repo): |
| 170 | + ssh_keygen = shutil.which("ssh-keygen") |
| 171 | + if ssh_keygen is None: |
| 172 | + pytest.skip("ssh-keygen is required to create a signed commit") |
| 173 | + |
| 174 | + signing_key = repo / "signing_key" |
| 175 | + subprocess.run( # noqa: S603 |
| 176 | + [ssh_keygen, "-q", "-t", "ed25519", "-N", "", "-f", str(signing_key)], |
| 177 | + check=True, |
| 178 | + capture_output=True, |
| 179 | + ) |
| 180 | + allowed_signers = repo / "allowed_signers" |
| 181 | + allowed_signers.write_text( |
| 182 | + "[email protected] " + signing_key. with_suffix( ".pub"). read_text( encoding="utf-8"), encoding="utf-8" |
| 183 | + ) |
| 184 | + git(repo, "config", "gpg.format", "ssh") |
| 185 | + git(repo, "config", "user.signingkey", str(signing_key)) |
| 186 | + git(repo, "config", "gpg.ssh.allowedSignersFile", str(allowed_signers)) |
| 187 | + git(repo, "config", "log.showSignature", "true") |
| 188 | + path = commit(repo, SAMPLE, str(CURRENT_YEAR), year=CURRENT_YEAR) |
| 189 | + path.write_text(path.read_text(encoding="utf-8") + "# signed edit\n", encoding="utf-8") |
| 190 | + git(repo, "add", SAMPLE) |
| 191 | + git(repo, "-c", "commit.gpgsign=true", "commit", "-m", "signed edit", year=CURRENT_YEAR) |
| 192 | + |
| 193 | + result = run_checker(repo) |
| 194 | + |
| 195 | + assert result.returncode == 0, result.stdout + result.stderr |
0 commit comments